Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
| 5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
| 6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 // | 11 // |
| 12 // Unit tests for intelligibility enhancer. | 12 // Unit tests for intelligibility enhancer. |
| 13 // | 13 // |
| 14 | 14 |
| 15 #include <math.h> | 15 #include <math.h> |
| 16 #include <stdlib.h> | 16 #include <stdlib.h> |
| 17 #include <algorithm> | 17 #include <algorithm> |
| 18 #include <vector> | 18 #include <vector> |
| 19 | 19 |
| 20 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 21 #include "webrtc/base/arraysize.h" | 21 #include "webrtc/base/arraysize.h" |
| 22 #include "webrtc/base/scoped_ptr.h" | |
| 22 #include "webrtc/common_audio/signal_processing/include/signal_processing_librar y.h" | 23 #include "webrtc/common_audio/signal_processing/include/signal_processing_librar y.h" |
| 23 #include "webrtc/modules/audio_processing/intelligibility/intelligibility_enhanc er.h" | 24 #include "webrtc/modules/audio_processing/intelligibility/intelligibility_enhanc er.h" |
| 24 | 25 |
| 25 namespace webrtc { | 26 namespace webrtc { |
| 26 | 27 |
| 27 namespace { | 28 namespace { |
| 28 | 29 |
| 29 // Target output for ERB create test. Generated with matlab. | 30 // Target output for ERB create test. Generated with matlab. |
| 30 const float kTestCenterFreqs[] = { | 31 const float kTestCenterFreqs[] = { |
| 31 13.169f, 26.965f, 41.423f, 56.577f, 72.461f, 89.113f, 106.57f, 124.88f, | 32 13.169f, 26.965f, 41.423f, 56.577f, 72.461f, 89.113f, 106.57f, 124.88f, |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 67 1.f, 1.f, 1.f, 1.f, 1.f, 1.f, 1.f, 1.f, | 68 1.f, 1.f, 1.f, 1.f, 1.f, 1.f, 1.f, 1.f, |
| 68 1.f, 1.f, 1.f, 0.f, 0.f, 0.0351f, 0.0636f, 0.0863f, | 69 1.f, 1.f, 1.f, 0.f, 0.f, 0.0351f, 0.0636f, 0.0863f, |
| 69 0.1037f, 0.1162f, 0.1236f, 0.1251f, 0.1189f, 0.0993f}; | 70 0.1037f, 0.1162f, 0.1236f, 0.1251f, 0.1189f, 0.0993f}; |
| 70 static_assert(arraysize(kTestCenterFreqs) == | 71 static_assert(arraysize(kTestCenterFreqs) == |
| 71 arraysize(kTestNonZeroVarLambdaTop), | 72 arraysize(kTestNonZeroVarLambdaTop), |
| 72 "Variance test data badly initialized."); | 73 "Variance test data badly initialized."); |
| 73 const float kMaxTestError = 0.005f; | 74 const float kMaxTestError = 0.005f; |
| 74 | 75 |
| 75 // Enhancer initialization parameters. | 76 // Enhancer initialization parameters. |
| 76 const int kSamples = 2000; | 77 const int kSamples = 2000; |
| 77 const int kErbResolution = 2; | |
| 78 const int kSampleRate = 1000; | 78 const int kSampleRate = 1000; |
| 79 const int kFragmentSize = kSampleRate / 100; | 79 const int kFragmentSize = kSampleRate / 100; |
| 80 const int kNumChannels = 1; | |
| 81 const float kDecayRate = 0.9f; | |
| 82 const int kWindowSize = 800; | |
| 83 const int kAnalyzeRate = 800; | |
| 84 const int kVarianceRate = 2; | |
| 85 const float kGainLimit = 0.1f; | |
| 86 | 80 |
| 87 } // namespace | 81 } // namespace |
| 88 | 82 |
| 89 using std::vector; | 83 using std::vector; |
| 90 using intelligibility::VarianceArray; | 84 using intelligibility::VarianceArray; |
| 91 | 85 |
| 92 class IntelligibilityEnhancerTest : public ::testing::Test { | 86 class IntelligibilityEnhancerTest : public ::testing::Test { |
| 93 protected: | 87 protected: |
| 94 IntelligibilityEnhancerTest() | 88 IntelligibilityEnhancerTest() |
| 95 : enh_(kErbResolution, | 89 : clear_data_(kSamples), noise_data_(kSamples), orig_data_(kSamples) { |
| 96 kSampleRate, | 90 config_.sample_rate_hz = kSampleRate; |
| 97 kNumChannels, | 91 enh_.reset(new IntelligibilityEnhancer(config_)); |
| 98 VarianceArray::kStepInfinite, | 92 } |
| 99 kDecayRate, | |
| 100 kWindowSize, | |
| 101 kAnalyzeRate, | |
| 102 kVarianceRate, | |
| 103 kGainLimit), | |
| 104 clear_data_(kSamples), | |
| 105 noise_data_(kSamples), | |
| 106 orig_data_(kSamples) {} | |
| 107 | 93 |
| 108 bool CheckUpdate(VarianceArray::StepType step_type) { | 94 bool CheckUpdate(VarianceArray::StepType step_type) { |
| 109 IntelligibilityEnhancer enh(kErbResolution, kSampleRate, kNumChannels, | 95 config_.sample_rate_hz = kSampleRate; |
|
aluebs-webrtc
2015/07/15 01:02:05
Shouldn't the step_type be set here as well?
ekm
2015/07/17 19:59:38
Done. Nice catch.
| |
| 110 step_type, kDecayRate, kWindowSize, | 96 enh_.reset(new IntelligibilityEnhancer(config_)); |
| 111 kAnalyzeRate, kVarianceRate, kGainLimit); | |
| 112 float* clear_cursor = &clear_data_[0]; | 97 float* clear_cursor = &clear_data_[0]; |
| 113 float* noise_cursor = &noise_data_[0]; | 98 float* noise_cursor = &noise_data_[0]; |
| 114 for (int i = 0; i < kSamples; i += kFragmentSize) { | 99 for (int i = 0; i < kSamples; i += kFragmentSize) { |
| 115 enh.ProcessCaptureAudio(&noise_cursor); | 100 enh_->ProcessCaptureAudio(&noise_cursor); |
| 116 enh.ProcessRenderAudio(&clear_cursor); | 101 enh_->ProcessRenderAudio(&clear_cursor); |
| 117 clear_cursor += kFragmentSize; | 102 clear_cursor += kFragmentSize; |
| 118 noise_cursor += kFragmentSize; | 103 noise_cursor += kFragmentSize; |
| 119 } | 104 } |
| 120 for (int i = 0; i < kSamples; i++) { | 105 for (int i = 0; i < kSamples; i++) { |
| 121 if (std::fabs(clear_data_[i] - orig_data_[i]) > kMaxTestError) { | 106 if (std::fabs(clear_data_[i] - orig_data_[i]) > kMaxTestError) { |
| 122 return true; | 107 return true; |
| 123 } | 108 } |
| 124 } | 109 } |
| 125 return false; | 110 return false; |
| 126 } | 111 } |
| 127 | 112 |
| 128 IntelligibilityEnhancer enh_; | 113 IntelligibilityEnhancer::Config config_; |
| 114 rtc::scoped_ptr<IntelligibilityEnhancer> enh_; | |
| 129 vector<float> clear_data_; | 115 vector<float> clear_data_; |
| 130 vector<float> noise_data_; | 116 vector<float> noise_data_; |
| 131 vector<float> orig_data_; | 117 vector<float> orig_data_; |
| 132 }; | 118 }; |
| 133 | 119 |
| 134 // For each class of generated data, tests that render stream is | 120 // For each class of generated data, tests that render stream is |
| 135 // updated when it should be for each variance update method. | 121 // updated when it should be for each variance update method. |
| 136 TEST_F(IntelligibilityEnhancerTest, TestRenderUpdate) { | 122 TEST_F(IntelligibilityEnhancerTest, TestRenderUpdate) { |
| 137 vector<VarianceArray::StepType> step_types; | 123 vector<VarianceArray::StepType> step_types; |
| 138 step_types.push_back(VarianceArray::kStepInfinite); | 124 step_types.push_back(VarianceArray::kStepInfinite); |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 154 } | 140 } |
| 155 for (auto step_type : step_types) { | 141 for (auto step_type : step_types) { |
| 156 std::generate(clear_data_.begin(), clear_data_.end(), float_rand); | 142 std::generate(clear_data_.begin(), clear_data_.end(), float_rand); |
| 157 orig_data_ = clear_data_; | 143 orig_data_ = clear_data_; |
| 158 EXPECT_TRUE(CheckUpdate(step_type)); | 144 EXPECT_TRUE(CheckUpdate(step_type)); |
| 159 } | 145 } |
| 160 } | 146 } |
| 161 | 147 |
| 162 // Tests ERB bank creation, comparing against matlab output. | 148 // Tests ERB bank creation, comparing against matlab output. |
| 163 TEST_F(IntelligibilityEnhancerTest, TestErbCreation) { | 149 TEST_F(IntelligibilityEnhancerTest, TestErbCreation) { |
| 164 ASSERT_EQ(static_cast<int>(arraysize(kTestCenterFreqs)), enh_.bank_size_); | 150 ASSERT_EQ(static_cast<int>(arraysize(kTestCenterFreqs)), enh_->bank_size_); |
| 165 for (int i = 0; i < enh_.bank_size_; ++i) { | 151 for (int i = 0; i < enh_->bank_size_; ++i) { |
| 166 EXPECT_NEAR(kTestCenterFreqs[i], enh_.center_freqs_[i], kMaxTestError); | 152 EXPECT_NEAR(kTestCenterFreqs[i], enh_->center_freqs_[i], kMaxTestError); |
| 167 ASSERT_EQ(static_cast<int>(arraysize(kTestFilterBank[0])), enh_.freqs_); | 153 ASSERT_EQ(static_cast<int>(arraysize(kTestFilterBank[0])), enh_->freqs_); |
| 168 for (int j = 0; j < enh_.freqs_; ++j) { | 154 for (int j = 0; j < enh_->freqs_; ++j) { |
| 169 EXPECT_NEAR(kTestFilterBank[i][j], enh_.filter_bank_[i][j], | 155 EXPECT_NEAR(kTestFilterBank[i][j], enh_->filter_bank_[i][j], |
| 170 kMaxTestError); | 156 kMaxTestError); |
| 171 } | 157 } |
| 172 } | 158 } |
| 173 } | 159 } |
| 174 | 160 |
| 175 // Tests analytic solution for optimal gains, comparing | 161 // Tests analytic solution for optimal gains, comparing |
| 176 // against matlab output. | 162 // against matlab output. |
| 177 TEST_F(IntelligibilityEnhancerTest, TestSolveForGains) { | 163 TEST_F(IntelligibilityEnhancerTest, TestSolveForGains) { |
| 178 ASSERT_EQ(kTestStartFreq, enh_.start_freq_); | 164 ASSERT_EQ(kTestStartFreq, enh_->start_freq_); |
| 179 vector<float> sols(enh_.bank_size_); | 165 vector<float> sols(enh_->bank_size_); |
| 180 float lambda = -0.001f; | 166 float lambda = -0.001f; |
| 181 for (int i = 0; i < enh_.bank_size_; i++) { | 167 for (int i = 0; i < enh_->bank_size_; i++) { |
| 182 enh_.filtered_clear_var_[i] = 0.0f; | 168 enh_->filtered_clear_var_[i] = 0.0f; |
| 183 enh_.filtered_noise_var_[i] = 0.0f; | 169 enh_->filtered_noise_var_[i] = 0.0f; |
| 184 enh_.rho_[i] = 0.02f; | 170 enh_->rho_[i] = 0.02f; |
| 185 } | 171 } |
| 186 enh_.SolveForGainsGivenLambda(lambda, enh_.start_freq_, &sols[0]); | 172 enh_->SolveForGainsGivenLambda(lambda, enh_->start_freq_, &sols[0]); |
| 187 for (int i = 0; i < enh_.bank_size_; i++) { | 173 for (int i = 0; i < enh_->bank_size_; i++) { |
| 188 EXPECT_NEAR(kTestZeroVar[i], sols[i], kMaxTestError); | 174 EXPECT_NEAR(kTestZeroVar[i], sols[i], kMaxTestError); |
| 189 } | 175 } |
| 190 for (int i = 0; i < enh_.bank_size_; i++) { | 176 for (int i = 0; i < enh_->bank_size_; i++) { |
| 191 enh_.filtered_clear_var_[i] = static_cast<float>(i + 1); | 177 enh_->filtered_clear_var_[i] = static_cast<float>(i + 1); |
| 192 enh_.filtered_noise_var_[i] = static_cast<float>(enh_.bank_size_ - i); | 178 enh_->filtered_noise_var_[i] = static_cast<float>(enh_->bank_size_ - i); |
| 193 } | 179 } |
| 194 enh_.SolveForGainsGivenLambda(lambda, enh_.start_freq_, &sols[0]); | 180 enh_->SolveForGainsGivenLambda(lambda, enh_->start_freq_, &sols[0]); |
| 195 for (int i = 0; i < enh_.bank_size_; i++) { | 181 for (int i = 0; i < enh_->bank_size_; i++) { |
| 196 EXPECT_NEAR(kTestNonZeroVarLambdaTop[i], sols[i], kMaxTestError); | 182 EXPECT_NEAR(kTestNonZeroVarLambdaTop[i], sols[i], kMaxTestError); |
| 197 } | 183 } |
| 198 lambda = -1.0; | 184 lambda = -1.0; |
| 199 enh_.SolveForGainsGivenLambda(lambda, enh_.start_freq_, &sols[0]); | 185 enh_->SolveForGainsGivenLambda(lambda, enh_->start_freq_, &sols[0]); |
| 200 for (int i = 0; i < enh_.bank_size_; i++) { | 186 for (int i = 0; i < enh_->bank_size_; i++) { |
| 201 EXPECT_NEAR(kTestZeroVar[i], sols[i], kMaxTestError); | 187 EXPECT_NEAR(kTestZeroVar[i], sols[i], kMaxTestError); |
| 202 } | 188 } |
| 203 } | 189 } |
| 204 | 190 |
| 205 } // namespace webrtc | 191 } // namespace webrtc |
| OLD | NEW |