So, the other day I needed to compute some low-pass filters on the fly on an Arduino Mega. By “on the fly” I mean that the filters’ parameters would eventually be recomputed mid-operation, so setting some equation with static const parameters would not cut it. Using a static filter, is, however, the most common application scenario. If that’s your case – and I insist – tweak the sh*t out of your filter with a decent tool then just add it to your code. If, on the other hand, you need to update your filter dynamically (or if you’re just plain lazy to compute poles and zeros), then this is for you.
I ended up putting together a minimalist library for that, libFilter (github!). Works with the Arduino IDE and as a regular C++ library (just uncommenting one #define does the trick).
For now it only implements low-pass filters based on normalized butterworth polynomials, but who knows what necessity might add to it next. Let’s take a look at some ADC filtering:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
#include <filters.h> const float cutoff_freq = 20.0; //Cutoff frequency in Hz const float sampling_time = 0.005; //Sampling time in seconds. IIR::ORDER order = IIR::ORDER::OD3; // Order (OD1 to OD4) // Low-pass filter Filter f(cutoff_freq, sampling_time, order); void setup() { Serial.begin(115200); pinMode(A0, INPUT); // Enable pull-ups if necessary digitalWrite(A0, HIGH); } void loop() { int value = analogRead(0); float filteredval = f.filterIn(value); //View with Serial Plotter Serial.print(value, DEC); Serial.print(","); Serial.println(filteredval, 4); delay(5); // Loop time will approx. match the sampling time. } |
’til next time!
P.S.: I just can’t avoid letting this page eventually fall into temporary ostracism. Geez.