| 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 kNumChannels = 1; |
| 79 const int kFragmentSize = kSampleRate / 100; | 80 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 | 81 |
| 87 } // namespace | 82 } // namespace |
| 88 | 83 |
| 89 using std::vector; | 84 using std::vector; |
| 90 using intelligibility::VarianceArray; | 85 using intelligibility::VarianceArray; |
| 91 | 86 |
| 92 class IntelligibilityEnhancerTest : public ::testing::Test { | 87 class IntelligibilityEnhancerTest : public ::testing::Test { |
| 93 protected: | 88 protected: |
| 94 IntelligibilityEnhancerTest() | 89 IntelligibilityEnhancerTest() |
| 95 : enh_(kErbResolution, | 90 : clear_data_(kSamples), noise_data_(kSamples), orig_data_(kSamples) { |
| 96 kSampleRate, | 91 config_.sample_rate_hz = kSampleRate; |
| 97 kNumChannels, | 92 enh_.reset(new IntelligibilityEnhancer(config_)); |
| 98 VarianceArray::kStepInfinite, | 93 } |
| 99 kDecayRate, | |
| 100 kWindowSize, | |
| 101 kAnalyzeRate, | |
| 102 kVarianceRate, | |
| 103 kGainLimit), | |
| 104 clear_data_(kSamples), | |
| 105 noise_data_(kSamples), | |
| 106 orig_data_(kSamples) {} | |
| 107 | 94 |
| 108 bool CheckUpdate(VarianceArray::StepType step_type) { | 95 bool CheckUpdate(VarianceArray::StepType step_type) { |
| 109 IntelligibilityEnhancer enh(kErbResolution, kSampleRate, kNumChannels, | 96 config_.sample_rate_hz = kSampleRate; |
| 110 step_type, kDecayRate, kWindowSize, | 97 config_.var_type = step_type; |
| 111 kAnalyzeRate, kVarianceRate, kGainLimit); | 98 enh_.reset(new IntelligibilityEnhancer(config_)); |
| 112 float* clear_cursor = &clear_data_[0]; | 99 float* clear_cursor = &clear_data_[0]; |
| 113 float* noise_cursor = &noise_data_[0]; | 100 float* noise_cursor = &noise_data_[0]; |
| 114 for (int i = 0; i < kSamples; i += kFragmentSize) { | 101 for (int i = 0; i < kSamples; i += kFragmentSize) { |
| 115 enh.ProcessCaptureAudio(&noise_cursor); | 102 enh_->AnalyzeCaptureAudio(&noise_cursor, kSampleRate, kNumChannels); |
| 116 enh.ProcessRenderAudio(&clear_cursor); | 103 enh_->ProcessRenderAudio(&clear_cursor, kSampleRate, kNumChannels); |
| 117 clear_cursor += kFragmentSize; | 104 clear_cursor += kFragmentSize; |
| 118 noise_cursor += kFragmentSize; | 105 noise_cursor += kFragmentSize; |
| 119 } | 106 } |
| 120 for (int i = 0; i < kSamples; i++) { | 107 for (int i = 0; i < kSamples; i++) { |
| 121 if (std::fabs(clear_data_[i] - orig_data_[i]) > kMaxTestError) { | 108 if (std::fabs(clear_data_[i] - orig_data_[i]) > kMaxTestError) { |
| 122 return true; | 109 return true; |
| 123 } | 110 } |
| 124 } | 111 } |
| 125 return false; | 112 return false; |
| 126 } | 113 } |
| 127 | 114 |
| 128 IntelligibilityEnhancer enh_; | 115 IntelligibilityEnhancer::Config config_; |
| 116 rtc::scoped_ptr<IntelligibilityEnhancer> enh_; |
| 129 vector<float> clear_data_; | 117 vector<float> clear_data_; |
| 130 vector<float> noise_data_; | 118 vector<float> noise_data_; |
| 131 vector<float> orig_data_; | 119 vector<float> orig_data_; |
| 132 }; | 120 }; |
| 133 | 121 |
| 134 // For each class of generated data, tests that render stream is | 122 // For each class of generated data, tests that render stream is |
| 135 // updated when it should be for each variance update method. | 123 // updated when it should be for each variance update method. |
| 136 TEST_F(IntelligibilityEnhancerTest, TestRenderUpdate) { | 124 TEST_F(IntelligibilityEnhancerTest, TestRenderUpdate) { |
| 137 vector<VarianceArray::StepType> step_types; | 125 vector<VarianceArray::StepType> step_types; |
| 138 step_types.push_back(VarianceArray::kStepInfinite); | 126 step_types.push_back(VarianceArray::kStepInfinite); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 154 } | 142 } |
| 155 for (auto step_type : step_types) { | 143 for (auto step_type : step_types) { |
| 156 std::generate(clear_data_.begin(), clear_data_.end(), float_rand); | 144 std::generate(clear_data_.begin(), clear_data_.end(), float_rand); |
| 157 orig_data_ = clear_data_; | 145 orig_data_ = clear_data_; |
| 158 EXPECT_TRUE(CheckUpdate(step_type)); | 146 EXPECT_TRUE(CheckUpdate(step_type)); |
| 159 } | 147 } |
| 160 } | 148 } |
| 161 | 149 |
| 162 // Tests ERB bank creation, comparing against matlab output. | 150 // Tests ERB bank creation, comparing against matlab output. |
| 163 TEST_F(IntelligibilityEnhancerTest, TestErbCreation) { | 151 TEST_F(IntelligibilityEnhancerTest, TestErbCreation) { |
| 164 ASSERT_EQ(static_cast<int>(arraysize(kTestCenterFreqs)), enh_.bank_size_); | 152 ASSERT_EQ(static_cast<int>(arraysize(kTestCenterFreqs)), enh_->bank_size_); |
| 165 for (int i = 0; i < enh_.bank_size_; ++i) { | 153 for (int i = 0; i < enh_->bank_size_; ++i) { |
| 166 EXPECT_NEAR(kTestCenterFreqs[i], enh_.center_freqs_[i], kMaxTestError); | 154 EXPECT_NEAR(kTestCenterFreqs[i], enh_->center_freqs_[i], kMaxTestError); |
| 167 ASSERT_EQ(static_cast<int>(arraysize(kTestFilterBank[0])), enh_.freqs_); | 155 ASSERT_EQ(static_cast<int>(arraysize(kTestFilterBank[0])), enh_->freqs_); |
| 168 for (int j = 0; j < enh_.freqs_; ++j) { | 156 for (int j = 0; j < enh_->freqs_; ++j) { |
| 169 EXPECT_NEAR(kTestFilterBank[i][j], enh_.filter_bank_[i][j], | 157 EXPECT_NEAR(kTestFilterBank[i][j], enh_->filter_bank_[i][j], |
| 170 kMaxTestError); | 158 kMaxTestError); |
| 171 } | 159 } |
| 172 } | 160 } |
| 173 } | 161 } |
| 174 | 162 |
| 175 // Tests analytic solution for optimal gains, comparing | 163 // Tests analytic solution for optimal gains, comparing |
| 176 // against matlab output. | 164 // against matlab output. |
| 177 TEST_F(IntelligibilityEnhancerTest, TestSolveForGains) { | 165 TEST_F(IntelligibilityEnhancerTest, TestSolveForGains) { |
| 178 ASSERT_EQ(kTestStartFreq, enh_.start_freq_); | 166 ASSERT_EQ(kTestStartFreq, enh_->start_freq_); |
| 179 vector<float> sols(enh_.bank_size_); | 167 vector<float> sols(enh_->bank_size_); |
| 180 float lambda = -0.001f; | 168 float lambda = -0.001f; |
| 181 for (int i = 0; i < enh_.bank_size_; i++) { | 169 for (int i = 0; i < enh_->bank_size_; i++) { |
| 182 enh_.filtered_clear_var_[i] = 0.0f; | 170 enh_->filtered_clear_var_[i] = 0.0f; |
| 183 enh_.filtered_noise_var_[i] = 0.0f; | 171 enh_->filtered_noise_var_[i] = 0.0f; |
| 184 enh_.rho_[i] = 0.02f; | 172 enh_->rho_[i] = 0.02f; |
| 185 } | 173 } |
| 186 enh_.SolveForGainsGivenLambda(lambda, enh_.start_freq_, &sols[0]); | 174 enh_->SolveForGainsGivenLambda(lambda, enh_->start_freq_, &sols[0]); |
| 187 for (int i = 0; i < enh_.bank_size_; i++) { | 175 for (int i = 0; i < enh_->bank_size_; i++) { |
| 188 EXPECT_NEAR(kTestZeroVar[i], sols[i], kMaxTestError); | 176 EXPECT_NEAR(kTestZeroVar[i], sols[i], kMaxTestError); |
| 189 } | 177 } |
| 190 for (int i = 0; i < enh_.bank_size_; i++) { | 178 for (int i = 0; i < enh_->bank_size_; i++) { |
| 191 enh_.filtered_clear_var_[i] = static_cast<float>(i + 1); | 179 enh_->filtered_clear_var_[i] = static_cast<float>(i + 1); |
| 192 enh_.filtered_noise_var_[i] = static_cast<float>(enh_.bank_size_ - i); | 180 enh_->filtered_noise_var_[i] = static_cast<float>(enh_->bank_size_ - i); |
| 193 } | 181 } |
| 194 enh_.SolveForGainsGivenLambda(lambda, enh_.start_freq_, &sols[0]); | 182 enh_->SolveForGainsGivenLambda(lambda, enh_->start_freq_, &sols[0]); |
| 195 for (int i = 0; i < enh_.bank_size_; i++) { | 183 for (int i = 0; i < enh_->bank_size_; i++) { |
| 196 EXPECT_NEAR(kTestNonZeroVarLambdaTop[i], sols[i], kMaxTestError); | 184 EXPECT_NEAR(kTestNonZeroVarLambdaTop[i], sols[i], kMaxTestError); |
| 197 } | 185 } |
| 198 lambda = -1.0; | 186 lambda = -1.0; |
| 199 enh_.SolveForGainsGivenLambda(lambda, enh_.start_freq_, &sols[0]); | 187 enh_->SolveForGainsGivenLambda(lambda, enh_->start_freq_, &sols[0]); |
| 200 for (int i = 0; i < enh_.bank_size_; i++) { | 188 for (int i = 0; i < enh_->bank_size_; i++) { |
| 201 EXPECT_NEAR(kTestZeroVar[i], sols[i], kMaxTestError); | 189 EXPECT_NEAR(kTestZeroVar[i], sols[i], kMaxTestError); |
| 202 } | 190 } |
| 203 } | 191 } |
| 204 | 192 |
| 205 } // namespace webrtc | 193 } // namespace webrtc |
| OLD | NEW |