Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

find time shift between two similar waveforms

I have to compare two time-vs-voltage waveforms. Because of the peculiarity of the sources of these waveforms, one of them can be a time shifted version of the other.

How can i find whether there is a time shift? and if yes, how much is it.

I am doing this in Python and wish to use numpy/scipy libraries.

like image 490
Vishal Avatar asked Jan 14 '11 07:01

Vishal


People also ask

How do you find time delay using cross correlation?

For delay analysis, correlation in the time domain is widely used. The correlation function plots the similarity between two signals for all possible lags τ. The peak of the correlation function occurs at the lag with the best similarity between the two signals, i.e. the estimated delay.

How to measure similarity between two signals?

Similarity in energy (or power if different lengths): Square the two signals and sum each (and divide by signal length for power). (Since the signals were detrended, this should be signal variance.) Then subtract and take absolute value for a measure of signal variance similarity.

How do you know if two signals are correlated?

In words, we compute a correlation by multiplying two signals together and then summing the product. The result is a single number that indicates the similarity between the signals x[n] and y[n].


2 Answers

scipy provides a correlation function which will work fine for small input and also if you want non-circular correlation meaning that the signal will not wrap around. note that in mode='full' , the size of the array returned by signal.correlation is sum of the signal sizes minus one (i.e. len(a) + len(b) - 1), so the value from argmax is off by (signal size -1 = 20) from what you seem to expect.

from scipy import signal, fftpack import numpy a = numpy.array([0, 1, 2, 3, 4, 3, 2, 1, 0, 1, 2, 3, 4, 3, 2, 1, 0, 0, 0, 0, 0]) b = numpy.array([0, 0, 0, 0, 0, 1, 2, 3, 4, 3, 2, 1, 0, 1, 2, 3, 4, 3, 2, 1, 0]) numpy.argmax(signal.correlate(a,b)) -> 16 numpy.argmax(signal.correlate(b,a)) -> 24 

The two different values correspond to whether the shift is in a or b.

If you want circular correlation and for big signal size, you can use the convolution/Fourier transform theorem with the caveat that correlation is very similar to but not identical to convolution.

A = fftpack.fft(a) B = fftpack.fft(b) Ar = -A.conjugate() Br = -B.conjugate() numpy.argmax(numpy.abs(fftpack.ifft(Ar*B))) -> 4 numpy.argmax(numpy.abs(fftpack.ifft(A*Br))) -> 17 

again the two values correspond to whether your interpreting a shift in a or a shift in b.

The negative conjugation is due to convolution flipping one of the functions, but in correlation there is no flipping. You can undo the flipping by either reversing one of the signals and then taking the FFT, or taking the FFT of the signal and then taking the negative conjugate. i.e. the following is true: Ar = -A.conjugate() = fft(a[::-1])

like image 143
Gus Avatar answered Oct 13 '22 00:10

Gus


If one is time-shifted by the other, you will see a peak in the correlation. Since calculating the correlation is expensive, it is better to use FFT. So, something like this should work:

af = scipy.fft(a) bf = scipy.fft(b) c = scipy.ifft(af * scipy.conj(bf))  time_shift = argmax(abs(c)) 
like image 35
highBandWidth Avatar answered Oct 12 '22 23:10

highBandWidth