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

Unified 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, 6 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 side-by-side diff with in-line comments
Download patch
Index: webrtc/modules/audio_processing/intelligibility/intelligibility_utils_unittest.cc
diff --git a/webrtc/modules/audio_processing/intelligibility/intelligibility_utils_unittest.cc b/webrtc/modules/audio_processing/intelligibility/intelligibility_utils_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..01344e248fc3dad8fd77b618ec9ca41a25667626
--- /dev/null
+++ b/webrtc/modules/audio_processing/intelligibility/intelligibility_utils_unittest.cc
@@ -0,0 +1,195 @@
+/*
+ * 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.
+ *
+ * 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>
hlundin-webrtc 2015/07/01 09:09:27 math.h
ekm 2015/07/01 23:48:27 Done.
+#include <iostream>
+#include <vector>
+
+#include "testing/gtest/include/gtest/gtest.h"
+#include "webrtc/modules/audio_processing/intelligibility/intelligibility_utils.h"
+
+using std::complex;
+using std::vector;
+
+namespace webrtc {
+
+namespace intelligibility {
+
+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(IntelligibilityUtilsTest, TestUpdateFactor) {
+ 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.
+ EXPECT_EQ(intelligibility::UpdateFactor(4, 2, 3), 4);
+ EXPECT_EQ(intelligibility::UpdateFactor(4, 2, 1), 3);
+ EXPECT_EQ(intelligibility::UpdateFactor(2, 4, 3), 2);
+ EXPECT_EQ(intelligibility::UpdateFactor(2, 4, 1), 3);
+}
+
+// Tests cplxfinite, cplxnormal, and zerofudge.
+TEST(IntelligibilityUtilsTest, TestCplx) {
+ complex<float> t;
+ t.real(1.f);
+ t.imag(0.f);
+ EXPECT_TRUE(intelligibility::cplxfinite(t));
+ EXPECT_FALSE(intelligibility::cplxnormal(t));
+ t = intelligibility::zerofudge(t);
+ EXPECT_NE(t.imag(), 0.f);
+ EXPECT_NE(t.real(), 0.f);
+ t.imag(1.f/0.f);
+ EXPECT_FALSE(intelligibility::cplxfinite(t));
+ EXPECT_FALSE(intelligibility::cplxnormal(t));
+ t.imag(sqrt(-1.f));
+ EXPECT_FALSE(intelligibility::cplxfinite(t));
+ EXPECT_FALSE(intelligibility::cplxnormal(t));
+ t.imag(1.f);
+ EXPECT_TRUE(intelligibility::cplxfinite(t));
+ EXPECT_TRUE(intelligibility::cplxnormal(t));
+}
+
+// Tests NewMean and AddToMean.
+/*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.
+ 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(IntelligibilityUtilsTest, 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 = {
hlundin-webrtc 2015/07/01 09:09:27 const vector
ekm 2015/07/01 23:48:27 Done.
+ VarianceArray::kStepInfinite, VarianceArray::kStepDecaying,
+ VarianceArray::kStepWindowed, VarianceArray::kStepBlocked,
+ VarianceArray::kStepBlockBasedMovingAverage};
+ const vector<vector<complex<float>>> test_data(
+ GenerateTestData(kFreqs, kSamples));
+ 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.
+ VarianceArray variance_array(kFreqs, step_type, kWindowSize, kDecay);
+ 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);
+ }
+ }
+ variance_array.Clear();
+ EXPECT_EQ(variance_array.variance()[0], 0);
+ EXPECT_EQ(variance_array.array_mean(), 0);
+ }
+}
+
+// Tests exact computation on synthetic data.
+TEST(IntelligibilityUtilsTest, TestMovingBlockAverage) {
+ // Exact, not unbiased estimates.
+ const float kTestVarianceBufferNotFull = 16.5f;
+ const float kTestVarianceBufferFull1 = 66.5f;
+ const float kTestVarianceBufferFull2 = 333.375f;
+ const int kFreqs = 2;
+ const int kSamples = 50;
+ const int kWindowSize = 2;
+ const float kDecay = 0.5f;
+ const float kMaxError = 0.0001f;
+
+ 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++) {
+ if (i < 30) {
+ test_data[i][j].real(static_cast<float>(kSamples - i));
+ test_data[i][j].imag(static_cast<float>(i + 1));
+ } else {
+ test_data[i][j].real(0.f);
+ test_data[i][j].imag(0.f);
+ }
+ }
+ }
+
+ 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 if (i < 19) {
+ EXPECT_NEAR(variance_array.variance()[j], kTestVarianceBufferNotFull,
+ 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.
+ } else if (i < 39) {
+ EXPECT_NEAR(variance_array.variance()[j], kTestVarianceBufferFull1,
+ kMaxError);
+ } else if (i < 49) {
+ EXPECT_NEAR(variance_array.variance()[j], kTestVarianceBufferFull2,
+ kMaxError);
+ } else {
+ EXPECT_EQ(variance_array.variance()[j], 0);
+ }
+ }
+ }
+}
+
+// Tests gain applier.
+TEST(IntelligibilityUtilsTest, 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 intelligibility
+
+} // namespace webrtc

Powered by Google App Engine
This is Rietveld 408576698