Index: webrtc/modules/audio_processing/aec3/subtractor_unittest.cc |
diff --git a/webrtc/modules/audio_processing/aec3/subtractor_unittest.cc b/webrtc/modules/audio_processing/aec3/subtractor_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..34f40528b5e086bc0aab0309ba98e6aa3bfba7e0 |
--- /dev/null |
+++ b/webrtc/modules/audio_processing/aec3/subtractor_unittest.cc |
@@ -0,0 +1,170 @@ |
+/* |
+ * 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/subtractor.h" |
+ |
+#include <algorithm> |
+#include <numeric> |
+#include <string> |
+ |
+#include "webrtc/base/random.h" |
+#include "webrtc/modules/audio_processing/aec3/delay_handler.h" |
+#include "webrtc/modules/audio_processing/test/echo_canceller_test_tools.h" |
+#include "webrtc/test/gtest.h" |
+ |
+namespace webrtc { |
+namespace { |
+ |
+void RunSubtractorTest(int num_blocks_to_process, |
aleloi
2017/02/13 16:44:51
I can't find a reason for why echo_to_nearend_powe
peah-webrtc
2017/02/20 07:37:19
Done.
|
+ int delay_samples, |
+ bool uncorrelated_inputs, |
+ const std::vector<int>& blocks_with_echo_path_changes, |
+ float* echo_to_nearend_power) { |
+ ApmDataDumper data_dumper(42); |
+ Subtractor subtractor(&data_dumper); |
+ std::vector<float> x(kBlockSize, 0.f); |
+ std::vector<float> y(kBlockSize, 0.f); |
+ std::array<float, kBlockSize> x_old; |
+ SubtractorOutput output; |
+ FftBuffer X_buffer( |
+ subtractor.MinFarendBufferLength(), |
+ std::vector<size_t>(1, subtractor.MinFarendBufferLength())); |
+ RenderSignalAnalyzer render_signal_analyzer; |
+ Random random_generator(42U); |
+ Aec3Fft fft; |
+ FftData X; |
+ DelayHandler delay_handler; |
+ x_old.fill(0.f); |
+ |
+ DelayBuffer<float> delay_buffer(delay_samples); |
+ for (int k = 0; k < num_blocks_to_process; ++k) { |
+ RandomizeSampleVector(&random_generator, x); |
+ if (uncorrelated_inputs) { |
+ RandomizeSampleVector(&random_generator, y); |
+ } else { |
+ delay_buffer.Delay(x, y); |
+ } |
+ fft.PaddedFft(x, x_old, &X); |
+ X_buffer.Insert(X); |
+ render_signal_analyzer.Update(X_buffer, delay_handler.FilterDelay()); |
+ |
+ // Handle echo path changes. |
+ if (std::find(blocks_with_echo_path_changes.begin(), |
+ blocks_with_echo_path_changes.end(), |
+ k) != blocks_with_echo_path_changes.end()) { |
+ subtractor.HandleEchoPathChange(EchoPathVariability(true, true)); |
+ } |
+ subtractor.Process(X_buffer, y, render_signal_analyzer, false, &output); |
+ |
+ delay_handler.UpdateDelays( |
+ subtractor.FilterFrequencyResponse(), |
+ rtc::Optional<size_t>(delay_samples / kBlockSize)); |
+ } |
+ |
+ float output_power = std::inner_product( |
+ output.e_main.begin(), output.e_main.end(), output.e_main.begin(), 0.f); |
+ float y_power = std::inner_product(y.begin(), y.end(), y.begin(), 0.f); |
aleloi
2017/02/13 16:44:51
const float *_power
peah-webrtc
2017/02/20 07:37:19
Done.
|
+ ASSERT_LT(0.f, y_power); |
+ *echo_to_nearend_power = output_power / y_power; |
+} |
+ |
+std::string ProduceDebugText(size_t delay) { |
+ std::ostringstream ss; |
+ ss << "Delay: " << delay; |
+ return ss.str(); |
+} |
+ |
+} // namespace |
+ |
+#if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID) |
+ |
+// Verifies that the check for non data dumper works. |
+TEST(Subtractor, NullDataDumper) { |
+ EXPECT_DEATH(Subtractor(nullptr), ""); |
+} |
+ |
+// Verifies the check for null subtractor output. |
+TEST(Subtractor, NullOutput) { |
+ ApmDataDumper data_dumper(42); |
+ Subtractor subtractor(&data_dumper); |
+ FftBuffer X_buffer( |
+ subtractor.MinFarendBufferLength(), |
+ std::vector<size_t>(1, subtractor.MinFarendBufferLength())); |
+ RenderSignalAnalyzer render_signal_analyzer; |
+ std::vector<float> y(kBlockSize, 0.f); |
+ |
+ EXPECT_DEATH( |
+ subtractor.Process(X_buffer, y, render_signal_analyzer, false, nullptr), |
+ ""); |
+} |
+ |
+// Verifies the check for the capture signal size. |
+TEST(Subtractor, WrongCaptureSize) { |
+ ApmDataDumper data_dumper(42); |
+ Subtractor subtractor(&data_dumper); |
+ FftBuffer X_buffer( |
+ subtractor.MinFarendBufferLength(), |
+ std::vector<size_t>(1, subtractor.MinFarendBufferLength())); |
+ RenderSignalAnalyzer render_signal_analyzer; |
+ std::vector<float> y(kBlockSize - 1, 0.f); |
+ SubtractorOutput output; |
+ |
+ EXPECT_DEATH( |
+ subtractor.Process(X_buffer, y, render_signal_analyzer, false, &output), |
+ ""); |
+} |
+ |
+#endif |
+ |
+// Verifies that the subtractor is able to converge on correlated data. |
+TEST(Subtractor, Convergence) { |
+ std::vector<int> blocks_with_echo_path_changes; |
+ // blocks_with_echo_path_changes.push_back(99); |
aleloi
2017/02/13 16:44:51
remove comment
peah-webrtc
2017/02/20 07:37:19
Done.
|
+ for (size_t delay_samples : {0, 64, 150, 200, 301}) { |
+ SCOPED_TRACE(ProduceDebugText(delay_samples)); |
+ |
+ float echo_to_nearend_power; |
+ RunSubtractorTest(100, delay_samples, false, blocks_with_echo_path_changes, |
+ &echo_to_nearend_power); |
+ EXPECT_GT(0.1f, echo_to_nearend_power); |
+ } |
+} |
+ |
+// Verifies that the subtractor does not converge on uncorrelated signals. |
+TEST(Subtractor, NonConvergenceOnUncorrelatedSignals) { |
+ std::vector<int> blocks_with_echo_path_changes; |
+ // blocks_with_echo_path_changes.push_back(99); |
aleloi
2017/02/13 16:44:51
remove comment
peah-webrtc
2017/02/20 07:37:19
Done.
|
+ for (size_t delay_samples : {0, 64, 150, 200, 301}) { |
+ SCOPED_TRACE(ProduceDebugText(delay_samples)); |
+ |
+ float echo_to_nearend_power; |
+ RunSubtractorTest(100, delay_samples, true, blocks_with_echo_path_changes, |
+ &echo_to_nearend_power); |
+ EXPECT_LT(0.95f, echo_to_nearend_power); |
+ EXPECT_GT(1.05f, echo_to_nearend_power); |
aleloi
2017/02/13 16:44:51
I think EXPECT_NEAR(a, b, 0.05f) can be used here.
peah-webrtc
2017/02/20 07:37:19
Done.
|
+ } |
+} |
+ |
+// Verifies that the subtractor is properly reset when there is an echo path |
+// change. |
+TEST(Subtractor, EchoPathChangeReset) { |
+ std::vector<int> blocks_with_echo_path_changes; |
+ blocks_with_echo_path_changes.push_back(99); |
+ for (size_t delay_samples : {0, 64, 150, 200, 301}) { |
+ SCOPED_TRACE(ProduceDebugText(delay_samples)); |
+ |
+ float echo_to_nearend_power; |
+ RunSubtractorTest(100, delay_samples, false, blocks_with_echo_path_changes, |
+ &echo_to_nearend_power); |
+ EXPECT_EQ(1.f, echo_to_nearend_power); |
aleloi
2017/02/13 16:44:51
Does EXPECT_EQ use exact comparison for floats? Ma
peah-webrtc
2017/02/21 23:00:39
Done.
|
+ } |
+} |
+ |
+} // namespace webrtc |