1 /*
2 * Copyright (c) 2017 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 #include "modules/audio_processing/aec3/render_signal_analyzer.h"
12
13 #include <math.h>
14 #include <array>
15 #include <vector>
16
17 #include "api/array_view.h"
18 #include "modules/audio_processing/aec3/aec3_common.h"
19 #include "modules/audio_processing/aec3/aec3_fft.h"
20 #include "modules/audio_processing/aec3/fft_data.h"
21 #include "modules/audio_processing/aec3/render_buffer.h"
22 #include "modules/audio_processing/test/echo_canceller_test_tools.h"
23 #include "rtc_base/random.h"
24 #include "test/gtest.h"
25
26 namespace webrtc {
27 namespace {
28
29 constexpr float kPi = 3.141592f;
30
ProduceSinusoid(int sample_rate_hz,float sinusoidal_frequency_hz,size_t * sample_counter,rtc::ArrayView<float> x)31 void ProduceSinusoid(int sample_rate_hz,
32 float sinusoidal_frequency_hz,
33 size_t* sample_counter,
34 rtc::ArrayView<float> x) {
35 // Produce a sinusoid of the specified frequency.
36 for (size_t k = *sample_counter, j = 0; k < (*sample_counter + kBlockSize);
37 ++k, ++j) {
38 x[j] =
39 32767.f * sin(2.f * kPi * sinusoidal_frequency_hz * k / sample_rate_hz);
40 }
41 *sample_counter = *sample_counter + kBlockSize;
42 }
43
44 } // namespace
45
46 #if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
47 // Verifies that the check for non-null output parameter works.
TEST(RenderSignalAnalyzer,NullMaskOutput)48 TEST(RenderSignalAnalyzer, NullMaskOutput) {
49 RenderSignalAnalyzer analyzer;
50 EXPECT_DEATH(analyzer.MaskRegionsAroundNarrowBands(nullptr), "");
51 }
52
53 #endif
54
55 // Verify that no narrow bands are detected in a Gaussian noise signal.
TEST(RenderSignalAnalyzer,NoFalseDetectionOfNarrowBands)56 TEST(RenderSignalAnalyzer, NoFalseDetectionOfNarrowBands) {
57 RenderSignalAnalyzer analyzer;
58 Random random_generator(42U);
59 std::vector<std::vector<float>> x(3, std::vector<float>(kBlockSize, 0.f));
60 std::array<float, kBlockSize> x_old;
61 FftData X;
62 Aec3Fft fft;
63 RenderBuffer render_buffer(Aec3Optimization::kNone, 3, 1,
64 std::vector<size_t>(1, 1));
65 std::array<float, kFftLengthBy2Plus1> mask;
66 x_old.fill(0.f);
67
68 for (size_t k = 0; k < 100; ++k) {
69 RandomizeSampleVector(&random_generator, x[0]);
70 fft.PaddedFft(x[0], x_old, &X);
71 render_buffer.Insert(x);
72 analyzer.Update(render_buffer, 0);
73 }
74
75 mask.fill(1.f);
76 analyzer.MaskRegionsAroundNarrowBands(&mask);
77 EXPECT_TRUE(
78 std::all_of(mask.begin(), mask.end(), [](float a) { return a == 1.f; }));
79 EXPECT_FALSE(analyzer.PoorSignalExcitation());
80 }
81
82 // Verify that a sinusiod signal is detected as narrow bands.
TEST(RenderSignalAnalyzer,NarrowBandDetection)83 TEST(RenderSignalAnalyzer, NarrowBandDetection) {
84 RenderSignalAnalyzer analyzer;
85 Random random_generator(42U);
86 std::vector<std::vector<float>> x(3, std::vector<float>(kBlockSize, 0.f));
87 std::array<float, kBlockSize> x_old;
88 Aec3Fft fft;
89 RenderBuffer render_buffer(Aec3Optimization::kNone, 3, 1,
90 std::vector<size_t>(1, 1));
91 std::array<float, kFftLengthBy2Plus1> mask;
92 x_old.fill(0.f);
93 constexpr int kSinusFrequencyBin = 32;
94
95 auto generate_sinusoid_test = [&](bool known_delay) {
96 size_t sample_counter = 0;
97 for (size_t k = 0; k < 100; ++k) {
98 ProduceSinusoid(16000, 16000 / 2 * kSinusFrequencyBin / kFftLengthBy2,
99 &sample_counter, x[0]);
100 render_buffer.Insert(x);
101 analyzer.Update(render_buffer, known_delay ? rtc::Optional<size_t>(0)
102 : rtc::nullopt);
103 }
104 };
105
106 generate_sinusoid_test(true);
107 mask.fill(1.f);
108 analyzer.MaskRegionsAroundNarrowBands(&mask);
109 for (int k = 0; k < static_cast<int>(mask.size()); ++k) {
110 EXPECT_EQ(abs(k - kSinusFrequencyBin) <= 2 ? 0.f : 1.f, mask[k]);
111 }
112 EXPECT_TRUE(analyzer.PoorSignalExcitation());
113
114 // Verify that no bands are detected as narrow when the delay is unknown.
115 generate_sinusoid_test(false);
116 mask.fill(1.f);
117 analyzer.MaskRegionsAroundNarrowBands(&mask);
118 std::for_each(mask.begin(), mask.end(), [](float a) { EXPECT_EQ(1.f, a); });
119 EXPECT_FALSE(analyzer.PoorSignalExcitation());
120 }
121
122 } // namespace webrtc
123