I have a piece of raw accelerometer data consisted of 10,000 samples to be processed offline. Since the movement or rotation of the object is moderately slow and smooth, I would think that I can apply a strong filter to filter out as much noise as I could, and if necessary, I can re-apply the same filter to the already filtered data to further smoothen the curve.

I tested out a moving average filter by first filtering the raw data before re-filtering the output again (with n=100). As expected, the first filtering removes a significant amount of noise; the second filtering also smoothens the curve further. With MAF, I loose tracking performance so I want to check if a low-pass filter can achieve the same level of noise removal with lesser time delay.

My moving average filter:

% Moving average filter
function avg = MAF_IMU(Nsamples,n,x)

xbuf = x(1)*ones(n,1);
avg  = zeros(Nsamples,1);

for k=1:Nsamples
        xbuf = x(1)*ones(n,1);

    for m=1:n-1

    avg(k) = sum(xbuf)/n;

I did the same with a LPF. I also applied my $ \alpha $ to be as large as 0.9. A large $ \alpha $ should supposedly put less weight on newer data resulting in a longer time delay in exchange for the better noise filtering. But the result from my LPF is far off from that out of a simple MAF: the first LPF filtering removes so much less noise than that of a MAF. The second LPF filtering barely smoothens the curve, unlike that of a MAF. If I haven't zoomed in far enough, I would notice no difference between the first and second LPF filtering.

My low-pass filter:

% Low-pass filter
function [x_est] = LFP_IMU(ALPHA,Nsamples,x)

x_est = zeros(Nsamples,1);  % initialize estimate
prev_x_est = x(1);          % initialize previous estimate

for k=1:Nsamples
        prev_x_est = x(1);
    x_est(k) = ALPHA*prev_x_est + (1-ALPHA)*x(k);   % saved to matrix
    prev_x_est = x_est(k);                          % saved for next iteration

A graphical comparison: enter image description here

Why couldn't I remove as much noise in a LPF even with a large $ \alpha $? And why couldn't I re-apply the LPF to reduce the noise further?


2 Answers 2



Your "LPF" (as Tim mentioned in his answer, a moving average is also a Low Pass Filter) isn't nearly as strong as you think it is.

To match your Moving Average response, you need $\alpha \approx 0.98$:

enter image description here

A Couple notes

  • Since you're working offline, I would suggest zero-phase filtering using Matlab's filtfilt function. On top of preserving phase, it also doubles the attenuation since it filters the data twice.

  • If you want to use your own filtering implementation as in your question, go with the Moving Average, which is a linear-phase filter, because it will preserve your input signal's phase.

  • If you need even more attenuation, use a higher order LPF. See, for example, Butterworth filter design. You'll need to provide the cut-off frequency, which brings me to:

  • You haven't specified anything with regards to which frequencies you're interested in keeping / attenuating, attenuation, your signal bandwidth, etc. In general you want to design filters based on some specifications that would achieve what you want.

Sample code for both filters

% Moving average
b = 1/100 * ones(1,100);
out = filtfilt(b,1,in); % or out = filter(b,1,in);

alpha = 0.98;
b = 1-alpha;
a = [1 -alpha];
out = filtfilt(b,a,in); % or out = filter(b,a,in) but will distort phase
  • $\begingroup$ @KMC Have I answered your question? $\endgroup$
    – Jdip
    Dec 28, 2023 at 22:36

A moving-average filter is a low-pass filter, with it's own advantages and disadvantages in comparison to other low-pass filters. So it's not either/or -- it's just one kind vs. another.

For any situation, there's an optimal tradeoff between reducing noise and reducing tracking performance. In those cases where a linear filter can be optimum, there is one specific filter which is best.

So does the particular filter that you chose work better if you run it twice? Only if it wasn't optimal in the first place.

So, you have a couple of choices: experiment with different filters to see which one looks about right, or thoroughly model your system and find the optimal filter. Even if you do have the knowledge to do the latter, take it from me that practitioner will often throw their data through an educated guess first.

You don't define what sort of noise you have, or what sort of expected motion. If you can express your motion in frequency-domain terms that gives you a rough (or perfect) idea of the bandwidth, then the optimal filter (do a web search on "Wiener filter") will be a (probably) low-pass filter with (definitely) a somewhat, but not significantly, higher bandwidth than the bandwidth of your data.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.