use core::f32::consts::{PI, SQRT_2};
use nalgebra::Complex;
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub(crate) struct LowPass {
coefficients: Option<(Coefficients, f32, f32)>,
history: Option<History>,
}
impl LowPass {
pub(crate) fn apply(&mut self, value: f32, dt: f32, cutoff_hz: f32) -> f32 {
if !value.is_finite() {
return value;
}
let Some(coefficients) = self.coefficients(dt, cutoff_hz) else {
self.history = None;
return value;
};
let Coefficients { b0, a1, a2 } = coefficients;
let History { inputs, outputs } = *self.history.get_or_insert(History {
inputs: [value; 2],
outputs: [value; 2],
});
let output = b0 * (value + 2.0 * inputs[0] + inputs[1]) - a1 * outputs[0] - a2 * outputs[1];
self.history = Some(History {
inputs: [value, inputs[0]],
outputs: [output, outputs[0]],
});
output
}
pub(crate) fn response(cutoff_hz: f32, dt_s: f32, unit_delay: Complex<f32>) -> Complex<f32> {
let one = Complex::new(1.0, 0.0);
let Some(Coefficients { b0, a1, a2 }) = Coefficients::new(cutoff_hz, dt_s) else {
return one;
};
let z1 = unit_delay;
(one + z1 * 2.0 + z1 * z1) * b0 / (one + z1 * a1 + z1 * z1 * a2)
}
fn coefficients(&mut self, dt: f32, cutoff_hz: f32) -> Option<Coefficients> {
match self.coefficients {
Some((coefficients, cached_hz, cached_dt))
if cached_hz == cutoff_hz && cached_dt == dt =>
{
Some(coefficients)
}
_ => {
let coefficients = Coefficients::new(cutoff_hz, dt)?;
self.coefficients = Some((coefficients, cutoff_hz, dt));
Some(coefficients)
}
}
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
struct History {
inputs: [f32; 2],
outputs: [f32; 2],
}
#[derive(Clone, Copy, Debug, PartialEq)]
struct Coefficients {
b0: f32,
a1: f32,
a2: f32,
}
impl Coefficients {
fn new(cutoff_hz: f32, dt_s: f32) -> Option<Self> {
let passes = !(cutoff_hz > 0.0 && cutoff_hz * dt_s < 0.5 && cutoff_hz.is_finite());
if passes {
return None;
}
let k = libm::tanf(PI * cutoff_hz * dt_s);
let k_squared = k * k;
let norm = 1.0 / (1.0 + SQRT_2 * k + k_squared);
Some(Self {
b0: k_squared * norm,
a1: 2.0 * (k_squared - 1.0) * norm,
a2: (1.0 - SQRT_2 * k + k_squared) * norm,
})
}
}
#[cfg(test)]
mod tests;