Index: webrtc/modules/audio_processing/aec3/correlator_unittest.cc |
diff --git a/webrtc/modules/audio_processing/aec3/correlator_unittest.cc b/webrtc/modules/audio_processing/aec3/correlator_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..daca956893460c89357ffed5ad523e006e3d76d2 |
--- /dev/null |
+++ b/webrtc/modules/audio_processing/aec3/correlator_unittest.cc |
@@ -0,0 +1,213 @@ |
+/* |
+ * 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/correlator.h" |
+ |
+#include <algorithm> |
+#include <sstream> |
+#include <string> |
+ |
+#include "webrtc/modules/audio_processing/aec3/aec3_constants.h" |
+#include "webrtc/modules/audio_processing/logging/apm_data_dumper.h" |
+#include "webrtc/modules/audio_processing/test/echo_canceller_test_tools.h" |
+#include "webrtc/test/gtest.h" |
+ |
+namespace webrtc { |
+namespace { |
+ |
+std::string ProduceDebugText(size_t delay) { |
+ std::ostringstream ss; |
+ ss << "Delay: " << delay; |
+ return ss.str(); |
+} |
+ |
+} // namespace |
+ |
+// Verifies that the correlator produces proper lag estimates for artificially |
+// delayed signals. |
+TEST(Correlator, LagEstimation) { |
+ Random random_generator(42U); |
+ std::vector<float> render(kSubBlockSize, 0.f); |
+ std::vector<float> capture(kSubBlockSize, 0.f); |
+ ApmDataDumper data_dumper(0); |
+ for (size_t delay_samples : {0, 64, 150, 200, 800, 1000}) { |
+ SCOPED_TRACE(ProduceDebugText(delay_samples)); |
+ DelayBuffer<float> signal_delay_buffer(delay_samples); |
+ constexpr size_t kWindowSizeSubBlocks = 32; |
+ constexpr size_t kAlignmentShiftSubBlocks = kWindowSizeSubBlocks * 3 / 4; |
+ constexpr size_t kNumAlignmentShifts = 3; |
+ Correlator correlator(&data_dumper, kWindowSizeSubBlocks, |
+ kNumAlignmentShifts, kAlignmentShiftSubBlocks); |
+ |
+ // Analyze the correlation between render and capture. |
+ for (size_t k = 0; k < (100 + delay_samples / kSubBlockSize); ++k) { |
+ RandomizeSampleVector(&random_generator, render); |
+ signal_delay_buffer.Delay(render, capture); |
+ correlator.Update(render, capture); |
+ } |
+ |
+ // Obtain the lag estimates. |
+ rtc::ArrayView<const Correlator::LagEstimate> lag_estimates = |
hlundin-webrtc
2017/02/01 08:30:01
auto
peah-webrtc
2017/02/02 14:04:46
Done.
|
+ correlator.GetLagEstimates(); |
+ EXPECT_EQ(kNumAlignmentShifts + 1, lag_estimates.size()); |
hlundin-webrtc
2017/02/01 08:30:01
You are already checking this explicitly in a test
peah-webrtc
2017/02/02 14:04:46
Done.
|
+ |
+ // Find which lag estimate should be the most accurate. |
+ rtc::Optional<size_t> expected_most_accurate_lag_estimate; |
+ size_t alignment_shift_sub_blocks = 0; |
+ for (size_t k = 0; k < kNumAlignmentShifts; ++k) { |
+ if ((alignment_shift_sub_blocks + kWindowSizeSubBlocks / 2) * |
+ kSubBlockSize > |
+ delay_samples) { |
+ expected_most_accurate_lag_estimate = rtc::Optional<size_t>(k); |
+ break; |
+ } |
+ alignment_shift_sub_blocks += kAlignmentShiftSubBlocks; |
+ } |
+ RTC_DCHECK(expected_most_accurate_lag_estimate); |
hlundin-webrtc
2017/02/01 08:30:01
ASSERT_TRUE
peah-webrtc
2017/02/02 14:04:46
Done.
|
+ |
+ // Verify that the expected most accurate lag estimate is the most accurate |
+ // estimate. |
+ for (size_t k = 0; k < kNumAlignmentShifts; ++k) { |
+ if (k != static_cast<size_t>(*expected_most_accurate_lag_estimate)) { |
hlundin-webrtc
2017/02/01 08:30:01
Why the cast?
peah-webrtc
2017/02/02 14:04:46
Done.
|
+ EXPECT_GT(lag_estimates[*expected_most_accurate_lag_estimate].accuracy, |
+ lag_estimates[k].accuracy); |
+ } |
+ } |
+ |
+ // Verify that all lag estimates are updated as expected for signals |
+ // containing strong noise. |
+ for (size_t k = 0; k < kNumAlignmentShifts; ++k) { |
+ EXPECT_TRUE(lag_estimates[k].updated); |
+ } |
+ |
+ // Verify that the expected most accurate lag estimate is reliable. |
+ EXPECT_TRUE(lag_estimates[*expected_most_accurate_lag_estimate].reliable); |
+ |
+ // Verify that the expected most accurate lag estimate is correct. |
+ EXPECT_EQ(delay_samples, |
+ lag_estimates[*expected_most_accurate_lag_estimate].lag); |
+ } |
+} |
+ |
+// Verifies that the correlator does not produce reliable and accurate estimates |
+// for uncorrelated render and capture signals. |
+TEST(Correlator, LagNotUpdatedForLowLevelRender) { |
+ Random random_generator(42U); |
+ std::vector<float> render(kSubBlockSize, 0.f); |
+ std::vector<float> capture(kSubBlockSize, 0.f); |
+ ApmDataDumper data_dumper(0); |
+ constexpr size_t kWindowSizeSubBlocks = 32; |
+ constexpr size_t kAlignmentShiftSubBlocks = kWindowSizeSubBlocks * 3 / 4; |
+ constexpr size_t kNumAlignmentShifts = 3; |
+ Correlator correlator(&data_dumper, kWindowSizeSubBlocks, kNumAlignmentShifts, |
+ kAlignmentShiftSubBlocks); |
+ |
+ // Analyze the correlation between render and capture. |
+ for (size_t k = 0; k < 100; ++k) { |
+ RandomizeSampleVector(&random_generator, render); |
+ RandomizeSampleVector(&random_generator, capture); |
+ correlator.Update(render, capture); |
+ } |
+ |
+ // Obtain the lag estimates. |
+ rtc::ArrayView<const Correlator::LagEstimate> lag_estimates = |
+ correlator.GetLagEstimates(); |
+ EXPECT_EQ(kNumAlignmentShifts + 1, lag_estimates.size()); |
+ |
+ // Verify that no lag estimates are reliable. |
+ for (size_t k = 0; k < kNumAlignmentShifts; ++k) { |
hlundin-webrtc
2017/02/01 08:30:01
for (auto le : lag_estimates) {
EXPECT_FALSE(le.
peah-webrtc
2017/02/02 14:04:46
Done.
aleloi
2017/02/02 16:33:11
That's not equivalent. lag_estimates has kNumAlign
peah-webrtc
2017/02/03 06:59:59
You are correct. I mixed this up. It is now correc
|
+ EXPECT_FALSE(lag_estimates[k].reliable); |
+ } |
+} |
+ |
+// Verifies that the correlator does not produce updated lag estimates for |
+// render signals of low level. |
+TEST(Correlator, LagNotReliableForUncorrelatedRenderAndCapture) { |
+ Random random_generator(42U); |
+ std::vector<float> render(kSubBlockSize, 0.f); |
+ std::vector<float> capture(kSubBlockSize, 0.f); |
+ ApmDataDumper data_dumper(0); |
+ constexpr size_t kWindowSizeSubBlocks = 32; |
hlundin-webrtc
2017/02/01 08:30:01
These constexprs are the same for all tests. Consi
peah-webrtc
2017/02/02 14:04:46
Done.
|
+ constexpr size_t kAlignmentShiftSubBlocks = kWindowSizeSubBlocks * 3 / 4; |
+ constexpr size_t kNumAlignmentShifts = 3; |
+ Correlator correlator(&data_dumper, kWindowSizeSubBlocks, kNumAlignmentShifts, |
+ kAlignmentShiftSubBlocks); |
+ |
+ // Analyze the correlation between render and capture. |
+ for (size_t k = 0; k < 100; ++k) { |
+ RandomizeSampleVector(&random_generator, render); |
+ for (auto& render_k : render) { |
+ render_k *= 149.f / 32767.f; |
+ } |
+ std::copy(render.begin(), render.end(), capture.begin()); |
+ correlator.Update(render, capture); |
+ } |
+ |
+ // Obtain the lag estimates. |
+ rtc::ArrayView<const Correlator::LagEstimate> lag_estimates = |
+ correlator.GetLagEstimates(); |
+ EXPECT_EQ(kNumAlignmentShifts + 1, lag_estimates.size()); |
+ |
+ // Verify that no lag estimates are updated. |
+ for (size_t k = 0; k < kNumAlignmentShifts; ++k) { |
hlundin-webrtc
2017/02/01 08:30:01
Consider range-based. Also, you can merge this and
peah-webrtc
2017/02/02 14:04:46
Done.
|
+ EXPECT_FALSE(lag_estimates[k].updated); |
+ } |
+ |
+ // Verify that no lag estimates are reliable. |
+ for (size_t k = 0; k < kNumAlignmentShifts; ++k) { |
+ EXPECT_FALSE(lag_estimates[k].reliable); |
+ } |
+} |
+ |
+// Verifies that the correct number of lag estimates are produced for a certain |
+// number of alignment shifts. |
+TEST(Correlator, NumberOfLagEstimates) { |
+ ApmDataDumper data_dumper(0); |
+ for (size_t num_alignment_shifts = 0; num_alignment_shifts < 10; |
+ ++num_alignment_shifts) { |
+ Correlator correlator(&data_dumper, 32, num_alignment_shifts, 1); |
+ EXPECT_EQ(num_alignment_shifts + 1, correlator.GetLagEstimates().size()); |
+ } |
+} |
+ |
+#if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID) |
+ |
+// Verifies the check for the render signal sub block size. |
+TEST(Correlator, WrongRenderSize) { |
+ std::vector<float> render(kSubBlockSize - 1, 0.f); |
+ std::vector<float> capture(kSubBlockSize, 0.f); |
+ ApmDataDumper data_dumper(0); |
+ Correlator correlator(&data_dumper, 1, 1, 1); |
+ EXPECT_DEATH(correlator.Update(render, capture), ""); |
+} |
+ |
+// Verifies the check for the capture signal sub block size. |
+TEST(Correlator, WrongCaptureSize) { |
+ std::vector<float> render(kSubBlockSize, 0.f); |
+ std::vector<float> capture(kSubBlockSize - 1, 0.f); |
+ ApmDataDumper data_dumper(0); |
+ Correlator correlator(&data_dumper, 1, 1, 1); |
+ EXPECT_DEATH(correlator.Update(render, capture), ""); |
+} |
+ |
+// Verifies the check for non-zero windows size. |
+TEST(Correlator, ZeroWindowSize) { |
+ ApmDataDumper data_dumper(0); |
+ EXPECT_DEATH(Correlator(&data_dumper, 0, 1, 1), ""); |
+} |
+ |
+// Verifies the check for non-null data dumper. |
+TEST(Correlator, NullDataDumper) { |
+ EXPECT_DEATH(Correlator(nullptr, 1, 1, 1), ""); |
+} |
+ |
+#endif |
+ |
+} // namespace webrtc |