Skip to main content

LowpassFilter

Struct LowpassFilter 

Source
pub struct LowpassFilter<T> { /* private fields */ }
Expand description

A first-order lowpass filter compatible with f32 and f64.

It can consume and filter items one-by-one (iterator-style API) or operate on slices (LowpassFilter::run_slice).

It is mandatory to operate on values in range -1.0..=1.0, which is also the default in DSP.

§More Info

Implementations§

Source§

impl<T: Sample> LowpassFilter<T>

Source

pub fn new(sample_rate_hz: T, cutoff_frequency_hz: T) -> Self

Create a new lowpass filter.

§Arguments
  • sample_rate_hz: Sample rate in Hz (e.g., 48000.0).
  • cutoff_frequency_hz: Cutoff frequency in Hz (e.g., 1000.0).
Source

pub fn run(&mut self, input: T) -> T

Filter a single sample and return the filtered result.

It is mandatory to operate on values in range -1.0..=1.0, which is also the default in DSP. The returned value is also guaranteed to be in that range.

Source

pub fn run_slice(&mut self, samples: &mut [T])

Filter a whole slice of samples in-place.

Matches calling Self::run per sample up to tiny floating point rounding differences (roughly 1e-6 for f32), but is significantly faster. The filter state is updated, so consecutive calls compose, also when mixed with Self::run.

§Math

Unrolling y[n] = alpha * x[n] + beta * y[n-1] over a block of samples yields

y[i] = beta^(i+1) * prev + sum(alpha * beta^(i-j) * x[j] for j <= i)

so within a block, samples only depend on the state prev from before the block and can be computed in parallel, which enables compiler auto-vectorization (SIMD). Only prev propagates serially between blocks.

§Arguments
  • samples: Samples to filter in-place, in range -1.0..=1.0.
Source

pub const fn reset(&mut self)

Reset the internal filter state.

Trait Implementations§

Source§

impl<T: Clone> Clone for LowpassFilter<T>

Source§

fn clone(&self) -> LowpassFilter<T>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug> Debug for LowpassFilter<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T> Freeze for LowpassFilter<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for LowpassFilter<T>
where T: RefUnwindSafe,

§

impl<T> Send for LowpassFilter<T>
where T: Send,

§

impl<T> Sync for LowpassFilter<T>
where T: Sync,

§

impl<T> Unpin for LowpassFilter<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for LowpassFilter<T>
where T: UnsafeUnpin,

§

impl<T> UnwindSafe for LowpassFilter<T>
where T: UnwindSafe,

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> 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<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
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<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

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

Source§

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.