Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(115)

Side by Side Diff: webrtc/modules/audio_processing/intelligibility/intelligibility_utils_unittest.cc

Issue 1207353002: Add new variance update option and unittests for intelligibility (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Renamed tests + minor changes Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
hlundin-webrtc 2015/07/01 09:09:27 2015
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 utils.
13 //
14
15
16 #include <cmath>
hlundin-webrtc 2015/07/01 09:09:27 math.h
ekm 2015/07/01 23:48:27 Done.
17 #include <iostream>
18 #include <vector>
19
20 #include "testing/gtest/include/gtest/gtest.h"
21 #include "webrtc/modules/audio_processing/intelligibility/intelligibility_utils. h"
22
23 using std::complex;
24 using std::vector;
25
26 namespace webrtc {
27
28 namespace intelligibility {
29
30 vector<vector<complex<float>>> GenerateTestData(int freqs, int samples) {
31 vector<vector<complex<float>>> data(samples);
32 for (int i = 0; i < samples; i++) {
33 data[i].resize(freqs);
34 for (int j = 0; j < freqs; j++) {
35 data[i][j].real(0.99f / ((i+1)*(j+1)));
36 data[i][j].imag(0.99f / ((i+1)*(j+1)));
37 }
38 }
39 return data;
40 }
41
42 // Tests UpdateFactor.
43 TEST(IntelligibilityUtilsTest, TestUpdateFactor) {
44 EXPECT_EQ(intelligibility::UpdateFactor(0, 0, 0), 0);
hlundin-webrtc 2015/07/01 09:09:27 Switch the order of the parameters to EXPECT_EQ. S
ekm 2015/07/01 23:48:26 Done. It looks like for _NEAR order doesn't matter
Andrew MacDonald 2015/07/02 02:46:47 For consistency, I'd still use EXPECT_NEAR(expecte
ekm 2015/07/07 21:57:02 Done.
45 EXPECT_EQ(intelligibility::UpdateFactor(4, 2, 3), 4);
46 EXPECT_EQ(intelligibility::UpdateFactor(4, 2, 1), 3);
47 EXPECT_EQ(intelligibility::UpdateFactor(2, 4, 3), 2);
48 EXPECT_EQ(intelligibility::UpdateFactor(2, 4, 1), 3);
49 }
50
51 // Tests cplxfinite, cplxnormal, and zerofudge.
52 TEST(IntelligibilityUtilsTest, TestCplx) {
53 complex<float> t;
54 t.real(1.f);
55 t.imag(0.f);
56 EXPECT_TRUE(intelligibility::cplxfinite(t));
57 EXPECT_FALSE(intelligibility::cplxnormal(t));
58 t = intelligibility::zerofudge(t);
59 EXPECT_NE(t.imag(), 0.f);
60 EXPECT_NE(t.real(), 0.f);
61 t.imag(1.f/0.f);
62 EXPECT_FALSE(intelligibility::cplxfinite(t));
63 EXPECT_FALSE(intelligibility::cplxnormal(t));
64 t.imag(sqrt(-1.f));
65 EXPECT_FALSE(intelligibility::cplxfinite(t));
66 EXPECT_FALSE(intelligibility::cplxnormal(t));
67 t.imag(1.f);
68 EXPECT_TRUE(intelligibility::cplxfinite(t));
69 EXPECT_TRUE(intelligibility::cplxnormal(t));
70 }
71
72 // Tests NewMean and AddToMean.
73 /*TEST(IntelligibilityUtilsTest, TestMeanUpdate) {
hlundin-webrtc 2015/07/01 09:09:27 Don't comment out tests. Instead, disable them so
ekm 2015/07/01 23:48:26 Thanks for telling me about DISABLED_, that'll def
hlundin-webrtc 2015/07/02 10:53:13 Acknowledged.
74 vector<complex<float>> data = {{3, 8}, {7, 6}, {2, 1}, {8, 9}, {0, 6}};
75 vector<complex<float>> means = {{3, 8}, {5, 7}, {4, 5}, {5, 6}, {4, 6}};
76 complex<float> mean(3, 8);
77 for (vector<int>::size_type i = 0; i < data.size(); i++) {
78 EXPECT_EQ(NewMean(mean, data[i], i+1), means[i]);
79 AddToMean(data[i], i+1, &mean);
80 EXPECT_EQ(mean, means[i]);
81 }
82 }*/
83
84 // Tests VarianceArray, for all variance step types.
85 TEST(IntelligibilityUtilsTest, TestVarianceArray) {
86 const int kFreqs = 10;
87 const int kSamples = 100;
88 const int kWindowSize = 10; // Should pass for all kWindowSize > 1.
89 const float kDecay = 0.5;
90 vector<VarianceArray::StepType> step_types = {
hlundin-webrtc 2015/07/01 09:09:27 const vector
ekm 2015/07/01 23:48:27 Done.
91 VarianceArray::kStepInfinite, VarianceArray::kStepDecaying,
92 VarianceArray::kStepWindowed, VarianceArray::kStepBlocked,
93 VarianceArray::kStepBlockBasedMovingAverage};
94 const vector<vector<complex<float>>> test_data(
95 GenerateTestData(kFreqs, kSamples));
96 for (VarianceArray::StepType step_type : step_types) {
hlundin-webrtc 2015/07/01 09:09:27 for (auto ...)
ekm 2015/07/01 23:48:27 Done.
97 VarianceArray variance_array(kFreqs, step_type, kWindowSize, kDecay);
98 EXPECT_EQ(variance_array.variance()[0], 0);
99 EXPECT_EQ(variance_array.array_mean(), 0);
100 variance_array.ApplyScale(2.0f);
101 EXPECT_EQ(variance_array.variance()[0], 0);
102 EXPECT_EQ(variance_array.array_mean(), 0);
103
104 // Makes sure Step is doing something.
105 variance_array.Step(&test_data[0][0]);
106 for (int i = 1; i < kSamples; i++) {
107 variance_array.Step(&test_data[i][0]);
108 EXPECT_GE(variance_array.array_mean(), 0.0f);
109 EXPECT_LE(variance_array.array_mean(), 1.0f);
110 for (int j = 0; j < kFreqs; j++) {
111 EXPECT_GE(variance_array.variance()[j], 0.0f);
112 EXPECT_LE(variance_array.variance()[j], 1.0f);
113 }
114 }
115 variance_array.Clear();
116 EXPECT_EQ(variance_array.variance()[0], 0);
117 EXPECT_EQ(variance_array.array_mean(), 0);
118 }
119 }
120
121 // Tests exact computation on synthetic data.
122 TEST(IntelligibilityUtilsTest, TestMovingBlockAverage) {
123 // Exact, not unbiased estimates.
124 const float kTestVarianceBufferNotFull = 16.5f;
125 const float kTestVarianceBufferFull1 = 66.5f;
126 const float kTestVarianceBufferFull2 = 333.375f;
127 const int kFreqs = 2;
128 const int kSamples = 50;
129 const int kWindowSize = 2;
130 const float kDecay = 0.5f;
131 const float kMaxError = 0.0001f;
132
133 VarianceArray variance_array(
134 kFreqs, VarianceArray::kStepBlockBasedMovingAverage,
135 kWindowSize, kDecay);
136
137 vector<vector<complex<float>>> test_data(kSamples);
138 for (int i = 0; i < kSamples; i++) {
139 test_data[i].resize(kFreqs);
140 for (int j = 0; j < kFreqs; j++) {
141 if (i < 30) {
142 test_data[i][j].real(static_cast<float>(kSamples - i));
143 test_data[i][j].imag(static_cast<float>(i + 1));
144 } else {
145 test_data[i][j].real(0.f);
146 test_data[i][j].imag(0.f);
147 }
148 }
149 }
150
151 for (int i = 0; i < kSamples; i++) {
152 variance_array.Step(&test_data[i][0]);
153 for (int j = 0; j < kFreqs; j++) {
154 if (i < 9) { // In utils, kWindowBlockSize = 10.
155 EXPECT_EQ(variance_array.variance()[j], 0);
156 } else if (i < 19) {
157 EXPECT_NEAR(variance_array.variance()[j], kTestVarianceBufferNotFull,
158 kMaxError);
hlundin-webrtc 2015/07/01 09:09:27 Wrong indentation.
ekm 2015/07/01 23:48:27 Done.
Andrew MacDonald 2015/07/02 02:46:47 Strongly suggest running "git cl format" on all yo
ekm 2015/07/07 21:57:02 Acknowledged.
159 } else if (i < 39) {
160 EXPECT_NEAR(variance_array.variance()[j], kTestVarianceBufferFull1,
161 kMaxError);
162 } else if (i < 49) {
163 EXPECT_NEAR(variance_array.variance()[j], kTestVarianceBufferFull2,
164 kMaxError);
165 } else {
166 EXPECT_EQ(variance_array.variance()[j], 0);
167 }
168 }
169 }
170 }
171
172 // Tests gain applier.
173 TEST(IntelligibilityUtilsTest, TestGainApplier) {
174 const int kFreqs = 10;
175 const int kSamples = 100;
176 const float kChangeLimit = 0.1f;
177 GainApplier gain_applier(kFreqs, kChangeLimit);
178 const vector<vector<complex<float>>> in_data(
179 GenerateTestData(kFreqs, kSamples));
180 vector<vector<complex<float>>> out_data(
181 GenerateTestData(kFreqs, kSamples));
182 for (int i = 0; i < kSamples; i++) {
183 gain_applier.Apply(&in_data[i][0], &out_data[i][0]);
184 for (int j = 0; j < kFreqs; j++) {
185 EXPECT_GT(out_data[i][j].real(), 0.0f);
186 EXPECT_LT(out_data[i][j].real(), 1.0f);
187 EXPECT_GT(out_data[i][j].imag(), 0.0f);
188 EXPECT_LT(out_data[i][j].imag(), 1.0f);
189 }
190 }
191 }
192
193 } // namespace intelligibility
194
195 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698