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 float kTestCenterFreqs[] = { |
| 31 13.169f, 26.965f, 41.423f, 56.577f, 72.461f, 89.113f, 106.57f, 124.88f, |
| 32 144.08f, 164.21f, 185.34f, 207.5f, 230.75f, 255.16f, 280.77f, 307.66f, |
| 33 335.9f, 365.56f, 396.71f, 429.44f, 463.84f, 500.f}; |
| 34 const float kTestFilterBank[][2] = {{0.055556f, 0.f}, |
| 35 {0.055556f, 0.f}, |
| 36 {0.055556f, 0.f}, |
| 37 {0.055556f, 0.f}, |
| 38 {0.055556f, 0.f}, |
| 39 {0.055556f, 0.f}, |
| 40 {0.055556f, 0.f}, |
| 41 {0.055556f, 0.f}, |
| 42 {0.055556f, 0.f}, |
| 43 {0.055556f, 0.f}, |
| 44 {0.055556f, 0.f}, |
| 45 {0.055556f, 0.f}, |
| 46 {0.055556f, 0.f}, |
| 47 {0.055556f, 0.f}, |
| 48 {0.055556f, 0.f}, |
| 49 {0.055556f, 0.f}, |
| 50 {0.055556f, 0.f}, |
| 51 {0.055556f, 0.2f}, |
| 52 {0, 0.2f}, |
| 53 {0, 0.2f}, |
| 54 {0, 0.2f}, |
| 55 {0, 0.2f}}; |
| 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 float kTestZeroVar[] = {1.f, 1.f, 1.f, 1.f, 1.f, 1.f, 1.f, 1.f, |
| 62 1.f, 1.f, 1.f, 0.f, 0.f, 0.f, 0.f, 0.f, |
| 63 0.f, 0.f, 0.f, 0.f, 0.f, 0.f}; |
| 64 static_assert(arraysize(kTestCenterFreqs) == arraysize(kTestZeroVar), |
| 65 "Variance test data badly initialized."); |
| 66 const float kTestNonZeroVarLambdaTop[] = { |
| 67 1.f, 1.f, 1.f, 1.f, 1.f, 1.f, 1.f, 1.f, |
| 68 1.f, 1.f, 1.f, 0.f, 0.f, 0.0351f, 0.0636f, 0.0863f, |
| 69 0.1037f, 0.1162f, 0.1236f, 0.1251f, 0.1189f, 0.0993f}; |
| 70 static_assert(arraysize(kTestCenterFreqs) == |
| 71 arraysize(kTestNonZeroVarLambdaTop), |
| 72 "Variance test data badly initialized."); |
| 73 const float kMaxTestError = 0.005f; |
| 74 |
| 75 // Enhancer initialization parameters. |
| 76 const int kSamples = 2000; |
| 77 const int kErbResolution = 2; |
| 78 const int kSampleRate = 1000; |
| 79 const int kFragmentSize = kSampleRate / 100; |
| 80 const int kNumChannels = 1; |
| 81 const float kDecayRate = 0.9f; |
| 82 const int kWindowSize = 800; |
| 83 const int kAnalyzeRate = 800; |
| 84 const int kVarianceRate = 2; |
| 85 const float kGainLimit = 0.1f; |
| 86 |
| 87 } // namespace |
| 88 |
| 89 using std::vector; |
| 90 using intelligibility::VarianceArray; |
| 91 |
| 92 class IntelligibilityEnhancerTest : public ::testing::Test { |
| 93 protected: |
| 94 IntelligibilityEnhancerTest() |
| 95 : enh_(kErbResolution, |
| 96 kSampleRate, |
| 97 kNumChannels, |
| 98 VarianceArray::kStepInfinite, |
| 99 kDecayRate, |
| 100 kWindowSize, |
| 101 kAnalyzeRate, |
| 102 kVarianceRate, |
| 103 kGainLimit), |
| 104 clear_data_(kSamples), |
| 105 noise_data_(kSamples), |
| 106 orig_data_(kSamples) {} |
| 107 |
| 108 bool CheckUpdate(VarianceArray::StepType step_type) { |
| 109 IntelligibilityEnhancer enh(kErbResolution, kSampleRate, kNumChannels, |
| 110 step_type, kDecayRate, kWindowSize, |
| 111 kAnalyzeRate, kVarianceRate, kGainLimit); |
| 112 float* clear_cursor = &clear_data_[0]; |
| 113 float* noise_cursor = &noise_data_[0]; |
| 114 for (int i = 0; i < kSamples; i += kFragmentSize) { |
| 115 enh.ProcessCaptureAudio(&noise_cursor); |
| 116 enh.ProcessRenderAudio(&clear_cursor); |
| 117 clear_cursor += kFragmentSize; |
| 118 noise_cursor += kFragmentSize; |
| 119 } |
| 120 for (int i = 0; i < kSamples; i++) { |
| 121 if (std::fabs(clear_data_[i] - orig_data_[i]) > kMaxTestError) { |
| 122 return true; |
| 123 } |
| 124 } |
| 125 return false; |
| 126 } |
| 127 |
| 128 IntelligibilityEnhancer enh_; |
| 129 vector<float> clear_data_; |
| 130 vector<float> noise_data_; |
| 131 vector<float> orig_data_; |
| 132 }; |
| 133 |
| 134 // For each class of generated data, tests that render stream is |
| 135 // updated when it should be for each variance update method. |
| 136 TEST_F(IntelligibilityEnhancerTest, TestRenderUpdate) { |
| 137 vector<VarianceArray::StepType> step_types; |
| 138 step_types.push_back(VarianceArray::kStepInfinite); |
| 139 step_types.push_back(VarianceArray::kStepDecaying); |
| 140 step_types.push_back(VarianceArray::kStepWindowed); |
| 141 step_types.push_back(VarianceArray::kStepBlocked); |
| 142 step_types.push_back(VarianceArray::kStepBlockBasedMovingAverage); |
| 143 std::fill(noise_data_.begin(), noise_data_.end(), 0.0f); |
| 144 std::fill(orig_data_.begin(), orig_data_.end(), 0.0f); |
| 145 for (auto step_type : step_types) { |
| 146 std::fill(clear_data_.begin(), clear_data_.end(), 0.0f); |
| 147 EXPECT_FALSE(CheckUpdate(step_type)); |
| 148 } |
| 149 std::srand(1); |
| 150 auto float_rand = []() { return std::rand() * 2.f / RAND_MAX - 1; }; |
| 151 std::generate(noise_data_.begin(), noise_data_.end(), float_rand); |
| 152 for (auto step_type : step_types) { |
| 153 EXPECT_FALSE(CheckUpdate(step_type)); |
| 154 } |
| 155 for (auto step_type : step_types) { |
| 156 std::generate(clear_data_.begin(), clear_data_.end(), float_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.0f; |
| 183 enh_.filtered_noise_var_[i] = 0.0f; |
| 184 enh_.rho_[i] = 0.02f; |
| 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 |