This is a digital implementation of an analogue RC filter.
We can infer that the differential equation for this system is:
Discretising using euler's method:
We obtain:
This is calculated every time the .update()
method is called, and is very computationally inexpensive.
The RC filter is constructed with:
RCFilter acc_x(8); // Cutoff frequency of 8Hz
The filtered value is obtained by calling the .update()
method: (need an accurate timestamp in microseconds (us) )
double x = sensor.read();
unsigned long t = micros();
float out = acc_x.update(x, t);
The filter successfully filters out noise (accelerometer data shown below, Raw (Red) and Filtered (Green) ):
Be careful when choosing a cutoff frequency, if it's too low, the response lag's behind: