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 // | |
12 // Unit tests for intelligibility enhancer. | |
13 // | |
14 | |
15 #include <math.h> | 11 #include <math.h> |
16 #include <stdlib.h> | 12 #include <stdlib.h> |
17 #include <algorithm> | 13 #include <algorithm> |
18 #include <vector> | 14 #include <vector> |
19 | 15 |
20 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
21 #include "webrtc/base/arraysize.h" | 17 #include "webrtc/base/arraysize.h" |
22 #include "webrtc/base/scoped_ptr.h" | 18 #include "webrtc/base/scoped_ptr.h" |
23 #include "webrtc/common_audio/signal_processing/include/signal_processing_librar
y.h" | 19 #include "webrtc/common_audio/signal_processing/include/signal_processing_librar
y.h" |
24 #include "webrtc/modules/audio_processing/intelligibility/intelligibility_enhanc
er.h" | 20 #include "webrtc/modules/audio_processing/intelligibility/intelligibility_enhanc
er.h" |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
56 {0, 0.2f}}; | 52 {0, 0.2f}}; |
57 static_assert(arraysize(kTestCenterFreqs) == arraysize(kTestFilterBank), | 53 static_assert(arraysize(kTestCenterFreqs) == arraysize(kTestFilterBank), |
58 "Test filterbank badly initialized."); | 54 "Test filterbank badly initialized."); |
59 | 55 |
60 // Target output for gain solving test. Generated with matlab. | 56 // Target output for gain solving test. Generated with matlab. |
61 const size_t kTestStartFreq = 12; // Lowest integral frequency for ERBs. | 57 const size_t kTestStartFreq = 12; // Lowest integral frequency for ERBs. |
62 const float kTestZeroVar[] = {1.f, 1.f, 1.f, 1.f, 1.f, 1.f, 1.f, 1.f, | 58 const float kTestZeroVar[] = {1.f, 1.f, 1.f, 1.f, 1.f, 1.f, 1.f, 1.f, |
63 1.f, 1.f, 1.f, 0.f, 0.f, 0.f, 0.f, 0.f, | 59 1.f, 1.f, 1.f, 0.f, 0.f, 0.f, 0.f, 0.f, |
64 0.f, 0.f, 0.f, 0.f, 0.f, 0.f}; | 60 0.f, 0.f, 0.f, 0.f, 0.f, 0.f}; |
65 static_assert(arraysize(kTestCenterFreqs) == arraysize(kTestZeroVar), | 61 static_assert(arraysize(kTestCenterFreqs) == arraysize(kTestZeroVar), |
66 "Variance test data badly initialized."); | 62 "Power test data badly initialized."); |
67 const float kTestNonZeroVarLambdaTop[] = { | 63 const float kTestNonZeroVarLambdaTop[] = { |
68 1.f, 1.f, 1.f, 1.f, 1.f, 1.f, 1.f, 1.f, | 64 1.f, 1.f, 1.f, 1.f, 1.f, 1.f, 1.f, 1.f, |
69 1.f, 1.f, 1.f, 0.f, 0.f, 0.0351f, 0.0636f, 0.0863f, | 65 1.f, 1.f, 1.f, 0.f, 0.f, 0.0351f, 0.0636f, 0.0863f, |
70 0.1037f, 0.1162f, 0.1236f, 0.1251f, 0.1189f, 0.0993f}; | 66 0.1037f, 0.1162f, 0.1236f, 0.1251f, 0.1189f, 0.0993f}; |
71 static_assert(arraysize(kTestCenterFreqs) == | 67 static_assert(arraysize(kTestCenterFreqs) == |
72 arraysize(kTestNonZeroVarLambdaTop), | 68 arraysize(kTestNonZeroVarLambdaTop), |
73 "Variance test data badly initialized."); | 69 "Power test data badly initialized."); |
74 const float kMaxTestError = 0.005f; | 70 const float kMaxTestError = 0.005f; |
75 | 71 |
76 // Enhancer initialization parameters. | 72 // Enhancer initialization parameters. |
77 const int kSamples = 2000; | 73 const int kSamples = 2000; |
78 const int kSampleRate = 1000; | 74 const int kSampleRate = 1000; |
79 const int kNumChannels = 1; | 75 const int kNumChannels = 1; |
80 const int kFragmentSize = kSampleRate / 100; | 76 const int kFragmentSize = kSampleRate / 100; |
81 | 77 |
82 } // namespace | 78 } // namespace |
83 | 79 |
84 using std::vector; | |
85 using intelligibility::VarianceArray; | |
86 | |
87 class IntelligibilityEnhancerTest : public ::testing::Test { | 80 class IntelligibilityEnhancerTest : public ::testing::Test { |
88 protected: | 81 protected: |
89 IntelligibilityEnhancerTest() | 82 IntelligibilityEnhancerTest() |
90 : clear_data_(kSamples), noise_data_(kSamples), orig_data_(kSamples) { | 83 : clear_data_(kSamples), noise_data_(kSamples), orig_data_(kSamples) { |
91 config_.sample_rate_hz = kSampleRate; | 84 config_.sample_rate_hz = kSampleRate; |
92 enh_.reset(new IntelligibilityEnhancer(config_)); | 85 enh_.reset(new IntelligibilityEnhancer(config_)); |
93 } | 86 } |
94 | 87 |
95 bool CheckUpdate(VarianceArray::StepType step_type) { | 88 bool CheckUpdate() { |
96 config_.sample_rate_hz = kSampleRate; | 89 config_.sample_rate_hz = kSampleRate; |
97 config_.var_type = step_type; | |
98 enh_.reset(new IntelligibilityEnhancer(config_)); | 90 enh_.reset(new IntelligibilityEnhancer(config_)); |
99 float* clear_cursor = &clear_data_[0]; | 91 float* clear_cursor = &clear_data_[0]; |
100 float* noise_cursor = &noise_data_[0]; | 92 float* noise_cursor = &noise_data_[0]; |
101 for (int i = 0; i < kSamples; i += kFragmentSize) { | 93 for (int i = 0; i < kSamples; i += kFragmentSize) { |
102 enh_->ProcessRenderAudio(&clear_cursor, kSampleRate, kNumChannels); | 94 enh_->ProcessRenderAudio(&clear_cursor, kSampleRate, kNumChannels); |
103 clear_cursor += kFragmentSize; | 95 clear_cursor += kFragmentSize; |
104 noise_cursor += kFragmentSize; | 96 noise_cursor += kFragmentSize; |
105 } | 97 } |
106 for (int i = 0; i < kSamples; i++) { | 98 for (int i = 0; i < kSamples; i++) { |
107 if (std::fabs(clear_data_[i] - orig_data_[i]) > kMaxTestError) { | 99 if (std::fabs(clear_data_[i] - orig_data_[i]) > kMaxTestError) { |
108 return true; | 100 return true; |
109 } | 101 } |
110 } | 102 } |
111 return false; | 103 return false; |
112 } | 104 } |
113 | 105 |
114 IntelligibilityEnhancer::Config config_; | 106 IntelligibilityEnhancer::Config config_; |
115 rtc::scoped_ptr<IntelligibilityEnhancer> enh_; | 107 rtc::scoped_ptr<IntelligibilityEnhancer> enh_; |
116 vector<float> clear_data_; | 108 std::vector<float> clear_data_; |
117 vector<float> noise_data_; | 109 std::vector<float> noise_data_; |
118 vector<float> orig_data_; | 110 std::vector<float> orig_data_; |
119 }; | 111 }; |
120 | 112 |
121 // For each class of generated data, tests that render stream is | 113 // For each class of generated data, tests that render stream is updated when |
122 // updated when it should be for each variance update method. | 114 // it should be. |
123 TEST_F(IntelligibilityEnhancerTest, TestRenderUpdate) { | 115 TEST_F(IntelligibilityEnhancerTest, TestRenderUpdate) { |
124 vector<VarianceArray::StepType> step_types; | |
125 step_types.push_back(VarianceArray::kStepInfinite); | |
126 step_types.push_back(VarianceArray::kStepDecaying); | |
127 step_types.push_back(VarianceArray::kStepWindowed); | |
128 step_types.push_back(VarianceArray::kStepBlocked); | |
129 step_types.push_back(VarianceArray::kStepBlockBasedMovingAverage); | |
130 std::fill(noise_data_.begin(), noise_data_.end(), 0.0f); | 116 std::fill(noise_data_.begin(), noise_data_.end(), 0.0f); |
131 std::fill(orig_data_.begin(), orig_data_.end(), 0.0f); | 117 std::fill(orig_data_.begin(), orig_data_.end(), 0.0f); |
132 for (auto step_type : step_types) { | 118 std::fill(clear_data_.begin(), clear_data_.end(), 0.0f); |
133 std::fill(clear_data_.begin(), clear_data_.end(), 0.0f); | 119 EXPECT_FALSE(CheckUpdate()); |
134 EXPECT_FALSE(CheckUpdate(step_type)); | |
135 } | |
136 std::srand(1); | 120 std::srand(1); |
137 auto float_rand = []() { return std::rand() * 2.f / RAND_MAX - 1; }; | 121 auto float_rand = []() { return std::rand() * 2.f / RAND_MAX - 1; }; |
138 std::generate(noise_data_.begin(), noise_data_.end(), float_rand); | 122 std::generate(noise_data_.begin(), noise_data_.end(), float_rand); |
139 for (auto step_type : step_types) { | 123 EXPECT_FALSE(CheckUpdate()); |
140 EXPECT_FALSE(CheckUpdate(step_type)); | 124 std::generate(clear_data_.begin(), clear_data_.end(), float_rand); |
141 } | 125 orig_data_ = clear_data_; |
142 for (auto step_type : step_types) { | 126 EXPECT_TRUE(CheckUpdate()); |
143 std::generate(clear_data_.begin(), clear_data_.end(), float_rand); | |
144 orig_data_ = clear_data_; | |
145 EXPECT_TRUE(CheckUpdate(step_type)); | |
146 } | |
147 } | 127 } |
148 | 128 |
149 // Tests ERB bank creation, comparing against matlab output. | 129 // Tests ERB bank creation, comparing against matlab output. |
150 TEST_F(IntelligibilityEnhancerTest, TestErbCreation) { | 130 TEST_F(IntelligibilityEnhancerTest, TestErbCreation) { |
151 ASSERT_EQ(arraysize(kTestCenterFreqs), enh_->bank_size_); | 131 ASSERT_EQ(arraysize(kTestCenterFreqs), enh_->bank_size_); |
152 for (size_t i = 0; i < enh_->bank_size_; ++i) { | 132 for (size_t i = 0; i < enh_->bank_size_; ++i) { |
153 EXPECT_NEAR(kTestCenterFreqs[i], enh_->center_freqs_[i], kMaxTestError); | 133 EXPECT_NEAR(kTestCenterFreqs[i], enh_->center_freqs_[i], kMaxTestError); |
154 ASSERT_EQ(arraysize(kTestFilterBank[0]), enh_->freqs_); | 134 ASSERT_EQ(arraysize(kTestFilterBank[0]), enh_->freqs_); |
155 for (size_t j = 0; j < enh_->freqs_; ++j) { | 135 for (size_t j = 0; j < enh_->freqs_; ++j) { |
156 EXPECT_NEAR(kTestFilterBank[i][j], enh_->render_filter_bank_[i][j], | 136 EXPECT_NEAR(kTestFilterBank[i][j], enh_->render_filter_bank_[i][j], |
157 kMaxTestError); | 137 kMaxTestError); |
158 } | 138 } |
159 } | 139 } |
160 } | 140 } |
161 | 141 |
162 // Tests analytic solution for optimal gains, comparing | 142 // Tests analytic solution for optimal gains, comparing |
163 // against matlab output. | 143 // against matlab output. |
164 TEST_F(IntelligibilityEnhancerTest, TestSolveForGains) { | 144 TEST_F(IntelligibilityEnhancerTest, TestSolveForGains) { |
165 ASSERT_EQ(kTestStartFreq, enh_->start_freq_); | 145 ASSERT_EQ(kTestStartFreq, enh_->start_freq_); |
166 vector<float> sols(enh_->bank_size_); | 146 std::vector<float> sols(enh_->bank_size_); |
167 float lambda = -0.001f; | 147 float lambda = -0.001f; |
168 for (size_t i = 0; i < enh_->bank_size_; i++) { | 148 for (size_t i = 0; i < enh_->bank_size_; i++) { |
169 enh_->filtered_clear_var_[i] = 0.0f; | 149 enh_->filtered_clear_pow_[i] = 0.0f; |
170 enh_->filtered_noise_var_[i] = 0.0f; | 150 enh_->filtered_noise_pow_[i] = 0.0f; |
171 enh_->rho_[i] = 0.02f; | 151 enh_->rho_[i] = 0.02f; |
172 } | 152 } |
173 enh_->SolveForGainsGivenLambda(lambda, enh_->start_freq_, &sols[0]); | 153 enh_->SolveForGainsGivenLambda(lambda, enh_->start_freq_, &sols[0]); |
174 for (size_t i = 0; i < enh_->bank_size_; i++) { | 154 for (size_t i = 0; i < enh_->bank_size_; i++) { |
175 EXPECT_NEAR(kTestZeroVar[i], sols[i], kMaxTestError); | 155 EXPECT_NEAR(kTestZeroVar[i], sols[i], kMaxTestError); |
176 } | 156 } |
177 for (size_t i = 0; i < enh_->bank_size_; i++) { | 157 for (size_t i = 0; i < enh_->bank_size_; i++) { |
178 enh_->filtered_clear_var_[i] = static_cast<float>(i + 1); | 158 enh_->filtered_clear_pow_[i] = static_cast<float>(i + 1); |
179 enh_->filtered_noise_var_[i] = static_cast<float>(enh_->bank_size_ - i); | 159 enh_->filtered_noise_pow_[i] = static_cast<float>(enh_->bank_size_ - i); |
180 } | 160 } |
181 enh_->SolveForGainsGivenLambda(lambda, enh_->start_freq_, &sols[0]); | 161 enh_->SolveForGainsGivenLambda(lambda, enh_->start_freq_, &sols[0]); |
182 for (size_t i = 0; i < enh_->bank_size_; i++) { | 162 for (size_t i = 0; i < enh_->bank_size_; i++) { |
183 EXPECT_NEAR(kTestNonZeroVarLambdaTop[i], sols[i], kMaxTestError); | 163 EXPECT_NEAR(kTestNonZeroVarLambdaTop[i], sols[i], kMaxTestError); |
184 } | 164 } |
185 lambda = -1.0; | 165 lambda = -1.0; |
186 enh_->SolveForGainsGivenLambda(lambda, enh_->start_freq_, &sols[0]); | 166 enh_->SolveForGainsGivenLambda(lambda, enh_->start_freq_, &sols[0]); |
187 for (size_t i = 0; i < enh_->bank_size_; i++) { | 167 for (size_t i = 0; i < enh_->bank_size_; i++) { |
188 EXPECT_NEAR(kTestZeroVar[i], sols[i], kMaxTestError); | 168 EXPECT_NEAR(kTestZeroVar[i], sols[i], kMaxTestError); |
189 } | 169 } |
190 } | 170 } |
191 | 171 |
192 } // namespace webrtc | 172 } // namespace webrtc |
OLD | NEW |