blob: 1832101855242030ae0a9be625e6bcdd1d639e42 [file] [log] [blame]
peah522d71b2017-02-23 05:16:26 -08001/*
2 * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/audio_processing/aec3/aec3_fft.h"
peah522d71b2017-02-23 05:16:26 -080012
13#include <algorithm>
Jonas Olsson941a07c2018-09-13 10:07:07 +020014#include <functional>
Yves Gerey988cc082018-10-23 12:03:01 +020015#include <iterator>
peah522d71b2017-02-23 05:16:26 -080016
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "rtc_base/checks.h"
peah522d71b2017-02-23 05:16:26 -080018
19namespace webrtc {
20
Per Åhgrend20639f2018-01-11 10:29:49 +010021namespace {
22
23const float kHanning64[kFftLengthBy2] = {
24 0.f, 0.00248461f, 0.00991376f, 0.0222136f, 0.03926189f,
25 0.06088921f, 0.08688061f, 0.11697778f, 0.15088159f, 0.1882551f,
26 0.22872687f, 0.27189467f, 0.31732949f, 0.36457977f, 0.41317591f,
27 0.46263495f, 0.51246535f, 0.56217185f, 0.61126047f, 0.65924333f,
28 0.70564355f, 0.75f, 0.79187184f, 0.83084292f, 0.86652594f,
29 0.89856625f, 0.92664544f, 0.95048443f, 0.96984631f, 0.98453864f,
30 0.99441541f, 0.99937846f, 0.99937846f, 0.99441541f, 0.98453864f,
31 0.96984631f, 0.95048443f, 0.92664544f, 0.89856625f, 0.86652594f,
32 0.83084292f, 0.79187184f, 0.75f, 0.70564355f, 0.65924333f,
33 0.61126047f, 0.56217185f, 0.51246535f, 0.46263495f, 0.41317591f,
34 0.36457977f, 0.31732949f, 0.27189467f, 0.22872687f, 0.1882551f,
35 0.15088159f, 0.11697778f, 0.08688061f, 0.06088921f, 0.03926189f,
36 0.0222136f, 0.00991376f, 0.00248461f, 0.f};
37
Per Åhgren47d7fbd2018-04-24 12:44:29 +020038// Hanning window from Matlab command win = sqrt(hanning(128)).
39const float kSqrtHanning128[kFftLength] = {
40 0.00000000000000f, 0.02454122852291f, 0.04906767432742f, 0.07356456359967f,
41 0.09801714032956f, 0.12241067519922f, 0.14673047445536f, 0.17096188876030f,
42 0.19509032201613f, 0.21910124015687f, 0.24298017990326f, 0.26671275747490f,
43 0.29028467725446f, 0.31368174039889f, 0.33688985339222f, 0.35989503653499f,
44 0.38268343236509f, 0.40524131400499f, 0.42755509343028f, 0.44961132965461f,
45 0.47139673682600f, 0.49289819222978f, 0.51410274419322f, 0.53499761988710f,
46 0.55557023301960f, 0.57580819141785f, 0.59569930449243f, 0.61523159058063f,
47 0.63439328416365f, 0.65317284295378f, 0.67155895484702f, 0.68954054473707f,
48 0.70710678118655f, 0.72424708295147f, 0.74095112535496f, 0.75720884650648f,
49 0.77301045336274f, 0.78834642762661f, 0.80320753148064f, 0.81758481315158f,
50 0.83146961230255f, 0.84485356524971f, 0.85772861000027f, 0.87008699110871f,
51 0.88192126434835f, 0.89322430119552f, 0.90398929312344f, 0.91420975570353f,
52 0.92387953251129f, 0.93299279883474f, 0.94154406518302f, 0.94952818059304f,
53 0.95694033573221f, 0.96377606579544f, 0.97003125319454f, 0.97570213003853f,
54 0.98078528040323f, 0.98527764238894f, 0.98917650996478f, 0.99247953459871f,
55 0.99518472667220f, 0.99729045667869f, 0.99879545620517f, 0.99969881869620f,
56 1.00000000000000f, 0.99969881869620f, 0.99879545620517f, 0.99729045667869f,
57 0.99518472667220f, 0.99247953459871f, 0.98917650996478f, 0.98527764238894f,
58 0.98078528040323f, 0.97570213003853f, 0.97003125319454f, 0.96377606579544f,
59 0.95694033573221f, 0.94952818059304f, 0.94154406518302f, 0.93299279883474f,
60 0.92387953251129f, 0.91420975570353f, 0.90398929312344f, 0.89322430119552f,
61 0.88192126434835f, 0.87008699110871f, 0.85772861000027f, 0.84485356524971f,
62 0.83146961230255f, 0.81758481315158f, 0.80320753148064f, 0.78834642762661f,
63 0.77301045336274f, 0.75720884650648f, 0.74095112535496f, 0.72424708295147f,
64 0.70710678118655f, 0.68954054473707f, 0.67155895484702f, 0.65317284295378f,
65 0.63439328416365f, 0.61523159058063f, 0.59569930449243f, 0.57580819141785f,
66 0.55557023301960f, 0.53499761988710f, 0.51410274419322f, 0.49289819222978f,
67 0.47139673682600f, 0.44961132965461f, 0.42755509343028f, 0.40524131400499f,
68 0.38268343236509f, 0.35989503653499f, 0.33688985339222f, 0.31368174039889f,
69 0.29028467725446f, 0.26671275747490f, 0.24298017990326f, 0.21910124015687f,
70 0.19509032201613f, 0.17096188876030f, 0.14673047445536f, 0.12241067519922f,
71 0.09801714032956f, 0.07356456359967f, 0.04906767432742f, 0.02454122852291f};
72
Per Åhgrend20639f2018-01-11 10:29:49 +010073} // namespace
74
peah522d71b2017-02-23 05:16:26 -080075// TODO(peah): Change x to be std::array once the rest of the code allows this.
Per Åhgrend20639f2018-01-11 10:29:49 +010076void Aec3Fft::ZeroPaddedFft(rtc::ArrayView<const float> x,
77 Window window,
78 FftData* X) const {
peah522d71b2017-02-23 05:16:26 -080079 RTC_DCHECK(X);
80 RTC_DCHECK_EQ(kFftLengthBy2, x.size());
81 std::array<float, kFftLength> fft;
82 std::fill(fft.begin(), fft.begin() + kFftLengthBy2, 0.f);
Per Åhgrend20639f2018-01-11 10:29:49 +010083 switch (window) {
84 case Window::kRectangular:
85 std::copy(x.begin(), x.end(), fft.begin() + kFftLengthBy2);
86 break;
87 case Window::kHanning:
88 std::transform(x.begin(), x.end(), std::begin(kHanning64),
89 fft.begin() + kFftLengthBy2,
90 [](float a, float b) { return a * b; });
91 break;
Per Åhgren47d7fbd2018-04-24 12:44:29 +020092 case Window::kSqrtHanning:
93 RTC_NOTREACHED();
94 break;
Per Åhgrend20639f2018-01-11 10:29:49 +010095 default:
96 RTC_NOTREACHED();
97 }
98
peah522d71b2017-02-23 05:16:26 -080099 Fft(&fft, X);
100}
101
102void Aec3Fft::PaddedFft(rtc::ArrayView<const float> x,
Per Åhgren658ad882018-04-27 14:22:37 +0200103 rtc::ArrayView<const float> x_old,
Per Åhgren47d7fbd2018-04-24 12:44:29 +0200104 Window window,
peah522d71b2017-02-23 05:16:26 -0800105 FftData* X) const {
106 RTC_DCHECK(X);
107 RTC_DCHECK_EQ(kFftLengthBy2, x.size());
108 RTC_DCHECK_EQ(kFftLengthBy2, x_old.size());
109 std::array<float, kFftLength> fft;
Per Åhgren47d7fbd2018-04-24 12:44:29 +0200110
111 switch (window) {
112 case Window::kRectangular:
113 std::copy(x_old.begin(), x_old.end(), fft.begin());
114 std::copy(x.begin(), x.end(), fft.begin() + x_old.size());
Per Åhgren47d7fbd2018-04-24 12:44:29 +0200115 break;
116 case Window::kHanning:
117 RTC_NOTREACHED();
118 break;
119 case Window::kSqrtHanning:
120 std::transform(x_old.begin(), x_old.end(), std::begin(kSqrtHanning128),
121 fft.begin(), std::multiplies<float>());
122 std::transform(x.begin(), x.end(),
123 std::begin(kSqrtHanning128) + x_old.size(),
124 fft.begin() + x_old.size(), std::multiplies<float>());
125 break;
126 default:
127 RTC_NOTREACHED();
128 }
129
peah522d71b2017-02-23 05:16:26 -0800130 Fft(&fft, X);
131}
132
133} // namespace webrtc