python - 使用 Python 估计自相关

标签 python numpy signal-processing

我想对下面显示的信号执行自相关。两个连续点之间的时间为 2.5ms(或重复率为 400Hz)。

enter image description here

这是我想使用的估计自增相关的方程(取自 http://en.wikipedia.org/wiki/Autocorrelation,估计部分):

enter image description here

在 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/

相关文章:

python - 根据条件在多个数据帧的数据帧内的列之间交换行

python / NumPy : How to extract interior of any dimension of numpy array?

c - 归一化最小均方 - C 实现

c# - 使用 c# 规范化幅度和相位

ios - 在 iOS 中比较两个频谱图

python - lxml看不到解析标签的属性

Python 2D列表到字典

python - 使用 pandas 系列元素作为边界的简洁/优雅集成

php - Django 、Python : Is there a simple way to convert PHP-style bracketed POST keys to multidimensional dict?

python - OpenCV - 在将图像写入文件时指定格式 (cv2.imwrite)