2 unstable releases
Uses old Rust 2015
0.2.0 | Jan 1, 2018 |
---|---|
0.1.0 | Jan 1, 2018 |
#26 in #zero-allocation
10KB
155 lines
static_fir – FIR filter with static coefficients
This crate provides utilities for applying zero-allocation FIR filters with compile-time tap coefficients.
Usage
This crate can be used through cargo by
adding it as a dependency in Cargo.toml
:
[dependencies]
static_fir = "0.1.0"
and importing it in the crate root:
extern crate static_fir;
lib.rs
:
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);
}