Skip to content

Commit

Permalink
Add Butterworth lowpass filter-bank function
Browse files Browse the repository at this point in the history
  • Loading branch information
rhizoome authored and gitbutler-client committed Sep 7, 2024
1 parent d5992b8 commit ad4f432
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/biquad_bank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ use numeric_array::ArrayLength;
pub trait Realx<Size: ArrayLength>: Num + Sized + Neg<Output = Self> {
const PI: Self;
const TAU: Self;
const SQRT_2: Self;
fn tan(self) -> Self;
fn exp(self) -> Self;
fn cos(self) -> Self;
fn sqrt(self) -> Self;
Expand All @@ -24,7 +26,12 @@ pub trait Realx<Size: ArrayLength>: Num + Sized + Neg<Output = Self> {
impl Realx<U8> for f32x8 {
const PI: Self = f32x8::PI;
const TAU: Self = f32x8::TAU;
const SQRT_2: Self = f32x8::SQRT_2;

#[inline(always)]
fn tan(self) -> Self {
f32x8::tan(self)
}
#[inline(always)]
fn exp(self) -> Self {
f32x8::exp(self)
Expand Down Expand Up @@ -58,7 +65,12 @@ impl Realx<U8> for f32x8 {
impl Realx<U4> for f64x4 {
const PI: Self = f64x4::PI;
const TAU: Self = f64x4::TAU;
const SQRT_2: Self = f64x4::SQRT_2;

#[inline(always)]
fn tan(self) -> Self {
f64x4::tan(self)
}
#[inline(always)]
fn exp(self) -> Self {
f64x4::exp(self)
Expand Down Expand Up @@ -118,6 +130,30 @@ where
F: Realx<Size>,
Size: ArrayLength,
{
/// Return settings for a Butterworth lowpass filter.
/// Sample rate is in Hz.
/// Cutoff is the -3 dB point of the filter in Hz.
#[inline]
pub fn butter_lowpass(sample_rate: f32, cutoff: F) -> Self {
let c = F::from_f64;
let sr = F::from_f32(sample_rate);
let f: F = (cutoff * F::PI / sr).tan();
let a0r: F = c(1.0) / (c(1.0) + F::SQRT_2 * f + f * f);
let a1: F = (c(2.0) * f * f - c(2.0)) * a0r;
let a2: F = (c(1.0) - F::SQRT_2 * f + f * f) * a0r;
let b0: F = f * f * a0r;
let b1: F = c(2.0) * b0;
let b2: F = b0;
Self {
a1,
a2,
b0,
b1,
b2,
_marker: PhantomData,
}
}

/// Return settings for a constant-gain bandpass resonator-bank.
/// Sample rate and center frequency are in Hz.
/// The overall gain of the filter is independent of bandwidth.
Expand Down

0 comments on commit ad4f432

Please sign in to comment.