xref: /aosp_15_r20/external/webrtc/modules/audio_processing/aec3/render_signal_analyzer_unittest.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
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 
15 #include <array>
16 #include <cmath>
17 #include <vector>
18 
19 #include "api/array_view.h"
20 #include "modules/audio_processing/aec3/aec3_common.h"
21 #include "modules/audio_processing/aec3/aec3_fft.h"
22 #include "modules/audio_processing/aec3/fft_data.h"
23 #include "modules/audio_processing/aec3/render_delay_buffer.h"
24 #include "modules/audio_processing/test/echo_canceller_test_tools.h"
25 #include "rtc_base/random.h"
26 #include "rtc_base/strings/string_builder.h"
27 #include "test/gtest.h"
28 
29 namespace webrtc {
30 namespace {
31 
32 constexpr float kPi = 3.141592f;
33 
ProduceSinusoidInNoise(int sample_rate_hz,size_t sinusoid_channel,float sinusoidal_frequency_hz,Random * random_generator,size_t * sample_counter,Block * x)34 void ProduceSinusoidInNoise(int sample_rate_hz,
35                             size_t sinusoid_channel,
36                             float sinusoidal_frequency_hz,
37                             Random* random_generator,
38                             size_t* sample_counter,
39                             Block* x) {
40   // Fill x with low-amplitude noise.
41   for (int band = 0; band < x->NumBands(); ++band) {
42     for (int channel = 0; channel < x->NumChannels(); ++channel) {
43       RandomizeSampleVector(random_generator, x->View(band, channel),
44                             /*amplitude=*/500.f);
45     }
46   }
47   // Produce a sinusoid of the specified frequency in the specified channel.
48   for (size_t k = *sample_counter, j = 0; k < (*sample_counter + kBlockSize);
49        ++k, ++j) {
50     x->View(/*band=*/0, sinusoid_channel)[j] +=
51         32000.f *
52         std::sin(2.f * kPi * sinusoidal_frequency_hz * k / sample_rate_hz);
53   }
54   *sample_counter = *sample_counter + kBlockSize;
55 }
56 
RunNarrowBandDetectionTest(size_t num_channels)57 void RunNarrowBandDetectionTest(size_t num_channels) {
58   RenderSignalAnalyzer analyzer(EchoCanceller3Config{});
59   Random random_generator(42U);
60   constexpr int kSampleRateHz = 48000;
61   constexpr size_t kNumBands = NumBandsForRate(kSampleRateHz);
62   Block x(kNumBands, num_channels);
63   std::array<float, kBlockSize> x_old;
64   Aec3Fft fft;
65   EchoCanceller3Config config;
66   std::unique_ptr<RenderDelayBuffer> render_delay_buffer(
67       RenderDelayBuffer::Create(config, kSampleRateHz, num_channels));
68 
69   std::array<float, kFftLengthBy2Plus1> mask;
70   x_old.fill(0.f);
71   constexpr int kSinusFrequencyBin = 32;
72 
73   auto generate_sinusoid_test = [&](bool known_delay) {
74     size_t sample_counter = 0;
75     for (size_t k = 0; k < 100; ++k) {
76       ProduceSinusoidInNoise(16000, num_channels - 1,
77                              16000 / 2 * kSinusFrequencyBin / kFftLengthBy2,
78                              &random_generator, &sample_counter, &x);
79 
80       render_delay_buffer->Insert(x);
81       if (k == 0) {
82         render_delay_buffer->Reset();
83       }
84       render_delay_buffer->PrepareCaptureProcessing();
85 
86       analyzer.Update(*render_delay_buffer->GetRenderBuffer(),
87                       known_delay ? absl::optional<size_t>(0) : absl::nullopt);
88     }
89   };
90 
91   generate_sinusoid_test(true);
92   mask.fill(1.f);
93   analyzer.MaskRegionsAroundNarrowBands(&mask);
94   for (int k = 0; k < static_cast<int>(mask.size()); ++k) {
95     EXPECT_EQ(abs(k - kSinusFrequencyBin) <= 2 ? 0.f : 1.f, mask[k]);
96   }
97   EXPECT_TRUE(analyzer.PoorSignalExcitation());
98   EXPECT_TRUE(static_cast<bool>(analyzer.NarrowPeakBand()));
99   EXPECT_EQ(*analyzer.NarrowPeakBand(), 32);
100 
101   // Verify that no bands are detected as narrow when the delay is unknown.
102   generate_sinusoid_test(false);
103   mask.fill(1.f);
104   analyzer.MaskRegionsAroundNarrowBands(&mask);
105   std::for_each(mask.begin(), mask.end(), [](float a) { EXPECT_EQ(1.f, a); });
106   EXPECT_FALSE(analyzer.PoorSignalExcitation());
107 }
108 
ProduceDebugText(size_t num_channels)109 std::string ProduceDebugText(size_t num_channels) {
110   rtc::StringBuilder ss;
111   ss << "number of channels: " << num_channels;
112   return ss.Release();
113 }
114 }  // namespace
115 
116 #if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
117 // Verifies that the check for non-null output parameter works.
TEST(RenderSignalAnalyzerDeathTest,NullMaskOutput)118 TEST(RenderSignalAnalyzerDeathTest, NullMaskOutput) {
119   RenderSignalAnalyzer analyzer(EchoCanceller3Config{});
120   EXPECT_DEATH(analyzer.MaskRegionsAroundNarrowBands(nullptr), "");
121 }
122 
123 #endif
124 
125 // Verify that no narrow bands are detected in a Gaussian noise signal.
TEST(RenderSignalAnalyzer,NoFalseDetectionOfNarrowBands)126 TEST(RenderSignalAnalyzer, NoFalseDetectionOfNarrowBands) {
127   for (auto num_channels : {1, 2, 8}) {
128     SCOPED_TRACE(ProduceDebugText(num_channels));
129     RenderSignalAnalyzer analyzer(EchoCanceller3Config{});
130     Random random_generator(42U);
131     Block x(3, num_channels);
132     std::array<float, kBlockSize> x_old;
133     std::unique_ptr<RenderDelayBuffer> render_delay_buffer(
134         RenderDelayBuffer::Create(EchoCanceller3Config(), 48000, num_channels));
135     std::array<float, kFftLengthBy2Plus1> mask;
136     x_old.fill(0.f);
137 
138     for (int k = 0; k < 100; ++k) {
139       for (int band = 0; band < x.NumBands(); ++band) {
140         for (int channel = 0; channel < x.NumChannels(); ++channel) {
141           RandomizeSampleVector(&random_generator, x.View(band, channel));
142         }
143       }
144 
145       render_delay_buffer->Insert(x);
146       if (k == 0) {
147         render_delay_buffer->Reset();
148       }
149       render_delay_buffer->PrepareCaptureProcessing();
150 
151       analyzer.Update(*render_delay_buffer->GetRenderBuffer(),
152                       absl::optional<size_t>(0));
153     }
154 
155     mask.fill(1.f);
156     analyzer.MaskRegionsAroundNarrowBands(&mask);
157     EXPECT_TRUE(std::all_of(mask.begin(), mask.end(),
158                             [](float a) { return a == 1.f; }));
159     EXPECT_FALSE(analyzer.PoorSignalExcitation());
160     EXPECT_FALSE(static_cast<bool>(analyzer.NarrowPeakBand()));
161   }
162 }
163 
164 // Verify that a sinusoid signal is detected as narrow bands.
TEST(RenderSignalAnalyzer,NarrowBandDetection)165 TEST(RenderSignalAnalyzer, NarrowBandDetection) {
166   for (auto num_channels : {1, 2, 8}) {
167     SCOPED_TRACE(ProduceDebugText(num_channels));
168     RunNarrowBandDetectionTest(num_channels);
169   }
170 }
171 }  // namespace webrtc
172