blob: fea394c338116dddcbab3885aac90028d6fbdead [file] [log] [blame]
ekm35b72fb2015-07-10 14:11:52 -07001/*
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
Alejandro Luebs32348192016-02-17 20:04:19 -080011#include <cmath>
ekm35b72fb2015-07-10 14:11:52 -070012#include <complex>
ekm35b72fb2015-07-10 14:11:52 -070013#include <vector>
14
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020015#include "modules/audio_processing/intelligibility/intelligibility_utils.h"
16#include "rtc_base/arraysize.h"
17#include "test/gtest.h"
ekm35b72fb2015-07-10 14:11:52 -070018
ekm35b72fb2015-07-10 14:11:52 -070019namespace webrtc {
20
21namespace intelligibility {
22
Alejandro Luebs32348192016-02-17 20:04:19 -080023std::vector<std::vector<std::complex<float>>> GenerateTestData(size_t freqs,
24 size_t samples) {
25 std::vector<std::vector<std::complex<float>>> data(samples);
26 for (size_t i = 0; i < samples; ++i) {
27 for (size_t j = 0; j < freqs; ++j) {
ekm35b72fb2015-07-10 14:11:52 -070028 const float val = 0.99f / ((i + 1) * (j + 1));
Alejandro Luebs32348192016-02-17 20:04:19 -080029 data[i].push_back(std::complex<float>(val, val));
ekm35b72fb2015-07-10 14:11:52 -070030 }
31 }
32 return data;
33}
34
Alejandro Luebs32348192016-02-17 20:04:19 -080035// Tests PowerEstimator, for all power step types.
36TEST(IntelligibilityUtilsTest, TestPowerEstimator) {
37 const size_t kFreqs = 10;
38 const size_t kSamples = 100;
ekm35b72fb2015-07-10 14:11:52 -070039 const float kDecay = 0.5f;
Alejandro Luebs32348192016-02-17 20:04:19 -080040 const std::vector<std::vector<std::complex<float>>> test_data(
ekm35b72fb2015-07-10 14:11:52 -070041 GenerateTestData(kFreqs, kSamples));
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -080042 PowerEstimator<std::complex<float>> power_estimator(kFreqs, kDecay);
43 EXPECT_EQ(0, power_estimator.power()[0]);
ekm35b72fb2015-07-10 14:11:52 -070044
Alejandro Luebs32348192016-02-17 20:04:19 -080045 // Makes sure Step is doing something.
aluebs0a007592016-02-26 17:17:38 -080046 power_estimator.Step(test_data[0].data());
Alejandro Luebs32348192016-02-17 20:04:19 -080047 for (size_t i = 1; i < kSamples; ++i) {
aluebs0a007592016-02-26 17:17:38 -080048 power_estimator.Step(test_data[i].data());
Alejandro Luebs32348192016-02-17 20:04:19 -080049 for (size_t j = 0; j < kFreqs; ++j) {
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -080050 EXPECT_GE(power_estimator.power()[j], 0.f);
51 EXPECT_LE(power_estimator.power()[j], 1.f);
ekm35b72fb2015-07-10 14:11:52 -070052 }
53 }
54}
55
56// Tests gain applier.
57TEST(IntelligibilityUtilsTest, TestGainApplier) {
Alejandro Luebs32348192016-02-17 20:04:19 -080058 const size_t kFreqs = 10;
59 const size_t kSamples = 100;
ekm35b72fb2015-07-10 14:11:52 -070060 const float kChangeLimit = 0.1f;
61 GainApplier gain_applier(kFreqs, kChangeLimit);
Alejandro Luebs32348192016-02-17 20:04:19 -080062 const std::vector<std::vector<std::complex<float>>> in_data(
ekm35b72fb2015-07-10 14:11:52 -070063 GenerateTestData(kFreqs, kSamples));
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -080064 std::vector<std::vector<std::complex<float>>> out_data(
65 GenerateTestData(kFreqs, kSamples));
Alejandro Luebs32348192016-02-17 20:04:19 -080066 for (size_t i = 0; i < kSamples; ++i) {
aluebs0a007592016-02-26 17:17:38 -080067 gain_applier.Apply(in_data[i].data(), out_data[i].data());
Alejandro Luebs32348192016-02-17 20:04:19 -080068 for (size_t j = 0; j < kFreqs; ++j) {
69 EXPECT_GT(out_data[i][j].real(), 0.f);
70 EXPECT_LT(out_data[i][j].real(), 1.f);
71 EXPECT_GT(out_data[i][j].imag(), 0.f);
72 EXPECT_LT(out_data[i][j].imag(), 1.f);
ekm35b72fb2015-07-10 14:11:52 -070073 }
74 }
75}
76
77} // namespace intelligibility
78
79} // namespace webrtc