Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 /* | |
| 2 * Copyright (c) 2014 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 <cmath> | |
| 16 #include <algorithm> | |
| 17 | |
| 18 #include "testing/gtest/include/gtest/gtest.h" | |
| 19 #include "webrtc/common_audio/signal_processing/include/signal_processing_librar y.h" | |
| 20 #include "webrtc/modules/audio_processing/intelligibility/intelligibility_enhanc er.h" | |
| 21 | |
| 22 using std::vector; | |
| 23 using webrtc::intelligibility::VarianceArray; | |
| 24 | |
| 25 namespace webrtc { | |
| 26 | |
| 27 // Generated with matlab code: normrnd(0,1000,64,1). | |
| 28 const double kGaussianSamples[64] = {1689.1, 1437, -2251.1, 356.49, -850.24, | |
| 29 -299.55, -634.25, 1624.5, 1241.1, 555.28, 703.42, 458.16, 683.98, 251.29, | |
| 30 -178.5, 507.73, -309.9, -394.37, -269.74, -88.13, 8.0293, 2531.8, -1223.2, | |
| 31 -1071.8, 246.06, -50.611, -730.15, 326.99, 752.99, -1153.7, -407.87, | |
| 32 -1287.9, 83.578, 163.8, 682.57, -1086.4, 297.49, -143.31, 1392, 306.75, | |
| 33 -537.18, -228.93, -536.22, 1439, -511.1, -1606.8, -201.24, 1143.5, 663.29, | |
| 34 164.08, 1785.4, -587.71, 259.04, -871.83, -787.92, -344.34, 647.62, | |
| 35 2054.1, 798.94, -1071.1, -205.16, -554.44, -292.94, 1180.2}; | |
| 36 | |
| 37 // Target output for ERB create test. Generated with matlab. | |
| 38 const double kTestNumCenterFreqs = 22; | |
| 39 const double kTestCenterFreqs[22] = {13.169, 26.965, 41.423, 56.577, 72.461, | |
| 40 89.113, 106.57, 124.88, 144.08, 164.21, 185.34, 207.5, 230.75, 255.16, | |
| 41 280.77, 307.66, 335.9, 365.56, 396.71, 429.44, 463.84, 500}; | |
| 42 const double kTestNumFreqs = 2; | |
| 43 const double kTestFilterBank[22][2] = { {0.055556, 0}, {0.055556, 0}, | |
| 44 {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.2}, | |
| 52 {0, 0.2}, {0, 0.2}, | |
| 53 {0, 0.2}, {0, 0.2} }; | |
| 54 // Target output for gain solving test. Generated with matlab. | |
| 55 const double kTestZeroVar[22] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | |
| 56 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; | |
| 57 const double kTestNonZeroVarLambdaTop[22] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | |
| 58 0, 0, 0.0351, 0.0636, 0.0863, | |
| 59 0.1037, 0.1162, 0.1236, 0.1251, | |
| 60 0.1189, 0.0993}; | |
| 61 const float kMaxTestError = 0.005; | |
| 62 | |
| 63 // Enhancer initialization parameters. | |
| 64 const int kSamples = 2000; | |
| 65 const int kErbResolution = 2; | |
| 66 const int kSampleRate = 1000; | |
| 67 const int kFragmentSize = kSampleRate / 100; | |
| 68 const int kNumChannels = 1; | |
| 69 const float kDecayRate = 0.9f; | |
| 70 const int kWindowSize = 800; | |
| 71 const int kAnalyzeRate = 800; | |
| 72 const int kVarianceRate = 2; | |
| 73 const float kGainLimit = 0.1f; | |
| 74 | |
| 75 void GenerateConstantData(vector<float>& data, float constant) { | |
| 76 for (size_t i = 0; i < data.size(); i++) { | |
| 77 data[i] = constant; | |
| 78 } | |
| 79 } | |
| 80 | |
| 81 void GenerateGaussianData(vector<float>& data) { | |
| 82 static int count = 0; | |
| 83 for (size_t i = 0; i < data.size(); i++) { | |
| 84 data[i] = kGaussianSamples[count%64]; | |
| 85 count++; | |
| 86 } | |
| 87 } | |
| 88 | |
| 89 | |
| 90 class EnhancerTest : public ::testing::Test { | |
| 91 protected: | |
| 92 IntelligibilityEnhancer enh_; | |
| 93 vector<float> clear_data_; | |
| 94 vector<float> noise_data_; | |
| 95 EnhancerTest() : | |
| 96 enh_(kErbResolution, | |
| 97 kSampleRate, | |
| 98 kNumChannels, | |
| 99 VarianceArray::kStepInfinite, | |
| 100 kDecayRate, | |
| 101 kWindowSize, | |
| 102 kAnalyzeRate, | |
| 103 kVarianceRate, | |
| 104 kGainLimit), | |
| 105 clear_data_(kSamples), | |
| 106 noise_data_(kSamples) {} | |
| 107 | |
| 108 void RunEnhancer(VarianceArray::StepType step_type) { | |
|
turaj
2015/06/26 00:32:58
Could this function use |enh_| instead?
ekm
2015/06/26 19:07:09
That would be better, but I was having trouble mod
turaj
2015/06/29 17:33:35
Sorry, I didn't notice that |step_type| is an inpu
| |
| 109 IntelligibilityEnhancer enh(kErbResolution, | |
| 110 kSampleRate, | |
| 111 kNumChannels, | |
| 112 step_type, | |
| 113 kDecayRate, | |
| 114 kWindowSize, | |
| 115 kAnalyzeRate, | |
| 116 kVarianceRate, | |
| 117 kGainLimit); | |
| 118 float* clear_cursor = &clear_data_[0]; | |
| 119 float* noise_cursor = &noise_data_[0]; | |
| 120 for (int i = 0; i < kSamples; i+= kFragmentSize) { | |
| 121 enh.ProcessCaptureAudio(&noise_cursor); | |
| 122 enh.ProcessRenderAudio(&clear_cursor); | |
| 123 clear_cursor += kFragmentSize; | |
| 124 noise_cursor += kFragmentSize; | |
| 125 } | |
| 126 } | |
| 127 }; | |
| 128 | |
| 129 // For each class of generated data, tests plumbing for | |
| 130 // each variance update method. | |
| 131 TEST_F(EnhancerTest, TestPlumbing) { | |
|
turaj
2015/06/26 00:32:58
what is it that ids tested here? How could it fail
ekm
2015/06/26 19:07:09
Before we switched to doing nothing in case of und
turaj
2015/06/29 17:33:36
Agreed, this is a better test.
| |
| 132 vector<VarianceArray::StepType> step_types = { | |
| 133 VarianceArray::kStepInfinite, VarianceArray::kStepDecaying, | |
| 134 VarianceArray::kStepWindowed, VarianceArray::kStepBlocked, | |
| 135 VarianceArray::kStepBlockBasedMovingAverage}; | |
| 136 for (vector<VarianceArray::StepType>::iterator step_type = | |
| 137 step_types.begin(); step_type != step_types.end(); ++step_type) { | |
| 138 GenerateConstantData(clear_data_, 0.0f); | |
| 139 GenerateConstantData(noise_data_, 0.0f); | |
| 140 RunEnhancer(*step_type); | |
| 141 GenerateConstantData(clear_data_, 500.0f); | |
| 142 RunEnhancer(*step_type); | |
| 143 GenerateConstantData(noise_data_, 500.0f); | |
| 144 RunEnhancer(*step_type); | |
| 145 GenerateGaussianData(clear_data_); | |
| 146 RunEnhancer(*step_type); | |
| 147 GenerateGaussianData(noise_data_); | |
| 148 RunEnhancer(*step_type); | |
| 149 GenerateConstantData(clear_data_, 0); | |
| 150 RunEnhancer(*step_type); | |
| 151 } | |
| 152 } | |
| 153 | |
| 154 // Tests ERB bank creation, comparing against matlab output. | |
| 155 TEST_F(EnhancerTest, TestErbCreation) { | |
| 156 ASSERT_EQ(enh_.bank_size_, kTestNumCenterFreqs); | |
| 157 for (int i = 0; i < enh_.bank_size_; ++i) { | |
| 158 float error = std::fabs(enh_.center_freqs_[i] - kTestCenterFreqs[i]); | |
| 159 EXPECT_LT(error, kMaxTestError); | |
| 160 ASSERT_EQ(enh_.freqs_, kTestNumFreqs); | |
| 161 for (int j = 0; j < enh_.freqs_; ++j) { | |
| 162 float error = std::fabs(enh_.filter_bank_[i][j] - kTestFilterBank[i][j]); | |
| 163 EXPECT_LT(error, kMaxTestError); | |
| 164 } | |
| 165 } | |
| 166 } | |
| 167 | |
| 168 // Tests analytic solution for optimal gains, comparing | |
| 169 // against matlab output. | |
| 170 TEST_F(EnhancerTest, TestSolveForGains) { | |
| 171 ASSERT_EQ(enh_.start_freq_, 12); | |
| 172 vector<float> sols(enh_.bank_size_); | |
| 173 float lambda = -0.001; | |
|
turaj
2015/06/26 00:32:58
I guess you need -0.001f otherwise Visual Studio c
ekm
2015/06/26 19:07:09
Done.
| |
| 174 for (int i = 0; i < enh_.bank_size_; i++) { | |
| 175 enh_.filtered_clear_var_[i] = 0.0; | |
| 176 enh_.filtered_noise_var_[i] = 0.0; | |
| 177 enh_.rho_[i] = 0.02; | |
| 178 } | |
| 179 enh_.SolveForGainsGivenLambda(lambda, enh_.start_freq_, &sols[0]); | |
| 180 for (int i = 0; i < enh_.bank_size_; i++) { | |
| 181 float error = std::fabs(sols[i] - kTestZeroVar[i]); | |
| 182 EXPECT_LT(error, kMaxTestError); | |
|
turaj
2015/06/26 00:32:58
I guess you can use EXPECT_NEAR(v1, v2, tolerance)
ekm
2015/06/26 19:07:10
Done.
| |
| 183 } | |
| 184 for (int i = 0; i < enh_.bank_size_; i++) { | |
| 185 enh_.filtered_clear_var_[i] = static_cast<float>(i+1); | |
|
turaj
2015/06/26 00:32:58
'i + 1'
ekm
2015/06/26 19:07:10
Done.
| |
| 186 enh_.filtered_noise_var_[i] = static_cast<float>(enh_.bank_size_ - i); | |
| 187 } | |
| 188 enh_.SolveForGainsGivenLambda(lambda, enh_.start_freq_, &sols[0]); | |
| 189 for (int i = 0; i < enh_.bank_size_; i++) { | |
| 190 float error = std::fabs(sols[i] - kTestNonZeroVarLambdaTop[i]); | |
| 191 EXPECT_LT(error, kMaxTestError); | |
| 192 } | |
| 193 lambda = -1.0; | |
| 194 enh_.SolveForGainsGivenLambda(lambda, enh_.start_freq_, &sols[0]); | |
| 195 for (int i = 0; i < enh_.bank_size_; i++) { | |
| 196 float error = std::fabs(sols[i] - kTestZeroVar[i]); | |
| 197 EXPECT_LT(error, kMaxTestError); | |
| 198 } | |
| 199 } | |
| 200 | |
| 201 } // namespace webrtc | |
| OLD | NEW |