# Compensate delay of non-symmetric (non-linear phase) filter

Lets assume I want to apply a matched filter h which is non-symmetric to my signal x and the output is y in matlab:

y=filter(h,1,x);


Now I am interested in comparing the information between my filtered signal yand my input x. For this I want to compensate for the delay of the filter h.

When googling this topic I found the following tutorial which handles linear-phase fir filters. Obviously in such a case the group delay is $$\frac{N-1}{2}$$ given that the filter has the length $$N$$.

%this is the example from matlab
delay = mean(grpdelay(h,1,128));
x = x(1:end-delay);
y(1:delay) = [];


However, I am aware that the group delay of a non symmetric filter is frequency depending and I am not sure how to proceed to align my signals in time to compare them. As I am interested in real time application the function filtfilter is not applicable.

So my question is, how do I estimate in the best way the time delay to align (sync?) my signals in time given the coefficients of a non-symmetric filter h?

delay = .... h ....?
x = x(1:end-delay);
y(1:delay) = [];


The desired output should look like the red curve in the following figure Here the signal in blue is the input signal x and in green the filtered signal y. However, I would like to to compensate for the delay of the green signal so that the input and output align in time as in the case of the red curve.

• I think the solution lies in your definition of "time delay" of a filter with a delay that depends on frequency. There is no single way to align the input and output signals of such a filter. The question is what you really mean by "align". – Matt L. Sep 5 '19 at 7:02
• I edited my question and added a plot. – Irreducible Sep 5 '19 at 7:12
• It looks like cross-correlation should work here. – Matt L. Sep 5 '19 at 7:16
• I see, but this would be an approach which works well for offline analysis, however, is there a way, knowing the shape of the non-symmetric filter, to estimate the delay in advance and apply it online? Or is your idea to apply cross-correlation in advance and estimate this delay for later online processing? Would this delay not be dependent on the shape of the input signal? In other words can I generalize the delay from a cross-correlation of one input signal to all kind of input signals? – Irreducible Sep 5 '19 at 7:22

$$\hat{\tau}=\frac{\displaystyle\sum_nnh[n]}{\displaystyle\sum_nh[n]}\tag{1}$$
For more general impulse responses, other than low pass, you can modify $$(1)$$ by either taking magnitudes or squares of $$h[n]$$. Note that $$(1)$$ is the filter's group delay evaluated at DC.
• @Irreducible: Great that it seems to work! Well, the center of gravity (center of mass) is of course a very general concept, which can be applied to any function. So I don't have a specific reference right now, but just for your intuition, imagine that $h[n]$ were a probability mass function of some random variable, then $(1)$ would be the expected value. – Matt L. Sep 5 '19 at 8:42