Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement a LowPass Filter?

I'm doing some math on both gyroscope and accelerometer data combined and I'd like to low pass filter the resulting data. So could someone post some generic code for a Low Pass filter?

like image 943
Christian Gossain Avatar asked Nov 24 '10 22:11

Christian Gossain


2 Answers

A 1st order IIR low-pass filter can be of the form:

output_value = rate * input_value + (1.0 - rate) * previous_output_value;

which is pretty much what's inside Apple's AccelerometerGraph example. You select the rate parameter depending on what frequency (very very roughly shakes per second) you want to roll-off or start to attenuate to get a smoother resulting output, and the sample rate of the input data.

like image 173
hotpaw2 Avatar answered Oct 17 '22 01:10

hotpaw2


A low pass filter is simply smoothing of the results to remove the high frequencies. The simplest low pass filter is a box filter which is done by averaging n samples together.

For averaging 2 samples together this is as simple as doing:

sample[n] (sample[n] + sample[n + 1]) / 2;
like image 39
Goz Avatar answered Oct 17 '22 00:10

Goz