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/fft_data.h"
12 
13 #include "system_wrappers/include/cpu_features_wrapper.h"
14 #include "test/gtest.h"
15 #include "typedefs.h"  // NOLINT(build/include)
16 
17 namespace webrtc {
18 
19 #if defined(WEBRTC_ARCH_X86_FAMILY)
20 // Verifies that the optimized methods are bitexact to their reference
21 // counterparts.
TEST(FftData,TestOptimizations)22 TEST(FftData, TestOptimizations) {
23   if (WebRtc_GetCPUInfo(kSSE2) != 0) {
24     FftData x;
25 
26     for (size_t k = 0; k < x.re.size(); ++k) {
27       x.re[k] = k + 1;
28     }
29 
30     x.im[0] = x.im[x.im.size() - 1] = 0.f;
31     for (size_t k = 1; k < x.im.size() - 1; ++k) {
32       x.im[k] = 2.f * (k + 1);
33     }
34 
35     std::array<float, kFftLengthBy2Plus1> spectrum;
36     std::array<float, kFftLengthBy2Plus1> spectrum_sse2;
37     x.Spectrum(Aec3Optimization::kNone, &spectrum);
38     x.Spectrum(Aec3Optimization::kSse2, &spectrum_sse2);
39     EXPECT_EQ(spectrum, spectrum_sse2);
40   }
41 }
42 #endif
43 
44 #if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
45 
46 // Verifies the check for null output in CopyToPackedArray.
TEST(FftData,NonNullCopyToPackedArrayOutput)47 TEST(FftData, NonNullCopyToPackedArrayOutput) {
48   EXPECT_DEATH(FftData().CopyToPackedArray(nullptr), "");
49 }
50 
51 // Verifies the check for null output in Spectrum.
TEST(FftData,NonNullSpectrumOutput)52 TEST(FftData, NonNullSpectrumOutput) {
53   EXPECT_DEATH(FftData().Spectrum(Aec3Optimization::kNone, nullptr), "");
54 }
55 
56 #endif
57 
58 // Verifies that the Assign method properly copies the data from the source and
59 // ensures that the imaginary components for the DC and Nyquist bins are 0.
TEST(FftData,Assign)60 TEST(FftData, Assign) {
61   FftData x;
62   FftData y;
63 
64   x.re.fill(1.f);
65   x.im.fill(2.f);
66   y.Assign(x);
67   EXPECT_EQ(x.re, y.re);
68   EXPECT_EQ(0.f, y.im[0]);
69   EXPECT_EQ(0.f, y.im[x.im.size() - 1]);
70   for (size_t k = 1; k < x.im.size() - 1; ++k) {
71     EXPECT_EQ(x.im[k], y.im[k]);
72   }
73 }
74 
75 // Verifies that the Clear method properly clears all the data.
TEST(FftData,Clear)76 TEST(FftData, Clear) {
77   FftData x_ref;
78   FftData x;
79 
80   x_ref.re.fill(0.f);
81   x_ref.im.fill(0.f);
82 
83   x.re.fill(1.f);
84   x.im.fill(2.f);
85   x.Clear();
86 
87   EXPECT_EQ(x_ref.re, x.re);
88   EXPECT_EQ(x_ref.im, x.im);
89 }
90 
91 // Verifies that the spectrum is correctly computed.
TEST(FftData,Spectrum)92 TEST(FftData, Spectrum) {
93   FftData x;
94 
95   for (size_t k = 0; k < x.re.size(); ++k) {
96     x.re[k] = k + 1;
97   }
98 
99   x.im[0] = x.im[x.im.size() - 1] = 0.f;
100   for (size_t k = 1; k < x.im.size() - 1; ++k) {
101     x.im[k] = 2.f * (k + 1);
102   }
103 
104   std::array<float, kFftLengthBy2Plus1> spectrum;
105   x.Spectrum(Aec3Optimization::kNone, &spectrum);
106 
107   EXPECT_EQ(x.re[0] * x.re[0], spectrum[0]);
108   EXPECT_EQ(x.re[spectrum.size() - 1] * x.re[spectrum.size() - 1],
109             spectrum[spectrum.size() - 1]);
110   for (size_t k = 1; k < spectrum.size() - 1; ++k) {
111     EXPECT_EQ(x.re[k] * x.re[k] + x.im[k] * x.im[k], spectrum[k]);
112   }
113 }
114 
115 // Verifies that the functionality in CopyToPackedArray works as intended.
TEST(FftData,CopyToPackedArray)116 TEST(FftData, CopyToPackedArray) {
117   FftData x;
118   std::array<float, kFftLength> x_packed;
119 
120   for (size_t k = 0; k < x.re.size(); ++k) {
121     x.re[k] = k + 1;
122   }
123 
124   x.im[0] = x.im[x.im.size() - 1] = 0.f;
125   for (size_t k = 1; k < x.im.size() - 1; ++k) {
126     x.im[k] = 2.f * (k + 1);
127   }
128 
129   x.CopyToPackedArray(&x_packed);
130 
131   EXPECT_EQ(x.re[0], x_packed[0]);
132   EXPECT_EQ(x.re[x.re.size() - 1], x_packed[1]);
133   for (size_t k = 1; k < x_packed.size() / 2; ++k) {
134     EXPECT_EQ(x.re[k], x_packed[2 * k]);
135     EXPECT_EQ(x.im[k], x_packed[2 * k + 1]);
136   }
137 }
138 
139 // Verifies that the functionality in CopyFromPackedArray works as intended
140 // (relies on that the functionality in CopyToPackedArray has been verified in
141 // the test above).
TEST(FftData,CopyFromPackedArray)142 TEST(FftData, CopyFromPackedArray) {
143   FftData x_ref;
144   FftData x;
145   std::array<float, kFftLength> x_packed;
146 
147   for (size_t k = 0; k < x_ref.re.size(); ++k) {
148     x_ref.re[k] = k + 1;
149   }
150 
151   x_ref.im[0] = x_ref.im[x_ref.im.size() - 1] = 0.f;
152   for (size_t k = 1; k < x_ref.im.size() - 1; ++k) {
153     x_ref.im[k] = 2.f * (k + 1);
154   }
155 
156   x_ref.CopyToPackedArray(&x_packed);
157   x.CopyFromPackedArray(x_packed);
158 
159   EXPECT_EQ(x_ref.re, x.re);
160   EXPECT_EQ(x_ref.im, x.im);
161 }
162 
163 }  // namespace webrtc
164