Index: webrtc/modules/audio_processing/intelligibility/intelligibility_enhancer_unittest.cc |
diff --git a/webrtc/modules/audio_processing/intelligibility/intelligibility_enhancer_unittest.cc b/webrtc/modules/audio_processing/intelligibility/intelligibility_enhancer_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..845f4fc3f97c2ad3825c777540131b0743628afd |
--- /dev/null |
+++ b/webrtc/modules/audio_processing/intelligibility/intelligibility_enhancer_unittest.cc |
@@ -0,0 +1,226 @@ |
+/* |
+ * Copyright (c) 2015 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. |
+ */ |
+ |
+// |
+// Unit tests for intelligibility enhancer. |
+// |
+ |
+#include <math.h> |
+#include <algorithm> |
+#include <vector> |
+ |
+#include "testing/gtest/include/gtest/gtest.h" |
+#include "webrtc/base/arraysize.h" |
+#include "webrtc/common_audio/signal_processing/include/signal_processing_library.h" |
+#include "webrtc/modules/audio_processing/intelligibility/intelligibility_enhancer.h" |
+ |
+namespace { |
+ |
+// Generated with matlab code: normrnd(0,1000,64,1). |
+const double kGaussianSamples[] = { |
+ 1689.1, 1437, -2251.1, 356.49, -850.24, -299.55, -634.25, 1624.5, |
+ 1241.1, 555.28, 703.42, 458.16, 683.98, 251.29, -178.5, 507.73, |
+ -309.9, -394.37, -269.74, -88.13, 8.0293, 2531.8, -1223.2, -1071.8, |
+ 246.06, -50.611, -730.15, 326.99, 752.99, -1153.7, -407.87, -1287.9, |
+ 83.578, 163.8, 682.57, -1086.4, 297.49, -143.31, 1392, 306.75, |
+ -537.18, -228.93, -536.22, 1439, -511.1, -1606.8, -201.24, 1143.5, |
+ 663.29, 164.08, 1785.4, -587.71, 259.04, -871.83, -787.92, -344.34, |
+ 647.62, 2054.1, 798.94, -1071.1, -205.16, -554.44, -292.94, 1180.2}; |
+static_assert(arraysize(kGaussianSamples) == 64, "Samples badly initialized."); |
+ |
hlundin-webrtc
2015/07/02 10:53:13
You have a blank line here that is not present bet
ekm
2015/07/07 21:57:02
Done.
|
+// Target output for ERB create test. Generated with matlab. |
+const double kTestCenterFreqs[] = { |
+ 13.169, 26.965, 41.423, 56.577, 72.461, 89.113, 106.57, 124.88, |
+ 144.08, 164.21, 185.34, 207.5, 230.75, 255.16, 280.77, 307.66, |
+ 335.9, 365.56, 396.71, 429.44, 463.84, 500}; |
+const double kTestFilterBank[][2] = {{0.055556, 0}, |
+ {0.055556, 0}, |
+ {0.055556, 0}, |
+ {0.055556, 0}, |
+ {0.055556, 0}, |
+ {0.055556, 0}, |
+ {0.055556, 0}, |
+ {0.055556, 0}, |
+ {0.055556, 0}, |
+ {0.055556, 0}, |
+ {0.055556, 0}, |
+ {0.055556, 0}, |
+ {0.055556, 0}, |
+ {0.055556, 0}, |
+ {0.055556, 0}, |
+ {0.055556, 0}, |
+ {0.055556, 0}, |
+ {0.055556, 0.2}, |
+ {0, 0.2}, |
+ {0, 0.2}, |
+ {0, 0.2}, |
+ {0, 0.2}}; |
+static_assert(arraysize(kTestCenterFreqs) == arraysize(kTestFilterBank), |
+ "Test filterbank badly initialized."); |
+// Target output for gain solving test. Generated with matlab. |
+const int kTestStartFreq = 12; // Lowest integral frequency for ERBs. |
+const double kTestZeroVar[] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; |
+static_assert(arraysize(kTestCenterFreqs) == arraysize(kTestZeroVar), |
+ "Variance test data badly initialized."); |
+const double kTestNonZeroVarLambdaTop[] = { |
+ 1, 1, 1, 1, 1, 1, 1, 1, |
+ 1, 1, 1, 0, 0, 0.0351, 0.0636, 0.0863, |
+ 0.1037, 0.1162, 0.1236, 0.1251, 0.1189, 0.0993}; |
+static_assert(arraysize(kTestCenterFreqs) == |
+ arraysize(kTestNonZeroVarLambdaTop), |
+ "Variance test data badly initialized."); |
+const float kMaxTestError = 0.005f; |
+ |
+// Enhancer initialization parameters. |
+const int kSamples = 2000; |
+const int kErbResolution = 2; |
+const int kSampleRate = 1000; |
+const int kFragmentSize = kSampleRate / 100; |
+const int kNumChannels = 1; |
+const float kDecayRate = 0.9f; |
+const int kWindowSize = 800; |
+const int kAnalyzeRate = 800; |
+const int kVarianceRate = 2; |
+const float kGainLimit = 0.1f; |
+ |
+} // namespace |
+ |
+namespace webrtc { |
+ |
+using std::vector; |
+using intelligibility::VarianceArray; |
+ |
+void GenerateConstantData(vector<float>* data, float constant) { |
Andrew MacDonald
2015/07/02 02:46:48
Replace this function with:
std::fill(data.begin()
ekm
2015/07/07 21:57:02
Done.
|
+ for (size_t i = 0; i < data->size(); i++) { |
+ (*data)[i] = constant; |
+ } |
+} |
+ |
+void GenerateGaussianData(vector<float>* data, int* count) { |
Andrew MacDonald
2015/07/02 02:46:48
Does this need to be Gaussian distributed, or will
ekm
2015/07/07 21:57:02
Done.
|
+ for (size_t i = 0; i < data->size(); i++) { |
+ (*data)[i] = kGaussianSamples[(*count) % 64]; |
+ (*count)++; |
+ } |
+} |
+ |
+class IntelligibilityEnhancerTest : public ::testing::Test { |
+ protected: |
+ IntelligibilityEnhancerTest() |
+ : enh_(kErbResolution, |
+ kSampleRate, |
+ kNumChannels, |
+ VarianceArray::kStepInfinite, |
+ kDecayRate, |
+ kWindowSize, |
+ kAnalyzeRate, |
+ kVarianceRate, |
+ kGainLimit), |
+ clear_data_(kSamples), |
+ noise_data_(kSamples), |
+ orig_data_(kSamples) {} |
+ |
+ bool CheckUpdate(VarianceArray::StepType step_type) { |
+ IntelligibilityEnhancer enh(kErbResolution, kSampleRate, kNumChannels, |
+ step_type, kDecayRate, kWindowSize, |
+ kAnalyzeRate, kVarianceRate, kGainLimit); |
+ float* clear_cursor = &clear_data_[0]; |
+ float* noise_cursor = &noise_data_[0]; |
+ for (int i = 0; i < kSamples; i += kFragmentSize) { |
+ enh.ProcessCaptureAudio(&noise_cursor); |
+ enh.ProcessRenderAudio(&clear_cursor); |
+ clear_cursor += kFragmentSize; |
+ noise_cursor += kFragmentSize; |
+ } |
+ for (int i = 0; i < kSamples; i++) { |
+ if (std::fabs(clear_data_[i] - orig_data_[i]) > kMaxTestError) { |
+ return true; |
+ } |
+ } |
+ return false; |
+ } |
+ |
+ IntelligibilityEnhancer enh_; |
+ vector<float> clear_data_; |
+ vector<float> noise_data_; |
+ vector<float> orig_data_; |
+}; |
+ |
+// For each class of generated data, tests that render stream is |
+// updated when it should be for each variance update method. |
+TEST_F(IntelligibilityEnhancerTest, TestRenderUpdate) { |
+ vector<VarianceArray::StepType> step_types = { |
+ VarianceArray::kStepInfinite, |
+ VarianceArray::kStepDecaying, |
+ VarianceArray::kStepWindowed, |
+ VarianceArray::kStepBlocked, |
+ VarianceArray::kStepBlockBasedMovingAverage}; |
+ GenerateConstantData(&noise_data_, 0.0f); |
+ GenerateConstantData(&orig_data_, 0.0f); |
+ for (auto step_type : step_types) { |
+ GenerateConstantData(&clear_data_, 0.0f); |
+ EXPECT_FALSE(CheckUpdate(step_type)); |
+ } |
+ int samples_grabbed = 0; |
+ GenerateGaussianData(&noise_data_, &samples_grabbed); |
+ for (auto step_type : step_types) { |
+ EXPECT_FALSE(CheckUpdate(step_type)); |
+ } |
+ for (auto step_type : step_types) { |
+ GenerateGaussianData(&clear_data_, &samples_grabbed); |
+ orig_data_ = clear_data_; |
+ EXPECT_TRUE(CheckUpdate(step_type)); |
+ } |
+} |
+ |
+// Tests ERB bank creation, comparing against matlab output. |
+TEST_F(IntelligibilityEnhancerTest, TestErbCreation) { |
+ ASSERT_EQ(static_cast<int>(arraysize(kTestCenterFreqs)), enh_.bank_size_); |
+ for (int i = 0; i < enh_.bank_size_; ++i) { |
+ EXPECT_NEAR(enh_.center_freqs_[i], kTestCenterFreqs[i], kMaxTestError); |
+ ASSERT_EQ(static_cast<int>(arraysize(kTestFilterBank[0])), enh_.freqs_); |
+ for (int j = 0; j < enh_.freqs_; ++j) { |
+ EXPECT_NEAR(enh_.filter_bank_[i][j], kTestFilterBank[i][j], |
+ kMaxTestError); |
+ } |
+ } |
+} |
+ |
+// Tests analytic solution for optimal gains, comparing |
+// against matlab output. |
+TEST_F(IntelligibilityEnhancerTest, TestSolveForGains) { |
+ ASSERT_EQ(kTestStartFreq, enh_.start_freq_); |
+ vector<float> sols(enh_.bank_size_); |
+ float lambda = -0.001f; |
+ for (int i = 0; i < enh_.bank_size_; i++) { |
+ enh_.filtered_clear_var_[i] = 0.0; |
+ enh_.filtered_noise_var_[i] = 0.0; |
+ enh_.rho_[i] = 0.02; |
+ } |
+ enh_.SolveForGainsGivenLambda(lambda, enh_.start_freq_, &sols[0]); |
+ for (int i = 0; i < enh_.bank_size_; i++) { |
+ EXPECT_NEAR(sols[i], kTestZeroVar[i], kMaxTestError); |
+ } |
+ for (int i = 0; i < enh_.bank_size_; i++) { |
+ enh_.filtered_clear_var_[i] = static_cast<float>(i + 1); |
+ enh_.filtered_noise_var_[i] = static_cast<float>(enh_.bank_size_ - i); |
+ } |
+ enh_.SolveForGainsGivenLambda(lambda, enh_.start_freq_, &sols[0]); |
+ for (int i = 0; i < enh_.bank_size_; i++) { |
+ EXPECT_NEAR(sols[i], kTestNonZeroVarLambdaTop[i], kMaxTestError); |
+ } |
+ lambda = -1.0; |
+ enh_.SolveForGainsGivenLambda(lambda, enh_.start_freq_, &sols[0]); |
+ for (int i = 0; i < enh_.bank_size_; i++) { |
+ EXPECT_NEAR(sols[i], kTestZeroVar[i], kMaxTestError); |
+ } |
+} |
+ |
+} // namespace webrtc |