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" |
25 | 21 |
26 namespace webrtc { | 22 namespace webrtc { |
27 | 23 |
28 namespace { | 24 namespace { |
29 | 25 |
30 // Target output for ERB create test. Generated with matlab. | 26 // Target output for ERB create test. Generated with matlab. |
31 const float kTestCenterFreqs[] = { | 27 const float kTestCenterFreqs[] = { |
32 13.169f, 26.965f, 41.423f, 56.577f, 72.461f, 89.113f, 106.57f, 124.88f, | 28 13.169f, 26.965f, 41.423f, 56.577f, 72.461f, 89.113f, 106.57f, 124.88f, |
33 144.08f, 164.21f, 185.34f, 207.5f, 230.75f, 255.16f, 280.77f, 307.66f, | 29 144.08f, 164.21f, 185.34f, 207.5f, 230.75f, 255.16f, 280.77f, 307.66f, |
34 335.9f, 365.56f, 396.71f, 429.44f, 463.84f, 500.f}; | 30 335.9f, 365.56f, 396.71f, 429.44f, 463.84f, 500.f}; |
35 const float kTestFilterBank[][2] = {{0.055556f, 0.f}, | 31 const float kTestFilterBank[][9] = { |
36 {0.055556f, 0.f}, | 32 {0.2f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f}, |
37 {0.055556f, 0.f}, | 33 {0.2f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f}, |
38 {0.055556f, 0.f}, | 34 {0.2f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f}, |
39 {0.055556f, 0.f}, | 35 {0.2f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f}, |
40 {0.055556f, 0.f}, | 36 {0.2f, 0.25f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f}, |
41 {0.055556f, 0.f}, | 37 {0.f, 0.25f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f}, |
42 {0.055556f, 0.f}, | 38 {0.f, 0.25f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f}, |
43 {0.055556f, 0.f}, | 39 {0.f, 0.25f, 0.25f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f}, |
44 {0.055556f, 0.f}, | 40 {0.f, 0.f, 0.25f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f}, |
45 {0.055556f, 0.f}, | 41 {0.f, 0.f, 0.25f, 0.142857f, 0.f, 0.f, 0.f, 0.f, 0.f}, |
46 {0.055556f, 0.f}, | 42 {0.f, 0.f, 0.25f, 0.285714f, 0.f, 0.f, 0.f, 0.f, 0.f}, |
47 {0.055556f, 0.f}, | 43 {0.f, 0.f, 0.f, 0.285714f, 0.142857f, 0.f, 0.f, 0.f, 0.f}, |
48 {0.055556f, 0.f}, | 44 {0.f, 0.f, 0.f, 0.285714f, 0.285714f, 0.f, 0.f, 0.f, 0.f}, |
49 {0.055556f, 0.f}, | 45 {0.f, 0.f, 0.f, 0.f, 0.285714f, 0.142857f, 0.f, 0.f, 0.f}, |
50 {0.055556f, 0.f}, | 46 {0.f, 0.f, 0.f, 0.f, 0.285714f, 0.285714f, 0.f, 0.f, 0.f}, |
51 {0.055556f, 0.f}, | 47 {0.f, 0.f, 0.f, 0.f, 0.f, 0.285714f, 0.142857f, 0.f, 0.f}, |
52 {0.055556f, 0.2f}, | 48 {0.f, 0.f, 0.f, 0.f, 0.f, 0.285714f, 0.285714f, 0.f, 0.f}, |
53 {0, 0.2f}, | 49 {0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.285714f, 0.142857f, 0.f}, |
54 {0, 0.2f}, | 50 {0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.285714f, 0.285714f, 0.f}, |
55 {0, 0.2f}, | 51 {0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.285714f, 0.f}, |
56 {0, 0.2f}}; | 52 {0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.285714f, 0.5f}, |
| 53 {0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.5f}}; |
57 static_assert(arraysize(kTestCenterFreqs) == arraysize(kTestFilterBank), | 54 static_assert(arraysize(kTestCenterFreqs) == arraysize(kTestFilterBank), |
58 "Test filterbank badly initialized."); | 55 "Test filterbank badly initialized."); |
59 | 56 |
60 // Target output for gain solving test. Generated with matlab. | 57 // Target output for gain solving test. Generated with matlab. |
61 const size_t kTestStartFreq = 12; // Lowest integral frequency for ERBs. | 58 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, | 59 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, | 60 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}; | 61 0.f, 0.f, 0.f, 0.f, 0.f, 0.f}; |
65 static_assert(arraysize(kTestCenterFreqs) == arraysize(kTestZeroVar), | 62 static_assert(arraysize(kTestCenterFreqs) == arraysize(kTestZeroVar), |
66 "Variance test data badly initialized."); | 63 "Power test data badly initialized."); |
67 const float kTestNonZeroVarLambdaTop[] = { | 64 const float kTestNonZeroVarLambdaTop[] = { |
68 1.f, 1.f, 1.f, 1.f, 1.f, 1.f, 1.f, 1.f, | 65 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, | 66 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}; | 67 0.1037f, 0.1162f, 0.1236f, 0.1251f, 0.1189f, 0.0993f}; |
71 static_assert(arraysize(kTestCenterFreqs) == | 68 static_assert(arraysize(kTestCenterFreqs) == |
72 arraysize(kTestNonZeroVarLambdaTop), | 69 arraysize(kTestNonZeroVarLambdaTop), |
73 "Variance test data badly initialized."); | 70 "Power test data badly initialized."); |
74 const float kMaxTestError = 0.005f; | 71 const float kMaxTestError = 0.005f; |
75 | 72 |
76 // Enhancer initialization parameters. | 73 // Enhancer initialization parameters. |
77 const int kSamples = 2000; | 74 const int kSamples = 2000; |
78 const int kSampleRate = 1000; | 75 const int kSampleRate = 1000; |
79 const int kNumChannels = 1; | 76 const int kNumChannels = 1; |
80 const int kFragmentSize = kSampleRate / 100; | 77 const int kFragmentSize = kSampleRate / 100; |
81 | 78 |
82 } // namespace | 79 } // namespace |
83 | 80 |
84 using std::vector; | |
85 using intelligibility::VarianceArray; | |
86 | |
87 class IntelligibilityEnhancerTest : public ::testing::Test { | 81 class IntelligibilityEnhancerTest : public ::testing::Test { |
88 protected: | 82 protected: |
89 IntelligibilityEnhancerTest() | 83 IntelligibilityEnhancerTest() |
90 : clear_data_(kSamples), noise_data_(kSamples), orig_data_(kSamples) { | 84 : clear_data_(kSamples), noise_data_(kSamples), orig_data_(kSamples) { |
91 config_.sample_rate_hz = kSampleRate; | 85 config_.sample_rate_hz = kSampleRate; |
92 enh_.reset(new IntelligibilityEnhancer(config_)); | 86 enh_.reset(new IntelligibilityEnhancer(config_)); |
93 } | 87 } |
94 | 88 |
95 bool CheckUpdate(VarianceArray::StepType step_type) { | 89 bool CheckUpdate() { |
96 config_.sample_rate_hz = kSampleRate; | 90 config_.sample_rate_hz = kSampleRate; |
97 config_.var_type = step_type; | |
98 enh_.reset(new IntelligibilityEnhancer(config_)); | 91 enh_.reset(new IntelligibilityEnhancer(config_)); |
99 float* clear_cursor = &clear_data_[0]; | 92 float* clear_cursor = &clear_data_[0]; |
100 float* noise_cursor = &noise_data_[0]; | 93 float* noise_cursor = &noise_data_[0]; |
101 for (int i = 0; i < kSamples; i += kFragmentSize) { | 94 for (int i = 0; i < kSamples; i += kFragmentSize) { |
102 enh_->ProcessRenderAudio(&clear_cursor, kSampleRate, kNumChannels); | 95 enh_->ProcessRenderAudio(&clear_cursor, kSampleRate, kNumChannels); |
103 clear_cursor += kFragmentSize; | 96 clear_cursor += kFragmentSize; |
104 noise_cursor += kFragmentSize; | 97 noise_cursor += kFragmentSize; |
105 } | 98 } |
106 for (int i = 0; i < kSamples; i++) { | 99 for (int i = 0; i < kSamples; i++) { |
107 if (std::fabs(clear_data_[i] - orig_data_[i]) > kMaxTestError) { | 100 if (std::fabs(clear_data_[i] - orig_data_[i]) > kMaxTestError) { |
108 return true; | 101 return true; |
109 } | 102 } |
110 } | 103 } |
111 return false; | 104 return false; |
112 } | 105 } |
113 | 106 |
114 IntelligibilityEnhancer::Config config_; | 107 IntelligibilityEnhancer::Config config_; |
115 rtc::scoped_ptr<IntelligibilityEnhancer> enh_; | 108 rtc::scoped_ptr<IntelligibilityEnhancer> enh_; |
116 vector<float> clear_data_; | 109 std::vector<float> clear_data_; |
117 vector<float> noise_data_; | 110 std::vector<float> noise_data_; |
118 vector<float> orig_data_; | 111 std::vector<float> orig_data_; |
119 }; | 112 }; |
120 | 113 |
121 // For each class of generated data, tests that render stream is | 114 // For each class of generated data, tests that render stream is updated when |
122 // updated when it should be for each variance update method. | 115 // it should be. |
123 TEST_F(IntelligibilityEnhancerTest, TestRenderUpdate) { | 116 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); | 117 std::fill(noise_data_.begin(), noise_data_.end(), 0.0f); |
131 std::fill(orig_data_.begin(), orig_data_.end(), 0.0f); | 118 std::fill(orig_data_.begin(), orig_data_.end(), 0.0f); |
132 for (auto step_type : step_types) { | 119 std::fill(clear_data_.begin(), clear_data_.end(), 0.0f); |
133 std::fill(clear_data_.begin(), clear_data_.end(), 0.0f); | 120 EXPECT_FALSE(CheckUpdate()); |
134 EXPECT_FALSE(CheckUpdate(step_type)); | |
135 } | |
136 std::srand(1); | 121 std::srand(1); |
137 auto float_rand = []() { return std::rand() * 2.f / RAND_MAX - 1; }; | 122 auto float_rand = []() { return std::rand() * 2.f / RAND_MAX - 1; }; |
138 std::generate(noise_data_.begin(), noise_data_.end(), float_rand); | 123 std::generate(noise_data_.begin(), noise_data_.end(), float_rand); |
139 for (auto step_type : step_types) { | 124 EXPECT_FALSE(CheckUpdate()); |
140 EXPECT_FALSE(CheckUpdate(step_type)); | 125 std::generate(clear_data_.begin(), clear_data_.end(), float_rand); |
141 } | 126 orig_data_ = clear_data_; |
142 for (auto step_type : step_types) { | 127 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 } | 128 } |
148 | 129 |
149 // Tests ERB bank creation, comparing against matlab output. | 130 // Tests ERB bank creation, comparing against matlab output. |
150 TEST_F(IntelligibilityEnhancerTest, TestErbCreation) { | 131 TEST_F(IntelligibilityEnhancerTest, TestErbCreation) { |
151 ASSERT_EQ(arraysize(kTestCenterFreqs), enh_->bank_size_); | 132 ASSERT_EQ(arraysize(kTestCenterFreqs), enh_->bank_size_); |
152 for (size_t i = 0; i < enh_->bank_size_; ++i) { | 133 for (size_t i = 0; i < enh_->bank_size_; ++i) { |
153 EXPECT_NEAR(kTestCenterFreqs[i], enh_->center_freqs_[i], kMaxTestError); | 134 EXPECT_NEAR(kTestCenterFreqs[i], enh_->center_freqs_[i], kMaxTestError); |
154 ASSERT_EQ(arraysize(kTestFilterBank[0]), enh_->freqs_); | 135 ASSERT_EQ(arraysize(kTestFilterBank[0]), enh_->freqs_); |
155 for (size_t j = 0; j < enh_->freqs_; ++j) { | 136 for (size_t j = 0; j < enh_->freqs_; ++j) { |
156 EXPECT_NEAR(kTestFilterBank[i][j], enh_->render_filter_bank_[i][j], | 137 EXPECT_NEAR(kTestFilterBank[i][j], enh_->render_filter_bank_[i][j], |
157 kMaxTestError); | 138 kMaxTestError); |
158 } | 139 } |
159 } | 140 } |
160 } | 141 } |
161 | 142 |
162 // Tests analytic solution for optimal gains, comparing | 143 // Tests analytic solution for optimal gains, comparing |
163 // against matlab output. | 144 // against matlab output. |
164 TEST_F(IntelligibilityEnhancerTest, TestSolveForGains) { | 145 TEST_F(IntelligibilityEnhancerTest, TestSolveForGains) { |
165 ASSERT_EQ(kTestStartFreq, enh_->start_freq_); | 146 ASSERT_EQ(kTestStartFreq, enh_->start_freq_); |
166 vector<float> sols(enh_->bank_size_); | 147 std::vector<float> sols(enh_->bank_size_); |
167 float lambda = -0.001f; | 148 float lambda = -0.001f; |
168 for (size_t i = 0; i < enh_->bank_size_; i++) { | 149 for (size_t i = 0; i < enh_->bank_size_; i++) { |
169 enh_->filtered_clear_var_[i] = 0.0f; | 150 enh_->filtered_clear_pow_[i] = 0.0f; |
170 enh_->filtered_noise_var_[i] = 0.0f; | 151 enh_->filtered_noise_pow_[i] = 0.0f; |
171 enh_->rho_[i] = 0.02f; | 152 enh_->rho_[i] = 0.02f; |
172 } | 153 } |
173 enh_->SolveForGainsGivenLambda(lambda, enh_->start_freq_, &sols[0]); | 154 enh_->SolveForGainsGivenLambda(lambda, enh_->start_freq_, &sols[0]); |
174 for (size_t i = 0; i < enh_->bank_size_; i++) { | 155 for (size_t i = 0; i < enh_->bank_size_; i++) { |
175 EXPECT_NEAR(kTestZeroVar[i], sols[i], kMaxTestError); | 156 EXPECT_NEAR(kTestZeroVar[i], sols[i], kMaxTestError); |
176 } | 157 } |
177 for (size_t i = 0; i < enh_->bank_size_; i++) { | 158 for (size_t i = 0; i < enh_->bank_size_; i++) { |
178 enh_->filtered_clear_var_[i] = static_cast<float>(i + 1); | 159 enh_->filtered_clear_pow_[i] = static_cast<float>(i + 1); |
179 enh_->filtered_noise_var_[i] = static_cast<float>(enh_->bank_size_ - i); | 160 enh_->filtered_noise_pow_[i] = static_cast<float>(enh_->bank_size_ - i); |
180 } | 161 } |
181 enh_->SolveForGainsGivenLambda(lambda, enh_->start_freq_, &sols[0]); | 162 enh_->SolveForGainsGivenLambda(lambda, enh_->start_freq_, &sols[0]); |
182 for (size_t i = 0; i < enh_->bank_size_; i++) { | 163 for (size_t i = 0; i < enh_->bank_size_; i++) { |
183 EXPECT_NEAR(kTestNonZeroVarLambdaTop[i], sols[i], kMaxTestError); | 164 EXPECT_NEAR(kTestNonZeroVarLambdaTop[i], sols[i], kMaxTestError); |
184 } | 165 } |
185 lambda = -1.0; | 166 lambda = -1.0; |
186 enh_->SolveForGainsGivenLambda(lambda, enh_->start_freq_, &sols[0]); | 167 enh_->SolveForGainsGivenLambda(lambda, enh_->start_freq_, &sols[0]); |
187 for (size_t i = 0; i < enh_->bank_size_; i++) { | 168 for (size_t i = 0; i < enh_->bank_size_; i++) { |
188 EXPECT_NEAR(kTestZeroVar[i], sols[i], kMaxTestError); | 169 EXPECT_NEAR(kTestZeroVar[i], sols[i], kMaxTestError); |
189 } | 170 } |
190 } | 171 } |
191 | 172 |
192 } // namespace webrtc | 173 } // namespace webrtc |
OLD | NEW |