2013-11-21 27 views

cevap

4

, sahip olan denklem ile ifade edilebilir:

k örnekle x(i) sinyalin kayma açılımı k belirli bir sinyal (örneğin, belirli bir 1D vektör), bir

..., Nx(i) sinyalin uzunluğu ve:

enter image description here

... ortalamasından ibaret olan biz yazabiliriz:

''' 
Calculate the autocovarriance coefficient. 
''' 

import numpy as np 

Xi = np.array([1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5]) 
N = np.size(Xi) 
k = 5 
Xs = np.average(Xi) 

def autocovariance(Xi, N, k, Xs): 
    autoCov = 0 
    for i in np.arange(0, N-k): 
     autoCov += ((Xi[i+k])-Xs)*(Xi[i]-Xs) 
    return (1/(N-1))*autoCov 

print("Autocovariance:", autocovariance(Xi, N, k, Xs)) 

Eğer isterseniz Sadece yukarıdaki koda eklemek için sadece iki ek hatları var daha ...

enter image description here

:

def autocorrelation(): 
    return autocovariance(Xi, N, k, Xs)/autocovariance(Xi, N, 0, Xs) 

İşte şeklinde ifade otokorelasyon katsayısı olacak otokovaryansı katsayısı, normalleştirmek tam komut dosyası:

''' 
Calculate the autocovarriance and autocorrelation coefficients. 
''' 

import numpy as np 

Xi = np.array([1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5]) 
N = np.size(Xi) 
k = 5 
Xs = np.average(Xi) 

def autocovariance(Xi, N, k, Xs): 
    autoCov = 0 
    for i in np.arange(0, N-k): 
     autoCov += ((Xi[i+k])-Xs)*(Xi[i]-Xs) 
    return (1/(N-1))*autoCov 

def autocorrelation(): 
    return autocovariance(Xi, N, k, Xs)/autocovariance(Xi, N, 0, Xs) 

print("Autocovariance:", autocovariance(Xi, N, k, Xs)) 
print("Autocorrelation:", autocorrelation()) 
+0

Numpy zaten [korelasyon] hesaplamak için gereken her şeye sahiptir (https://docs.scipy.org/doc/numpy/reference/generated/numpy.correlate.html). (Hangi [scipy.signal.fftconvolve] ile hızlandırılabilir (http://docs.scipy.org/doc/scipy-0.15.1/reference/generated/scipy.signal.fftconvolve.html). Otokovaryansı elde etmek için [varyans] (http://docs.scipy.org/doc/numpy/reference/generated/numpy.var.html) ile çarpılmalıdır. – Celelibi

1

alın örnek oto kovaryans:

# cov_auto_samp(X,delta)/cov_auto_samp(X,0) = auto correlation 
def cov_auto_samp(X,delta): 
    N = len(X) 
    Xs = np.average(X) 
    autoCov = 0.0 
    times = 0.0 
    for i in np.arange(0, N-delta): 
     autoCov += (X[i+delta]-Xs)*(X[i]-Xs) 
     times +=1 
    return autoCov/times 
0

Python for döngüsünü önleyen ve bunun yerine numpy dizi işlemlerini kullanan önceki yanıtlara küçük bir ince ayar. Çok fazla veri varsa bu daha hızlı olacaktır. 50.000 veri noktası bir zaman-serisi kullanılarak ve gecikme tek bir sabit değeri için otomatik korelasyon işlem, @bluevoxel 'in kod karşı bu karşılaştırma

def lagged_auto_cov(Xi,t): 
    """ 
    for series of values x_i, length N, compute empirical auto-cov with lag t 
    defined: 1/(N-1) * \sum_{i=0}^{N-t} (x_i - x_s) * (x_{i+t} - x_s) 
    """ 
    N = len(time_series) 

    # use sample mean estimate from whole series 
    Xs = np.mean(Xi) 

    # construct copies of series shifted relative to each other, 
    # with mean subtracted from values 
    end_padded_series = np.zeros(N+t) 
    end_padded_series[:N] = Xi - Xs 
    start_padded_series = np.zeros(N+t) 
    start_padded_series[t:] = Xi - Xs 

    auto_cov = 1./(N-1) * np.sum(start_padded_series*end_padded_series) 
    return auto_cov 

, piton for döngü kod yaklaşık 30 mili-saniye ortalaması alınır ve numpy dizileri ortalama 0.3 mili saniyeden daha hızlı (dizüstü bilgisayarımda çalışıyor) kullanarak.

İlgili konular