我设法为1d-数组编写了一个无偏自相关函数:
import numpy as np
def autocorr(x):
x_norm = (x - x.mean())/x.std()
result = np.correlate(x_norm, x_norm, mode='full')
vec_unbi = np.array([x.size - abs(i) for i in range(0, x.size)])
return result[result.size//2:]/x.size
def autocorr_unbiased(x):
vec_unbi = np.array([x.size - abs(i) for i in range(0, x.size)])
return x.size*autocorr(x)/vec_unbi
效果很好:
N_samples = 1000
t = np.linspace(0,1000,N_samples)
y = np.sin(2*np.pi*t/200)
plt.figure()
plt.plot(autocorr(y), label = 'biased')
plt.plot(autocorr_unbiased(y), label = 'unbiased')
plt.legend()
plt.savefig('test.png')
plt.show()
现在,我具有2d自相关函数:
from scipy.signal import fftconvolve
def autocorr2D(A):
return fftconvolve(A,A[::-1, ::-1])
有偏见的人:
x = np.linspace(0,500,500)
y = np.linspace(0,500,500)
X, Y = np.meshgrid(y,x)
h = np.sin((2*np.pi/100)*(np.cos(np.pi/3)*X + np.sin(np.pi/3)*Y))
plt.figure()
plt.contourf(autocorr2D(h))
plt.show()
如何与我的2D正弦的理论统计自相关相匹配呢?