Index: webrtc/modules/audio_processing/aec3/decimator_by_4_unittest.cc |
diff --git a/webrtc/modules/audio_processing/aec3/decimator_by_4_unittest.cc b/webrtc/modules/audio_processing/aec3/decimator_by_4_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..009160a53c50d8cb59cb2d664463dab884e26372 |
--- /dev/null |
+++ b/webrtc/modules/audio_processing/aec3/decimator_by_4_unittest.cc |
@@ -0,0 +1,126 @@ |
+/* |
+ * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved. |
+ * |
+ * Use of this source code is governed by a BSD-style license |
+ * that can be found in the LICENSE file in the root of the source |
+ * tree. An additional intellectual property rights grant can be found |
+ * in the file PATENTS. All contributing project authors may |
+ * be found in the AUTHORS file in the root of the source tree. |
+ */ |
+ |
+#include "webrtc/modules/audio_processing/aec3/decimator_by_4.h" |
+ |
+#include <math.h> |
+#include <algorithm> |
+#include <array> |
+#include <sstream> |
+#include <string> |
+#include <vector> |
+ |
+#include "webrtc/modules/audio_processing/aec3/aec3_constants.h" |
+#include "webrtc/test/gtest.h" |
+ |
+namespace webrtc { |
+ |
+namespace { |
+ |
+std::string ProduceDebugText(int sample_rate_hz) { |
+ std::ostringstream ss; |
+ ss << "Sample rate: " << sample_rate_hz; |
+ return ss.str(); |
+} |
+ |
+constexpr float kPi = 3.141592f; |
+constexpr size_t kNumStartupBlocks = 50; |
+constexpr size_t kNumBlocks = 1000; |
+ |
+void ProduceDecimatedSinusoidalOutputPower(int rate, |
hlundin-webrtc
2017/02/06 09:13:18
sample_rate
peah-webrtc
2017/02/06 11:25:38
Done.
|
+ float sinusoidal_frequency, |
+ double* input_power, |
+ double* output_power) { |
+ float input[kBlockSize * kNumBlocks]; |
+ |
+ // Produce a sinusoid of frequency 3/8 * sample rate. |
hlundin-webrtc
2017/02/06 09:13:18
Two spaces between "sinusoid" and "of". And commen
peah-webrtc
2017/02/06 11:25:38
Ah, thanks!
Done.
|
+ for (size_t k = 0; k < kBlockSize * kNumBlocks; ++k) { |
+ input[k] = 32767.f * sin(2.f * kPi * sinusoidal_frequency * k / rate); |
+ } |
+ |
+ DecimatorBy4 decimator; |
+ std::array<float, kSubBlockSize * kNumBlocks> output; |
+ |
+ for (size_t k = 0; k < kNumBlocks; ++k) { |
+ std::array<float, kSubBlockSize> sub_block; |
+ |
+ decimator.Decimate( |
+ rtc::ArrayView<const float>(&input[k * kBlockSize], kBlockSize), |
+ &sub_block); |
+ |
+ std::copy(sub_block.begin(), sub_block.end(), |
+ output.begin() + k * kSubBlockSize); |
+ } |
+ |
+ RTC_DCHECK_GT(kNumBlocks, kNumStartupBlocks); |
hlundin-webrtc
2017/02/06 09:13:18
ASSERT_GT.
Don't crash the test binary.
peah-webrtc
2017/02/06 11:25:38
Done.
|
+ rtc::ArrayView<const float> input_to_evaluate( |
+ &input[kNumStartupBlocks * kBlockSize], |
+ (kNumBlocks - kNumStartupBlocks) * kBlockSize); |
+ rtc::ArrayView<const float> output_to_evaluate( |
+ &output[kNumStartupBlocks * kSubBlockSize], |
+ (kNumBlocks - kNumStartupBlocks) * kSubBlockSize); |
+ *input_power = |
+ std::inner_product(input_to_evaluate.begin(), input_to_evaluate.end(), |
+ input_to_evaluate.begin(), 0.) / |
hlundin-webrtc
2017/02/06 09:13:18
Looks a bit odd with just "0."; maybe write "0.f"
peah-webrtc
2017/02/06 11:25:38
I actually think that 0.0 would be the correct her
|
+ input_to_evaluate.size(); |
+ *output_power = |
+ std::inner_product(output_to_evaluate.begin(), output_to_evaluate.end(), |
+ output_to_evaluate.begin(), 0.) / |
hlundin-webrtc
2017/02/06 09:13:18
Same here.
peah-webrtc
2017/02/06 11:25:38
Done.
|
+ output_to_evaluate.size(); |
+} |
+ |
+} // namespace |
+ |
+// Verifies that there is little aliasing from upper frequencies in the |
+// downsampling. |
+TEST(DecimatorBy4, NoLeakageFromUpperFrequencies) { |
+ double input_power; |
+ double output_power; |
+ for (auto rate : {8000, 16000, 32000, 48000}) { |
+ ProduceDebugText(rate); |
+ ProduceDecimatedSinusoidalOutputPower(rate, 3.f / 8.f * rate, &input_power, |
+ &output_power); |
+ EXPECT_GT(0.0001f * input_power, output_power); |
+ } |
+} |
+ |
+// Verifies that the impact of low-frequency content is small during the |
+// downsampling. |
+TEST(DecimatorBy4, NoImpactOnLowerFrequencies) { |
+ double input_power; |
+ double output_power; |
+ for (auto rate : {8000, 16000, 32000, 48000}) { |
+ ProduceDebugText(rate); |
+ ProduceDecimatedSinusoidalOutputPower(rate, 200.f, &input_power, |
+ &output_power); |
+ EXPECT_LT(0.7f * input_power, output_power); |
+ } |
+} |
+ |
+#if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID) |
+// Verifies the check for the input size. |
+TEST(DecimatorBy4, WrongInputSize) { |
+ DecimatorBy4 decimator; |
+ std::vector<float> x(std::vector<float>(kBlockSize - 1, 0.f)); |
+ std::array<float, kSubBlockSize> x_downsampled; |
+ EXPECT_DEATH(decimator.Decimate(x, &x_downsampled), ""); |
+} |
+ |
+// Verifies the check for non-null output. |
+TEST(DecimatorBy4, WrongOutputSize) { |
hlundin-webrtc
2017/02/06 09:13:18
The name of the test is not consistent with the co
peah-webrtc
2017/02/06 11:25:38
Not fully sure that I understood your comment corr
hlundin-webrtc
2017/02/06 14:33:34
But the test name is WrongOutputSize.
peah-webrtc
2017/02/07 05:40:57
(facepalm) Thanks! I cannot see how I could miss t
|
+ DecimatorBy4 decimator; |
+ std::vector<float> x(std::vector<float>(kBlockSize, 0.f)); |
+ |
hlundin-webrtc
2017/02/06 09:13:18
You can drop this blank line.
peah-webrtc
2017/02/06 11:25:38
Done.
|
+ EXPECT_DEATH(decimator.Decimate(x, nullptr), ""); |
+} |
+ |
+#endif |
+ |
+} // namespace webrtc |