Struct fixed_filters::biquad::Biquad
source · pub struct Biquad<const X: i32, const Y: i32> { /* private fields */ }
Expand description
A fixed-point digital Biquad filter
This is a Direct Form 1 fixed point implementation that uses the fixed crate.
The filter works on fixed-point 32 bit signals where the fractional bits is passed as a constant generic
Biquad<16, 20>
will create a 32-bit biquad filter for a 32-bit signal with 16 fractional bits on the
input and 20 fractional bits on the output.
While the run methods for the filter use fixed-point arithmetic, all of the generating functions use floating-point arithmetic and inputs to simplify generation of filters.
While you can create Biqad filters by passing raw coefficients using the Biquad::new()
method,
there are also a number of associated functions for generating Biquads of different standard types.
Examples
use fixed_filters::Biquad;
use fixed::FixedI32;
let ts = 1./10e3; // sampling period
let f0 = 15.0; // cutoff frequency
let q = core::f32::consts::FRAC_1_SQRT_2; // butterworth
let mut filter = Biquad::<20, 20>::lowpass(ts, f0, q);
let signal = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0];
let mut y = FixedI32::<20>::ZERO;
for s in signal {
let x = FixedI32::<20>::from_num(s); // convert to fixed-point number
y = filter.update(x);
}
Implementations§
source§impl<const X: i32, const Y: i32> Biquad<X, Y>
impl<const X: i32, const Y: i32> Biquad<X, Y>
sourcepub fn new(a0: f32, a1: f32, a2: f32, b0: f32, b1: f32, b2: f32) -> Self
pub fn new(a0: f32, a1: f32, a2: f32, b0: f32, b1: f32, b2: f32) -> Self
Create a new biquad filter from the biquad coefficients
While this method can be used to create any biqauad filter, most use cases should use one of the associated methods which will automatically calculate the coefficients for the standard filter forms from the sampling frequency, center frequency, and the quality factor.
sourcepub fn with_min(self, y: f32) -> Self
pub fn with_min(self, y: f32) -> Self
Consume filter and return new filter with a set minimum for the output
sourcepub fn with_max(self, y: f32) -> Self
pub fn with_max(self, y: f32) -> Self
Consume filter and return new filter with a set maximum for the output
sourcepub fn with_limit(self, y: f32) -> Self
pub fn with_limit(self, y: f32) -> Self
Consume filter and return new filter with a +- limit for the output
sourcepub fn update(&mut self, x: FixedI32<X>) -> FixedI32<Y>
pub fn update(&mut self, x: FixedI32<X>) -> FixedI32<Y>
Add a new input sample and get the resulting output
sourcepub fn single_pole_lowpass(ts: f32, f0: f32) -> Self
pub fn single_pole_lowpass(ts: f32, f0: f32) -> Self
Constructs a biquad filter with single pole lowpass (i.e. “RC”) behavior
Effectively makes y[n] = y[n-1] + alpha * (x[n]-y[n-1])
style filter
Examples
use fixed_filters::Biquad;
let ts = 1./10e3; // sampling period
let f0 = 15.0; // cutoff frequency
let mut filter = Biquad::<25, 25>::single_pole_lowpass(ts, f0);
sourcepub fn lowpass(ts: f32, f0: f32, q: f32) -> Self
pub fn lowpass(ts: f32, f0: f32, q: f32) -> Self
Constructs a lowpass biquad filter
Uses arithmetic from https://webaudio.github.io/Audio-EQ-Cookbook/audio-eq-cookbook.html
Examples
use fixed_filters::Biquad;
let ts = 1./10e3; // sampling period
let f0 = 15.0; // cutoff frequency
let q = core::f32::consts::FRAC_1_SQRT_2; // butterworth
let mut filter = Biquad::<20, 20>::lowpass(ts, f0, q);
sourcepub fn highpass(ts: f32, f0: f32, q: f32) -> Self
pub fn highpass(ts: f32, f0: f32, q: f32) -> Self
Constructs a highpass biquad filter
Uses arithmetic from https://webaudio.github.io/Audio-EQ-Cookbook/audio-eq-cookbook.html
Examples
use fixed_filters::Biquad;
let ts = 1./10e3; // sampling period
let f0 = 15.0; // cutoff frequency
let q = core::f32::consts::FRAC_1_SQRT_2; // butterworth
let mut filter = Biquad::<20, 20>::highpass(ts, f0, q);
sourcepub fn bandpass(ts: f32, f0: f32, q: f32) -> Self
pub fn bandpass(ts: f32, f0: f32, q: f32) -> Self
Constructs a bandpass biquad filter
Uses arithmetic from https://webaudio.github.io/Audio-EQ-Cookbook/audio-eq-cookbook.html
Examples
use fixed_filters::Biquad;
let ts = 1./10e3; // sampling period
let f0 = 15.0; // center frequency
let q = core::f32::consts::FRAC_1_SQRT_2; // butterworth
let mut filter = Biquad::<20, 20>::bandpass(ts, f0, q);
sourcepub fn notch(ts: f32, f0: f32, q: f32) -> Self
pub fn notch(ts: f32, f0: f32, q: f32) -> Self
Constructs a notch biquad filter
Uses arithmetic from https://webaudio.github.io/Audio-EQ-Cookbook/audio-eq-cookbook.html
Examples
use fixed_filters::Biquad;
let ts = 1./10e3; // sampling period
let f0 = 15.0; // notch frequency
let q = core::f32::consts::FRAC_1_SQRT_2; // butterworth
let mut filter = Biquad::<20, 20>::notch(ts, f0, q);