机器学习(1)互相关

机器学习(1)互相关Computingthecrosscorrelationfunctionisusefulforfindingthetimedelayoffsetbetweentwotimeseries.Pythonhasthenumpy.correlatefunction.

大家好,欢迎来到IT知识分享网。机器学习(1)互相关"

Computing the cross-correlation function is useful for finding the time-delay offset between two time series.
Python has the numpy.correlate function. But there is a much faster FFT-based implementation.
Check out the following paper for an application of this function:

import numpy as np
from numpy.fft import fft, ifft, fft2, ifft2, fftshift

def cross_correlation_using_fft(x, y):
    f1 = fft(x)
    f2 = fft(np.flipud(y))
    cc = np.real(ifft(f1 * f2))
    return fftshift(cc)

# shift < 0 means that y starts 'shift' time steps before x # shift > 0 means that y starts 'shift' time steps after x
def compute_shift(x, y):
    assert len(x) == len(y)
    c = cross_correlation_using_fft(x, y)
    assert len(c) == len(x)
    zero_index = int(len(x) / 2) - 1
    shift = zero_index - np.argmax(c)
    return shift

We can test the above function by shifting the second series manually and seeing if the shift is accurately computed:

for n in range(1000, 1050, 7):
    for s in range(-5, 5):        
        a = [random.random() for _ in xrange(n)] # big random sequence of values
        b = a
        if s >= 1:
            a = a[s:]
            b = b[:-s]
        elif s <= -1:            
            a = a[:s]
            b = b[-s:]
        assert s_optimal == s

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/32018.html

(0)

相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注微信