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