Line data Source code
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 : #include "webrtc/modules/audio_processing/aec3/cascaded_biquad_filter.h"
11 :
12 : #include "webrtc/base/checks.h"
13 :
14 : namespace webrtc {
15 :
16 0 : CascadedBiQuadFilter::CascadedBiQuadFilter(
17 : const CascadedBiQuadFilter::BiQuadCoefficients& coefficients,
18 0 : size_t num_biquads)
19 0 : : biquad_states_(num_biquads), coefficients_(coefficients) {}
20 :
21 : CascadedBiQuadFilter::~CascadedBiQuadFilter() = default;
22 :
23 0 : void CascadedBiQuadFilter::Process(rtc::ArrayView<const float> x,
24 : rtc::ArrayView<float> y) {
25 0 : ApplyBiQuad(x, y, &biquad_states_[0]);
26 0 : for (size_t k = 1; k < biquad_states_.size(); ++k) {
27 0 : ApplyBiQuad(y, y, &biquad_states_[k]);
28 : }
29 0 : }
30 :
31 0 : void CascadedBiQuadFilter::Process(rtc::ArrayView<float> y) {
32 0 : for (auto& biquad : biquad_states_) {
33 0 : ApplyBiQuad(y, y, &biquad);
34 : }
35 0 : }
36 :
37 0 : void CascadedBiQuadFilter::ApplyBiQuad(
38 : rtc::ArrayView<const float> x,
39 : rtc::ArrayView<float> y,
40 : CascadedBiQuadFilter::BiQuadState* biquad_state) {
41 0 : RTC_DCHECK_EQ(x.size(), y.size());
42 0 : RTC_DCHECK(biquad_state);
43 0 : const auto c_b = coefficients_.b;
44 0 : const auto c_a = coefficients_.a;
45 0 : auto m_x = biquad_state->x;
46 0 : auto m_y = biquad_state->y;
47 0 : for (size_t k = 0; k < x.size(); ++k) {
48 0 : const float tmp = x[k];
49 0 : y[k] = c_b[0] * tmp + c_b[1] * m_x[0] + c_b[2] * m_x[1] - c_a[0] * m_y[0] -
50 0 : c_a[1] * m_y[1];
51 0 : m_x[1] = m_x[0];
52 0 : m_x[0] = tmp;
53 0 : m_y[1] = m_y[0];
54 0 : m_y[0] = y[k];
55 : }
56 0 : }
57 :
58 : } // namespace webrtc
|