1. 程式人生 > 其它 >Numpy實現Matlab的xorr函式

Numpy實現Matlab的xorr函式

技術標籤:Python

廢話不多說,直接上程式碼

def AutoCorrelation(x, lags):
    N = len(x)
    auto_corr = np.correlate(x, x, mode = 'full')
    assert N >= lags - 1
    auto_corr = auto_corr[N - lags - 1 : N + lags]
    auto_corr = auto_corr / np.max(auto_corr)
    return auto_corr

測試一下

a = np.array([1, 2, 3, 4, 5, 6])
print(AutoCorrelation(a, 2))

輸出為:

用Matlab跑一下