Index: webrtc/modules/audio_processing/intelligibility/test/utils_unittest.cc |
diff --git a/webrtc/modules/audio_processing/intelligibility/test/utils_unittest.cc b/webrtc/modules/audio_processing/intelligibility/test/utils_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..d68f2e95032b35299c6602ad11f47bcf9821398b |
--- /dev/null |
+++ b/webrtc/modules/audio_processing/intelligibility/test/utils_unittest.cc |
@@ -0,0 +1,178 @@ |
+/* |
+ * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. |
+ * |
+ * Use of this source code is governed by a BSD-style license |
+ * that can be found in the LICENSE file in the root of the source |
+ * tree. An additional intellectual property rights grant can be found |
+ * in the file PATENTS. All contributing project authors may |
+ * be found in the AUTHORS file in the root of the source tree. |
+ */ |
+ |
+// |
+// Unit tests for intelligibility utils. |
+// |
+ |
+ |
+#include <cmath> |
+ |
+#include "testing/gtest/include/gtest/gtest.h" |
+#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.
|
+#include "webrtc/modules/audio_processing/intelligibility/intelligibility_utils.h" |
+ |
+namespace webrtc { |
+ |
+using std::complex; |
+using intelligibility::VarianceArray; |
+using intelligibility::GainApplier; |
+using std::vector; |
+ |
+vector<vector<complex<float>>> GenerateTestData(int freqs, int samples) { |
+ vector<vector<complex<float>>> data(samples); |
+ for (int i = 0; i < samples; i++) { |
+ data[i].resize(freqs); |
+ for (int j = 0; j < freqs; j++) { |
+ data[i][j].real(0.99f / ((i+1)*(j+1))); |
+ data[i][j].imag(0.99f / ((i+1)*(j+1))); |
+ } |
+ } |
+ return data; |
+} |
+ |
+// Tests UpdateFactor. |
+TEST(UtilsTest, TestUpdateFactor) { |
+ EXPECT_EQ(UpdateFactor(0, 0, 0), 0); |
+ EXPECT_EQ(UpdateFactor(4, 2, 3), 4); |
+ EXPECT_EQ(UpdateFactor(4, 2, 1), 3); |
+ EXPECT_EQ(UpdateFactor(2, 4, 3), 2); |
+ EXPECT_EQ(UpdateFactor(2, 4, 1), 3); |
+} |
+ |
+// Tests cplxfinite, cplxnormal, and zerofudge. |
+TEST(UtilsTest, TestCplx) { |
+ complex<float> t; |
+ t.real(1.f); |
+ t.imag(0.f); |
+ EXPECT_TRUE(cplxfinite(t)); |
+ EXPECT_FALSE(cplxnormal(t)); |
+ t = zerofudge(t); |
+ 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.
|
+ EXPECT_GT(t.real(), 0.f); |
+ t.imag(1.f/0.f); |
+ EXPECT_FALSE(cplxfinite(t)); |
+ EXPECT_FALSE(cplxnormal(t)); |
+ t.imag(sqrt(-1.f)); |
+ EXPECT_FALSE(cplxfinite(t)); |
+ EXPECT_FALSE(cplxnormal(t)); |
+ t.imag(1.f); |
+ EXPECT_TRUE(cplxfinite(t)); |
+ EXPECT_TRUE(cplxnormal(t)); |
+} |
+ |
+// Tests NewMean and AddToMean. |
+TEST(UtilsTest, TestMeanUpdate) { |
+ vector<complex<float>> data = {{3, 8}, {7, 6}, {2, 1}, {8, 9}, {0, 6}}; |
+ vector<complex<float>> means = {{3, 8}, {5, 7}, {4, 5}, {5, 6}, {4, 6}}; |
+ complex<float> mean(3, 8); |
+ for (vector<int>::size_type i = 0; i < data.size(); i++) { |
+ EXPECT_EQ(NewMean(mean, data[i], i+1), means[i]); |
+ AddToMean(data[i], i+1, &mean); |
+ EXPECT_EQ(mean, means[i]); |
+ } |
+} |
+ |
+// Tests VarianceArray, for all variance step types. |
+TEST(UtilsTest, TestVarianceArray) { |
+ const int kFreqs = 10; |
+ const int kSamples = 100; |
+ const int kWindowSize = 10; // Should pass for all kWindowSize > 1. |
+ const float kDecay = 0.5; |
+ vector<VarianceArray::StepType> step_types = { |
+ VarianceArray::kStepInfinite, VarianceArray::kStepDecaying, |
+ VarianceArray::kStepWindowed, VarianceArray::kStepBlocked, |
+ VarianceArray::kStepBlockBasedMovingAverage}; |
+ const vector<vector<complex<float>>> test_data( |
+ GenerateTestData(kFreqs, kSamples)); |
+ for (vector<VarianceArray::StepType>::iterator step_type = |
+ step_types.begin(); step_type != step_types.end(); ++step_type) { |
+ VarianceArray variance_array(kFreqs, *step_type, kWindowSize, kDecay); |
+ EXPECT_EQ(variance_array.variance()[0], 0); |
+ EXPECT_EQ(variance_array.array_mean(), 0); |
+ 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.
|
+ EXPECT_EQ(variance_array.variance()[0], 0); |
+ EXPECT_EQ(variance_array.array_mean(), 0); |
+ variance_array.ApplyScale(2.0f); |
+ EXPECT_EQ(variance_array.variance()[0], 0); |
+ EXPECT_EQ(variance_array.array_mean(), 0); |
+ |
+ // Makes sure Step is doing something. |
+ variance_array.Step(&test_data[0][0]); |
+ for (int i = 1; i < kSamples; i++) { |
+ variance_array.Step(&test_data[i][0]); |
+ EXPECT_GE(variance_array.array_mean(), 0.0f); |
+ EXPECT_LE(variance_array.array_mean(), 1.0f); |
+ for (int j = 0; j < kFreqs; j++) { |
+ EXPECT_GE(variance_array.variance()[j], 0.0f); |
+ EXPECT_LE(variance_array.variance()[j], 1.0f); |
+ } |
+ } |
+ } |
+} |
+ |
+// Tests exact computation on synthetic data. |
+TEST(UtilsTest, TestMovingBlockAverage) { |
+ const float kTestVariance = 8.25; // Exact, not unbiased estimate. |
+ const int kFreqs = 2; |
+ const int kSamples = 30; |
+ 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.
|
+ const float kDecay = 0.5; |
+ const float kMaxError = 0.001; |
+ |
+ VarianceArray variance_array( |
+ kFreqs, VarianceArray::kStepBlockBasedMovingAverage, |
+ kWindowSize, kDecay); |
+ |
+ vector<vector<complex<float>>> test_data(kSamples); |
+ for (int i = 0; i < kSamples; i++) { |
+ test_data[i].resize(kFreqs); |
+ for (int j = 0; j < kFreqs; j++) { |
+ test_data[i][j].real(static_cast<float>(i+1)); |
+ 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.
|
+ } |
+ } |
+ |
+ for (int i = 0; i < kSamples; i++) { |
+ variance_array.Step(&test_data[i][0]); |
+ for (int j = 0; j < kFreqs; j++) { |
+ if (i < 9) { // In utils, kWindowBlockSize = 10. |
+ EXPECT_EQ(variance_array.variance()[j], 0); |
+ } else { |
+ float error = std::fabs( |
+ variance_array.variance()[j] - kTestVariance); |
+ EXPECT_LT(error, kMaxError); |
+ } |
+ } |
+ } |
+} |
+ |
+// Tests gain applier. |
+TEST(UtilsTest, TestGainApplier) { |
+ const int kFreqs = 10; |
+ const int kSamples = 100; |
+ const float kChangeLimit = 0.1f; |
+ GainApplier gain_applier(kFreqs, kChangeLimit); |
+ const vector<vector<complex<float>>> in_data( |
+ GenerateTestData(kFreqs, kSamples)); |
+ vector<vector<complex<float>>> out_data( |
+ GenerateTestData(kFreqs, kSamples)); |
+ for (int i = 0; i < kSamples; i++) { |
+ gain_applier.Apply(&in_data[i][0], &out_data[i][0]); |
+ for (int j = 0; j < kFreqs; j++) { |
+ EXPECT_GT(out_data[i][j].real(), 0.0f); |
+ EXPECT_LT(out_data[i][j].real(), 1.0f); |
+ EXPECT_GT(out_data[i][j].imag(), 0.0f); |
+ EXPECT_LT(out_data[i][j].imag(), 1.0f); |
+ } |
+ } |
+} |
+ |
+} // namespace webrtc |