| Crates.io | flaw |
| lib.rs | flaw |
| version | 0.3.0 |
| created_at | 2024-04-07 12:49:29.138753+00 |
| updated_at | 2025-09-01 17:03:57.314264+00 |
| description | Embedded signal filtering, no-std and no-alloc compatible. |
| homepage | https://github.com/jlogan03/flaw/flaw |
| repository | https://github.com/jlogan03/flaw/ |
| max_upload_size | |
| id | 1199120 |
| size | 186,839 |
Embedded signal filtering, no-std and no-alloc compatible.
This library provides a simple method for initializing and updating single-input, single-output infinite-impulse-response filters using 32-bit floats, as well as tabulated filter coefficients for some common filters. Filters evaluate in 4N-1 floating-point operations for a filter of order N.
The name flaw is short for filter-law, but also refers to the fact that
digital IIR filtering with small floating-point types is an inherently flawed
approach, in that higher-order and lower-cutoff filters produce very small
coefficients that result in floating-point roundoff error. This library mitigates
that problem by providing filter coefficients for a tested
domain of validity. The result is a limited, but useful, range of operation
where these filters can achieve both accuracy and performance as well
as be formulated and initialized in an embedded environment.
// First, choose a cutoff frequency as a fraction of sampling frequency
let cutoff_ratio = 1e-3;
// Construct a filter, interpolating coefficients to that cutoff ratio.
// Initializes internal state to zero by default.
let mut filter = flaw::butter2(cutoff_ratio).unwrap(); // Errors if extrapolating
// Initialize the internal state of the filter
// to match the steady-state associated with some input value.
let initial_steady_measurement = 1.57; // Some number
filter.initialize(initial_steady_measurement);
// Update the filter with a new raw measurement
let measurement = 0.3145; // Some number
let estimate = filter.update(measurement); // Latest state estimate
Tabulated filters are tested to enforce
Each filter with tabulated coefficients has a minimum and maximum cutoff ratio. The minimum value is determined by floating-point error in convergence of a step response, while the maximum value is determined by the accuracy of attenuation at the cutoff frequency as the cutoff ratio approaches the Nyquist frequency.
Coefficients for a given filter are interpolated on these tables using a cubic Hermite method with the log10(cutoff_ratio) as the independent variable. Tabulated values are stored and interpolated as 64-bit floats, and only converted to 32-bit floats at the final stage of calculation.
After interpolation, the state-space measurement coefficient vector (C) is scaled
to correct steady-state gain for interpolation error, targeting unity gain.
Filter coefficients are extracted from scipy's state-space representations, which are the result of a bilinear transform of the transfer function polynomials.
| Filter | Min. Cutoff Ratio | Max. Cutoff Ratio |
|---|---|---|
| Butter1 | 10^-4 | 0.4 |
| Butter2 | 10^-3 | 0.4 |
| Butter3 | 10^-2 | 0.4 |
| Butter4 | 10^-1.5 (~0.032) | 0.4 |
| Butter5 | 10^-1.25 (~0.056) | 0.4 |
| Butter6 | 0.1 | 0.4 |
Licensed under either of
at your option.