## Solution

It is possible to do it in many ways. In this KB we provide a solution using the cross-correlation function.

In signal

processing, cross-correlation is a measure

of

similarity of two series as a function of the displacement of one relative to the other. This is very useful to determine the delay between two signals.

This example uses cross-correlation to determine the sample delay between two signals that are identical but have been shifted.

- Generates a signal of 100 samples
- Make a copy of the signal and shift it by a user controlled number of samples
- Delete off the shifted number of samples from the back so both waveform are still the same size.
- Cross correlate the signals
- Find the peak of the cross correlated function
- Find the x-value of the peak amplitude
- Subtract the number of samples of one waveform minus one because of cross correlation math

If you calculated the cross-correlation between both signals, the maximum (or minimum if the signals are negatively correlated) of the cross-correlation function indicates the point in time where the signals are best aligned, i.e. the time delay between the two signals is determined by the argument of the maximum, or arg max of the cross-correlation. This is true for time continous signal.

In discrete system you must take into account that the time is substitute by samples so after calculating the maximum of the cross-correlation function (6) you must related him to where the cross-correlation is max, usually at the middle of the band. Thus, you must subtract to the position of the max the total number of samples of the signal minus 1 (zero indexed) in order to calculate the sample delay (7)