1 /*
2 * Copyright (c) 2018 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/agc2/rnn_vad/lp_residual.h"
12
13 #include <algorithm>
14 #include <array>
15 #include <cmath>
16 #include <numeric>
17
18 #include "rtc_base/checks.h"
19 #include "rtc_base/numerics/safe_compare.h"
20
21 namespace webrtc {
22 namespace rnn_vad {
23 namespace {
24
25 // Computes auto-correlation coefficients for `x` and writes them in
26 // `auto_corr`. The lag values are in {0, ..., max_lag - 1}, where max_lag
27 // equals the size of `auto_corr`.
ComputeAutoCorrelation(rtc::ArrayView<const float> x,rtc::ArrayView<float,kNumLpcCoefficients> auto_corr)28 void ComputeAutoCorrelation(
29 rtc::ArrayView<const float> x,
30 rtc::ArrayView<float, kNumLpcCoefficients> auto_corr) {
31 constexpr int max_lag = auto_corr.size();
32 RTC_DCHECK_LT(max_lag, x.size());
33 for (int lag = 0; lag < max_lag; ++lag) {
34 auto_corr[lag] =
35 std::inner_product(x.begin(), x.end() - lag, x.begin() + lag, 0.f);
36 }
37 }
38
39 // Applies denoising to the auto-correlation coefficients.
DenoiseAutoCorrelation(rtc::ArrayView<float,kNumLpcCoefficients> auto_corr)40 void DenoiseAutoCorrelation(
41 rtc::ArrayView<float, kNumLpcCoefficients> auto_corr) {
42 // Assume -40 dB white noise floor.
43 auto_corr[0] *= 1.0001f;
44 // Hard-coded values obtained as
45 // [np.float32((0.008*0.008*i*i)) for i in range(1,5)].
46 auto_corr[1] -= auto_corr[1] * 0.000064f;
47 auto_corr[2] -= auto_corr[2] * 0.000256f;
48 auto_corr[3] -= auto_corr[3] * 0.000576f;
49 auto_corr[4] -= auto_corr[4] * 0.001024f;
50 static_assert(kNumLpcCoefficients == 5, "Update `auto_corr`.");
51 }
52
53 // Computes the initial inverse filter coefficients given the auto-correlation
54 // coefficients of an input frame.
ComputeInitialInverseFilterCoefficients(rtc::ArrayView<const float,kNumLpcCoefficients> auto_corr,rtc::ArrayView<float,kNumLpcCoefficients-1> lpc_coeffs)55 void ComputeInitialInverseFilterCoefficients(
56 rtc::ArrayView<const float, kNumLpcCoefficients> auto_corr,
57 rtc::ArrayView<float, kNumLpcCoefficients - 1> lpc_coeffs) {
58 float error = auto_corr[0];
59 for (int i = 0; i < kNumLpcCoefficients - 1; ++i) {
60 float reflection_coeff = 0.f;
61 for (int j = 0; j < i; ++j) {
62 reflection_coeff += lpc_coeffs[j] * auto_corr[i - j];
63 }
64 reflection_coeff += auto_corr[i + 1];
65
66 // Avoid division by numbers close to zero.
67 constexpr float kMinErrorMagnitude = 1e-6f;
68 if (std::fabs(error) < kMinErrorMagnitude) {
69 error = std::copysign(kMinErrorMagnitude, error);
70 }
71
72 reflection_coeff /= -error;
73 // Update LPC coefficients and total error.
74 lpc_coeffs[i] = reflection_coeff;
75 for (int j = 0; j < ((i + 1) >> 1); ++j) {
76 const float tmp1 = lpc_coeffs[j];
77 const float tmp2 = lpc_coeffs[i - 1 - j];
78 lpc_coeffs[j] = tmp1 + reflection_coeff * tmp2;
79 lpc_coeffs[i - 1 - j] = tmp2 + reflection_coeff * tmp1;
80 }
81 error -= reflection_coeff * reflection_coeff * error;
82 if (error < 0.001f * auto_corr[0]) {
83 break;
84 }
85 }
86 }
87
88 } // namespace
89
ComputeAndPostProcessLpcCoefficients(rtc::ArrayView<const float> x,rtc::ArrayView<float,kNumLpcCoefficients> lpc_coeffs)90 void ComputeAndPostProcessLpcCoefficients(
91 rtc::ArrayView<const float> x,
92 rtc::ArrayView<float, kNumLpcCoefficients> lpc_coeffs) {
93 std::array<float, kNumLpcCoefficients> auto_corr;
94 ComputeAutoCorrelation(x, auto_corr);
95 if (auto_corr[0] == 0.f) { // Empty frame.
96 std::fill(lpc_coeffs.begin(), lpc_coeffs.end(), 0);
97 return;
98 }
99 DenoiseAutoCorrelation(auto_corr);
100 std::array<float, kNumLpcCoefficients - 1> lpc_coeffs_pre{};
101 ComputeInitialInverseFilterCoefficients(auto_corr, lpc_coeffs_pre);
102 // LPC coefficients post-processing.
103 // TODO(bugs.webrtc.org/9076): Consider removing these steps.
104 lpc_coeffs_pre[0] *= 0.9f;
105 lpc_coeffs_pre[1] *= 0.9f * 0.9f;
106 lpc_coeffs_pre[2] *= 0.9f * 0.9f * 0.9f;
107 lpc_coeffs_pre[3] *= 0.9f * 0.9f * 0.9f * 0.9f;
108 constexpr float kC = 0.8f;
109 lpc_coeffs[0] = lpc_coeffs_pre[0] + kC;
110 lpc_coeffs[1] = lpc_coeffs_pre[1] + kC * lpc_coeffs_pre[0];
111 lpc_coeffs[2] = lpc_coeffs_pre[2] + kC * lpc_coeffs_pre[1];
112 lpc_coeffs[3] = lpc_coeffs_pre[3] + kC * lpc_coeffs_pre[2];
113 lpc_coeffs[4] = kC * lpc_coeffs_pre[3];
114 static_assert(kNumLpcCoefficients == 5, "Update `lpc_coeffs(_pre)`.");
115 }
116
ComputeLpResidual(rtc::ArrayView<const float,kNumLpcCoefficients> lpc_coeffs,rtc::ArrayView<const float> x,rtc::ArrayView<float> y)117 void ComputeLpResidual(
118 rtc::ArrayView<const float, kNumLpcCoefficients> lpc_coeffs,
119 rtc::ArrayView<const float> x,
120 rtc::ArrayView<float> y) {
121 RTC_DCHECK_GT(x.size(), kNumLpcCoefficients);
122 RTC_DCHECK_EQ(x.size(), y.size());
123 // The code below implements the following operation:
124 // y[i] = x[i] + dot_product({x[i], ..., x[i - kNumLpcCoefficients + 1]},
125 // lpc_coeffs)
126 // Edge case: i < kNumLpcCoefficients.
127 y[0] = x[0];
128 for (int i = 1; i < kNumLpcCoefficients; ++i) {
129 y[i] =
130 std::inner_product(x.crend() - i, x.crend(), lpc_coeffs.cbegin(), x[i]);
131 }
132 // Regular case.
133 auto last = x.crend();
134 for (int i = kNumLpcCoefficients; rtc::SafeLt(i, y.size()); ++i, --last) {
135 y[i] = std::inner_product(last - kNumLpcCoefficients, last,
136 lpc_coeffs.cbegin(), x[i]);
137 }
138 }
139
140 } // namespace rnn_vad
141 } // namespace webrtc
142