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