This crate contains a collection of filters and utilities for digital signal processing.
This crate is no_std, no alloc, no panic, no unwrap, no expect, and the Minimum Supported Rust Version (MSRV) is Rust 1.89.
This crate contains the following filters:
Pt1Filter, Pt2Filter, and Pt3Filter : basic first, second, and third order low-pass filters.
BiquadFilter: second order filter that can be used as a low-pass, high-pass, or notch filter.
MedianFilter3 and MedianFilter5: median filters for spike rejection.
SlewRateLimiter: filter that limits the rate of change of a signal.
MovingAverageFilter : simple moving average filter.
The filters have aliases for f32 and f64 versions:
f32 |
f64 |
|---|---|
Pt1Filterf32 |
Pt1Filterf64 |
Pt2Filterf32 |
Pt2Filterf64 |
Pt3Filterf32 |
Pt3Filterf64 |
BiquadFilterf32 |
BiquadFilterf64 |
MedianFilter3f32 |
MedianFilter3f64 |
MedianFilter5f32 |
MedianFilter5f64 |
SlewRateLimiterf32 |
SlewRateLimiterf64 |
MovingAverageFilterf32 |
MovingAverageFilterf64 |
Additionally the all the filters except the Median Filters and Slew Rate Limiters have aliases for their vectorized forms for both f32 and f64 vectors.
So for Pt1Filter we have:
vector f32 |
vector f64 |
|---|---|
Pt1FilterVector2f32 |
Pt1FilterVector2f64 |
Pt1FilterVector3f32 |
Pt1FilterVector3f64 |
Pt1FilterVector4f32 |
Pt1FilterVector4f64 |
and similarly for the other filters.
This crate also has basic circular and rolling buffers:
CircularBuffer<T, const N: usize> and RollingBuffer<T, const N: usize>.
A rolling buffer is similar to a circular buffer, except, once full, old items fall off the front when new items are added.
These filters have been developed for use in stabilized vehicles (self balancing robots and aircraft)and have been used to:
- Filter gyro and accelerometer output for use in the Attitude and Heading Reference System (AHRS).
- Filter motor encoder values for use in the motor controller.
- Filter derivative terms in a PID controller.
- Filter motor power input values to smooth the motor speed.
use signal_filters::Pt1Filterf32;
use signal_filters::Pt1FilterVector3f32;
use signal_filters::BiquadFilterf32;
use signal_filters::BiquadFilterVector3f32;
use signal_filters::SignalFilter;
use vqm::Vector3f32;
//
// Pt1 low pass filter.
//
let mut filter = Pt1Filterf32::new();
let sample_interval_s: f32 = 0.001; // 1 kHz sampling rate
filter.set_cutoff_frequency(100.0, sample_interval_s);
let input: f32 = 2.7;
let output = filter.update(input);
//
// Pt1 low pass filter with vector input.
//
let mut filter = Pt1FilterVector3f32::new();
filter.set_cutoff_frequency(80.0, sample_interval_s);
let gyro = Vector3f32 { x: 0.2, y: 0.5, z: 1.5 };
let output = filter.update(gyro);
//
// Biquad filter used as low-pass filter.
//
let sample_interval_s: f32 = 0.001; // 1 kHz sampling rate
let q_factor: f32 = 2.0;
let cutoff_frequency_hz: f32 = 80.0;
let mut notch_filter = BiquadFilterf32::new().with_q(q_factor).with_sample_interval(sample_interval_s);
notch_filter.set_low_pass_frequency_assuming_q(cutoff_frequency_hz);
let input: f32 = 0.8;
let output = notch_filter.update(input);
//
// Biquad filter used as notch filter.
//
let notch_frequency_hz: f32 = 50.0; // 50 Hz power line hum filter
let q_factor: f32 = 10.0; // Narrow notch width
let mut notch_filter = BiquadFilterf32::new().with_q(q_factor).with_sample_interval(sample_interval_s);
notch_filter.set_notch_frequency_assuming_q(notch_frequency_hz);
let input: f32 = 0.8;
let output = notch_filter.update(input);
//
// Biquad notch filter with vector input.
//
let mut notch_filter = BiquadFilterVector3f32::new().with_q(q_factor).with_sample_interval(sample_interval_s);
notch_filter.set_notch_frequency_assuming_q(notch_frequency_hz);
let gyro = Vector3f32 { x: 0.8, y: 2.1, z: -0.2 };
let output = notch_filter.update(gyro);I originally implemented this crate as a C++ library: Library-Filter.
Licensed under either of:
- Apache License, Version 2.0, see: LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0
- MIT license, see: LICENSE-MIT or http://opensource.org/licenses/MIT
at your option.