2

I'm trying to calculate the lag between two signals in Python using cross correlation. The two signals are almost identical except for a very small timelag. I've tried numpy.correlate and scipy.convolve (alot faster) and both works relatively well but gives a small error. I'm starting to suspect that the error is the result of Python/scipy/numpy truncating a float somewhere. Has anyone been able to get high accuracy signal delay calculations working in Python?

Best regards Fredrik

4

1 回答 1

0

根据两个信号的功率谱,您确实会得到一个小误差,因为互相关在每个滞后时都没有正确归一化。这是我使用的一个小功能;它使每个滞后的重叠区域正常化,我发现它给出了准确的结果:

def NormCrossCorrSlow(x1, x2,
                  nlags=400):
res=[]
for i in range(-(nlags/2),nlags/2,1):
    if i<0:
        xx1=x1[:i]
        xx2=x2[-i:]
    elif i==0:
        xx1=x1
        xx2=x2
    else:
        xx1=x1[i:]
        xx2=x2[:-i]
    res.append( (xx1*xx2).sum() /( (xx1**2).sum() *(xx2**2).sum() )**0.5)
return numpy.array(res)
于 2011-03-05T20:01:57.703 回答