2021-02-04 22:21:05 +08:00
|
|
|
use core::f64::consts::PI;
|
2021-02-21 03:27:30 +08:00
|
|
|
use miniconf::StringSet;
|
|
|
|
use serde::Deserialize;
|
2020-12-22 23:49:12 +08:00
|
|
|
|
2021-01-27 01:49:58 +08:00
|
|
|
/// Generic vector for integer IIR filter.
|
|
|
|
/// This struct is used to hold the x/y input/output data vector or the b/a coefficient
|
|
|
|
/// vector.
|
2021-02-17 23:13:30 +08:00
|
|
|
pub type Vec5 = [i32; 5];
|
2020-12-22 23:49:12 +08:00
|
|
|
|
2021-02-17 23:13:30 +08:00
|
|
|
trait Coeff {
|
2021-01-27 01:49:58 +08:00
|
|
|
/// Lowpass biquad filter using cutoff and sampling frequencies. Taken from:
|
|
|
|
/// https://webaudio.github.io/Audio-EQ-Cookbook/audio-eq-cookbook.html
|
|
|
|
///
|
|
|
|
/// # Args
|
|
|
|
/// * `f` - Corner frequency, or 3dB cutoff frequency (in units of sample rate).
|
2021-01-27 02:19:03 +08:00
|
|
|
/// This is only accurate for low corner frequencies less than ~0.01.
|
2021-01-27 01:49:58 +08:00
|
|
|
/// * `q` - Quality factor (1/sqrt(2) for critical).
|
|
|
|
/// * `k` - DC gain.
|
|
|
|
///
|
|
|
|
/// # Returns
|
|
|
|
/// 2nd-order IIR filter coefficients in the form [b0,b1,b2,a1,a2]. a0 is set to -1.
|
2021-02-17 23:13:30 +08:00
|
|
|
fn lowpass(f: f64, q: f64, k: f64) -> Self;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Coeff for Vec5 {
|
|
|
|
fn lowpass(f: f64, q: f64, k: f64) -> Self {
|
2021-01-27 02:19:03 +08:00
|
|
|
// 3rd order Taylor approximation of sin and cos.
|
|
|
|
let f = f * 2. * PI;
|
2021-02-02 22:41:47 +08:00
|
|
|
let f2 = f * f * 0.5;
|
|
|
|
let fcos = 1. - f2;
|
|
|
|
let fsin = f * (1. - f2 / 3.);
|
2021-01-27 01:49:58 +08:00
|
|
|
let alpha = fsin / (2. * q);
|
|
|
|
// IIR uses Q2.30 fixed point
|
2021-02-04 22:21:05 +08:00
|
|
|
let a0 = (1. + alpha) / (1 << IIR::SHIFT) as f64;
|
2021-02-09 19:17:48 +08:00
|
|
|
let b0 = (k / 2. * (1. - fcos) / a0 + 0.5) as _;
|
|
|
|
let a1 = (2. * fcos / a0 + 0.5) as _;
|
|
|
|
let a2 = ((alpha - 1.) / a0 + 0.5) as _;
|
2021-01-27 01:49:58 +08:00
|
|
|
|
2021-02-17 23:13:30 +08:00
|
|
|
[b0, 2 * b0, b0, a1, a2]
|
2021-01-27 01:49:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-22 23:49:12 +08:00
|
|
|
fn macc(y0: i32, x: &[i32], a: &[i32], shift: u32) -> i32 {
|
|
|
|
// Rounding bias, half up
|
|
|
|
let y0 = ((y0 as i64) << shift) + (1 << (shift - 1));
|
|
|
|
let y = x
|
|
|
|
.iter()
|
|
|
|
.zip(a)
|
|
|
|
.map(|(x, a)| *x as i64 * *a as i64)
|
|
|
|
.fold(y0, |y, xa| y + xa);
|
|
|
|
(y >> shift) as i32
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Integer biquad IIR
|
|
|
|
///
|
|
|
|
/// See `dsp::iir::IIR` for general implementation details.
|
|
|
|
/// Offset and limiting disabled to suit lowpass applications.
|
|
|
|
/// Coefficient scaling fixed and optimized.
|
2021-02-21 03:27:30 +08:00
|
|
|
#[derive(Copy, Clone, Default, Debug, StringSet, Deserialize)]
|
2020-12-22 23:49:12 +08:00
|
|
|
pub struct IIR {
|
2021-02-01 19:22:50 +08:00
|
|
|
pub ba: Vec5,
|
2020-12-22 23:49:12 +08:00
|
|
|
// pub y_offset: i32,
|
|
|
|
// pub y_min: i32,
|
|
|
|
// pub y_max: i32,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl IIR {
|
2021-01-22 22:11:16 +08:00
|
|
|
/// Coefficient fixed point format: signed Q2.30.
|
|
|
|
/// Tailored to low-passes, PI, II etc.
|
2021-01-18 05:19:14 +08:00
|
|
|
pub const SHIFT: u32 = 30;
|
2020-12-22 23:49:12 +08:00
|
|
|
|
|
|
|
/// Feed a new input value into the filter, update the filter state, and
|
|
|
|
/// return the new output. Only the state `xy` is modified.
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
/// * `xy` - Current filter state.
|
|
|
|
/// * `x0` - New input.
|
2021-02-01 19:22:50 +08:00
|
|
|
pub fn update(&self, xy: &mut Vec5, x0: i32) -> i32 {
|
2021-02-17 23:13:30 +08:00
|
|
|
let n = self.ba.len();
|
|
|
|
debug_assert!(xy.len() == n);
|
2020-12-22 23:49:12 +08:00
|
|
|
// `xy` contains x0 x1 y0 y1 y2
|
|
|
|
// Increment time x1 x2 y1 y2 y3
|
|
|
|
// Shift x1 x1 x2 y1 y2
|
|
|
|
// This unrolls better than xy.rotate_right(1)
|
2021-02-17 23:13:30 +08:00
|
|
|
xy.copy_within(0..n - 1, 1);
|
2020-12-22 23:49:12 +08:00
|
|
|
// Store x0 x0 x1 x2 y1 y2
|
2021-02-17 23:13:30 +08:00
|
|
|
xy[0] = x0;
|
2020-12-22 23:49:12 +08:00
|
|
|
// Compute y0 by multiply-accumulate
|
2021-02-17 23:13:30 +08:00
|
|
|
let y0 = macc(0, xy, &self.ba, IIR::SHIFT);
|
2020-12-22 23:49:12 +08:00
|
|
|
// Limit y0
|
|
|
|
// let y0 = y0.max(self.y_min).min(self.y_max);
|
|
|
|
// Store y0 x0 x1 y0 y1 y2
|
2021-02-17 23:13:30 +08:00
|
|
|
xy[n / 2] = y0;
|
2020-12-22 23:49:12 +08:00
|
|
|
y0
|
|
|
|
}
|
|
|
|
}
|
2021-01-27 02:19:03 +08:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
2021-02-17 23:25:21 +08:00
|
|
|
use super::{Coeff, Vec5};
|
2021-01-27 02:19:03 +08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn lowpass_gen() {
|
2021-02-09 19:17:48 +08:00
|
|
|
let ba = Vec5::lowpass(1e-5, 1. / 2f64.sqrt(), 2.);
|
2021-02-17 23:13:30 +08:00
|
|
|
println!("{:?}", ba);
|
2021-01-27 02:19:03 +08:00
|
|
|
}
|
|
|
|
}
|