aec3_fft.cc (6496B)
1 /* 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 11 #include "modules/audio_processing/aec3/aec3_fft.h" 12 13 #include <algorithm> 14 #include <array> 15 #include <functional> 16 #include <iterator> 17 18 #include "api/array_view.h" 19 #include "modules/audio_processing/aec3/aec3_common.h" 20 #include "modules/audio_processing/aec3/fft_data.h" 21 #include "rtc_base/checks.h" 22 #include "rtc_base/cpu_info.h" 23 24 namespace webrtc { 25 26 namespace { 27 28 const float kHanning64[kFftLengthBy2] = { 29 0.f, 0.00248461f, 0.00991376f, 0.0222136f, 0.03926189f, 30 0.06088921f, 0.08688061f, 0.11697778f, 0.15088159f, 0.1882551f, 31 0.22872687f, 0.27189467f, 0.31732949f, 0.36457977f, 0.41317591f, 32 0.46263495f, 0.51246535f, 0.56217185f, 0.61126047f, 0.65924333f, 33 0.70564355f, 0.75f, 0.79187184f, 0.83084292f, 0.86652594f, 34 0.89856625f, 0.92664544f, 0.95048443f, 0.96984631f, 0.98453864f, 35 0.99441541f, 0.99937846f, 0.99937846f, 0.99441541f, 0.98453864f, 36 0.96984631f, 0.95048443f, 0.92664544f, 0.89856625f, 0.86652594f, 37 0.83084292f, 0.79187184f, 0.75f, 0.70564355f, 0.65924333f, 38 0.61126047f, 0.56217185f, 0.51246535f, 0.46263495f, 0.41317591f, 39 0.36457977f, 0.31732949f, 0.27189467f, 0.22872687f, 0.1882551f, 40 0.15088159f, 0.11697778f, 0.08688061f, 0.06088921f, 0.03926189f, 41 0.0222136f, 0.00991376f, 0.00248461f, 0.f}; 42 43 // Hanning window from Matlab command win = sqrt(hanning(128)). 44 const float kSqrtHanning128[kFftLength] = { 45 0.00000000000000f, 0.02454122852291f, 0.04906767432742f, 0.07356456359967f, 46 0.09801714032956f, 0.12241067519922f, 0.14673047445536f, 0.17096188876030f, 47 0.19509032201613f, 0.21910124015687f, 0.24298017990326f, 0.26671275747490f, 48 0.29028467725446f, 0.31368174039889f, 0.33688985339222f, 0.35989503653499f, 49 0.38268343236509f, 0.40524131400499f, 0.42755509343028f, 0.44961132965461f, 50 0.47139673682600f, 0.49289819222978f, 0.51410274419322f, 0.53499761988710f, 51 0.55557023301960f, 0.57580819141785f, 0.59569930449243f, 0.61523159058063f, 52 0.63439328416365f, 0.65317284295378f, 0.67155895484702f, 0.68954054473707f, 53 0.70710678118655f, 0.72424708295147f, 0.74095112535496f, 0.75720884650648f, 54 0.77301045336274f, 0.78834642762661f, 0.80320753148064f, 0.81758481315158f, 55 0.83146961230255f, 0.84485356524971f, 0.85772861000027f, 0.87008699110871f, 56 0.88192126434835f, 0.89322430119552f, 0.90398929312344f, 0.91420975570353f, 57 0.92387953251129f, 0.93299279883474f, 0.94154406518302f, 0.94952818059304f, 58 0.95694033573221f, 0.96377606579544f, 0.97003125319454f, 0.97570213003853f, 59 0.98078528040323f, 0.98527764238894f, 0.98917650996478f, 0.99247953459871f, 60 0.99518472667220f, 0.99729045667869f, 0.99879545620517f, 0.99969881869620f, 61 1.00000000000000f, 0.99969881869620f, 0.99879545620517f, 0.99729045667869f, 62 0.99518472667220f, 0.99247953459871f, 0.98917650996478f, 0.98527764238894f, 63 0.98078528040323f, 0.97570213003853f, 0.97003125319454f, 0.96377606579544f, 64 0.95694033573221f, 0.94952818059304f, 0.94154406518302f, 0.93299279883474f, 65 0.92387953251129f, 0.91420975570353f, 0.90398929312344f, 0.89322430119552f, 66 0.88192126434835f, 0.87008699110871f, 0.85772861000027f, 0.84485356524971f, 67 0.83146961230255f, 0.81758481315158f, 0.80320753148064f, 0.78834642762661f, 68 0.77301045336274f, 0.75720884650648f, 0.74095112535496f, 0.72424708295147f, 69 0.70710678118655f, 0.68954054473707f, 0.67155895484702f, 0.65317284295378f, 70 0.63439328416365f, 0.61523159058063f, 0.59569930449243f, 0.57580819141785f, 71 0.55557023301960f, 0.53499761988710f, 0.51410274419322f, 0.49289819222978f, 72 0.47139673682600f, 0.44961132965461f, 0.42755509343028f, 0.40524131400499f, 73 0.38268343236509f, 0.35989503653499f, 0.33688985339222f, 0.31368174039889f, 74 0.29028467725446f, 0.26671275747490f, 0.24298017990326f, 0.21910124015687f, 75 0.19509032201613f, 0.17096188876030f, 0.14673047445536f, 0.12241067519922f, 76 0.09801714032956f, 0.07356456359967f, 0.04906767432742f, 0.02454122852291f}; 77 78 bool IsSse2Available() { 79 #if defined(WEBRTC_ARCH_X86_FAMILY) 80 return cpu_info::Supports(cpu_info::ISA::kSSE2); 81 #else 82 return false; 83 #endif 84 } 85 86 } // namespace 87 88 Aec3Fft::Aec3Fft() : ooura_fft_(IsSse2Available()) {} 89 90 // TODO(peah): Change x to be std::array once the rest of the code allows this. 91 void Aec3Fft::ZeroPaddedFft(ArrayView<const float> x, 92 Window window, 93 FftData* X) const { 94 RTC_DCHECK(X); 95 RTC_DCHECK_EQ(kFftLengthBy2, x.size()); 96 std::array<float, kFftLength> fft; 97 std::fill(fft.begin(), fft.begin() + kFftLengthBy2, 0.f); 98 switch (window) { 99 case Window::kRectangular: 100 std::copy(x.begin(), x.end(), fft.begin() + kFftLengthBy2); 101 break; 102 case Window::kHanning: 103 std::transform(x.begin(), x.end(), std::begin(kHanning64), 104 fft.begin() + kFftLengthBy2, 105 [](float a, float b) { return a * b; }); 106 break; 107 case Window::kSqrtHanning: 108 RTC_DCHECK_NOTREACHED(); 109 break; 110 default: 111 RTC_DCHECK_NOTREACHED(); 112 } 113 114 Fft(&fft, X); 115 } 116 117 void Aec3Fft::PaddedFft(ArrayView<const float> x, 118 ArrayView<const float> x_old, 119 Window window, 120 FftData* X) const { 121 RTC_DCHECK(X); 122 RTC_DCHECK_EQ(kFftLengthBy2, x.size()); 123 RTC_DCHECK_EQ(kFftLengthBy2, x_old.size()); 124 std::array<float, kFftLength> fft; 125 126 switch (window) { 127 case Window::kRectangular: 128 std::copy(x_old.begin(), x_old.end(), fft.begin()); 129 std::copy(x.begin(), x.end(), fft.begin() + x_old.size()); 130 break; 131 case Window::kHanning: 132 RTC_DCHECK_NOTREACHED(); 133 break; 134 case Window::kSqrtHanning: 135 std::transform(x_old.begin(), x_old.end(), std::begin(kSqrtHanning128), 136 fft.begin(), std::multiplies<float>()); 137 std::transform(x.begin(), x.end(), 138 std::begin(kSqrtHanning128) + x_old.size(), 139 fft.begin() + x_old.size(), std::multiplies<float>()); 140 break; 141 default: 142 RTC_DCHECK_NOTREACHED(); 143 } 144 145 Fft(&fft, X); 146 } 147 148 } // namespace webrtc