Optimize the AGC2 Biquad filter.
Bug: None
Change-Id: Idde77efd209be1687405d3f256ca52e2da640c1e
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/264561
Reviewed-by: Alessio Bazzica <alessiob@webrtc.org>
Commit-Queue: Christian Schuldt <cschuldt@google.com>
Cr-Commit-Position: refs/heads/main@{#37278}
diff --git a/modules/audio_processing/agc2/biquad_filter.cc b/modules/audio_processing/agc2/biquad_filter.cc
index 453125f..c1b80d7 100644
--- a/modules/audio_processing/agc2/biquad_filter.cc
+++ b/modules/audio_processing/agc2/biquad_filter.cc
@@ -31,17 +31,30 @@
void BiQuadFilter::Process(rtc::ArrayView<const float> x,
rtc::ArrayView<float> y) {
RTC_DCHECK_EQ(x.size(), y.size());
- for (size_t k = 0; k < x.size(); ++k) {
+ const float config_a0 = config_.a[0];
+ const float config_a1 = config_.a[1];
+ const float config_b0 = config_.b[0];
+ const float config_b1 = config_.b[1];
+ const float config_b2 = config_.b[2];
+ float state_a0 = state_.a[0];
+ float state_a1 = state_.a[1];
+ float state_b0 = state_.b[0];
+ float state_b1 = state_.b[1];
+ for (size_t k = 0, x_size = x.size(); k < x_size; ++k) {
// Use a temporary variable for `x[k]` to allow in-place processing.
const float tmp = x[k];
- y[k] = config_.b[0] * tmp + config_.b[1] * state_.b[0] +
- config_.b[2] * state_.b[1] - config_.a[0] * state_.a[0] -
- config_.a[1] * state_.a[1];
- state_.b[1] = state_.b[0];
- state_.b[0] = tmp;
- state_.a[1] = state_.a[0];
- state_.a[0] = y[k];
+ float y_k = config_b0 * tmp + config_b1 * state_b0 + config_b2 * state_b1 -
+ config_a0 * state_a0 - config_a1 * state_a1;
+ state_b1 = state_b0;
+ state_b0 = tmp;
+ state_a1 = state_a0;
+ state_a0 = y_k;
+ y[k] = y_k;
}
+ state_.a[0] = state_a0;
+ state_.a[1] = state_a1;
+ state_.b[0] = state_b0;
+ state_.b[1] = state_b1;
}
} // namespace webrtc