OLD | NEW |
---|---|
(Empty) | |
1 /* | |
2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. | |
3 * | |
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 | |
6 * tree. An additional intellectual property rights grant can be found | |
7 * in the file PATENTS. All contributing project authors may | |
8 * be found in the AUTHORS file in the root of the source tree. | |
9 */ | |
10 | |
11 // | |
12 // Unit tests for intelligibility enhancer. | |
13 // | |
14 | |
15 #include <math.h> | |
16 #include <stdlib.h> | |
17 #include <algorithm> | |
18 #include <vector> | |
19 | |
20 #include "testing/gtest/include/gtest/gtest.h" | |
21 #include "webrtc/base/arraysize.h" | |
22 #include "webrtc/common_audio/signal_processing/include/signal_processing_librar y.h" | |
23 #include "webrtc/modules/audio_processing/intelligibility/intelligibility_enhanc er.h" | |
24 | |
25 namespace webrtc { | |
26 | |
27 namespace { | |
28 | |
29 // Target output for ERB create test. Generated with matlab. | |
30 const double kTestCenterFreqs[] = { | |
31 13.169, 26.965, 41.423, 56.577, 72.461, 89.113, 106.57, 124.88, | |
32 144.08, 164.21, 185.34, 207.5, 230.75, 255.16, 280.77, 307.66, | |
33 335.9, 365.56, 396.71, 429.44, 463.84, 500}; | |
34 const double kTestFilterBank[][2] = {{0.055556, 0}, | |
35 {0.055556, 0}, | |
36 {0.055556, 0}, | |
37 {0.055556, 0}, | |
38 {0.055556, 0}, | |
39 {0.055556, 0}, | |
40 {0.055556, 0}, | |
41 {0.055556, 0}, | |
42 {0.055556, 0}, | |
43 {0.055556, 0}, | |
44 {0.055556, 0}, | |
45 {0.055556, 0}, | |
46 {0.055556, 0}, | |
47 {0.055556, 0}, | |
48 {0.055556, 0}, | |
49 {0.055556, 0}, | |
50 {0.055556, 0}, | |
51 {0.055556, 0.2}, | |
52 {0, 0.2}, | |
53 {0, 0.2}, | |
54 {0, 0.2}, | |
55 {0, 0.2}}; | |
56 static_assert(arraysize(kTestCenterFreqs) == arraysize(kTestFilterBank), | |
57 "Test filterbank badly initialized."); | |
58 | |
59 // Target output for gain solving test. Generated with matlab. | |
60 const int kTestStartFreq = 12; // Lowest integral frequency for ERBs. | |
61 const double kTestZeroVar[] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | |
62 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; | |
63 static_assert(arraysize(kTestCenterFreqs) == arraysize(kTestZeroVar), | |
64 "Variance test data badly initialized."); | |
65 const double kTestNonZeroVarLambdaTop[] = { | |
66 1, 1, 1, 1, 1, 1, 1, 1, | |
67 1, 1, 1, 0, 0, 0.0351, 0.0636, 0.0863, | |
68 0.1037, 0.1162, 0.1236, 0.1251, 0.1189, 0.0993}; | |
69 static_assert(arraysize(kTestCenterFreqs) == | |
70 arraysize(kTestNonZeroVarLambdaTop), | |
71 "Variance test data badly initialized."); | |
72 const float kMaxTestError = 0.005f; | |
73 | |
74 // Enhancer initialization parameters. | |
75 const int kSamples = 2000; | |
76 const int kErbResolution = 2; | |
77 const int kSampleRate = 1000; | |
78 const int kFragmentSize = kSampleRate / 100; | |
79 const int kNumChannels = 1; | |
80 const float kDecayRate = 0.9f; | |
81 const int kWindowSize = 800; | |
82 const int kAnalyzeRate = 800; | |
83 const int kVarianceRate = 2; | |
84 const float kGainLimit = 0.1f; | |
85 | |
86 } // namespace | |
87 | |
88 using std::vector; | |
89 using intelligibility::VarianceArray; | |
90 | |
91 class IntelligibilityEnhancerTest : public ::testing::Test { | |
92 protected: | |
93 IntelligibilityEnhancerTest() | |
94 : enh_(kErbResolution, | |
95 kSampleRate, | |
96 kNumChannels, | |
97 VarianceArray::kStepInfinite, | |
98 kDecayRate, | |
99 kWindowSize, | |
100 kAnalyzeRate, | |
101 kVarianceRate, | |
102 kGainLimit), | |
103 clear_data_(kSamples), | |
104 noise_data_(kSamples), | |
105 orig_data_(kSamples) {} | |
106 | |
107 bool CheckUpdate(VarianceArray::StepType step_type) { | |
108 IntelligibilityEnhancer enh(kErbResolution, kSampleRate, kNumChannels, | |
109 step_type, kDecayRate, kWindowSize, | |
110 kAnalyzeRate, kVarianceRate, kGainLimit); | |
111 float* clear_cursor = &clear_data_[0]; | |
112 float* noise_cursor = &noise_data_[0]; | |
113 for (int i = 0; i < kSamples; i += kFragmentSize) { | |
114 enh.ProcessCaptureAudio(&noise_cursor); | |
115 enh.ProcessRenderAudio(&clear_cursor); | |
116 clear_cursor += kFragmentSize; | |
117 noise_cursor += kFragmentSize; | |
118 } | |
119 for (int i = 0; i < kSamples; i++) { | |
120 if (std::fabs(clear_data_[i] - orig_data_[i]) > kMaxTestError) { | |
121 return true; | |
122 } | |
123 } | |
124 return false; | |
125 } | |
126 | |
127 IntelligibilityEnhancer enh_; | |
128 vector<float> clear_data_; | |
129 vector<float> noise_data_; | |
130 vector<float> orig_data_; | |
131 }; | |
132 | |
133 // For each class of generated data, tests that render stream is | |
134 // updated when it should be for each variance update method. | |
135 TEST_F(IntelligibilityEnhancerTest, TestRenderUpdate) { | |
136 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.
| |
137 VarianceArray::kStepInfinite, | |
138 VarianceArray::kStepDecaying, | |
139 VarianceArray::kStepWindowed, | |
140 VarianceArray::kStepBlocked, | |
141 VarianceArray::kStepBlockBasedMovingAverage}; | |
142 std::fill(noise_data_.begin(), noise_data_.end(), 0.0f); | |
143 std::fill(orig_data_.begin(), orig_data_.end(), 0.0f); | |
144 for (auto step_type : step_types) { | |
145 std::fill(clear_data_.begin(), clear_data_.end(), 0.0f); | |
146 EXPECT_FALSE(CheckUpdate(step_type)); | |
147 } | |
148 std::srand(1); | |
149 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.
| |
150 static_cast<float>(std::rand())); | |
151 for (auto step_type : step_types) { | |
152 EXPECT_FALSE(CheckUpdate(step_type)); | |
153 } | |
154 for (auto step_type : step_types) { | |
155 std::fill(clear_data_.begin(), clear_data_.end(), | |
156 static_cast<float>(std::rand())); | |
157 orig_data_ = clear_data_; | |
158 EXPECT_TRUE(CheckUpdate(step_type)); | |
159 } | |
160 } | |
161 | |
162 // Tests ERB bank creation, comparing against matlab output. | |
163 TEST_F(IntelligibilityEnhancerTest, TestErbCreation) { | |
164 ASSERT_EQ(static_cast<int>(arraysize(kTestCenterFreqs)), enh_.bank_size_); | |
165 for (int i = 0; i < enh_.bank_size_; ++i) { | |
166 EXPECT_NEAR(kTestCenterFreqs[i], enh_.center_freqs_[i], kMaxTestError); | |
167 ASSERT_EQ(static_cast<int>(arraysize(kTestFilterBank[0])), enh_.freqs_); | |
168 for (int j = 0; j < enh_.freqs_; ++j) { | |
169 EXPECT_NEAR(kTestFilterBank[i][j], enh_.filter_bank_[i][j], | |
170 kMaxTestError); | |
171 } | |
172 } | |
173 } | |
174 | |
175 // Tests analytic solution for optimal gains, comparing | |
176 // against matlab output. | |
177 TEST_F(IntelligibilityEnhancerTest, TestSolveForGains) { | |
178 ASSERT_EQ(kTestStartFreq, enh_.start_freq_); | |
179 vector<float> sols(enh_.bank_size_); | |
180 float lambda = -0.001f; | |
181 for (int i = 0; i < enh_.bank_size_; i++) { | |
182 enh_.filtered_clear_var_[i] = 0.0; | |
183 enh_.filtered_noise_var_[i] = 0.0; | |
184 enh_.rho_[i] = 0.02; | |
185 } | |
186 enh_.SolveForGainsGivenLambda(lambda, enh_.start_freq_, &sols[0]); | |
187 for (int i = 0; i < enh_.bank_size_; i++) { | |
188 EXPECT_NEAR(kTestZeroVar[i], sols[i], kMaxTestError); | |
189 } | |
190 for (int i = 0; i < enh_.bank_size_; i++) { | |
191 enh_.filtered_clear_var_[i] = static_cast<float>(i + 1); | |
192 enh_.filtered_noise_var_[i] = static_cast<float>(enh_.bank_size_ - i); | |
193 } | |
194 enh_.SolveForGainsGivenLambda(lambda, enh_.start_freq_, &sols[0]); | |
195 for (int i = 0; i < enh_.bank_size_; i++) { | |
196 EXPECT_NEAR(kTestNonZeroVarLambdaTop[i], sols[i], kMaxTestError); | |
197 } | |
198 lambda = -1.0; | |
199 enh_.SolveForGainsGivenLambda(lambda, enh_.start_freq_, &sols[0]); | |
200 for (int i = 0; i < enh_.bank_size_; i++) { | |
201 EXPECT_NEAR(kTestZeroVar[i], sols[i], kMaxTestError); | |
202 } | |
203 } | |
204 | |
205 } // namespace webrtc | |
OLD | NEW |