blob: 0b155f6ecf0e2a7b506bc8597e0a741687b95bb3 [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>
Jonas Olssona4d87372019-07-05 19:08:33 +020014
peah522d71b2017-02-23 05:16:26 -080015#include <algorithm>
Yves Gerey988cc082018-10-23 12:03:01 +020016#include <utility>
17#include <vector>
peah522d71b2017-02-23 05:16:26 -080018
Yves Gerey988cc082018-10-23 12:03:01 +020019#include "api/array_view.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "rtc_base/checks.h"
peah522d71b2017-02-23 05:16:26 -080021
22namespace webrtc {
23
24namespace {
25constexpr size_t kCounterThreshold = 5;
26
peah14c11a42017-07-11 06:13:43 -070027// Identifies local bands with narrow characteristics.
28void IdentifySmallNarrowBandRegions(
29 const RenderBuffer& render_buffer,
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +020030 const absl::optional<size_t>& delay_partitions,
peah14c11a42017-07-11 06:13:43 -070031 std::array<size_t, kFftLengthBy2 - 1>* narrow_band_counters) {
Sam Zackrisson3f172212019-09-12 12:32:44 +020032 RTC_DCHECK(narrow_band_counters);
33
peah14c11a42017-07-11 06:13:43 -070034 if (!delay_partitions) {
35 narrow_band_counters->fill(0);
36 return;
37 }
38
Sam Zackrisson3f172212019-09-12 12:32:44 +020039 std::array<size_t, kFftLengthBy2 - 1> channel_counters;
40 channel_counters.fill(0);
41 for (size_t channel = 0; channel < render_buffer.Block(0)[0].size();
42 ++channel) {
43 rtc::ArrayView<const float> X2 =
44 render_buffer.Spectrum(*delay_partitions, channel);
45 RTC_DCHECK_EQ(kFftLengthBy2Plus1, X2.size());
46 for (size_t k = 1; k < kFftLengthBy2; ++k) {
47 if (X2[k] > 3 * std::max(X2[k - 1], X2[k + 1])) {
48 ++channel_counters[k - 1];
49 }
50 }
51 }
52 for (size_t k = 1; k < kFftLengthBy2; ++k) {
53 (*narrow_band_counters)[k - 1] =
54 channel_counters[k - 1] > 0 ? (*narrow_band_counters)[k - 1] + 1 : 0;
peah14c11a42017-07-11 06:13:43 -070055 }
56}
57
58// Identifies whether the signal has a single strong narrow-band component.
59void IdentifyStrongNarrowBandComponent(const RenderBuffer& render_buffer,
Per Åhgren971de072018-03-14 23:23:47 +010060 int strong_peak_freeze_duration,
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +020061 absl::optional<int>* narrow_peak_band,
peah14c11a42017-07-11 06:13:43 -070062 size_t* narrow_peak_counter) {
Sam Zackrisson3f172212019-09-12 12:32:44 +020063 RTC_DCHECK(narrow_peak_band);
64 RTC_DCHECK(narrow_peak_counter);
65 if (*narrow_peak_band &&
66 ++(*narrow_peak_counter) >
67 static_cast<size_t>(strong_peak_freeze_duration)) {
68 *narrow_peak_band = absl::nullopt;
peah14c11a42017-07-11 06:13:43 -070069 }
70
Per Åhgrence202a02019-09-02 17:01:19 +020071 const std::vector<std::vector<std::vector<float>>>& x_latest =
72 render_buffer.Block(0);
Sam Zackrisson3f172212019-09-12 12:32:44 +020073 float max_peak_level = 0.f;
74 for (size_t channel = 0; channel < x_latest[0].size(); ++channel) {
75 const auto X2_latest = render_buffer.Spectrum(0, channel);
peah14c11a42017-07-11 06:13:43 -070076
Sam Zackrisson3f172212019-09-12 12:32:44 +020077 // Identify the spectral peak.
78 const int peak_bin =
79 static_cast<int>(std::max_element(X2_latest.begin(), X2_latest.end()) -
80 X2_latest.begin());
peah14c11a42017-07-11 06:13:43 -070081
Sam Zackrisson3f172212019-09-12 12:32:44 +020082 // Compute the level around the peak.
83 float non_peak_power = 0.f;
84 for (int k = std::max(0, peak_bin - 14); k < peak_bin - 4; ++k) {
85 non_peak_power = std::max(X2_latest[k], non_peak_power);
86 }
87 for (int k = peak_bin + 5;
88 k < std::min(peak_bin + 15, static_cast<int>(kFftLengthBy2Plus1));
89 ++k) {
90 non_peak_power = std::max(X2_latest[k], non_peak_power);
91 }
92
93 // Assess the render signal strength.
94 auto result0 = std::minmax_element(x_latest[0][channel].begin(),
95 x_latest[0][channel].end());
96 float max_abs = std::max(fabs(*result0.first), fabs(*result0.second));
97
98 if (x_latest.size() > 1) {
99 const auto result1 = std::minmax_element(x_latest[1][channel].begin(),
100 x_latest[1][channel].end());
101 max_abs =
102 std::max(max_abs, static_cast<float>(std::max(
103 fabs(*result1.first), fabs(*result1.second))));
104 }
105
106 // Detect whether the spectral peak has as strong narrowband nature.
107 const float peak_level = X2_latest[peak_bin];
108 if (peak_bin > 0 && max_abs > 100 && peak_level > 100 * non_peak_power) {
109 // Store the strongest peak across channels.
110 if (peak_level > max_peak_level) {
111 max_peak_level = peak_level;
112 *narrow_peak_band = peak_bin;
113 *narrow_peak_counter = 0;
114 }
peah14c11a42017-07-11 06:13:43 -0700115 }
116 }
117}
118
peah522d71b2017-02-23 05:16:26 -0800119} // namespace
120
Per Åhgren971de072018-03-14 23:23:47 +0100121RenderSignalAnalyzer::RenderSignalAnalyzer(const EchoCanceller3Config& config)
122 : strong_peak_freeze_duration_(config.filter.main.length_blocks) {
peah522d71b2017-02-23 05:16:26 -0800123 narrow_band_counters_.fill(0);
124}
125RenderSignalAnalyzer::~RenderSignalAnalyzer() = default;
126
127void RenderSignalAnalyzer::Update(
peahcf02cf12017-04-05 14:18:07 -0700128 const RenderBuffer& render_buffer,
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +0200129 const absl::optional<size_t>& delay_partitions) {
peah14c11a42017-07-11 06:13:43 -0700130 // Identify bands of narrow nature.
131 IdentifySmallNarrowBandRegions(render_buffer, delay_partitions,
132 &narrow_band_counters_);
peah522d71b2017-02-23 05:16:26 -0800133
peah14c11a42017-07-11 06:13:43 -0700134 // Identify the presence of a strong narrow band.
Per Åhgren971de072018-03-14 23:23:47 +0100135 IdentifyStrongNarrowBandComponent(render_buffer, strong_peak_freeze_duration_,
136 &narrow_peak_band_, &narrow_peak_counter_);
peah522d71b2017-02-23 05:16:26 -0800137}
138
139void RenderSignalAnalyzer::MaskRegionsAroundNarrowBands(
140 std::array<float, kFftLengthBy2Plus1>* v) const {
141 RTC_DCHECK(v);
142
143 // Set v to zero around narrow band signal regions.
144 if (narrow_band_counters_[0] > kCounterThreshold) {
145 (*v)[1] = (*v)[0] = 0.f;
146 }
147 for (size_t k = 2; k < kFftLengthBy2 - 1; ++k) {
148 if (narrow_band_counters_[k - 1] > kCounterThreshold) {
149 (*v)[k - 2] = (*v)[k - 1] = (*v)[k] = (*v)[k + 1] = (*v)[k + 2] = 0.f;
150 }
151 }
152 if (narrow_band_counters_[kFftLengthBy2 - 2] > kCounterThreshold) {
153 (*v)[kFftLengthBy2] = (*v)[kFftLengthBy2 - 1] = 0.f;
154 }
155}
156
157} // namespace webrtc