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>
impl<T: Sample> LowpassFilter<T>
Sourcepub fn new(sample_rate_hz: T, cutoff_frequency_hz: T) -> Self
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).
Sourcepub fn run(&mut self, input: T) -> T
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.
Sourcepub fn run_slice(&mut self, samples: &mut [T])
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.
Trait Implementations§
Source§impl<T: Clone> Clone for LowpassFilter<T>
impl<T: Clone> Clone for LowpassFilter<T>
Source§fn clone(&self) -> LowpassFilter<T>
fn clone(&self) -> LowpassFilter<T>
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more