18
$\begingroup$

I need to design a moving average filter that has a cut-off frequency of 7.8 Hz. I have used moving average filters before, but as far as I'm aware, the only parameter that can be fed in is the number of points to be averaged... How can this relate to a cut-off frequency?

The inverse of 7.8 Hz is ~130 ms, and I'm working with data that are sampled at 1000 Hz. Does this imply that I ought to be using a moving average filter window size of 130 samples, or is there something else that I'm missing here?

$\endgroup$
  • $\begingroup$ You should first define your understanding of "cut-off". If it is the last frequency above(below) which the response of a filter is zero, then the answer would be "none", since the kernel of a moving average filter has a finite support, and finite wavelets transform to infinite fourier images. $\endgroup$ – mbaitoff Feb 26 '14 at 17:26
  • $\begingroup$ The moving average filter is the filter used in the time domain to remove the noise added and also for smoothing purpose but if you use the same moving average filter in the frequency domain for frequency separation then performance will be worst...... so in that case use frequency domain filters $\endgroup$ – user19373 Feb 3 '16 at 5:53
27
$\begingroup$

The moving average filter (sometimes known colloquially as a boxcar filter) has a rectangular impulse response:

$$ h[n] = \frac{1}{N}\sum_{k=0}^{N-1} \delta[n-k] $$

Or, stated differently:

$$ h[n] = \begin{cases} \frac{1}{N}, && 0 \le n < N \\ 0, && \text{otherwise} \end{cases} $$

Remembering that a discrete-time system's frequency response is equal to the discrete-time Fourier transform of its impulse response, we can calculate it as follows:

$$ \begin{align} H(\omega) &= \sum_{n=-\infty}^{\infty} x[n] e^{-j\omega n} \\ &= \frac{1}{N}\sum_{n=0}^{N-1} e^{-j\omega n} \end{align} $$

To simplify this, we can use the known formula for the sum of the first $N$ terms of a geometric series:

$$ \sum_{n=0}^{N-1} e^{-j\omega n} = \frac{1-e^{-j \omega N}}{1 - e^{-j\omega}} $$

What we're most interested in for your case is the magnitude response of the filter, $|H(\omega)|$. Using a couple simple manipulations, we can get that in an easier-to-comprehend form:

$$ \begin{align} H(\omega) &= \frac{1}{N}\sum_{n=0}^{N-1} e^{-j\omega n} \\ &= \frac{1}{N} \frac{1-e^{-j \omega N}}{1 - e^{-j\omega}} \\ &= \frac{1}{N} \frac{e^{-j \omega N/2}}{e^{-j \omega/2}} \frac{e^{j\omega N/2} - e^{-j\omega N/2}}{e^{j\omega /2} - e^{-j\omega /2}} \end{align} $$

This may not look any easier to understand. However, due to Euler's identity, recall that:

$$ \sin(\omega) = \frac{e^{j\omega} - e^{-j\omega}}{j2} $$

Therefore, we can write the above as:

$$ \begin{align} H(\omega) &= \frac{1}{N} \frac{e^{-j \omega N/2}}{e^{-j \omega/2}} \frac{j2 \sin\left(\frac{\omega N}{2}\right)}{j2 \sin\left(\frac{\omega}{2}\right)} \\ &= \frac{1}{N} \frac{e^{-j \omega N/2}}{e^{-j \omega/2}} \frac{\sin\left(\frac{\omega N}{2}\right)}{\sin\left(\frac{\omega}{2}\right)} \end{align} $$

As I stated before, what you're really concerned about is the magnitude of the frequency response. So, we can take the magnitude of the above to simplify it further:

$$ |H(\omega)| = \frac{1}{N} \left|\frac{\sin\left(\frac{\omega N}{2}\right)}{\sin\left(\frac{\omega}{2}\right)}\right| $$

Note: We are able to drop the exponential terms out because they don't affect the magnitude of the result; $|e^{j\omega}| = 1$ for all values of $\omega$. Since $|xy| = |x||y|$ for any two finite complex numbers $x$ and $y$, we can conclude that the presence of the exponential terms don't affect the overall magnitude response (instead, they affect the system's phase response).

The resulting function inside the magnitude brackets is a form of a Dirichlet kernel. It is sometimes called a periodic sinc function, because it resembles the sinc function somewhat in appearance, but is periodic instead.

Anyway, since the definition of cutoff frequency is somewhat underspecified (-3 dB point? -6 dB point? first sidelobe null?), you can use the above equation to solve for whatever you need. Specifically, you can do the following:

  1. Set $|H(\omega)|$ to the value corresponding to the filter response that you want at the cutoff frequency.

  2. Set $\omega$ equal to the cutoff frequency. To map a continuous-time frequency to the discrete-time domain, remember that $\omega = 2\pi \frac{f}{f_s}$, where $f_s$ is your sample rate.

  3. Find the value of $N$ that gives you the best agreement between the left and right hand sides of the equation. That should be the length of your moving average.

$\endgroup$
  • $\begingroup$ By my reckoning, that's a 'yes'? As far as I can tell, 130 samples seems to fit N with ω = 7.8, but I'm no mathematician. $\endgroup$ – CaptainProg Jul 18 '13 at 15:41
  • $\begingroup$ @CaptainProg: Only you can say for sure; I'm not sure what you wanted the magnitude response to be at the cutoff frequency. $\endgroup$ – Jason R Jul 19 '13 at 2:53
  • 1
    $\begingroup$ Could you define what n and N are? An example with a given sampling frequency would also be very helpful. This may sound simple, but this question is the top result for "moving average cutoff frequency", so I am sure there will be many other viewers who have fallen out of touch with the math behind filters. $\endgroup$ – FvD Jun 8 '17 at 6:34
  • $\begingroup$ @FvD $n$ is the sample index for the signal $x[n]$, as is typically used for discrete-time signals. $N$ is defined in the first equation above. If I get a chance I can add an example, but I suspect that anyone who is choosing to design a filter to meet a particular cutoff frequency can follow the math. $\endgroup$ – Jason R Jun 8 '17 at 11:17
10
$\begingroup$

If $N$ is the length of the moving average, then an approximate cut-off frequency $F_{co}$ (valid for $N >= 2$) in normalized frequency $F=f/fs$ is:

$F_{co} = \frac {0.442947} {\sqrt{N^2-1}}$

The inverse of this is

$N = \frac {\sqrt{0.196202 + F_{co}^2}}{F_{co}}$

This formula is asymptotically correct for large N, and has about 2% error for N=2, and less than 0.5% for N>=4.

P.S.: After two years, here finally what was the approach followed. The result was based on approximating the MA amplitude spectrum around $f=0$ as a parabola (2nd order Series) according to

$MA(\Omega) = \frac {Sin(\Omega∗N/2)}{Sin(\Omega/2)}$

$MA(\Omega) \approx 1+(\frac{1}{24}-\frac{N^2}{24})\Omega^2$

which can be made more exact near the zero crossing of $MA(\Omega)-\frac{\sqrt2}{2}$ by multiplying $\Omega$ by a coefficient

$\alpha=0.95264$

obtaining $MA(\Omega) \approx 1+0.907523(\frac{1}{24}-\frac{N^2}{24})\Omega^2$

The solution of $MA(\Omega)-\frac{\sqrt2}{2}=0$ gives the results above, where $2\pi F_{co}=\Omega_{co}$.

All of the above relates to the -3dB cut off frequency, the subject of this post.

Sometimes though it is interesting to obtain an attenuation profile in stop-band which is comparable with that of a 1st order IIR Low Pass Filter (single pole LPF) with a given -3dB cut off frequency (such a LPF is also called leaky integrator, having a pole not exactly at DC but near to it).

relations between a MA filter (FIR, N-1 zeros) and a 1-pole IIR LPF

In fact both the MA and the 1st order IIR LPF have -20dB/decade slope in the stop band (one needs a larger N than the one used in the figure, N=32, to see this), but whereas MA has spectral nulls at $F=k/N$ and a $1/f$ evelope, the IIR filter only has a $1/f$ profile.

$H_{IIR} = \frac{1-Exp(-\Omega_{co})}{1-Exp(-\Omega_{co})*Exp(j \Omega)}$

If one wants to obtain an MA filter with similar noise filtering capabilities as this IIR filter, and matches the 3dB cut off frequencies to be the same, upon comparing the two spectra, he would realize that the stop band ripple of the MA filter ends up ~3dB below that of the IIR filter.

In order to get the same stop-band ripple (i.e. same noise power attenuation) as the IIR filter the formulas can be modified as follows:

$F_{co,IIR} = \frac {0.32} {\sqrt{N^2-1}}$

$N = \frac {\sqrt{0.1024 + F_{co,IIR}^2}}{F_{co,IIR}}$

$\endgroup$
  • $\begingroup$ I changed your formula to the latex format. Please double check and confirm both of them are correct. Thanks. $\endgroup$ – lennon310 Feb 25 '14 at 1:22
  • $\begingroup$ I added a derivation of this approximation here dsp.stackexchange.com/a/28186/15347 $\endgroup$ – Olli Niemitalo Jan 10 '16 at 2:01
  • 2
    $\begingroup$ As far as i remember i derived this formula with pragmatic concerns in mind, by means of numerical methods (either NSolve in Mathematica or something similar in Matlab), which should be asymptotically correct for large N. The number you gave is about 3% off, so i am not sure what to say. $\endgroup$ – Massimo Jan 14 '16 at 9:31
  • 1
    $\begingroup$ @Massimo we did a lot of work on this and other approximations in the other question. If you ever need more decimal places this is your magic number: 0.442946470689452340308369 $\endgroup$ – Olli Niemitalo Jan 15 '16 at 23:03
  • 1
    $\begingroup$ I found back the Mathematica script where i calculated the cut off for several filters, including the MA one. The result was based on approximating the MA spectrum around f=0 as a parabola according to $MA(\Omega) = Sin(\Omega*N/2) / Sin(\Omega/2)$ ; $Omega = 2*\pi*F$; $MA(F) \approx N+1/6*F^2*(N-N^3)*\pi^2$ . And deriving the crossing with $1/\sqrt{2}$ from there. $\endgroup$ – Massimo Jan 17 '16 at 2:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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