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 utils. |
| 13 // |
| 14 |
| 15 #include <math.h> |
| 16 #include <complex> |
| 17 #include <iostream> |
| 18 #include <vector> |
| 19 |
| 20 #include "testing/gtest/include/gtest/gtest.h" |
| 21 #include "webrtc/base/arraysize.h" |
| 22 #include "webrtc/modules/audio_processing/intelligibility/intelligibility_utils.
h" |
| 23 |
| 24 using std::complex; |
| 25 using std::vector; |
| 26 |
| 27 namespace webrtc { |
| 28 |
| 29 namespace intelligibility { |
| 30 |
| 31 vector<vector<complex<float>>> GenerateTestData(int freqs, int samples) { |
| 32 vector<vector<complex<float>>> data(samples); |
| 33 for (int i = 0; i < samples; i++) { |
| 34 for (int j = 0; j < freqs; j++) { |
| 35 const float val = 0.99f / ((i + 1) * (j + 1)); |
| 36 data[i].push_back(complex<float>(val, val)); |
| 37 } |
| 38 } |
| 39 return data; |
| 40 } |
| 41 |
| 42 // Tests UpdateFactor. |
| 43 TEST(IntelligibilityUtilsTest, TestUpdateFactor) { |
| 44 EXPECT_EQ(0, intelligibility::UpdateFactor(0, 0, 0)); |
| 45 EXPECT_EQ(4, intelligibility::UpdateFactor(4, 2, 3)); |
| 46 EXPECT_EQ(3, intelligibility::UpdateFactor(4, 2, 1)); |
| 47 EXPECT_EQ(2, intelligibility::UpdateFactor(2, 4, 3)); |
| 48 EXPECT_EQ(3, intelligibility::UpdateFactor(2, 4, 1)); |
| 49 } |
| 50 |
| 51 // Tests cplxfinite, cplxnormal, and zerofudge. |
| 52 TEST(IntelligibilityUtilsTest, TestCplx) { |
| 53 complex<float> t0(1.f, 0.f); |
| 54 EXPECT_TRUE(intelligibility::cplxfinite(t0)); |
| 55 EXPECT_FALSE(intelligibility::cplxnormal(t0)); |
| 56 t0 = intelligibility::zerofudge(t0); |
| 57 EXPECT_NE(t0.imag(), 0.f); |
| 58 EXPECT_NE(t0.real(), 0.f); |
| 59 const complex<float> t1(1.f, std::sqrt(-1.f)); |
| 60 EXPECT_FALSE(intelligibility::cplxfinite(t1)); |
| 61 EXPECT_FALSE(intelligibility::cplxnormal(t1)); |
| 62 const complex<float> t2(1.f, 1.f); |
| 63 EXPECT_TRUE(intelligibility::cplxfinite(t2)); |
| 64 EXPECT_TRUE(intelligibility::cplxnormal(t2)); |
| 65 } |
| 66 |
| 67 // Tests NewMean and AddToMean. |
| 68 TEST(IntelligibilityUtilsTest, TestMeanUpdate) { |
| 69 const complex<float> data[] = {{3, 8}, {7, 6}, {2, 1}, {8, 9}, {0, 6}}; |
| 70 const complex<float> means[] = {{3, 8}, {5, 7}, {4, 5}, {5, 6}, {4, 6}}; |
| 71 complex<float> mean(3, 8); |
| 72 for (size_t i = 0; i < arraysize(data); i++) { |
| 73 EXPECT_EQ(means[i], NewMean(mean, data[i], i + 1)); |
| 74 AddToMean(data[i], i + 1, &mean); |
| 75 EXPECT_EQ(means[i], mean); |
| 76 } |
| 77 } |
| 78 |
| 79 // Tests VarianceArray, for all variance step types. |
| 80 TEST(IntelligibilityUtilsTest, TestVarianceArray) { |
| 81 const int kFreqs = 10; |
| 82 const int kSamples = 100; |
| 83 const int kWindowSize = 10; // Should pass for all kWindowSize > 1. |
| 84 const float kDecay = 0.5f; |
| 85 vector<VarianceArray::StepType> step_types; |
| 86 step_types.push_back(VarianceArray::kStepInfinite); |
| 87 step_types.push_back(VarianceArray::kStepDecaying); |
| 88 step_types.push_back(VarianceArray::kStepWindowed); |
| 89 step_types.push_back(VarianceArray::kStepBlocked); |
| 90 step_types.push_back(VarianceArray::kStepBlockBasedMovingAverage); |
| 91 const vector<vector<complex<float>>> test_data( |
| 92 GenerateTestData(kFreqs, kSamples)); |
| 93 for (auto step_type : step_types) { |
| 94 VarianceArray variance_array(kFreqs, step_type, kWindowSize, kDecay); |
| 95 EXPECT_EQ(0, variance_array.variance()[0]); |
| 96 EXPECT_EQ(0, variance_array.array_mean()); |
| 97 variance_array.ApplyScale(2.0f); |
| 98 EXPECT_EQ(0, variance_array.variance()[0]); |
| 99 EXPECT_EQ(0, variance_array.array_mean()); |
| 100 |
| 101 // Makes sure Step is doing something. |
| 102 variance_array.Step(&test_data[0][0]); |
| 103 for (int i = 1; i < kSamples; i++) { |
| 104 variance_array.Step(&test_data[i][0]); |
| 105 EXPECT_GE(variance_array.array_mean(), 0.0f); |
| 106 EXPECT_LE(variance_array.array_mean(), 1.0f); |
| 107 for (int j = 0; j < kFreqs; j++) { |
| 108 EXPECT_GE(variance_array.variance()[j], 0.0f); |
| 109 EXPECT_LE(variance_array.variance()[j], 1.0f); |
| 110 } |
| 111 } |
| 112 variance_array.Clear(); |
| 113 EXPECT_EQ(0, variance_array.variance()[0]); |
| 114 EXPECT_EQ(0, variance_array.array_mean()); |
| 115 } |
| 116 } |
| 117 |
| 118 // Tests exact computation on synthetic data. |
| 119 TEST(IntelligibilityUtilsTest, TestMovingBlockAverage) { |
| 120 // Exact, not unbiased estimates. |
| 121 const float kTestVarianceBufferNotFull = 16.5f; |
| 122 const float kTestVarianceBufferFull1 = 66.5f; |
| 123 const float kTestVarianceBufferFull2 = 333.375f; |
| 124 const int kFreqs = 2; |
| 125 const int kSamples = 50; |
| 126 const int kWindowSize = 2; |
| 127 const float kDecay = 0.5f; |
| 128 const float kMaxError = 0.0001f; |
| 129 |
| 130 VarianceArray variance_array( |
| 131 kFreqs, VarianceArray::kStepBlockBasedMovingAverage, kWindowSize, kDecay); |
| 132 |
| 133 vector<vector<complex<float>>> test_data(kSamples); |
| 134 for (int i = 0; i < kSamples; i++) { |
| 135 for (int j = 0; j < kFreqs; j++) { |
| 136 if (i < 30) { |
| 137 test_data[i].push_back(complex<float>(static_cast<float>(kSamples - i), |
| 138 static_cast<float>(i + 1))); |
| 139 } else { |
| 140 test_data[i].push_back(complex<float>(0.f, 0.f)); |
| 141 } |
| 142 } |
| 143 } |
| 144 |
| 145 for (int i = 0; i < kSamples; i++) { |
| 146 variance_array.Step(&test_data[i][0]); |
| 147 for (int j = 0; j < kFreqs; j++) { |
| 148 if (i < 9) { // In utils, kWindowBlockSize = 10. |
| 149 EXPECT_EQ(0, variance_array.variance()[j]); |
| 150 } else if (i < 19) { |
| 151 EXPECT_NEAR(kTestVarianceBufferNotFull, variance_array.variance()[j], |
| 152 kMaxError); |
| 153 } else if (i < 39) { |
| 154 EXPECT_NEAR(kTestVarianceBufferFull1, variance_array.variance()[j], |
| 155 kMaxError); |
| 156 } else if (i < 49) { |
| 157 EXPECT_NEAR(kTestVarianceBufferFull2, variance_array.variance()[j], |
| 158 kMaxError); |
| 159 } else { |
| 160 EXPECT_EQ(0, variance_array.variance()[j]); |
| 161 } |
| 162 } |
| 163 } |
| 164 } |
| 165 |
| 166 // Tests gain applier. |
| 167 TEST(IntelligibilityUtilsTest, TestGainApplier) { |
| 168 const int kFreqs = 10; |
| 169 const int kSamples = 100; |
| 170 const float kChangeLimit = 0.1f; |
| 171 GainApplier gain_applier(kFreqs, kChangeLimit); |
| 172 const vector<vector<complex<float>>> in_data( |
| 173 GenerateTestData(kFreqs, kSamples)); |
| 174 vector<vector<complex<float>>> out_data(GenerateTestData(kFreqs, kSamples)); |
| 175 for (int i = 0; i < kSamples; i++) { |
| 176 gain_applier.Apply(&in_data[i][0], &out_data[i][0]); |
| 177 for (int j = 0; j < kFreqs; j++) { |
| 178 EXPECT_GT(out_data[i][j].real(), 0.0f); |
| 179 EXPECT_LT(out_data[i][j].real(), 1.0f); |
| 180 EXPECT_GT(out_data[i][j].imag(), 0.0f); |
| 181 EXPECT_LT(out_data[i][j].imag(), 1.0f); |
| 182 } |
| 183 } |
| 184 } |
| 185 |
| 186 } // namespace intelligibility |
| 187 |
| 188 } // namespace webrtc |
OLD | NEW |