Crate static_fir [] [src]

Finite-impulse response (FIR) convolution with static tap coefficients.

Example

The following example shows typical API usage:

#[macro_use]
extern crate static_fir;

use static_fir::FirFilter;

impl_fir!(LowpassFir, f32, 21, [
    -0.0022183273232,
    -0.00364708336518,
    -0.0058179856702,
    -0.00616633506547,
    2.60007787671e-18,
    0.0172901503422,
    0.0472883481821,
    0.0864914386425,
    0.126465151635,
    0.156489628279,
    0.167650028687,
    0.156489628279,
    0.126465151635,
    0.0864914386425,
    0.0472883481821,
    0.0172901503422,
    2.60007787671e-18,
    -0.00616633506547,
    -0.0058179856702,
    -0.00364708336518,
    -0.0022183273232,
]);

fn main() {
    let mut filt = FirFilter::<LowpassFir>::new();

    // Run filter over a couple samples.
    assert_eq!(filt.feed(1.0), -0.0022183273232);
    assert_eq!(filt.feed(2.0), -0.008083738011580001);

    // Pad out rest of history.
    for _ in 0..19 {
        filt.feed(0.0);
    }

    // Iterate over history in order.
    let mut hist = filt.history();
    assert_eq!(hist.next().unwrap(), &1.0);
    assert_eq!(hist.next().unwrap(), &2.0);
    assert_eq!(hist.next().unwrap(), &0.0);

    // Compute energy of stored samples.
    assert_eq!(filt.history_unordered().fold(0.0, |s, x| {
        s + x.powi(2)
    }), 5.0);
}

Macros

impl_fir

Implement FirCoefs for a history buffer with the given name, input sample type, storage size, and sequence of coefficients.

Structs

FirFilter

A FIR filter for convolving with a series of samples.

Traits

FirCoefs

Provides a sequence of coefficients and storage for sample history.