- Notifications
You must be signed in to change notification settings - Fork10
An Arduino finite impulse response and infinite impulse response filter library.
License
tttapa/Filters
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
An Arduino finite impulse response and infinite impulse response filter library.
I created a new repository with an updated and improved version of this library.
You can find it here:Arduino-Filters.
This library implements digitalfinite impulse response filters (FIR)andinfinite impulse response filters (IIR).Double precision floating point arithmetic is used.
The difference equation for FIR filters is given by
To initialize it in code, you can use:
constdouble b_coefficients[] = { b_0, b_1, b_2, ... , b_N };FIRFilterfir(b_coefficients);
The difference equation for IIR filters is given by
constdouble b_coefficients[] = { b_0, b_1, b_2, ... , b_P };constdouble a_coefficients[] = { a_0, a_1, a_2, ... , a_Q };IIRFilteriir(b_coefficients, a_coefficients);
When dealing with high-order IIR filters, they can get unstable.
To prevent this, BiQuadratic filters (second order) are used.
Both Direct Form 1 and 2 are implemented.
constdouble b_coefficients[] = { b_0, b_1, b_2 };constdouble a_coefficients[] = { a_0, a_1, a_2 };BiQuadFilterDF1biquad1(b_coefficients, a_coefficients);BiQuadFilterDF2biquad2(b_coefficients, a_coefficients);
Optionally, you can specify a gain:
constdouble b_coefficients[] = { b_0, b_1, b_2 };constdouble a_coefficients[] = { a_0, a_1, a_2 };constdouble gain =1;BiQuadFilterDF1biquad1(b_coefficients, a_coefficients, gain);BiQuadFilterDF2biquad2(b_coefficients, a_coefficients, gain);
Instead of manually cascading BiQuad filters, you can use a Second Order Sections filter (SOS).
constdouble sosmatrix[][6] = { {b1_0, b1_1, b1_2, a1_0, a1_1, a1_2 }, {b2_0, b2_1, b2_2, a2_0, a2_1, a2_2 }};constdouble gainarray[] = {1,1};SOSFilter<2>filter(sosmatrix, gainarray);
Here,<2>
is the number of BiQuad filters in the SOS filter.
You can apply multiple filters together in a cascade:
Cascade<N>cascade({&filter1, &filter2, ..., &filterN});
The library provides aFilter
interface with afilter
method that takes the new (raw) value as an intput, and outputs the new (filtered) value.
double raw_value = getRawValue();double filtered_value = fir.filter(raw_value);
You can use multiple filters on the input.
double filtered_value = fir.filter(iir.filter(raw_value));