# Moving average filter magnitude attenuation

I am dealing with a moving average filter which filters power system signals to rid them of noise or harmonics etc. The fundamental or useful component of these signals is centered around 50 Hz or 60 Hz. The magnitude response of the filter at an angular frequency say as per some standard papers is given as $$|H(e^{(j\omega_1)})|=|\frac{\sin(L_m \omega_1 /2)}{L_m \sin(\omega_1/2)}|$$ , where $$L_m$$ is the window length of the filter and the value of $$\omega_1=\frac{2\pi f_f}{F_s L_m}$$ . Sampling frequency is given by $$F_s$$ whereas the input signal frequency to the filter is given by $$f_f$$ . The idea is when the filter input signal frequency is equal to the fundamental (no noise or harmonics etc) or even close to it (say 59.5 Hz or 59.7 Hz considering a 60 Hz system) the magnitude response (gain) of the above mentioned filter should be equal to one or close to it for any sensibly chosen value of the window length and the sampling frequency. For example if i choose a value of $$L_m$$=31 and and consider the input signal frequency $$f_f$$ to be 59 Hz, with a sampling frequency $$F_s$$ of say 1440 Hz, i end of up with a gain of 0.9972, then if i use $$f_f$$=58, i end up with a gain of 0.9973, similarly along the lines, for $$f_f$$ equal to 50 Hz i get a gain of 0.9980, showing that gain increases as frequency deviates from the nominal (which does not make any sense). I am having a hard time understanding this, could you clear this confusion for me. Could you show me how the gain is close to unity for small frequency deviations from the nominal

• Maybe a bandpass filter would be better, and even if you want a lowpass, then a moving average is, most probably, the worst choice. Unless you're dealing with filtering of powers (p-q theory & co), when you actually need a lowpass with the lowest latency possible. – a concerned citizen Apr 10 at 10:15
• Bandpass is not and option plus, lowpass via simple averaging is what i am trying here. – mka_07 Apr 10 at 10:21
• Why is bandpass not an option? That sounds like exactly what you need and the implementation as an 2nd order IIR is quite simple; would you consider it? See this post here on implementation of that dsp.stackexchange.com/questions/40482/… – Dan Boschen Apr 10 at 12:34
• This seems very much like an xy problem. Can you elaborate on what you actually need to do? To me it sounds like you need to preserve the fundamental, and that's a bandpass. Do you also need DC? If not, again bandpass. If you do, then I have to wonder what good is all this for, to keep both DC and mains fundamental? – a concerned citizen Apr 10 at 17:16

Here is the plot of $$|H(e^{j\omega})|=|\frac{\sin(L_m\omega_1/2)}{L_m\sin(\omega_1/2)}|$$ for $$f_f$$ varying from $$-Fs/2:1:F_s/2-1$$ (steps of 1Hz).

If you want to filter out 50 or 60Hz, you need to have the gain to be zero around these digital frequencies of $$2\pi 50/F_s$$ or $$2\pi 60/F_s$$. I think the mistake is that there is $$L_m$$ in computation $$\omega_1$$. $$\omega_1$$ is just normalized frequency $$2\pi f_f/F_s$$. If I remove $$L_m$$, I can see zeros in the gain function. Adjust $$L_m$$ such that zeros fall in corresponding position of 50Hz or 60Hz which you want to filter.

In order to center the frequency response over the nominal frequency $$f_f$$, need to shift the frequency response by $$\omega_s = 2\pi f_f/Fs$$. That is, apply phase shift of $$h_1[n] = h[n]e^{-j\omega_s n}$$.

EDIT: Frequency response of $$L_m$$ moving average filter $$H(e^{j\omega}) = \frac{1}{L_m}\sum_0^{L_m-1}e^{-j\omega n}\\ = \frac{1}{L_m}\frac{1-e^{-j\omega L_m}}{1-e^{-j\omega}}\\ = \frac{1}{L_m}\frac{e^{-j\omega L_m/2}}{e^{-j\omega /2}}\frac{\sin(\omega L_m/2)}{\sin(\omega /2)}\\ |H(e^{j\omega})| = |\frac{\sin(\omega L_m/2)}{L_m\sin(\omega /2)}|$$

• Thanks for the time and effort, but i think you misunderstood my question. I am using input signals whose fundamental component is centered around 50 or 60 Hz, and i am passing them through my averaging filter as given above to remove any noise or harmonics present. I am not supposed to filter out my 50 Hz or 60 Hz component, i am supposed to retain it (you have mentioned that gain around these frequencies should be zero). – mka_07 Apr 10 at 10:07
• In that case you can shift the moving average frequency response so that it centers around 50/60Hz and not 0Hz. You can apply the corresponding phase correction in time domain of the filter. If that is the solution you want, shall I modify the answer? – jithin Apr 10 at 10:20
• Are you able to relate to my confusion regarding the increase of gain with deviation of frequency from the nominal or center frequency. Even the removal of the Lm term from the omega equation does not help with the erroneous gain that i am getting. – mka_07 Apr 10 at 10:28
• You will get highest value of 1 when the $L_m$ contains integer number of time periods of signal at 50Hz. Otherwise, the gain will be less than 1. Also, it depends on the floating point resolution of the program you are using. In MATLAB you can make it double format so that instead of 0.9972 you will see 0.9969393920 something. – jithin Apr 10 at 10:40
• Hear me out once, in case i follow your idea of omitting out Lm from the omega1 equation and use the frequency deviation from the nominal (instead of the direct frequency ff) for calculating omega1, the values of H i get make sense and decrease as the frequency deviation increases. Does that make sense. – mka_07 Apr 10 at 10:52

Question : How is the gain close to unity in the vicinity of 60Hz?

Answer : It's not. Your $$\omega_1$$ mapping is not correct. Digital frequency $$\omega \in [-\pi, \pi]$$ in a filter response $$H(e^{j\omega})$$ is mapped to continuous frequency as follows :$$f = \frac{\omega}{\pi} .\frac{f_s}{2}$$ $$\omega \in [-\pi, \pi] \rightarrow [-\frac{f_s}{2}, \frac{f_s}{2}]$$.

A Moving Average filter is a causal Low-Pass filter of which the cut-off frequency($$\omega$$) depends on $$L_m$$.

Intuition : It averages $$L_m$$ number of input samples, i.e. average of 1 current sample $$x[n]$$ and $$L_m - 1$$ past input samples. Averaging smoothens out variations from the input samples because the consecutive samples of output are changing very slowly. Why? Because, the variation in output sample can only be caused by the current input sample $$x[n]$$ which is small compared to sum of $$L_m - 1$$ past input samples. so, a large part of consecutive output samples remain approximately same. Hence, Moving average is a LPF and that is why useful to filter out noise with low computational complexity.

Now, since you have $$|H(e^{j\omega})|$$, so you can plot it against frequency and see where your desired band of 55Hz to 65Hz lies inside the Moving Average Filter response.

Figure 1 : Frequency response of the filter you have mentioned $$L_m=30$$, $$F_s = 1440Hz$$ and grey patch shows where the band 55Hz to 65Hz lies. It actually lies in the side lobe of MA response.

Figure 2 : Zoomed in Frequency response of the filter you have mentioned $$L_m=30$$, $$F_s = 1440Hz$$ and the patch shows where the band 55Hz to 65Hz lies.

You need to bring your interested band containing frequency from 55Hz to 65Hz inside the main Lobe and close to DC to get a less distorted 60Hz sinusoidal signal.

You can do 2 things to achieve your goal :

1. Decrease $$L_m$$ so that cut-off frequency of Moving Average(MA) filter moves away from DC and 60Hz sinusoid falls inside main lobe.

2. Increase sampling frequency $$f_s$$, so that input signal spectrum shrinks and as a result 60Hz comes inside the main and close to DC.

Option 1 : Decrease Filter length, $$L_m = 10$$, and $$F_s = 1440Hz$$

Option 2: Increase Sampling rate, $$L_m = 30$$, and $$F_s = 30*1440Hz$$

As you can see that the effect both methods is that 60Hz now falls inside Main Lobe and hence you will retain you r desired signal of 60Hz. But the 2 options are not same and $$2^{nd}$$ option of increasing sampling rate while keeping $$L_m = 31$$ will perform way better than decreasing $$L_m$$ to 10. Why?

Two main reasons:

1. Decreasing $$L_m$$ to a lower value will not have as good denoising effect as a higher $$L_m$$, since the averaging is done over a shorter length.

2. Increasing sampling rate and then low-pass filtering your spectrum will inherently increase SNR. You can think about how!

A moving average filter is a type of low pass filter with unity gain at DC. The cutoff frequency at which attenuation starts to occur in an appreciable way is a function of the length of the average, such that longer lengths result in a lower cutoff frequency. Your magnitude values are close to unity because the frequencies are small relative to the cutoff frequency.