OLD | NEW |
---|---|
(Empty) | |
1 /* | |
2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. | |
hlundin-webrtc
2015/06/30 14:00:53
2015
And please check all other files that have be
ekm
2015/07/01 23:48:26
Done.
| |
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 <cmath> | |
hlundin-webrtc
2015/06/30 14:00:53
math.h
ekm
2015/07/01 23:48:25
Done.
| |
16 #include <algorithm> | |
17 #include <vector> | |
18 | |
19 #include "testing/gtest/include/gtest/gtest.h" | |
20 #include "webrtc/common_audio/signal_processing/include/signal_processing_librar y.h" | |
21 #include "webrtc/modules/audio_processing/intelligibility/intelligibility_enhanc er.h" | |
22 | |
23 using std::vector; | |
24 using webrtc::intelligibility::VarianceArray; | |
25 | |
26 namespace webrtc { | |
27 | |
28 // Generated with matlab code: normrnd(0,1000,64,1). | |
29 const double kGaussianSamples[64] = {1689.1, 1437, -2251.1, 356.49, -850.24, | |
hlundin-webrtc
2015/06/30 14:00:52
Encapsulate all constants in an anonymous namespac
hlundin-webrtc
2015/06/30 14:00:53
The constant declarations are a bit messy. I sugge
ekm
2015/07/01 23:48:26
Done.
ekm
2015/07/01 23:48:26
Done.
Andrew MacDonald
2015/07/02 02:46:47
In fact, it's a good practice to wrap the entire t
ekm
2015/07/07 21:57:02
Got it. Leaving as is in this case since I'm using
| |
30 -299.55, -634.25, 1624.5, 1241.1, 555.28, 703.42, 458.16, 683.98, 251.29, | |
31 -178.5, 507.73, -309.9, -394.37, -269.74, -88.13, 8.0293, 2531.8, -1223.2, | |
32 -1071.8, 246.06, -50.611, -730.15, 326.99, 752.99, -1153.7, -407.87, | |
33 -1287.9, 83.578, 163.8, 682.57, -1086.4, 297.49, -143.31, 1392, 306.75, | |
34 -537.18, -228.93, -536.22, 1439, -511.1, -1606.8, -201.24, 1143.5, 663.29, | |
35 164.08, 1785.4, -587.71, 259.04, -871.83, -787.92, -344.34, 647.62, | |
36 2054.1, 798.94, -1071.1, -205.16, -554.44, -292.94, 1180.2}; | |
37 | |
38 // Target output for ERB create test. Generated with matlab. | |
39 const double kTestNumCenterFreqs = 22; | |
40 const double kTestCenterFreqs[22] = {13.169, 26.965, 41.423, 56.577, 72.461, | |
41 89.113, 106.57, 124.88, 144.08, 164.21, 185.34, 207.5, 230.75, 255.16, | |
42 280.77, 307.66, 335.9, 365.56, 396.71, 429.44, 463.84, 500}; | |
43 const double kTestNumFreqs = 2; | |
44 const double kTestFilterBank[22][2] = { {0.055556, 0}, {0.055556, 0}, | |
45 {0.055556, 0}, {0.055556, 0}, | |
46 {0.055556, 0}, {0.055556, 0}, | |
47 {0.055556, 0}, {0.055556, 0}, | |
48 {0.055556, 0}, {0.055556, 0}, | |
49 {0.055556, 0}, {0.055556, 0}, | |
50 {0.055556, 0}, {0.055556, 0}, | |
51 {0.055556, 0}, {0.055556, 0}, | |
52 {0.055556, 0}, {0.055556, 0.2}, | |
53 {0, 0.2}, {0, 0.2}, | |
54 {0, 0.2}, {0, 0.2} }; | |
55 // Target output for gain solving test. Generated with matlab. | |
56 const double kTestZeroVar[22] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | |
57 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; | |
58 const double kTestNonZeroVarLambdaTop[22] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | |
59 0, 0, 0.0351, 0.0636, 0.0863, | |
60 0.1037, 0.1162, 0.1236, 0.1251, | |
61 0.1189, 0.0993}; | |
62 const float kMaxTestError = 0.005f; | |
63 | |
64 // Enhancer initialization parameters. | |
65 const int kSamples = 2000; | |
66 const int kErbResolution = 2; | |
67 const int kSampleRate = 1000; | |
68 const int kFragmentSize = kSampleRate / 100; | |
69 const int kNumChannels = 1; | |
70 const float kDecayRate = 0.9f; | |
71 const int kWindowSize = 800; | |
72 const int kAnalyzeRate = 800; | |
73 const int kVarianceRate = 2; | |
74 const float kGainLimit = 0.1f; | |
75 | |
76 void GenerateConstantData(vector<float>& data, float constant) { | |
hlundin-webrtc
2015/06/30 14:00:53
Don't use non-const reference arguments; use point
ekm
2015/07/01 23:48:25
Done.
| |
77 for (size_t i = 0; i < data.size(); i++) { | |
78 data[i] = constant; | |
79 } | |
80 } | |
81 | |
82 void GenerateGaussianData(vector<float>& data) { | |
hlundin-webrtc
2015/06/30 14:00:53
Again, don't use non-const reference.
ekm
2015/07/01 23:48:26
Done.
| |
83 static int count = 0; | |
hlundin-webrtc
2015/06/30 14:00:53
Static... <cringe>...
ekm
2015/07/01 23:48:26
Done.
| |
84 for (size_t i = 0; i < data.size(); i++) { | |
85 data[i] = kGaussianSamples[count%64]; | |
86 count++; | |
87 } | |
88 } | |
89 | |
90 | |
91 class IntelligibilityEnhancerTest : public ::testing::Test { | |
92 protected: | |
93 IntelligibilityEnhancer enh_; | |
hlundin-webrtc
2015/06/30 14:00:53
Declaration order is wrong.
http://google-stylegui
ekm
2015/07/01 23:48:26
Done.
| |
94 vector<float> clear_data_; | |
95 vector<float> noise_data_; | |
96 vector<float> orig_data_; | |
97 IntelligibilityEnhancerTest() : | |
98 enh_(kErbResolution, | |
99 kSampleRate, | |
100 kNumChannels, | |
101 VarianceArray::kStepInfinite, | |
102 kDecayRate, | |
103 kWindowSize, | |
104 kAnalyzeRate, | |
105 kVarianceRate, | |
106 kGainLimit), | |
107 clear_data_(kSamples), | |
108 noise_data_(kSamples), | |
109 orig_data_(kSamples) {} | |
110 | |
111 bool CheckUpdate(VarianceArray::StepType step_type) { | |
112 IntelligibilityEnhancer enh(kErbResolution, | |
113 kSampleRate, | |
114 kNumChannels, | |
115 step_type, | |
116 kDecayRate, | |
117 kWindowSize, | |
118 kAnalyzeRate, | |
119 kVarianceRate, | |
120 kGainLimit); | |
121 float* clear_cursor = &clear_data_[0]; | |
122 float* noise_cursor = &noise_data_[0]; | |
123 for (int i = 0; i < kSamples; i+= kFragmentSize) { | |
hlundin-webrtc
2015/06/30 14:00:53
Spaces around +=
ekm
2015/07/01 23:48:26
Done.
| |
124 enh.ProcessCaptureAudio(&noise_cursor); | |
125 enh.ProcessRenderAudio(&clear_cursor); | |
126 clear_cursor += kFragmentSize; | |
127 noise_cursor += kFragmentSize; | |
128 } | |
129 float updated = false; | |
hlundin-webrtc
2015/06/30 14:00:52
bool
ekm
2015/07/01 23:48:26
Done.
| |
130 for (int i = 0; i < kSamples; i++) { | |
131 if (std::fabs(clear_data_[i] - orig_data_[i]) > kMaxTestError) { | |
132 updated = true; | |
hlundin-webrtc
2015/06/30 14:00:53
You can return true here, skip the bool variable,
ekm
2015/07/01 23:48:26
Done.
| |
133 } | |
134 } | |
135 return updated; | |
136 } | |
137 }; | |
138 | |
139 // For each class of generated data, tests that render stream is | |
140 // updated when it should be for each variance update method. | |
141 TEST_F(IntelligibilityEnhancerTest, TestRenderUpdate) { | |
142 vector<VarianceArray::StepType> step_types = { | |
143 VarianceArray::kStepInfinite, VarianceArray::kStepDecaying, | |
144 VarianceArray::kStepWindowed, VarianceArray::kStepBlocked, | |
145 VarianceArray::kStepBlockBasedMovingAverage}; | |
146 for (VarianceArray::StepType step_type : step_types) { | |
hlundin-webrtc
2015/06/30 14:00:53
for (auto step_type : step_types)
ekm
2015/07/01 23:48:26
Done. Thanks; nice to know auto is supported in th
| |
147 GenerateConstantData(clear_data_, 0.0f); | |
hlundin-webrtc
2015/06/30 14:00:52
Do you have to regenerate the data on each loop?
ekm
2015/07/01 23:48:26
Done. The clear data needs to be regenerated, but
| |
148 orig_data_ = clear_data_; | |
149 GenerateConstantData(noise_data_, 0.0f); | |
150 EXPECT_FALSE(CheckUpdate(step_type)); | |
151 GenerateGaussianData(noise_data_); | |
152 EXPECT_FALSE(CheckUpdate(step_type)); | |
153 GenerateGaussianData(clear_data_); | |
154 orig_data_ = clear_data_; | |
155 EXPECT_TRUE(CheckUpdate(step_type)); | |
156 } | |
157 } | |
158 | |
159 // Tests ERB bank creation, comparing against matlab output. | |
160 TEST_F(IntelligibilityEnhancerTest, TestErbCreation) { | |
161 ASSERT_EQ(enh_.bank_size_, kTestNumCenterFreqs); | |
162 for (int i = 0; i < enh_.bank_size_; ++i) { | |
163 EXPECT_NEAR(enh_.center_freqs_[i], kTestCenterFreqs[i], kMaxTestError); | |
164 ASSERT_EQ(enh_.freqs_, kTestNumFreqs); | |
165 for (int j = 0; j < enh_.freqs_; ++j) { | |
166 EXPECT_NEAR(enh_.filter_bank_[i][j], kTestFilterBank[i][j], | |
167 kMaxTestError); | |
hlundin-webrtc
2015/06/30 14:00:52
Weird line wrapping. Run "git cl format" on the pa
ekm
2015/07/01 23:48:26
Done.
| |
168 } | |
169 } | |
170 } | |
171 | |
172 // Tests analytic solution for optimal gains, comparing | |
173 // against matlab output. | |
174 TEST_F(IntelligibilityEnhancerTest, TestSolveForGains) { | |
175 ASSERT_EQ(enh_.start_freq_, 12); | |
hlundin-webrtc
2015/06/30 14:00:52
What is 12? And why is it crucial that start_freq_
ekm
2015/07/01 23:48:26
Done.
| |
176 vector<float> sols(enh_.bank_size_); | |
177 float lambda = -0.001f; | |
178 for (int i = 0; i < enh_.bank_size_; i++) { | |
179 enh_.filtered_clear_var_[i] = 0.0; | |
180 enh_.filtered_noise_var_[i] = 0.0; | |
181 enh_.rho_[i] = 0.02; | |
182 } | |
183 enh_.SolveForGainsGivenLambda(lambda, enh_.start_freq_, &sols[0]); | |
184 for (int i = 0; i < enh_.bank_size_; i++) { | |
185 EXPECT_NEAR(sols[i], kTestZeroVar[i], kMaxTestError); | |
186 } | |
187 for (int i = 0; i < enh_.bank_size_; i++) { | |
188 enh_.filtered_clear_var_[i] = static_cast<float>(i + 1); | |
189 enh_.filtered_noise_var_[i] = static_cast<float>(enh_.bank_size_ - i); | |
190 } | |
191 enh_.SolveForGainsGivenLambda(lambda, enh_.start_freq_, &sols[0]); | |
192 for (int i = 0; i < enh_.bank_size_; i++) { | |
193 EXPECT_NEAR(sols[i], kTestNonZeroVarLambdaTop[i], kMaxTestError); | |
194 } | |
195 lambda = -1.0; | |
196 enh_.SolveForGainsGivenLambda(lambda, enh_.start_freq_, &sols[0]); | |
197 for (int i = 0; i < enh_.bank_size_; i++) { | |
198 EXPECT_NEAR(sols[i], kTestZeroVar[i], kMaxTestError); | |
199 } | |
200 } | |
201 | |
202 } // namespace webrtc | |
OLD | NEW |