Skip to content

Commit

Permalink
perf: specialized iir filter (#174)
Browse files Browse the repository at this point in the history
  • Loading branch information
kahojyun authored Jun 1, 2024
1 parent 8d4a37d commit f52345c
Show file tree
Hide file tree
Showing 4 changed files with 319 additions and 47 deletions.
18 changes: 1 addition & 17 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ crate-type = ["cdylib"]

[dependencies]
anyhow = "1.0.82"
biquad = "0.4.2"
bspline = "1.1.0"
cached = "0.50.0"
float-cmp = "0.9.0"
Expand Down
32 changes: 3 additions & 29 deletions src/pulse.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
mod iir;

use std::{
ops::{Add, Mul},
sync::Arc,
};

use anyhow::{bail, Context, Result};
use biquad::Biquad as _;
use cached::proc_macro::cached;
use float_cmp::approx_eq;
use hashbrown::HashMap;
Expand Down Expand Up @@ -480,34 +481,7 @@ pub(crate) fn apply_offset_inplace(waveform: &mut ArrayViewMut2<f64>, offset: Ar
}

pub(crate) fn apply_iir_inplace(waveform: &mut ArrayViewMut2<f64>, sos: ArrayView2<f64>) {
let mut biquads: Vec<_> = sos
.axis_iter(Axis(0))
.map(|row| {
let b0 = row[0];
let b1 = row[1];
let b2 = row[2];
let a1 = row[4];
let a2 = row[5];
let coef = biquad::Coefficients { b0, a1, a2, b1, b2 };
biquad::DirectForm2Transposed::<f64>::new(coef)
})
.collect();
for mut row in waveform.axis_iter_mut(Axis(0)) {
apply_iir_inplace_1d(row.as_slice_mut().unwrap(), &mut biquads);
}
}

fn apply_iir_inplace_1d(waveform: &mut [f64], biquads: &mut [biquad::DirectForm2Transposed<f64>]) {
for biquad in biquads.iter_mut() {
biquad.reset_state();
}
for y in waveform.iter_mut() {
let mut x = *y;
for biquad in biquads.iter_mut() {
x = biquad.run(x);
}
*y = x;
}
self::iir::iir_filter_inplace(waveform.view_mut(), sos).unwrap()
}

pub(crate) fn apply_fir_inplace(waveform: &mut ArrayViewMut2<f64>, taps: ArrayView1<f64>) {
Expand Down
Loading

0 comments on commit f52345c

Please sign in to comment.