# A basic question about the use of moving average vs low-pass filters in DSP

I'm newbie novice in digital signal processing. If one has a signal with some noise superimposed in time series, for which type of information/analysis one would use moving average or LP filter in practice.

Imagine you have a 5 seconds of sampled data in time where the sampling rate is 200Hz.

Can you give a practical example when would you use LP filter and when would you use a moving average filter? I read that moving average is a type of LP filter but I would like to see the differences in big picture.

I see many engineers use MATLAB or Python functions and perform Low Pass filter using FFT but not moving average. So this made me to ask this question.

A moving average filter can be thought of as a type of low-pass filter that doesn't have any control over its bandwidth for a fixed number of taps.

For a finite impulse response (FIR) filter, the output signal $y[n]$ is given in terms of the input signal $x[n]$ and the filter taps $h[n]$: $$y[k] = \sum_{n=0}^{N-1}h[n]x[k-n].$$ The filter length in this case is $N$ "taps" (or $N$ coefficients). A moving average filter has coefficients that are all equal: $$h[n] = \frac{1}{N}, \qquad n = 0, 1, \ldots, N-1,$$ whereas in general, a low-pass filter (LPF), can have different values for each tap. This allows you to control the frequency selectivity of the filter.

Here is an example showing the difference, for a system with a sample rate of 200 Hz and a filter with 32 taps:

The LPF was designed to have a cutoff frequency of 50 Hz, but it can be set to any value you want. With a moving average filter the filter is narrowly focused around the 0 Hz component ("DC"), and the peak gets narrower the more taps you have in the filter. Another problem with using a moving average filter as an LPF is that it has high sidelobes (the ripples to either side of the main peak) compared to a "properly designed" filter.

The taps for the LPF are:

[-0.00116417 -0.00139094  0.00195951  0.00293134 -0.00437535 -0.00637313
0.00902803  0.01248116 -0.0169409  -0.02273977  0.03045372  0.04118039
-0.05729369 -0.08500841  0.14720004  0.45005217  0.45005217  0.14720004
-0.08500841 -0.05729369  0.04118039  0.03045372 -0.02273977 -0.0169409
0.01248116  0.00902803 -0.00637313 -0.00437535  0.00293134  0.00195951
-0.00139094 -0.00116417]


Python code for generating this figure:

from pylab import *
from scipy.signal import windows, firwin

Fs = 200.

N = 32
h_lpf = firwin(N, 50, nyq=Fs/2., window='hamming')
h_ma = ones(N)*1./N

print h_lpf

M = 512

X_lpf = fftshift(abs(fft(h_lpf, M)))
X_lpf /= X_lpf.max()
X_ma = fftshift(abs(fft(h_ma, M)))
X_ma /= X_ma.max()

f = arange(M)*Fs/M - Fs/2.

figure()
plot(f, X_lpf, f, X_ma)
xlabel('$f$ (Hz)')
ylabel('$|H(f)|$')
legend(('LPF', 'Moving Average'))
grid()
show()

• i dunno that the moving average or "sliding average" (i presume of the 32 most current samples) can be compared directly to that LPF with cutoff frequency set to half Nyquist. most of the time, with a LPF that is supposed to implement some rendition of a moving average (any LPF with DC gain = 1 is such a filter) there aren't negative coefficients. May 14, 2018 at 6:15
• I agree with @robertbristow-johnson . What's more, you can use a bare windowing function as a weighted moving average and it will perform better than the basic 1/N, even closer to a lowpass. This is why I think a moving average is usually meant for DC, or close. But, sure, the lack of control over the frequency response is the main difference. May 14, 2018 at 6:23

There are two basic categories of Linear Time-Invariant (LTI) filters. Some filters are not linear and/or time-invariant (e.g. median filters or Kalman filters or Particle filters) but of those that are LTI (and discrete-time or "digital") there are Finite Impulse Response (FIR) and Infinite Impulse Response (IIR) filters and either can be low pass.

As you implied, filters are much more general and a moving average is a specific case of a FIR filter where the coefficients are the same value $1/N$ for a length $N$ moving average. This is typically called a box car filter.

As one of the other posts mentioned, the term MA is often associated with modeling things like sun spots.

You mentioned the FFT but that is just a way to do FIR convolution efficiency. The FFT comes in handy for other things as well, like multiplying large integers and with Toeplitz matrices. I’m not going to explain convolution because there are many explanations available I tend to be “convoluted” when I try, but if you want to understand filters doing so without having a handle on convolution is futile. (I don't know why they named it convolution. Most things that are convoluted are at best like matrimony. It's really just superposition)

https://en.m.wikipedia.org/wiki/Convolution

Essentially, a moving average or box car is something that smooths but you use it if you’re goal is a literal “moving average”. In other words a moving average will smooth but that is kind of side effect.