Skip to main content

Crate lowpass_filter

Crate lowpass_filter 

Source
Expand description

High performance no_std lowpass filter for digital signal processing.

This crate implements a simple first-order digital lowpass filter for f32 and f64 samples. Use it, for example, to extract the bass from a song or to smooth noisy sensor data. It has no dependencies, no unsafe code, and performs no allocations, making it suitable for any target from desktop to embedded.

Samples must be in range -1.0..=1.0, which is the default in DSP.

§Usage

To filter a buffer of samples in one go, use lowpass_filter_slice (or lowpass_filter_slice_f64). This is the fastest option: it processes samples in blocks that compilers auto-vectorize (SIMD), several times faster than per-sample processing.

use lowpass_filter::lowpass_filter_slice;

// Mono audio samples, recorded at 44.1 kHz sample rate.
let mut samples = [0.0, 0.3, -0.6, 0.8, 0.5, -0.2];
// Only keep frequencies below 120 Hz; mutates the buffer in-place.
lowpass_filter_slice(&mut samples, 44100.0, 120.0);

For streaming data, e.g. in an audio callback, keep a LowpassFilter around: its state carries over between calls, so chunked processing equals processing everything at once. It also filters single samples, e.g. inside iterator chains.

use lowpass_filter::LowpassFilter;

let mut filter = LowpassFilter::<f32>::new(44100.0, 120.0);
// Process data as it arrives (fast block processing) ...
for mut chunk in [[0.0, 0.3, -0.6, 0.8], [0.5, -0.2, 0.1, 0.4]] {
    filter.run_slice(&mut chunk);
}
// ... or one sample at a time.
let filtered = filter.run(0.25);

The iterator-based lowpass_filter and lowpass_filter_f64 functions are convenient when the samples do not live in a slice.

Structs§

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

Traits§

Sample
A sample type LowpassFilter can operate on: f32 or f64.

Functions§

lowpass_filter
Applies a LowpassFilter to the data provided in the mutable buffer and changes the items in-place.
lowpass_filter_f64
Applies a LowpassFilter to the data provided in the mutable buffer and changes the items in-place.
lowpass_filter_slice
Applies a LowpassFilter to the slice in-place via LowpassFilter::run_slice.
lowpass_filter_slice_f64
Applies a LowpassFilter to the slice in-place via LowpassFilter::run_slice.