Chromium Code Reviews

Unified Diff: webrtc/modules/audio_processing/intelligibility/intelligibility_enhancer_unittest.cc

Issue 1207353002: Add new variance update option and unittests for intelligibility (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Renamed tests + minor changes Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
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..91b27d5a98f6323feb391b16b3d354ab71e57156
--- /dev/null
+++ b/webrtc/modules/audio_processing/intelligibility/intelligibility_enhancer_unittest.cc
@@ -0,0 +1,202 @@
+/*
+ * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
hlundin-webrtc 2015/06/30 14:00:53 2015 And please check all other files that have be
ekm 2015/07/01 23:48:26 Done.
+ *
+ * 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 <cmath>
hlundin-webrtc 2015/06/30 14:00:53 math.h
ekm 2015/07/01 23:48:25 Done.
+#include <algorithm>
+#include <vector>
+
+#include "testing/gtest/include/gtest/gtest.h"
+#include "webrtc/common_audio/signal_processing/include/signal_processing_library.h"
+#include "webrtc/modules/audio_processing/intelligibility/intelligibility_enhancer.h"
+
+using std::vector;
+using webrtc::intelligibility::VarianceArray;
+
+namespace webrtc {
+
+// Generated with matlab code: normrnd(0,1000,64,1).
+const double kGaussianSamples[64] = {1689.1, 1437, -2251.1, 356.49, -850.24,
hlundin-webrtc 2015/06/30 14:00:52 Encapsulate all constants in an anonymous namespac
hlundin-webrtc 2015/06/30 14:00:53 The constant declarations are a bit messy. I sugge
ekm 2015/07/01 23:48:26 Done.
ekm 2015/07/01 23:48:26 Done.
Andrew MacDonald 2015/07/02 02:46:47 In fact, it's a good practice to wrap the entire t
ekm 2015/07/07 21:57:02 Got it. Leaving as is in this case since I'm using
+ -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};
+
+// Target output for ERB create test. Generated with matlab.
+const double kTestNumCenterFreqs = 22;
+const double kTestCenterFreqs[22] = {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 kTestNumFreqs = 2;
+const double kTestFilterBank[22][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} };
+// Target output for gain solving test. Generated with matlab.
+const double kTestZeroVar[22] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
+const double kTestNonZeroVarLambdaTop[22] = {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};
+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;
+
+void GenerateConstantData(vector<float>& data, float constant) {
hlundin-webrtc 2015/06/30 14:00:53 Don't use non-const reference arguments; use point
ekm 2015/07/01 23:48:25 Done.
+ for (size_t i = 0; i < data.size(); i++) {
+ data[i] = constant;
+ }
+}
+
+void GenerateGaussianData(vector<float>& data) {
hlundin-webrtc 2015/06/30 14:00:53 Again, don't use non-const reference.
ekm 2015/07/01 23:48:26 Done.
+ static int count = 0;
hlundin-webrtc 2015/06/30 14:00:53 Static... <cringe>...
ekm 2015/07/01 23:48:26 Done.
+ for (size_t i = 0; i < data.size(); i++) {
+ data[i] = kGaussianSamples[count%64];
+ count++;
+ }
+}
+
+
+class IntelligibilityEnhancerTest : public ::testing::Test {
+ protected:
+ IntelligibilityEnhancer enh_;
hlundin-webrtc 2015/06/30 14:00:53 Declaration order is wrong. http://google-stylegui
ekm 2015/07/01 23:48:26 Done.
+ vector<float> clear_data_;
+ vector<float> noise_data_;
+ vector<float> orig_data_;
+ 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) {
hlundin-webrtc 2015/06/30 14:00:53 Spaces around +=
ekm 2015/07/01 23:48:26 Done.
+ enh.ProcessCaptureAudio(&noise_cursor);
+ enh.ProcessRenderAudio(&clear_cursor);
+ clear_cursor += kFragmentSize;
+ noise_cursor += kFragmentSize;
+ }
+ float updated = false;
hlundin-webrtc 2015/06/30 14:00:52 bool
ekm 2015/07/01 23:48:26 Done.
+ for (int i = 0; i < kSamples; i++) {
+ if (std::fabs(clear_data_[i] - orig_data_[i]) > kMaxTestError) {
+ updated = true;
hlundin-webrtc 2015/06/30 14:00:53 You can return true here, skip the bool variable,
ekm 2015/07/01 23:48:26 Done.
+ }
+ }
+ return updated;
+ }
+};
+
+// 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};
+ for (VarianceArray::StepType step_type : step_types) {
hlundin-webrtc 2015/06/30 14:00:53 for (auto step_type : step_types)
ekm 2015/07/01 23:48:26 Done. Thanks; nice to know auto is supported in th
+ GenerateConstantData(clear_data_, 0.0f);
hlundin-webrtc 2015/06/30 14:00:52 Do you have to regenerate the data on each loop?
ekm 2015/07/01 23:48:26 Done. The clear data needs to be regenerated, but
+ orig_data_ = clear_data_;
+ GenerateConstantData(noise_data_, 0.0f);
+ EXPECT_FALSE(CheckUpdate(step_type));
+ GenerateGaussianData(noise_data_);
+ EXPECT_FALSE(CheckUpdate(step_type));
+ GenerateGaussianData(clear_data_);
+ orig_data_ = clear_data_;
+ EXPECT_TRUE(CheckUpdate(step_type));
+ }
+}
+
+// Tests ERB bank creation, comparing against matlab output.
+TEST_F(IntelligibilityEnhancerTest, TestErbCreation) {
+ ASSERT_EQ(enh_.bank_size_, kTestNumCenterFreqs);
+ for (int i = 0; i < enh_.bank_size_; ++i) {
+ EXPECT_NEAR(enh_.center_freqs_[i], kTestCenterFreqs[i], kMaxTestError);
+ ASSERT_EQ(enh_.freqs_, kTestNumFreqs);
+ for (int j = 0; j < enh_.freqs_; ++j) {
+ EXPECT_NEAR(enh_.filter_bank_[i][j], kTestFilterBank[i][j],
+ kMaxTestError);
hlundin-webrtc 2015/06/30 14:00:52 Weird line wrapping. Run "git cl format" on the pa
ekm 2015/07/01 23:48:26 Done.
+ }
+ }
+}
+
+// Tests analytic solution for optimal gains, comparing
+// against matlab output.
+TEST_F(IntelligibilityEnhancerTest, TestSolveForGains) {
+ ASSERT_EQ(enh_.start_freq_, 12);
hlundin-webrtc 2015/06/30 14:00:52 What is 12? And why is it crucial that start_freq_
ekm 2015/07/01 23:48:26 Done.
+ 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

Powered by Google App Engine