I have a periodic signal(ECG) with period of ~1 seconds. It does not have features that are shorter than 0.04 seconds. For removal of 60Hz, I thought instead of implementing a notch filter, doing a simple moving average of 1/60~0.016 seconds.(Averaging over a larger window might destroy signal information, and also it's not needed) This is much simpler than a notch filter which can be of several orders. Am I missing something?
-
$\begingroup$ Moving averages are generally very poor low pass filters. They are useful when you don't have knowledge or a model for the noise or interferer. Here you know the interference is a narrow 60 Hz spike, so design a filter to do the job right: a notch filter is the correct thing to use. $\endgroup$– Andy WallsOct 2, 2018 at 22:43
2 Answers
No you are not missing anything. A moving average with a period of $T = 1/60$ seconds will indeed have a notch at 60 Hz, since the frequency response is a Sinc function with the first null at 1/T. (So it is both a low pass filter with an magnitude envelope that goes down as 1/f combined with a notches at integers of 1/T not including 0).
This is because a moving average has an impulse response that is a rectangular function of duration T, and the Fourier Transform of such a function is a Sinc (and the Fourier Transform of the Impulse Response is the Frequency Response). Digital Filters are similarly formed when the impulse response is a sampled rectangular function and thus their Fourier Transform will approach a Sinc function (what I like to refer as an "aliased" Sinc function similar to other aliasing in discrete time systems). If we position the sampling rate at integer multiples of 60 Hz, starting at 120 Hz and higher, the null will be at 60 Hz and it's higher harmonics if the digital impulse response (formally called the unit sample response) is uniform samples for duration T, where T is 1/60 Hz (which is a moving average specifically when the weighting of each sample is 1/N for N samples, otherwise it is a scaled moving average). Therefore the minimum sampling rate required to do this digitally would be 120 Hz, otherwise 180 Hz, 240 Hz, and higher multiples of 60 Hz can also be used). Note that I said this would approach a Sinc function; notice the plots below of the actual frequency responses for each choice of sampling rate when we position the null at 60 Hz and do the moving average, given by the actual digital transfer function according to:
$$H(z) = \frac{1}{N}\sum_{n=0}^{N-1}z^{-n}$$
With the frequency responses below determined by using the unit circle for z ($z= e^{j\omega}) $ where $\omega$ is the normalized angular frequency from 0 to $2\pi$ corresponding to 0 to the sampling rate. The actual Matlab/ octave command that can be used to readily determine this is as follows (and Python SciPy has a similar command):
freqz([ones(N,1), 1/N]);
Where N for each of the cases below is 2, 3, 4, and 10.
Also what you could alternatively consider which is even simpler is a "comb filter" structure which is simply the addition of the input with a delayed version of itself where the delay is equal to T/2.
This will have the frequency response according to:
$$H(\omega) = 1 + e^{-j\omega T/2}$$
Since the Fourier Transform of a delay is given as
$$x(t) = \delta(t-T) <==> x(\omega) = e^{-j\omega T}$$
With the magnitude plotted below:
This is equivalent to the digital filter with a delay of one sample when the sampling rate is 120 Hz, and a transfer function given as:
$$H(z) = 1+z^{-1}$$
with as stated previously the frequency response when z is the unit circle as
$$H(e^{j\omega}) = 1 + e^{-j\omega}$$
with $\omega$ going from 0 to $2\pi$ in this case representing the normalized angular freq from 0 to the sampling rate.
Like the moving average case above, this could also be implemented at any sampling frequency that is an integer multiple of 60 Hz, starting at the minimum 120 Hz rate.
If you are concerned about the "frequency droop" in your passband area for either of the above two solutions, you could consider using this very simple three tap FIR compensator that I described at this link to help flatten the passband:
how to make CIC compensation filter
In all the cases above, the sampling rate must be an integer multiple of the notch frequency desired. If that is too restrictive, here is a digital notch filter that I would typically implement when a very tight notch is required with a flat passband, to compare to the approaches above (and does not have the same limitations on sampling rate):
Transfer function of second order notch filter
Side note: The relatively high sidelobes, or stated another way, the slow frequency roll-off of 1/f in magnitude, is why a moving average is often a poor choice as an FIR filter for low pass filtering (or estimating the DC value which IS the average) UNLESS you are filtering what is known to be white noise. For the case of white noise it is one of the best choices. Why? Because the moving average does have the narrowest main lobe for the given number of taps compared to any other FIR filter, and if the noise is known to be white, then the 1/f magnitude roll-off will be sufficient to suppress the higher frequency components.
-
$\begingroup$ Thank you for the detailed answer and effort. I was trying to figure out with the specific assumptions of the signal in time domain, if moving average would do just right. Freq. domain analysis is certainly insightful. $\endgroup$– doubleEOct 3, 2018 at 7:19
A moving average is always a low pass filter, you are never going to get a notch at your desired frequency. You technically might be able to design it just right to have a notch at 60Hz, but you'll have notches at other frequencies. Also while the period of your signal may be 1 second, I suspect it will have higher frequency components.
Type this into matlab/octave with any number of N you'll find it is always a low pass.
N = 80 freqz(ones(1,N) * 1/N, 1)
A moving average is just a filter where all the coefficients are 1/N.
-
$\begingroup$ That's true in general. My question was specific considering the specific signal timings and moving average timings purposed.. $\endgroup$– doubleEOct 2, 2018 at 22:26
-
$\begingroup$ If you think that is absolutely true and the signal characteristics will not go beyond that range then just low pass filter out to the highest frequency you expect. $\endgroup$ Oct 2, 2018 at 22:31