I want to filter the accelerometer values using a moving average, how is this done? Thanks
1 Answers
A simple, single pole, low pass, recursive IIR filter is quick and easy to implement, e.g.
xf = k * xf + (1.0 - k) * x;
yf = k * yf + (1.0 - k) * y;
where x, y are the raw (unfiltered) X/Y accelerometer signals, xf, yf are the filtered output signals, and k determines the time constant of the filters (typically a value between 0.9 and 0.9999..., where a bigger k means a longer time constant).
You can determine k
empirically, or if you know your required cut-off frequency, Fc
, then you can use the formula:
k = 1 - exp(-2.0 * PI * Fc / Fs)
where Fs
is the sample rate.
Note that xf, yf are the previous values of the output signal on the RHS, and the new output values on the LHS of the expression above.
Note also that we are assuming here that you will be sampling the accelerometer signals at regular time intervals, e.g. every 10 ms. The time constant will be a function both of k and of this sampling interval.

- 208,748
- 37
- 389
- 560
-
so x and y are the accelerometer.x and accelerometer.y, I am confused, i dont no what 'raw input signal' is. :/ – DotSlashSlash Feb 16 '10 at 11:45
-
1No, y is the filtered signal, x is what the accelerometer gave. It would be clearer to write that `xf = k*xf + (1.0-k)*x` where xf is the filtered version of x, and a similar equation for the y axis. – Andrew McGregor Feb 16 '10 at 11:48
-
@Andrew - thanks for the suggestion - now edited for improved clarity (I hope !). – Paul R Feb 16 '10 at 12:10
-
@PaulR Ok, so we smooth the accelerometer data, which value should we react to? – gran33 Aug 31 '14 at 10:31
-
@gran33: what do you mean by "react to" ? – Paul R Aug 31 '14 at 15:35
-
@PaulR I want just right/left/up/down user movements, how should I react means starting from which friction point should I can determined that the user really move to the direction? – gran33 Aug 31 '14 at 20:06
-
@gran33: OK - that really doesn't have anything to do with this question, which is about filtering the accelerometer signal. You should start a new question, with details of what you are trying to achieve etc. – Paul R Aug 31 '14 at 20:32
-
@PaulR already started :), PLZ check this out: http://stackoverflow.com/questions/25591310/accelerometer-low-pass-filter-smoothing – gran33 Aug 31 '14 at 20:33
-
1You can simply take the difference between two sampled accelerations. And @PaulR this works wonderfully ! Thank you :) – cyc115 Jul 29 '15 at 15:51
-
how is k dependent on sampling rate . I mean can you tell the relation like if the sampling is rate is 30ms would the k change accordingly? Thanks for the good answer. – Nikita Chopra Mar 21 '16 at 05:51
-
`k` determines the nominal cut-off frequency, but it is a normalised dimensionless quantity. So if you choose a value of k which gives a cut-off at 0.1 * Fs, then that is independent of sample rate, and it will be 4.41 kHz if Fs is 44100 Hz, or 4.8 kHz if Fs is 48 kHz, etc. Normally you will just choose a value of `k` empirically, but there is a formula which you can use if you already know you need a specific cut-off frequency - see edit above. – Paul R Mar 21 '16 at 07:18