问题
I'm learning about cross-correlation, and to do so I'm running scipy.signal.correlate on some simple examples. However, there are instances for which the maximum of the correlation function does not coincide with the input time shift (even though the correct time shift IS present in dt
). I suspect the issue lies in my definition of dt
, but after several iterations I haven't managed to get the shift consistently correct.
def cross_corr(a, b, t):
ccf = signal.correlate(a,b,mode='full')
#Convert from array spacing into time shift
step = np.mean(t[-1]-t[0])/t.size
dt = np.linspace(-b.size + 1, a.size - 1, ccf.size)*step
time_shift = dt[np.argmax(ccf)]
return dt, ccf, time_shift
#Generate some toy data
t = np.linspace(0.,2.*np.pi,100)
y1 = np.sin(x)
y2 = np.sin(x+2.45)
dt, ccf, time_shift = cross_corr(y1, y2, t)
This particular example returns time_shift = 2.32
when it should be 2.45
.
来源:https://stackoverflow.com/questions/44013166/python-cross-correlation-not-returning-correct-shift