我想对下面显示的信号执行自相关。两个连续点之间的时间为 2.5ms(或重复率为 400Hz)。
这是我想使用的估计自增相关的方程(取自 http://en.wikipedia.org/wiki/Autocorrelation,估计部分):
在 python 中查找我的数据的估计自相关的最简单方法是什么?我可以使用类似于 numpy.correlate
的东西吗?
或者我应该只计算均值和方差?
编辑:
在 unutbu 的帮助下,我写过:
from numpy import *
import numpy as N
import pylab as P
fn = 'data.txt'
x = loadtxt(fn,unpack=True,usecols=[1])
time = loadtxt(fn,unpack=True,usecols=[0])
def estimated_autocorrelation(x):
n = len(x)
variance = x.var()
x = x-x.mean()
r = N.correlate(x, x, mode = 'full')[-n:]
#assert N.allclose(r, N.array([(x[:n-k]*x[-(n-k):]).sum() for k in range(n)]))
result = r/(variance*(N.arange(n, 0, -1)))
return result
P.plot(time,estimated_autocorrelation(x))
P.xlabel('time (s)')
P.ylabel('autocorrelation')
P.show()
最佳答案
我认为这个特定的计算没有 NumPy 函数。这是我的写法:
def estimated_autocorrelation(x):
"""
http://stackoverflow.com/q/14297012/190597
http://en.wikipedia.org/wiki/Autocorrelation#Estimation
"""
n = len(x)
variance = x.var()
x = x-x.mean()
r = np.correlate(x, x, mode = 'full')[-n:]
assert np.allclose(r, np.array([(x[:n-k]*x[-(n-k):]).sum() for k in range(n)]))
result = r/(variance*(np.arange(n, 0, -1)))
return result
断言语句用于检查计算并记录其意图。
当您确信此函数按预期运行时,您可以注释掉 assert
语句,或使用 python -O
运行您的脚本。 (-O
标志告诉 Python 忽略断言语句。)
关于python - 使用 Python 估计自相关,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14297012/