Python cross-correlation not returning correct shift

谁都会走 提交于 2019-12-11 06:14:02

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!