Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(725)

Unified Diff: webrtc/modules/audio_processing/intelligibility/test/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: Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: webrtc/modules/audio_processing/intelligibility/test/enhancer_unittest.cc
diff --git a/webrtc/modules/audio_processing/intelligibility/test/enhancer_unittest.cc b/webrtc/modules/audio_processing/intelligibility/test/enhancer_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..169234c643a64af5798631d62fa6592be60c69cd
--- /dev/null
+++ b/webrtc/modules/audio_processing/intelligibility/test/enhancer_unittest.cc
@@ -0,0 +1,201 @@
+/*
+ * Copyright (c) 2014 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 <cmath>
+#include <algorithm>
+
+#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,
+ -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.005;
+
+// 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) {
+ for (size_t i = 0; i < data.size(); i++) {
+ data[i] = constant;
+ }
+}
+
+void GenerateGaussianData(vector<float>& data) {
+ static int count = 0;
+ for (size_t i = 0; i < data.size(); i++) {
+ data[i] = kGaussianSamples[count%64];
+ count++;
+ }
+}
+
+
+class EnhancerTest : public ::testing::Test {
+ protected:
+ IntelligibilityEnhancer enh_;
+ vector<float> clear_data_;
+ vector<float> noise_data_;
+ EnhancerTest() :
+ enh_(kErbResolution,
+ kSampleRate,
+ kNumChannels,
+ VarianceArray::kStepInfinite,
+ kDecayRate,
+ kWindowSize,
+ kAnalyzeRate,
+ kVarianceRate,
+ kGainLimit),
+ clear_data_(kSamples),
+ noise_data_(kSamples) {}
+
+ void RunEnhancer(VarianceArray::StepType step_type) {
turaj 2015/06/26 00:32:58 Could this function use |enh_| instead?
ekm 2015/06/26 19:07:09 That would be better, but I was having trouble mod
turaj 2015/06/29 17:33:35 Sorry, I didn't notice that |step_type| is an inpu
+ 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 each class of generated data, tests plumbing for
+// each variance update method.
+TEST_F(EnhancerTest, TestPlumbing) {
turaj 2015/06/26 00:32:58 what is it that ids tested here? How could it fail
ekm 2015/06/26 19:07:09 Before we switched to doing nothing in case of und
turaj 2015/06/29 17:33:36 Agreed, this is a better test.
+ vector<VarianceArray::StepType> step_types = {
+ VarianceArray::kStepInfinite, VarianceArray::kStepDecaying,
+ VarianceArray::kStepWindowed, VarianceArray::kStepBlocked,
+ VarianceArray::kStepBlockBasedMovingAverage};
+ for (vector<VarianceArray::StepType>::iterator step_type =
+ step_types.begin(); step_type != step_types.end(); ++step_type) {
+ GenerateConstantData(clear_data_, 0.0f);
+ GenerateConstantData(noise_data_, 0.0f);
+ RunEnhancer(*step_type);
+ GenerateConstantData(clear_data_, 500.0f);
+ RunEnhancer(*step_type);
+ GenerateConstantData(noise_data_, 500.0f);
+ RunEnhancer(*step_type);
+ GenerateGaussianData(clear_data_);
+ RunEnhancer(*step_type);
+ GenerateGaussianData(noise_data_);
+ RunEnhancer(*step_type);
+ GenerateConstantData(clear_data_, 0);
+ RunEnhancer(*step_type);
+ }
+}
+
+// Tests ERB bank creation, comparing against matlab output.
+TEST_F(EnhancerTest, TestErbCreation) {
+ ASSERT_EQ(enh_.bank_size_, kTestNumCenterFreqs);
+ for (int i = 0; i < enh_.bank_size_; ++i) {
+ float error = std::fabs(enh_.center_freqs_[i] - kTestCenterFreqs[i]);
+ EXPECT_LT(error, kMaxTestError);
+ ASSERT_EQ(enh_.freqs_, kTestNumFreqs);
+ for (int j = 0; j < enh_.freqs_; ++j) {
+ float error = std::fabs(enh_.filter_bank_[i][j] - kTestFilterBank[i][j]);
+ EXPECT_LT(error, kMaxTestError);
+ }
+ }
+}
+
+// Tests analytic solution for optimal gains, comparing
+// against matlab output.
+TEST_F(EnhancerTest, TestSolveForGains) {
+ ASSERT_EQ(enh_.start_freq_, 12);
+ vector<float> sols(enh_.bank_size_);
+ float lambda = -0.001;
turaj 2015/06/26 00:32:58 I guess you need -0.001f otherwise Visual Studio c
ekm 2015/06/26 19:07:09 Done.
+ 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++) {
+ float error = std::fabs(sols[i] - kTestZeroVar[i]);
+ EXPECT_LT(error, kMaxTestError);
turaj 2015/06/26 00:32:58 I guess you can use EXPECT_NEAR(v1, v2, tolerance)
ekm 2015/06/26 19:07:10 Done.
+ }
+ for (int i = 0; i < enh_.bank_size_; i++) {
+ enh_.filtered_clear_var_[i] = static_cast<float>(i+1);
turaj 2015/06/26 00:32:58 'i + 1'
ekm 2015/06/26 19:07:10 Done.
+ 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++) {
+ float error = std::fabs(sols[i] - kTestNonZeroVarLambdaTop[i]);
+ EXPECT_LT(error, kMaxTestError);
+ }
+ lambda = -1.0;
+ enh_.SolveForGainsGivenLambda(lambda, enh_.start_freq_, &sols[0]);
+ for (int i = 0; i < enh_.bank_size_; i++) {
+ float error = std::fabs(sols[i] - kTestZeroVar[i]);
+ EXPECT_LT(error, kMaxTestError);
+ }
+}
+
+} // namespace webrtc

Powered by Google App Engine
This is Rietveld 408576698