blob: b02f3425b71f7175f78b60dfd27b27873d466e83 [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>
14
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020015#include "rtc_base/checks.h"
peah522d71b2017-02-23 05:16:26 -080016
17namespace webrtc {
18
Per Åhgrend20639f2018-01-11 10:29:49 +010019namespace {
20
21const float kHanning64[kFftLengthBy2] = {
22 0.f, 0.00248461f, 0.00991376f, 0.0222136f, 0.03926189f,
23 0.06088921f, 0.08688061f, 0.11697778f, 0.15088159f, 0.1882551f,
24 0.22872687f, 0.27189467f, 0.31732949f, 0.36457977f, 0.41317591f,
25 0.46263495f, 0.51246535f, 0.56217185f, 0.61126047f, 0.65924333f,
26 0.70564355f, 0.75f, 0.79187184f, 0.83084292f, 0.86652594f,
27 0.89856625f, 0.92664544f, 0.95048443f, 0.96984631f, 0.98453864f,
28 0.99441541f, 0.99937846f, 0.99937846f, 0.99441541f, 0.98453864f,
29 0.96984631f, 0.95048443f, 0.92664544f, 0.89856625f, 0.86652594f,
30 0.83084292f, 0.79187184f, 0.75f, 0.70564355f, 0.65924333f,
31 0.61126047f, 0.56217185f, 0.51246535f, 0.46263495f, 0.41317591f,
32 0.36457977f, 0.31732949f, 0.27189467f, 0.22872687f, 0.1882551f,
33 0.15088159f, 0.11697778f, 0.08688061f, 0.06088921f, 0.03926189f,
34 0.0222136f, 0.00991376f, 0.00248461f, 0.f};
35
Per Åhgren47d7fbd2018-04-24 12:44:29 +020036// Hanning window from Matlab command win = sqrt(hanning(128)).
37const float kSqrtHanning128[kFftLength] = {
38 0.00000000000000f, 0.02454122852291f, 0.04906767432742f, 0.07356456359967f,
39 0.09801714032956f, 0.12241067519922f, 0.14673047445536f, 0.17096188876030f,
40 0.19509032201613f, 0.21910124015687f, 0.24298017990326f, 0.26671275747490f,
41 0.29028467725446f, 0.31368174039889f, 0.33688985339222f, 0.35989503653499f,
42 0.38268343236509f, 0.40524131400499f, 0.42755509343028f, 0.44961132965461f,
43 0.47139673682600f, 0.49289819222978f, 0.51410274419322f, 0.53499761988710f,
44 0.55557023301960f, 0.57580819141785f, 0.59569930449243f, 0.61523159058063f,
45 0.63439328416365f, 0.65317284295378f, 0.67155895484702f, 0.68954054473707f,
46 0.70710678118655f, 0.72424708295147f, 0.74095112535496f, 0.75720884650648f,
47 0.77301045336274f, 0.78834642762661f, 0.80320753148064f, 0.81758481315158f,
48 0.83146961230255f, 0.84485356524971f, 0.85772861000027f, 0.87008699110871f,
49 0.88192126434835f, 0.89322430119552f, 0.90398929312344f, 0.91420975570353f,
50 0.92387953251129f, 0.93299279883474f, 0.94154406518302f, 0.94952818059304f,
51 0.95694033573221f, 0.96377606579544f, 0.97003125319454f, 0.97570213003853f,
52 0.98078528040323f, 0.98527764238894f, 0.98917650996478f, 0.99247953459871f,
53 0.99518472667220f, 0.99729045667869f, 0.99879545620517f, 0.99969881869620f,
54 1.00000000000000f, 0.99969881869620f, 0.99879545620517f, 0.99729045667869f,
55 0.99518472667220f, 0.99247953459871f, 0.98917650996478f, 0.98527764238894f,
56 0.98078528040323f, 0.97570213003853f, 0.97003125319454f, 0.96377606579544f,
57 0.95694033573221f, 0.94952818059304f, 0.94154406518302f, 0.93299279883474f,
58 0.92387953251129f, 0.91420975570353f, 0.90398929312344f, 0.89322430119552f,
59 0.88192126434835f, 0.87008699110871f, 0.85772861000027f, 0.84485356524971f,
60 0.83146961230255f, 0.81758481315158f, 0.80320753148064f, 0.78834642762661f,
61 0.77301045336274f, 0.75720884650648f, 0.74095112535496f, 0.72424708295147f,
62 0.70710678118655f, 0.68954054473707f, 0.67155895484702f, 0.65317284295378f,
63 0.63439328416365f, 0.61523159058063f, 0.59569930449243f, 0.57580819141785f,
64 0.55557023301960f, 0.53499761988710f, 0.51410274419322f, 0.49289819222978f,
65 0.47139673682600f, 0.44961132965461f, 0.42755509343028f, 0.40524131400499f,
66 0.38268343236509f, 0.35989503653499f, 0.33688985339222f, 0.31368174039889f,
67 0.29028467725446f, 0.26671275747490f, 0.24298017990326f, 0.21910124015687f,
68 0.19509032201613f, 0.17096188876030f, 0.14673047445536f, 0.12241067519922f,
69 0.09801714032956f, 0.07356456359967f, 0.04906767432742f, 0.02454122852291f};
70
Per Åhgrend20639f2018-01-11 10:29:49 +010071} // namespace
72
peah522d71b2017-02-23 05:16:26 -080073// TODO(peah): Change x to be std::array once the rest of the code allows this.
Per Åhgrend20639f2018-01-11 10:29:49 +010074void Aec3Fft::ZeroPaddedFft(rtc::ArrayView<const float> x,
75 Window window,
76 FftData* X) const {
peah522d71b2017-02-23 05:16:26 -080077 RTC_DCHECK(X);
78 RTC_DCHECK_EQ(kFftLengthBy2, x.size());
79 std::array<float, kFftLength> fft;
80 std::fill(fft.begin(), fft.begin() + kFftLengthBy2, 0.f);
Per Åhgrend20639f2018-01-11 10:29:49 +010081 switch (window) {
82 case Window::kRectangular:
83 std::copy(x.begin(), x.end(), fft.begin() + kFftLengthBy2);
84 break;
85 case Window::kHanning:
86 std::transform(x.begin(), x.end(), std::begin(kHanning64),
87 fft.begin() + kFftLengthBy2,
88 [](float a, float b) { return a * b; });
89 break;
Per Åhgren47d7fbd2018-04-24 12:44:29 +020090 case Window::kSqrtHanning:
91 RTC_NOTREACHED();
92 break;
Per Åhgrend20639f2018-01-11 10:29:49 +010093 default:
94 RTC_NOTREACHED();
95 }
96
peah522d71b2017-02-23 05:16:26 -080097 Fft(&fft, X);
98}
99
100void Aec3Fft::PaddedFft(rtc::ArrayView<const float> x,
Per Åhgren658ad882018-04-27 14:22:37 +0200101 rtc::ArrayView<const float> x_old,
Per Åhgren47d7fbd2018-04-24 12:44:29 +0200102 Window window,
peah522d71b2017-02-23 05:16:26 -0800103 FftData* X) const {
104 RTC_DCHECK(X);
105 RTC_DCHECK_EQ(kFftLengthBy2, x.size());
106 RTC_DCHECK_EQ(kFftLengthBy2, x_old.size());
107 std::array<float, kFftLength> fft;
Per Åhgren47d7fbd2018-04-24 12:44:29 +0200108
109 switch (window) {
110 case Window::kRectangular:
111 std::copy(x_old.begin(), x_old.end(), fft.begin());
112 std::copy(x.begin(), x.end(), fft.begin() + x_old.size());
Per Åhgren47d7fbd2018-04-24 12:44:29 +0200113 break;
114 case Window::kHanning:
115 RTC_NOTREACHED();
116 break;
117 case Window::kSqrtHanning:
118 std::transform(x_old.begin(), x_old.end(), std::begin(kSqrtHanning128),
119 fft.begin(), std::multiplies<float>());
120 std::transform(x.begin(), x.end(),
121 std::begin(kSqrtHanning128) + x_old.size(),
122 fft.begin() + x_old.size(), std::multiplies<float>());
123 break;
124 default:
125 RTC_NOTREACHED();
126 }
127
peah522d71b2017-02-23 05:16:26 -0800128 Fft(&fft, X);
129}
130
131} // namespace webrtc