# How to reliably compute the group delay of a comb filter

I applied the following FIR comb filter in real-time:

y[n]=x[n]-x[n-40]


Since this is an FIR, the group delay is D=(N-1)/2=20 samples. After applying the filter to a signal, I tried to use cross correlation between the filtered and unfiltered signal, to reproduce D computationally by determining the argmax of the cross correlation (I do have a need to the delay this way). The issue is that I get too peaks in the cross correlation, one at zero lag and another at 20 lag. But the peak at zero lag is the maxima which means the peak at 20 lag which is the correct lag is ignored. This method work really well with other filters like averaging filters.

Does anyone know while I get a the peak at zero which is overshadowing the real peak? Is this normal for comb filters? Is there another method to compute delays using the filtered and unfiltered signal other than cross correlation?

Since this is an FIR, the group delay is D=(N-1)/2=20 samples.

No, since this is a linear phase (i.e. symmetric or anti-symmetric) filter, the group delay is half the length! (being a FIR isn't sufficient.)

The issue is that I get too peaks in the cross correlation, one at zero lag and another at 20 lag.

Write down the formula for auto-correlation at zero lag. Compare that to the formula of "energy of a signal". They are identical!

This really shouldn't surprise you!

This method work really well with other filters like averaging filters.

This method works with anything that has a non-zero zero-lag coefficient.

Does anyone know while I get a the peak at zero which is overshadowing the real peak?

Yes, because autocorrelation at zero is simply the energy. And since correlation is a linear, and your system passes through the original signal, plus a delayed version of it, you get the sum of the auto-correlation of the input signal and the cross-correlation of your delayed signal and the input signal.

The 20-lag peak is no "realer" than the 0-lag peak.

Is this normal for comb filters?

This is normal for any linear time-invariant system.

Is there another method to compute delays using the filtered and unfiltered signal other than cross correlation?

The group delay is really defined as derivative of the phase of your signal over frequency. If in doubt, estimate the spectrum of your system, and derive its phase. You'll notice that only a few specific systems (linear-phase, see above) have constant group delay.

Hence, I'm not sure your cross-correlation had much to do with group delay to begin with.

• Thanks a lot for your quick answer a lots of corrections which are well taken. I did not think about the autocorrelation at zero lag. But mind you the energy of a signal is not identical to the cross correlation at zero lag given a delayed version of the same signal. Because the cross correlation can still undergo destructive interference even at zero lag while the energy always gives constructive interference. Nov 15 '19 at 20:50
• yes, it's the sum of the autocorrelation and the cross-correlation with the delayed signal, exactly as I said – the latter can have negative real part, indeed. So, you're agreeing with me :) Nov 15 '19 at 20:52
• on correlation having anything to do with group delay; I have not explained that I have a signal X which was acted upon by a system which I do not have much information about. The output is Y. The delay between X and Y is the group delay of the acting system whose spectrum i do not know. Because I do not know much about the system, I cannot use phase analysis. So aside from cross-correlation, how do I determine the group delay of the acting system? Nov 15 '19 at 21:02
• This needs to be part of your question, not a comment to an answer. Nov 15 '19 at 21:03
• @Chika you know everything about the system if you can excite it with an arbitrary waveform. Nov 15 '19 at 21:21

Update: Since this question was asked again I demonstrated how the delay can be determined using channel estimation techniques at this post here:

How determine the delay in my signal practically

For the cross correlation of the output of the filter with the input you should see the result of the two impulses in your filter convolved with the cross correlation properties of your actual waveform. If your waveform is random such that there is only a correlation at 0 lag then the result here would be a positive correlation peak at lag zero and then an equal and opposite (negative) correlation peak at lag 40 samples. This is exactly what your filter formula is giving you in two parts:

$$y[n] = x[n]$$ produces the positive correlation peak at lag = 0

$$y[n] = x[n-40]$$ produces the negative correlation peak at lag = 40

With a random input (that itself has an impulse autocorrelation), the cross correlation should look something like the plot below (using xcorr(out, in)) For a non random input that has non-zero values for its autocorrelation at non-zero lag, you would see a replica of its autocorrelation at the two locations above similar to below: The only way I can think of that you would see a result with a lag of 20 is if your signal itself has a dominant autocorrelation result with a lag of 20 and the opposite sign with a lead of 20 such that these would combine in the cross correlation with the output of your filter that has a lag of 0 negatively summed with a lag of 40.

As for group delay, do not use cross correlation to estimate group delay. As you can see the cross-correlation is resolving all the "echos" of your filter given by each coefficient, as well as the auto-correlation properties of your waveform itself so would not indicate group delay at all. To determine the group delay simply take the negative derivative of the phase response of the DTFT, or even easier use the group delay function (grpdelay) directly that is available in Matlab/Octave/Python Scipy.

When the input signal is known to be random or pseudo-random (does not have dominant auto-correlation magnitudes at lags other than 0) and the filter itself has a dominant center tap, then in this particular case, the cross correlation of the input with the output would have a maximum at the delay of the filter. (This clearly is not that condition with the comb filter). Even in this case, the wider the width of the filter's impulse response is about it's dominant center tap, and similarly the wider the width of the autocorrelation of the input (indicative of having already been low pass filtered), the wider will be the resulting cross-correlation centered about the delay and thus the more difficult it would be to accurately estimate delay from such a process.

• Still looking through your answer. Regarding you suggestion to use grpdelay and other analytic approaches, those were the first thing i did and got 20samples. But I need to know away to actually confirm this filter delay of 20 samples practically when I only have the input signal and the output signal, i.e without using the impulse response of the filter. So this comes down to a problem of delay between two signals, filter delay is still a delay? And google says it can be solved with xcorr; Are you saying that if the delay is cause by a filter that xcorr cannot determine the delay. Nov 28 '19 at 19:09
• When you only have the input sample and output sample you can do inverse convolution to solve for the channel response (and xcorr comes into use in doing that.) As you found, xcorr alone will not readily give you the delay directly when your signals correlate to different delays. Nov 28 '19 at 21:01
• I think a clear and easy way to see why xcorr will not readily give you the delay is to do an autocorrelation on an arbitrary waveform; you will find many cases where the waveform has strong correlations to delayed versions of itself! (It is only waveforms with random or pseudo-random samples that will have a strong correlation at lag=0 and 0 everywhere else). So if this case can give you results at different lags, how could you use it to compare input and output of a filter to measure delay? In many cases the input signal is pseudorandom and the filter (most low pass filters .... Nov 28 '19 at 22:36
• ... and all linear phase low pass filters) has a dominant center tap — in these cases yes you can use xcorr to estimate delay. You don’t have this case with your comb filter and my answer details why you can’t use the xcorr result to measure the delay. You can do inverse convolution to estimate the filter response and then from that compute the group delay (which is the time delay within a “group” of frequencies.) Nov 28 '19 at 22:39
• Here it is including the Matlab code- dsp.stackexchange.com/questions/31318/… just swap rx and tx and it will return the coefficients for the channel instead of the inverse (channel compensator). You don't want the length of the resulting filter to be much longer than your overall delay spread as you get noise enhancement the longer the filter is. Nov 29 '19 at 17:06

the group delay is D=(N-1)/2=20 samples

No. Group delay is a function of frequency. Assigning a single number to group delay is somewhat questionable. While the phase is indeed piece wise linear, at the "dips" of the combfilter, the phase jumps from $$-\pi$$ /2 to $$\pi/2$$. This is a real discontinuity, not a wrapping issue. At these frequencies the group delay is undefined

• You are totally right but for practical purposes as i using this filter it can be considered constant. I am practically aligning the filtered and the original and 20 samples works fine. My first time of working with comb filter and I find it quite confusing Nov 15 '19 at 21:17
• I would agree with that- within any practical passband that can be used the delay is constant given it is a linear phase filter. Nov 15 '19 at 21:24
• @dan I take back my comment ; but I don't really know any reason I am getting zero delay with the comb filter. When I align the comb filter input with the output or use cross correlation between them, I get a zero delay. But all the analysis using the comb filter's impulse response say that the average group delay is 20samples. The frequency response of the output signal confirms that the comb filter is clearly working. Nov 28 '19 at 12:34
• @Chika which comment are you taking back as I was agreeing with you-- the group delay for a linear phase filter should be constant-- I read your original question and have an answer for you that I will add now. Nov 28 '19 at 12:53
• @dan I know you agreed with me. But When I actually use the filter the output is not delayed at all. If you can could you please try the filter yourself on a signal and try to determine the shift on the output. I am just getting zero instead of the 20samples that you agreed with and it is confusing me. Nov 28 '19 at 14:46