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

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: Simplified test data generation Created 5 years, 5 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/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..5013e4c63f35ee322737168096f4246699cbd757
--- /dev/null
+++ b/webrtc/modules/audio_processing/intelligibility/intelligibility_enhancer_unittest.cc
@@ -0,0 +1,205 @@
+/*
+ * 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 <stdlib.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 webrtc {
+
+namespace {
+
+// 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
+
+using std::vector;
+using intelligibility::VarianceArray;
+
+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 = {
Andrew MacDonald 2015/07/09 03:22:47 I don't think you should bother now, but just so y
ekm 2015/07/09 18:19:22 Acknowledged. Will use in future tests.
+ VarianceArray::kStepInfinite,
+ VarianceArray::kStepDecaying,
+ VarianceArray::kStepWindowed,
+ VarianceArray::kStepBlocked,
+ VarianceArray::kStepBlockBasedMovingAverage};
+ std::fill(noise_data_.begin(), noise_data_.end(), 0.0f);
+ std::fill(orig_data_.begin(), orig_data_.end(), 0.0f);
+ for (auto step_type : step_types) {
+ std::fill(clear_data_.begin(), clear_data_.end(), 0.0f);
+ EXPECT_FALSE(CheckUpdate(step_type));
+ }
+ std::srand(1);
+ std::fill(noise_data_.begin(), noise_data_.end(),
Andrew MacDonald 2015/07/09 03:22:47 This isn't doing what you want. It's filling noise
ekm 2015/07/09 18:19:22 Whoops. This is a very nice snippet, thanks. Done.
+ static_cast<float>(std::rand()));
+ for (auto step_type : step_types) {
+ EXPECT_FALSE(CheckUpdate(step_type));
+ }
+ for (auto step_type : step_types) {
+ std::fill(clear_data_.begin(), clear_data_.end(),
+ static_cast<float>(std::rand()));
+ 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(kTestCenterFreqs[i], enh_.center_freqs_[i], kMaxTestError);
+ ASSERT_EQ(static_cast<int>(arraysize(kTestFilterBank[0])), enh_.freqs_);
+ for (int j = 0; j < enh_.freqs_; ++j) {
+ EXPECT_NEAR(kTestFilterBank[i][j], enh_.filter_bank_[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(kTestZeroVar[i], sols[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(kTestNonZeroVarLambdaTop[i], sols[i], kMaxTestError);
+ }
+ lambda = -1.0;
+ enh_.SolveForGainsGivenLambda(lambda, enh_.start_freq_, &sols[0]);
+ for (int i = 0; i < enh_.bank_size_; i++) {
+ EXPECT_NEAR(kTestZeroVar[i], sols[i], kMaxTestError);
+ }
+}
+
+} // namespace webrtc

Powered by Google App Engine
This is Rietveld 408576698