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

Side by Side Diff: webrtc/modules/audio_processing/intelligibility/test/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: 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.
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>
17
18 #include "testing/gtest/include/gtest/gtest.h"
19 #include "webrtc/modules/audio_processing/intelligibility/intelligibility_utils. cc"
turaj 2015/06/26 00:32:58 Do you need to include .cc file?
ekm 2015/06/26 19:07:10 This is to test functions from anonymous namespace
turaj 2015/06/29 17:33:36 If you feel that they are simple and don't need te
ekm 2015/06/29 23:44:02 Added them to the intelligibility namespace.
20 #include "webrtc/modules/audio_processing/intelligibility/intelligibility_utils. h"
21
22 namespace webrtc {
23
24 using std::complex;
25 using intelligibility::VarianceArray;
26 using intelligibility::GainApplier;
27 using std::vector;
28
29 vector<vector<complex<float>>> GenerateTestData(int freqs, int samples) {
30 vector<vector<complex<float>>> data(samples);
31 for (int i = 0; i < samples; i++) {
32 data[i].resize(freqs);
33 for (int j = 0; j < freqs; j++) {
34 data[i][j].real(0.99f / ((i+1)*(j+1)));
35 data[i][j].imag(0.99f / ((i+1)*(j+1)));
36 }
37 }
38 return data;
39 }
40
41 // Tests UpdateFactor.
42 TEST(UtilsTest, TestUpdateFactor) {
43 EXPECT_EQ(UpdateFactor(0, 0, 0), 0);
44 EXPECT_EQ(UpdateFactor(4, 2, 3), 4);
45 EXPECT_EQ(UpdateFactor(4, 2, 1), 3);
46 EXPECT_EQ(UpdateFactor(2, 4, 3), 2);
47 EXPECT_EQ(UpdateFactor(2, 4, 1), 3);
48 }
49
50 // Tests cplxfinite, cplxnormal, and zerofudge.
51 TEST(UtilsTest, TestCplx) {
52 complex<float> t;
53 t.real(1.f);
54 t.imag(0.f);
55 EXPECT_TRUE(cplxfinite(t));
56 EXPECT_FALSE(cplxnormal(t));
57 t = zerofudge(t);
58 EXPECT_GT(t.imag(), 0.f);
turaj 2015/06/26 00:32:58 I guess EXPECT_NE() is a better choice here.
ekm 2015/06/26 19:07:10 Done.
59 EXPECT_GT(t.real(), 0.f);
60 t.imag(1.f/0.f);
61 EXPECT_FALSE(cplxfinite(t));
62 EXPECT_FALSE(cplxnormal(t));
63 t.imag(sqrt(-1.f));
64 EXPECT_FALSE(cplxfinite(t));
65 EXPECT_FALSE(cplxnormal(t));
66 t.imag(1.f);
67 EXPECT_TRUE(cplxfinite(t));
68 EXPECT_TRUE(cplxnormal(t));
69 }
70
71 // Tests NewMean and AddToMean.
72 TEST(UtilsTest, TestMeanUpdate) {
73 vector<complex<float>> data = {{3, 8}, {7, 6}, {2, 1}, {8, 9}, {0, 6}};
74 vector<complex<float>> means = {{3, 8}, {5, 7}, {4, 5}, {5, 6}, {4, 6}};
75 complex<float> mean(3, 8);
76 for (vector<int>::size_type i = 0; i < data.size(); i++) {
77 EXPECT_EQ(NewMean(mean, data[i], i+1), means[i]);
78 AddToMean(data[i], i+1, &mean);
79 EXPECT_EQ(mean, means[i]);
80 }
81 }
82
83 // Tests VarianceArray, for all variance step types.
84 TEST(UtilsTest, TestVarianceArray) {
85 const int kFreqs = 10;
86 const int kSamples = 100;
87 const int kWindowSize = 10; // Should pass for all kWindowSize > 1.
88 const float kDecay = 0.5;
89 vector<VarianceArray::StepType> step_types = {
90 VarianceArray::kStepInfinite, VarianceArray::kStepDecaying,
91 VarianceArray::kStepWindowed, VarianceArray::kStepBlocked,
92 VarianceArray::kStepBlockBasedMovingAverage};
93 const vector<vector<complex<float>>> test_data(
94 GenerateTestData(kFreqs, kSamples));
95 for (vector<VarianceArray::StepType>::iterator step_type =
96 step_types.begin(); step_type != step_types.end(); ++step_type) {
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.Clear();
turaj 2015/06/26 00:32:58 Can we move the for Clear() after following loop?
ekm 2015/06/26 19:07:10 Done.
101 EXPECT_EQ(variance_array.variance()[0], 0);
102 EXPECT_EQ(variance_array.array_mean(), 0);
103 variance_array.ApplyScale(2.0f);
104 EXPECT_EQ(variance_array.variance()[0], 0);
105 EXPECT_EQ(variance_array.array_mean(), 0);
106
107 // Makes sure Step is doing something.
108 variance_array.Step(&test_data[0][0]);
109 for (int i = 1; i < kSamples; i++) {
110 variance_array.Step(&test_data[i][0]);
111 EXPECT_GE(variance_array.array_mean(), 0.0f);
112 EXPECT_LE(variance_array.array_mean(), 1.0f);
113 for (int j = 0; j < kFreqs; j++) {
114 EXPECT_GE(variance_array.variance()[j], 0.0f);
115 EXPECT_LE(variance_array.variance()[j], 1.0f);
116 }
117 }
118 }
119 }
120
121 // Tests exact computation on synthetic data.
122 TEST(UtilsTest, TestMovingBlockAverage) {
123 const float kTestVariance = 8.25; // Exact, not unbiased estimate.
124 const int kFreqs = 2;
125 const int kSamples = 30;
126 const int kWindowSize = 1;
turaj 2015/06/26 00:32:58 Does window size of one actually test the circular
ekm 2015/06/26 19:07:10 Done.
127 const float kDecay = 0.5;
128 const float kMaxError = 0.001;
129
130 VarianceArray variance_array(
131 kFreqs, VarianceArray::kStepBlockBasedMovingAverage,
132 kWindowSize, kDecay);
133
134 vector<vector<complex<float>>> test_data(kSamples);
135 for (int i = 0; i < kSamples; i++) {
136 test_data[i].resize(kFreqs);
137 for (int j = 0; j < kFreqs; j++) {
138 test_data[i][j].real(static_cast<float>(i+1));
139 test_data[i][j].imag(0.f);
turaj 2015/06/26 00:32:58 Can we have non-zero imaginary part to be sure the
ekm 2015/06/26 19:07:10 Done.
140 }
141 }
142
143 for (int i = 0; i < kSamples; i++) {
144 variance_array.Step(&test_data[i][0]);
145 for (int j = 0; j < kFreqs; j++) {
146 if (i < 9) { // In utils, kWindowBlockSize = 10.
147 EXPECT_EQ(variance_array.variance()[j], 0);
148 } else {
149 float error = std::fabs(
150 variance_array.variance()[j] - kTestVariance);
151 EXPECT_LT(error, kMaxError);
152 }
153 }
154 }
155 }
156
157 // Tests gain applier.
158 TEST(UtilsTest, TestGainApplier) {
159 const int kFreqs = 10;
160 const int kSamples = 100;
161 const float kChangeLimit = 0.1f;
162 GainApplier gain_applier(kFreqs, kChangeLimit);
163 const vector<vector<complex<float>>> in_data(
164 GenerateTestData(kFreqs, kSamples));
165 vector<vector<complex<float>>> out_data(
166 GenerateTestData(kFreqs, kSamples));
167 for (int i = 0; i < kSamples; i++) {
168 gain_applier.Apply(&in_data[i][0], &out_data[i][0]);
169 for (int j = 0; j < kFreqs; j++) {
170 EXPECT_GT(out_data[i][j].real(), 0.0f);
171 EXPECT_LT(out_data[i][j].real(), 1.0f);
172 EXPECT_GT(out_data[i][j].imag(), 0.0f);
173 EXPECT_LT(out_data[i][j].imag(), 1.0f);
174 }
175 }
176 }
177
178 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698