blob: 10b68b0ff2d0775635d03506149d06fcc9073bb8 [file] [log] [blame]
peah522d71b2017-02-23 05:16:26 -08001/*
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/audio_processing/aec3/render_signal_analyzer.h"
peah522d71b2017-02-23 05:16:26 -080012
peah14c11a42017-07-11 06:13:43 -070013#include <math.h>
peah522d71b2017-02-23 05:16:26 -080014#include <algorithm>
15
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "rtc_base/checks.h"
peah522d71b2017-02-23 05:16:26 -080017
18namespace webrtc {
19
20namespace {
21constexpr size_t kCounterThreshold = 5;
22
peah14c11a42017-07-11 06:13:43 -070023// Identifies local bands with narrow characteristics.
24void IdentifySmallNarrowBandRegions(
25 const RenderBuffer& render_buffer,
26 const rtc::Optional<size_t>& delay_partitions,
27 std::array<size_t, kFftLengthBy2 - 1>* narrow_band_counters) {
28 if (!delay_partitions) {
29 narrow_band_counters->fill(0);
30 return;
31 }
32
Per Åhgren8ba58612017-12-01 23:01:44 +010033 rtc::ArrayView<const float> X2 = render_buffer.Spectrum(*delay_partitions);
34 RTC_DCHECK_EQ(kFftLengthBy2Plus1, X2.size());
peah14c11a42017-07-11 06:13:43 -070035
36 for (size_t k = 1; k < (X2.size() - 1); ++k) {
37 (*narrow_band_counters)[k - 1] = X2[k] > 3 * std::max(X2[k - 1], X2[k + 1])
38 ? (*narrow_band_counters)[k - 1] + 1
39 : 0;
40 }
41}
42
43// Identifies whether the signal has a single strong narrow-band component.
44void IdentifyStrongNarrowBandComponent(const RenderBuffer& render_buffer,
Per Åhgren971de072018-03-14 23:23:47 +010045 int strong_peak_freeze_duration,
peah14c11a42017-07-11 06:13:43 -070046 rtc::Optional<int>* narrow_peak_band,
47 size_t* narrow_peak_counter) {
48 const auto X2_latest = render_buffer.Spectrum(0);
49
50 // Identify the spectral peak.
51 const int peak_bin = static_cast<int>(
52 std::max_element(X2_latest.begin(), X2_latest.end()) - X2_latest.begin());
53
54 // Compute the level around the peak.
55 float non_peak_power = 0.f;
Per Åhgren971de072018-03-14 23:23:47 +010056 for (int k = std::max(0, peak_bin - 14); k < peak_bin - 4; ++k) {
peah14c11a42017-07-11 06:13:43 -070057 non_peak_power = std::max(X2_latest[k], non_peak_power);
58 }
59 for (int k = peak_bin + 5;
60 k < std::min(peak_bin + 15, static_cast<int>(kFftLengthBy2Plus1)); ++k) {
61 non_peak_power = std::max(X2_latest[k], non_peak_power);
62 }
63
Per Åhgren971de072018-03-14 23:23:47 +010064 // Assess the render signal strength.
Per Åhgrenec22e3f2017-12-20 15:20:37 +010065 const std::vector<std::vector<float>>& x_latest = render_buffer.Block(0);
peah14c11a42017-07-11 06:13:43 -070066 auto result0 = std::minmax_element(x_latest[0].begin(), x_latest[0].end());
67 float max_abs = std::max(fabs(*result0.first), fabs(*result0.second));
68
69 if (x_latest.size() > 1) {
70 const auto result1 =
71 std::minmax_element(x_latest[1].begin(), x_latest[1].end());
72 max_abs =
73 std::max(max_abs, static_cast<float>(std::max(fabs(*result1.first),
74 fabs(*result1.second))));
75 }
76
77 // Detect whether the spectal peak has as strong narrowband nature.
Per Åhgren971de072018-03-14 23:23:47 +010078 if (peak_bin > 0 && max_abs > 100 &&
peah14c11a42017-07-11 06:13:43 -070079 X2_latest[peak_bin] > 100 * non_peak_power) {
Oskar Sundbomaa8b67d2017-11-17 14:34:48 +010080 *narrow_peak_band = peak_bin;
peah14c11a42017-07-11 06:13:43 -070081 *narrow_peak_counter = 0;
82 } else {
Per Åhgren971de072018-03-14 23:23:47 +010083 if (*narrow_peak_band &&
84 ++(*narrow_peak_counter) >
85 static_cast<size_t>(strong_peak_freeze_duration)) {
Oskar Sundbomaa8b67d2017-11-17 14:34:48 +010086 *narrow_peak_band = rtc::nullopt;
peah14c11a42017-07-11 06:13:43 -070087 }
88 }
89}
90
peah522d71b2017-02-23 05:16:26 -080091} // namespace
92
Per Åhgren971de072018-03-14 23:23:47 +010093RenderSignalAnalyzer::RenderSignalAnalyzer(const EchoCanceller3Config& config)
94 : strong_peak_freeze_duration_(config.filter.main.length_blocks) {
peah522d71b2017-02-23 05:16:26 -080095 narrow_band_counters_.fill(0);
96}
97RenderSignalAnalyzer::~RenderSignalAnalyzer() = default;
98
99void RenderSignalAnalyzer::Update(
peahcf02cf12017-04-05 14:18:07 -0700100 const RenderBuffer& render_buffer,
peah522d71b2017-02-23 05:16:26 -0800101 const rtc::Optional<size_t>& delay_partitions) {
peah14c11a42017-07-11 06:13:43 -0700102 // Identify bands of narrow nature.
103 IdentifySmallNarrowBandRegions(render_buffer, delay_partitions,
104 &narrow_band_counters_);
peah522d71b2017-02-23 05:16:26 -0800105
peah14c11a42017-07-11 06:13:43 -0700106 // Identify the presence of a strong narrow band.
Per Åhgren971de072018-03-14 23:23:47 +0100107 IdentifyStrongNarrowBandComponent(render_buffer, strong_peak_freeze_duration_,
108 &narrow_peak_band_, &narrow_peak_counter_);
peah522d71b2017-02-23 05:16:26 -0800109}
110
111void RenderSignalAnalyzer::MaskRegionsAroundNarrowBands(
112 std::array<float, kFftLengthBy2Plus1>* v) const {
113 RTC_DCHECK(v);
114
115 // Set v to zero around narrow band signal regions.
116 if (narrow_band_counters_[0] > kCounterThreshold) {
117 (*v)[1] = (*v)[0] = 0.f;
118 }
119 for (size_t k = 2; k < kFftLengthBy2 - 1; ++k) {
120 if (narrow_band_counters_[k - 1] > kCounterThreshold) {
121 (*v)[k - 2] = (*v)[k - 1] = (*v)[k] = (*v)[k + 1] = (*v)[k + 2] = 0.f;
122 }
123 }
124 if (narrow_band_counters_[kFftLengthBy2 - 2] > kCounterThreshold) {
125 (*v)[kFftLengthBy2] = (*v)[kFftLengthBy2 - 1] = 0.f;
126 }
127}
128
129} // namespace webrtc