xref: /aosp_15_r20/external/webrtc/modules/audio_processing/agc2/biquad_filter.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright (c) 2016 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/biquad_filter.h"
12 
13 #include "rtc_base/arraysize.h"
14 
15 namespace webrtc {
16 
BiQuadFilter(const Config & config)17 BiQuadFilter::BiQuadFilter(const Config& config)
18     : config_(config), state_({}) {}
19 
20 BiQuadFilter::~BiQuadFilter() = default;
21 
SetConfig(const Config & config)22 void BiQuadFilter::SetConfig(const Config& config) {
23   config_ = config;
24   state_ = {};
25 }
26 
Reset()27 void BiQuadFilter::Reset() {
28   state_ = {};
29 }
30 
Process(rtc::ArrayView<const float> x,rtc::ArrayView<float> y)31 void BiQuadFilter::Process(rtc::ArrayView<const float> x,
32                            rtc::ArrayView<float> y) {
33   RTC_DCHECK_EQ(x.size(), y.size());
34   const float config_a0 = config_.a[0];
35   const float config_a1 = config_.a[1];
36   const float config_b0 = config_.b[0];
37   const float config_b1 = config_.b[1];
38   const float config_b2 = config_.b[2];
39   float state_a0 = state_.a[0];
40   float state_a1 = state_.a[1];
41   float state_b0 = state_.b[0];
42   float state_b1 = state_.b[1];
43   for (size_t k = 0, x_size = x.size(); k < x_size; ++k) {
44     // Use a temporary variable for `x[k]` to allow in-place processing.
45     const float tmp = x[k];
46     float y_k = config_b0 * tmp + config_b1 * state_b0 + config_b2 * state_b1 -
47                 config_a0 * state_a0 - config_a1 * state_a1;
48     state_b1 = state_b0;
49     state_b0 = tmp;
50     state_a1 = state_a0;
51     state_a0 = y_k;
52     y[k] = y_k;
53   }
54   state_.a[0] = state_a0;
55   state_.a[1] = state_a1;
56   state_.b[0] = state_b0;
57   state_.b[1] = state_b1;
58 }
59 
60 }  // namespace webrtc
61