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>

source

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.

source

pub fn set_min(&mut self, y: f32)

Set a minimum output for the filter

source

pub fn set_max(&mut self, y: f32)

Set a maximum output for the filter

source

pub fn set_limit(&mut self, y: f32)

Set a +- limit for the output of the filter

source

pub fn with_min(self, y: f32) -> Self

Consume filter and return new filter with a set minimum for the output

source

pub fn with_max(self, y: f32) -> Self

Consume filter and return new filter with a set maximum for the output

source

pub fn with_limit(self, y: f32) -> Self

Consume filter and return new filter with a +- limit for the output

source

pub fn update(&mut self, x: FixedI32<X>) -> FixedI32<Y>

Add a new input sample and get the resulting output

source

pub fn reset(&mut self)

Reset the filter

This will clear the fifos in the biquad filter

source

pub fn value(&self) -> FixedI32<Y>

Return the latest output of the filter without updating

source

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);
source

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);
source

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);
source

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);
source

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);
source

pub fn pi(kp: f32, ki: f32) -> Self

Constructs a biquad filter with proportional-integral behavior

Examples
use fixed_filters::Biquad;

let kp = 0.2;
let ki = 0.1;
let mut filter = Biquad::<25, 30>::pi(kp, ki);

Auto Trait Implementations§

§

impl<const X: i32, const Y: i32> RefUnwindSafe for Biquad<X, Y>

§

impl<const X: i32, const Y: i32> Send for Biquad<X, Y>

§

impl<const X: i32, const Y: i32> Sync for Biquad<X, Y>

§

impl<const X: i32, const Y: i32> Unpin for Biquad<X, Y>

§

impl<const X: i32, const Y: i32> UnwindSafe for Biquad<X, Y>

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Az for T

source§

fn az<Dst>(self) -> Dst
where T: Cast<Dst>,

Casts the value.
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<Src, Dst> CastFrom<Src> for Dst
where Src: Cast<Dst>,

source§

fn cast_from(src: Src) -> Dst

Casts the value.
source§

impl<T> CheckedAs for T

source§

fn checked_as<Dst>(self) -> Option<Dst>
where T: CheckedCast<Dst>,

Casts the value.
source§

impl<Src, Dst> CheckedCastFrom<Src> for Dst
where Src: CheckedCast<Dst>,

source§

fn checked_cast_from(src: Src) -> Option<Dst>

Casts the value.
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<Src, Dst> LosslessTryInto<Dst> for Src
where Dst: LosslessTryFrom<Src>,

source§

fn lossless_try_into(self) -> Option<Dst>

Performs the conversion.
source§

impl<Src, Dst> LossyInto<Dst> for Src
where Dst: LossyFrom<Src>,

source§

fn lossy_into(self) -> Dst

Performs the conversion.
source§

impl<T> OverflowingAs for T

source§

fn overflowing_as<Dst>(self) -> (Dst, bool)
where T: OverflowingCast<Dst>,

Casts the value.
source§

impl<Src, Dst> OverflowingCastFrom<Src> for Dst
where Src: OverflowingCast<Dst>,

source§

fn overflowing_cast_from(src: Src) -> (Dst, bool)

Casts the value.
source§

impl<T> SaturatingAs for T

source§

fn saturating_as<Dst>(self) -> Dst
where T: SaturatingCast<Dst>,

Casts the value.
source§

impl<Src, Dst> SaturatingCastFrom<Src> for Dst
where Src: SaturatingCast<Dst>,

source§

fn saturating_cast_from(src: Src) -> Dst

Casts the value.
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> UnwrappedAs for T

source§

fn unwrapped_as<Dst>(self) -> Dst
where T: UnwrappedCast<Dst>,

Casts the value.
source§

impl<Src, Dst> UnwrappedCastFrom<Src> for Dst
where Src: UnwrappedCast<Dst>,

source§

fn unwrapped_cast_from(src: Src) -> Dst

Casts the value.
source§

impl<T> WrappingAs for T

source§

fn wrapping_as<Dst>(self) -> Dst
where T: WrappingCast<Dst>,

Casts the value.
source§

impl<Src, Dst> WrappingCastFrom<Src> for Dst
where Src: WrappingCast<Dst>,

source§

fn wrapping_cast_from(src: Src) -> Dst

Casts the value.