Index: webrtc/modules/audio_processing/aec3/down_sampler_4khz_unittest.cc |
diff --git a/webrtc/modules/audio_processing/aec3/down_sampler_4khz_unittest.cc b/webrtc/modules/audio_processing/aec3/down_sampler_4khz_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..c3471f229cc60c2e9b85f1c6dc0f41f358f80ebc |
--- /dev/null |
+++ b/webrtc/modules/audio_processing/aec3/down_sampler_4khz_unittest.cc |
@@ -0,0 +1,154 @@ |
+/* |
+ * 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/down_sampler_4khz.h" |
+ |
+#include <math.h> |
+#include <sstream> |
+#include <string> |
+#include <vector> |
+ |
+#include "webrtc/base/array_view.h" |
aleloi
2017/01/27 15:37:47
Included in related header.
peah-webrtc
2017/02/02 14:04:47
Done.
|
+#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; |
+ |
+} // namespace |
+ |
+// Verifies that there is little aliasing from upper frequencies in the |
+// downsampling. |
+TEST(DownSampler4kHz, NoLeakageFromUpperFrequencies) { |
+ constexpr size_t kNumBlocks = 1000; |
+ |
+ for (auto rate : {8000, 16000, 32000, 48000}) { |
+ ProduceDebugText(rate); |
+ float input[kBlockSize * kNumBlocks]; |
+ |
+ for (size_t k = 0; k < kBlockSize * kNumBlocks; ++k) { |
+ input[k] = 32767.f * sin(2.f * kPi * 3.f / 8.f * k); |
hlundin-webrtc
2017/02/01 08:30:01
Produces a sinusoid at frequency 3/8*fs. Comment a
peah-webrtc
2017/02/02 14:04:47
Done.
|
+ } |
+ |
+ DownSampler4kHz down_sampler(rate); |
+ float output[kSubBlockSize * kNumBlocks]; |
+ |
+ for (size_t frame = 0; frame < kNumBlocks; ++frame) { |
+ down_sampler.DownSample( |
+ rtc::ArrayView<const float>(&input[frame * kBlockSize], kBlockSize), |
+ rtc::ArrayView<float>(&output[frame * kSubBlockSize], kSubBlockSize)); |
+ } |
+ |
+ constexpr size_t kNumStartupBlocks = 50; |
+ RTC_DCHECK_GT(kNumBlocks, kNumStartupBlocks); |
+ 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); |
+ const double input_power = |
+ std::accumulate(input_to_evaluate.begin(), input_to_evaluate.end(), 0., |
hlundin-webrtc
2017/02/01 08:30:01
std::inner_product
peah-webrtc
2017/02/02 14:04:47
Done.
|
+ [](float a, float b) -> double { return a + b * b; }) / |
+ input_to_evaluate.size(); |
+ const double output_power = |
+ std::accumulate(output_to_evaluate.begin(), output_to_evaluate.end(), |
hlundin-webrtc
2017/02/01 08:30:01
std::inner_product
peah-webrtc
2017/02/02 14:04:47
Done.
|
+ 0., |
+ [](float a, float b) -> double { return a + b * b; }) / |
+ output_to_evaluate.size(); |
+ // EXPECT_LT(0.25f, downsampled_power); |
aleloi
2017/01/27 15:37:47
Remove outcommented code
peah-webrtc
2017/02/02 14:04:47
Done.
|
+ EXPECT_GT(0.0001f * input_power, output_power); |
+ } |
+} |
+ |
+// Verifies that the impact of low-frequency content is small during the |
+// downsampling. |
+TEST(DownSampler4kHz, NoImpactOnLowerFrequencies) { |
hlundin-webrtc
2017/02/01 08:30:02
Soooo much code dupe with the above. Iiuc, the onl
peah-webrtc
2017/02/02 14:04:47
Done.
|
+ constexpr size_t kNumBlocks = 100; |
+ |
+ for (auto rate : {8000, 16000, 32000, 48000}) { |
+ ProduceDebugText(rate); |
+ float input[kBlockSize * kNumBlocks]; |
+ |
+ for (size_t k = 0; k < kBlockSize * kNumBlocks; ++k) { |
+ input[k] = 32767.f * sin(2.f * kPi * 200.f * k / rate); |
+ } |
+ |
+ DownSampler4kHz down_sampler(rate); |
+ float output[kSubBlockSize * kNumBlocks]; |
+ |
+ for (size_t frame = 0; frame < kNumBlocks; ++frame) { |
+ down_sampler.DownSample( |
+ rtc::ArrayView<const float>(&input[frame * kBlockSize], kBlockSize), |
+ rtc::ArrayView<float>(&output[frame * kSubBlockSize], kSubBlockSize)); |
+ } |
+ |
+ constexpr size_t kNumStartupBlocks = 50; |
+ RTC_DCHECK_GT(kNumBlocks, kNumStartupBlocks); |
+ 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); |
+ const double input_power = |
+ std::accumulate(input_to_evaluate.begin(), input_to_evaluate.end(), 0., |
+ [](float a, float b) -> double { return a + b * b; }) / |
+ input_to_evaluate.size(); |
+ const double output_power = |
+ std::accumulate(output_to_evaluate.begin(), output_to_evaluate.end(), |
+ 0., |
+ [](float a, float b) -> double { return a + b * b; }) / |
+ output_to_evaluate.size(); |
+ EXPECT_LT(0.5f * input_power, output_power); |
aleloi
2017/01/27 15:37:47
It seems wrong if as much as half the energy/time
peah-webrtc
2017/02/02 14:04:47
True. I increased it a bit, but the impact on 8 kH
|
+ } |
+} |
+ |
+#if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID) |
+// Verifies the check for correct sample rate. |
+TEST(DownSampler4kHz, WrongSampleRate) { |
+ EXPECT_DEATH(DownSampler4kHz down_sampler(8001), ""); |
+} |
+ |
+// Verifies the check for the input size. |
+TEST(DownSampler4kHz, WrongInputSize) { |
+ for (auto rate : {8000, 16000, 32000, 48000}) { |
+ ProduceDebugText(rate); |
+ DownSampler4kHz down_sampler(rate); |
+ std::vector<float> x(std::vector<float>(kBlockSize - 1, 0.f)); |
+ std::vector<float> x_downsampled(std::vector<float>(kSubBlockSize, 0.f)); |
+ EXPECT_DEATH(down_sampler.DownSample(x, x_downsampled), ""); |
+ } |
+} |
+ |
+// Verifies the check for the output size. |
+TEST(DownSampler4kHz, WrongOutputSize) { |
+ for (auto rate : {8000, 16000, 32000, 48000}) { |
+ ProduceDebugText(rate); |
+ DownSampler4kHz down_sampler(rate); |
+ std::vector<float> x(std::vector<float>(kBlockSize, 0.f)); |
+ std::vector<float> x_downsampled( |
+ std::vector<float>(kSubBlockSize - 1, 0.f)); |
+ EXPECT_DEATH(down_sampler.DownSample(x, x_downsampled), ""); |
+ } |
+} |
+ |
+#endif |
+ |
+} // namespace webrtc |