# How to select the length of a smoothing moving average filter?

I have a time series signal I need to smoothen to get rid of some noise.

I decided to apply a moving average filter to this signal. I know the choice of the length of the MA is critical.

How should I choose the length?

• What kind of spectra do your signal and the noise have? – Olli Niemitalo Feb 12 '16 at 11:11
• The signal is a person's respiratory wave (breathing frequency ranges from 0.1 Hz to 0.7 Hz ; spectrum main frequencies <= 1 or 2 Hz ). Noise seems to range from 0 to 150 Hz. – Clément F Feb 12 '16 at 14:41

The magnitude frequency response $|Y(\omega)|$ of moving average of length $N$ can be approximated by:
$$|Y(\omega)| \approx |\hat Y(\omega)| = \left|\frac{2\sin(\frac{N\omega}{2})}{N\omega}\right|$$
Figure 1. Horizontal axis: $N\omega$, vertical axis: $|\hat Y(\omega)|$.
The frequency $\omega$ is in radians, meaning that $\omega=2\pi$ corresponds to the sampling frequency. You should choose $N$ such that the noise is attenuated as much as possible while keeping a flat enough frequency response over the signal's bandwidth so that the signal's waveform is not distorted too much. What is flat enough depends on what you are going to do with the signal. A good first bet is to have the -3 dB cutoff frequency at the upper bandlimit of your signal so that no frequency of the signal is attenuated by more than 3 dB. The -3 dB cutoff frequency $\omega_c$ of moving average can be approximated by:
$$\omega_c \approx \hat\omega_c = \frac{2.783}{N}$$