blob: 33b04bf70c9ff80d3e903911e793121f3c99d55c [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>
Yves Gerey988cc082018-10-23 12:03:01 +020015#include <utility>
16#include <vector>
peah522d71b2017-02-23 05:16:26 -080017
Yves Gerey988cc082018-10-23 12:03:01 +020018#include "api/array_view.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "rtc_base/checks.h"
peah522d71b2017-02-23 05:16:26 -080020
21namespace webrtc {
22
23namespace {
24constexpr size_t kCounterThreshold = 5;
25
peah14c11a42017-07-11 06:13:43 -070026// Identifies local bands with narrow characteristics.
27void IdentifySmallNarrowBandRegions(
28 const RenderBuffer& render_buffer,
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +020029 const absl::optional<size_t>& delay_partitions,
peah14c11a42017-07-11 06:13:43 -070030 std::array<size_t, kFftLengthBy2 - 1>* narrow_band_counters) {
31 if (!delay_partitions) {
32 narrow_band_counters->fill(0);
33 return;
34 }
35
Per Åhgren8ba58612017-12-01 23:01:44 +010036 rtc::ArrayView<const float> X2 = render_buffer.Spectrum(*delay_partitions);
37 RTC_DCHECK_EQ(kFftLengthBy2Plus1, X2.size());
peah14c11a42017-07-11 06:13:43 -070038
39 for (size_t k = 1; k < (X2.size() - 1); ++k) {
40 (*narrow_band_counters)[k - 1] = X2[k] > 3 * std::max(X2[k - 1], X2[k + 1])
41 ? (*narrow_band_counters)[k - 1] + 1
42 : 0;
43 }
44}
45
46// Identifies whether the signal has a single strong narrow-band component.
47void IdentifyStrongNarrowBandComponent(const RenderBuffer& render_buffer,
Per Åhgren971de072018-03-14 23:23:47 +010048 int strong_peak_freeze_duration,
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +020049 absl::optional<int>* narrow_peak_band,
peah14c11a42017-07-11 06:13:43 -070050 size_t* narrow_peak_counter) {
51 const auto X2_latest = render_buffer.Spectrum(0);
52
53 // Identify the spectral peak.
54 const int peak_bin = static_cast<int>(
55 std::max_element(X2_latest.begin(), X2_latest.end()) - X2_latest.begin());
56
57 // Compute the level around the peak.
58 float non_peak_power = 0.f;
Per Åhgren971de072018-03-14 23:23:47 +010059 for (int k = std::max(0, peak_bin - 14); k < peak_bin - 4; ++k) {
peah14c11a42017-07-11 06:13:43 -070060 non_peak_power = std::max(X2_latest[k], non_peak_power);
61 }
62 for (int k = peak_bin + 5;
63 k < std::min(peak_bin + 15, static_cast<int>(kFftLengthBy2Plus1)); ++k) {
64 non_peak_power = std::max(X2_latest[k], non_peak_power);
65 }
66
Per Åhgren971de072018-03-14 23:23:47 +010067 // Assess the render signal strength.
Per Åhgrenec22e3f2017-12-20 15:20:37 +010068 const std::vector<std::vector<float>>& x_latest = render_buffer.Block(0);
peah14c11a42017-07-11 06:13:43 -070069 auto result0 = std::minmax_element(x_latest[0].begin(), x_latest[0].end());
70 float max_abs = std::max(fabs(*result0.first), fabs(*result0.second));
71
72 if (x_latest.size() > 1) {
73 const auto result1 =
74 std::minmax_element(x_latest[1].begin(), x_latest[1].end());
75 max_abs =
76 std::max(max_abs, static_cast<float>(std::max(fabs(*result1.first),
77 fabs(*result1.second))));
78 }
79
80 // Detect whether the spectal peak has as strong narrowband nature.
Per Åhgren971de072018-03-14 23:23:47 +010081 if (peak_bin > 0 && max_abs > 100 &&
peah14c11a42017-07-11 06:13:43 -070082 X2_latest[peak_bin] > 100 * non_peak_power) {
Oskar Sundbomaa8b67d2017-11-17 14:34:48 +010083 *narrow_peak_band = peak_bin;
peah14c11a42017-07-11 06:13:43 -070084 *narrow_peak_counter = 0;
85 } else {
Per Åhgren971de072018-03-14 23:23:47 +010086 if (*narrow_peak_band &&
87 ++(*narrow_peak_counter) >
88 static_cast<size_t>(strong_peak_freeze_duration)) {
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +020089 *narrow_peak_band = absl::nullopt;
peah14c11a42017-07-11 06:13:43 -070090 }
91 }
92}
93
peah522d71b2017-02-23 05:16:26 -080094} // namespace
95
Per Åhgren971de072018-03-14 23:23:47 +010096RenderSignalAnalyzer::RenderSignalAnalyzer(const EchoCanceller3Config& config)
97 : strong_peak_freeze_duration_(config.filter.main.length_blocks) {
peah522d71b2017-02-23 05:16:26 -080098 narrow_band_counters_.fill(0);
99}
100RenderSignalAnalyzer::~RenderSignalAnalyzer() = default;
101
102void RenderSignalAnalyzer::Update(
peahcf02cf12017-04-05 14:18:07 -0700103 const RenderBuffer& render_buffer,
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +0200104 const absl::optional<size_t>& delay_partitions) {
peah14c11a42017-07-11 06:13:43 -0700105 // Identify bands of narrow nature.
106 IdentifySmallNarrowBandRegions(render_buffer, delay_partitions,
107 &narrow_band_counters_);
peah522d71b2017-02-23 05:16:26 -0800108
peah14c11a42017-07-11 06:13:43 -0700109 // Identify the presence of a strong narrow band.
Per Åhgren971de072018-03-14 23:23:47 +0100110 IdentifyStrongNarrowBandComponent(render_buffer, strong_peak_freeze_duration_,
111 &narrow_peak_band_, &narrow_peak_counter_);
peah522d71b2017-02-23 05:16:26 -0800112}
113
114void RenderSignalAnalyzer::MaskRegionsAroundNarrowBands(
115 std::array<float, kFftLengthBy2Plus1>* v) const {
116 RTC_DCHECK(v);
117
118 // Set v to zero around narrow band signal regions.
119 if (narrow_band_counters_[0] > kCounterThreshold) {
120 (*v)[1] = (*v)[0] = 0.f;
121 }
122 for (size_t k = 2; k < kFftLengthBy2 - 1; ++k) {
123 if (narrow_band_counters_[k - 1] > kCounterThreshold) {
124 (*v)[k - 2] = (*v)[k - 1] = (*v)[k] = (*v)[k + 1] = (*v)[k + 2] = 0.f;
125 }
126 }
127 if (narrow_band_counters_[kFftLengthBy2 - 2] > kCounterThreshold) {
128 (*v)[kFftLengthBy2] = (*v)[kFftLengthBy2 - 1] = 0.f;
129 }
130}
131
132} // namespace webrtc