# Filter odd or even harmonics with notch or inverse notch filter

Hi i had the following question.

I have a signal containing a 200Hz sine wave and it's odd and even harmonics (no other frequencys or disturbing signals are contained). What i'm looking for is a kind of filter which is able to seperate the odd harmonics from the even ones. The end goal is to be able to determine the amplitude and phase of each single of the odd harmonics as accurate as possible. I know that you normally would use an FFT for this purpose but i'm trying to figure out if this wouldn't also be possible with a filter instead.

So i thought about two options:

1. Use a parallel circuit of notch filters (one for each harmonic to be filtered) to cut out the even harmonics i don't want to use and seperate the odd harmonics from eachother.

2. Do exactly the opposite and use "inverse notch filters" to filter out the odd harmonics i want to use and seperate them from eachother by using a parallel circuit of "inverse notch filters" (one for each harmonic i want to filter out and take amplitude and phase from).

I've added a little sketch as png to make things clearer.

The problem is that it's very difficult to find understandable information about notch filter or inverse notch filter. (Inverse notch filter is not the same as a peak filter)

For notch filters i only find spurious and short information like these: https://zone.ni.com/reference/en-XX/help/371325F-01/lvdfdtconcepts/notch_peak_filters/ https://www.onesdr.com/2019/11/21/fm-notch-filters-why-you-need-one-with-most-sdrs/ https://www.youtube.com/watch?v=tpAA5eUb6eo

And the only source of information i found about inverse notch-filter was this paper i mentioned (on top of the second page of the paper): https://www.scirp.org/pdf/_2013060615443504.pdf

The problem is i would need way more information about both filter types and how derive them from simpler filter types and calculate their transfer function and Z-transform.

So my question is (if i'm not completely on the wood path) which of the both approaches described you would prefer and can you give me some good sources of clear and understandable information about the two mentioned filter types and thier implementation in hardware ? Our would you recommend me a completely different filter type for this task ?

Greetings

• notch filters are really not obscure at all – I distinctively remember how pole/zero diagrams were introduced through exactly that! You'd not look in papers for these, but in basic signal theory textbooks; anyways, for your use case, the most sensible filterbank seems to be the DFT (you've got tones on a regular grid). What would be wrong with that? Why do you need an (inverse) notch filter? (Never heard that thing be called "inverse notch"; it's a narrowband filter, maybe a high-Q resonant IIR or something; I think your problems finding literature might stem from that unusual naming) – Marcus Müller May 7 at 18:59
• 3. Add a comb filter. – a concerned citizen May 7 at 19:38
• Yes you're right, it seems that i searched for the wrong namings. – Rabobsel May 8 at 8:32

What you are looking for are what we, in the audio space, call comb filters. Comb filters may or may not have a feedback path, just like FIR and IIR filters. In fact, there is a generalized theory about designing comb filters based simply on designing any digital filter.

In fact, you can think of every LTI digital filter as being a comb filter where the teeth of the comb are spaced apart by the sampling frequency. To space the teeth apart by a frequency spacing much less than the sampling frequency, you need a delay element that is longer than a single-sample delay ($$z^{-1}$$). If a delay with precision of an integer number of samples is good enough to tune your comb filter, good for you. But if it isn't, you need to deal with interpolation and precision delay.

• Ok thanks i will go into this topic. So it seems that the comb part of a CIC filter used for down- or upsampling a digitized signal is exactly such a comb filter or is this a completely different approach ? – Rabobsel May 8 at 8:34

If the OP is actually interested in selecting only one individual frequency from the even or odd harmonics, then a moving average filter (MAF) would be ideal since this can provide a null at every other frequency when the frequencies are harmonically related. The low-pass MAF with all the coefficients as 1 will pass DC at $$f=0$$ and provide nulls spaced by $$1/T$$ Hz where $$T$$ is the total duration of the filter in seconds. This means for a filter $$N$$ taps long sampled at sampling rate $$f_s$$, the nulls will be spaced by $$f_s/N$$, so selecting a sampling rate that is an integer multiple of the harmonic would provide for the simplest solution.

The low-pass can be converted to a bandpass such that any frequency is selected by rotating the filters coefficients using the following for each coefficient $$n$$ with $$n$$ going from $$0$$ to $$N-1$$ total number of coefficients in the filter:

$$c_n = e^{j 2\pi (f_o/f_s) n}$$

(This is identical in form to a bin in the DFT, and the DFT can similarly be shown to be a bank of such filters: (see Magic of twiddle factor in DFT).

For example, if we set the sampling rate $$f_s$$ to be 4 KHz as an integer multiple of 200 Hz and we want to have a null every 200 Hz, this would require a total filter duration of $$T = 1/200 = 0.005$$ seconds which is $$N = T f_s = 20$$ total coefficients. Below shows the case where all the coefficients are 1 passing DC and nulling the rest of the harmonics, and when all the coefficients are $$e^{j 2\pi 600/4000 n}$$ for each coefficient n from $$0$$ to $$19$$, passing 600 Hz and nulling the rest.

This is simplest as shown above when the sampling rate is an integer multiple of the frequencies to be rejected, but non-integer multiples can be created by summing delay elements with an inverse that is a multiple of the harmonics to be rejected, using all-pass delay structures for the implementation.

If all even or all odd harmonics were desired to be passed through, this would be very simple to do with an interpolated sum and difference FIR filter (which is a comb filter given the frequency response has "teeth" like a comb) if the sampling rate can be an integer multiple of the fundamental frequency 200 Hz (or as described above implemented with an all pass delay structure instead of sample delay elements).

First note below the basic form of a sum and difference with a single delay:

Adding more delays is identical to zero-insert interpolation, which causes the spectrum to replicate for every zero inserted. For example with one additional delay, the filter responses would appear as below:

For the OP's case, and depending on how many harmonics are desired to be processed, we could for example set the sampling rate to be 4 KHz and then use 4000/400 = 10 delays to result in the following solution providing even and odd notches up to the Nyquist frequency of 2 KHz while providing a maximum response at exactly each desired harmonic:

Mathematically the above forms result from the factoring of the polynomials $$z^N-1$$ and $$z^N+1$$ which complex N roots in either case are evenly spaced around the unit circle. The first case with a subtraction includes a root that is exactly at $$z=1$$ while the second case with the addition has $$z=1$$ equidistant between two of the roots. These roots are the zeros of the filter and the unit circle is the frequency axis, resulting in the frequency response patterns shown.

• just FYI, Dan, in my app space (audio), we call these comb filters and there are forms with or without feedback. if you are making a comb filter with feedback, you must account for the one-sample inherent delay in the feedback path. and if you want to tune in every harmonic, the length of the delay line is one period of the periodic waveform. if you want to tune in either just the even harmonics or just the odd harmonics, the length of the delay line is half of the period. to tune in the comb perfectly, you need to do good interpolation for a fractional-sample precision delay. – robert bristow-johnson May 8 at 1:00
• Yes these are called Comb filters as I refer to them as well in my app space; the frequency response appears like a comb - This is the "Comb" in the CIC filters we were recently discussing regarding floating and fixed point. – Dan Boschen May 8 at 1:53
• @Rabobsel You would use each of the two filters shown to separate the two; one filter would provide the even harmonics while the other provides the odd harmonics. – Dan Boschen May 8 at 18:16
• @Rabobsel if you are looking to select an individual tone, consider the bandpass exponential averager as described in this post dsp.stackexchange.com/questions/40482/… or using the Goertzel algorithm for a small number of tones instead of the FFT – Dan Boschen May 9 at 2:12
• @Rabobsel see the bottom of that suggested post where that is all specifically explained and derived – Dan Boschen May 9 at 11:17

You can use a window as an FIR filter (convolve with window in time), such that the zero crossings of the window transform are at $$\omega_0 = 2\pi * 2k*200$$ to filter the even harmonics, and phase shift the window by transform $$\frac{\pi}{2}$$ to filter the odd harmonics. For example, a rectangular window of length $$M$$ will have zero crossings at $$\omega = \frac{2\pi k}{M}$$. You want to select $$M$$, such that $$\omega_0 = \frac{2\pi k}{M}$$, or, $$M = \frac{1}{400}s$$.

In Matlab you'd write

fs = 10000; %whatever your sampling rate is
f0 = 200;
M = round(fs/(2*f0));  %window length in samples
win = rect(win);
x_no_even = conv(x,win);
x_no_odd = conv(x,circshift(win,M/4));

• Using a window means the FIR will have a lowpass characteristic, which may be undesirable. OP seems to want to preserve the harmonics. – a concerned citizen May 7 at 22:50
• Yes precisely i wnat to further process the harmonics (e.g. calculating amplitude and phase). – Rabobsel May 8 at 8:31
• A window in time will actually not do any of this. A window in time is convolution in frequency. So the Fourier Transform of the user's waveform will convolve with the Kernel (Fourier Transform of the Window). – Dan Boschen May 9 at 18:59
• I mentioned convolving with a window in the time domain (therefore, multiplying in the frequency domain). See the Maltab pseudo-code. The real issue with this method is the low pass characteristic, so a comb filter is the way to go. – orchi_d May 9 at 20:50
• Ok I follow now, yes that makes more sense once I looked at the code – Dan Boschen May 10 at 21:18