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 #ifndef MODULES_AUDIO_PROCESSING_AGC2_BIQUAD_FILTER_H_ 12 #define MODULES_AUDIO_PROCESSING_AGC2_BIQUAD_FILTER_H_ 13 14 #include <algorithm> 15 16 #include "api/array_view.h" 17 #include "rtc_base/arraysize.h" 18 #include "rtc_base/constructor_magic.h" 19 20 namespace webrtc { 21 22 class BiQuadFilter { 23 public: 24 // Normalized filter coefficients. 25 // b_0 + b_1 • z^(-1) + b_2 • z^(-2) 26 // H(z) = --------------------------------- 27 // 1 + a_1 • z^(-1) + a_2 • z^(-2) 28 struct BiQuadCoefficients { 29 float b[3]; 30 float a[2]; 31 }; 32 33 BiQuadFilter() = default; 34 Initialize(const BiQuadCoefficients & coefficients)35 void Initialize(const BiQuadCoefficients& coefficients) { 36 coefficients_ = coefficients; 37 } 38 Reset()39 void Reset() { biquad_state_.Reset(); } 40 41 // Produces a filtered output y of the input x. Both x and y need to 42 // have the same length. In-place modification is allowed. 43 void Process(rtc::ArrayView<const float> x, rtc::ArrayView<float> y); 44 45 private: 46 struct BiQuadState { BiQuadStateBiQuadState47 BiQuadState() { Reset(); } 48 ResetBiQuadState49 void Reset() { 50 std::fill(b, b + arraysize(b), 0.f); 51 std::fill(a, a + arraysize(a), 0.f); 52 } 53 54 float b[2]; 55 float a[2]; 56 }; 57 58 BiQuadState biquad_state_; 59 BiQuadCoefficients coefficients_; 60 61 RTC_DISALLOW_COPY_AND_ASSIGN(BiQuadFilter); 62 }; 63 64 } // namespace webrtc 65 66 #endif // MODULES_AUDIO_PROCESSING_AGC2_BIQUAD_FILTER_H_ 67