如何使用 numpy.correlate 进行自相关? [英] How can I use numpy.correlate to do autocorrelation?

查看:61
本文介绍了如何使用 numpy.correlate 进行自相关?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要对一组数字进行自相关,据我所知,这只是该集合与其自身的相关性.

I need to do auto-correlation of a set of numbers, which as I understand it is just the correlation of the set with itself.

我已经使用 numpy 的相关函数尝试过它,但我不相信结果,因为它几乎总是给出一个向量,其中第一个数字不是最大的,因为它应该是.

I've tried it using numpy's correlate function, but I don't believe the result, as it almost always gives a vector where the first number is not the largest, as it ought to be.

所以,这个问题实际上是两个问题:

So, this question is really two questions:

  1. 究竟什么是numpy.correlate 在做什么?
  2. 我如何使用它(或其他东西)进行自相关?

推荐答案

为了回答你的第一个问题,numpy.correlate(a, v, mode) 正在执行 a<的卷积/code> 与 v 的反面,并给出由指定模式裁剪的结果.卷积的定义,C(t)=∑ -∞ <我<∞ aivt+i 其中 -∞ <<∞,允许从 -∞ 到 ∞ 的结果,但您显然不能存储无限长的数组.所以它必须被剪裁,这就是模式的用武之地.有 3 种不同的模式:full、same、&有效:

To answer your first question, numpy.correlate(a, v, mode) is performing the convolution of a with the reverse of v and giving the results clipped by the specified mode. The definition of convolution, C(t)=∑ -∞ < i < ∞ aivt+i where -∞ < t < ∞, allows for results from -∞ to ∞, but you obviously can't store an infinitely long array. So it has to be clipped, and that is where the mode comes in. There are 3 different modes: full, same, & valid:

  • 完整"模式为​​每个 t 返回结果,其中 av 有一些重叠.
  • 相同"模式返回与最短向量(av)长度相同的结果.
  • "valid" 模式仅在 av 彼此完全重叠时才返回结果.numpy.convolve 的文档代码> 提供了有关模式的更多详细信息.
  • "full" mode returns results for every t where both a and v have some overlap.
  • "same" mode returns a result with the same length as the shortest vector (a or v).
  • "valid" mode returns results only when a and v completely overlap each other. The documentation for numpy.convolve gives more detail on the modes.

对于你的第二个问题,我认为 numpy.correlate 给你自相关,它只是给你更多一点.自相关用于确定信号或函数在特定时间差与其自身的相似程度.在时间差为 0 时,自相关应该最高,因为信号与其自身相同,因此您预计自相关结果数组中的第一个元素将是最大的.但是,相关性不是从时间差 0 开始.它从负时间差开始,接近 0,然后变为正值.也就是说,您期望:

For your second question, I think numpy.correlate is giving you the autocorrelation, it is just giving you a little more as well. The autocorrelation is used to find how similar a signal, or function, is to itself at a certain time difference. At a time difference of 0, the auto-correlation should be the highest because the signal is identical to itself, so you expected that the first element in the autocorrelation result array would be the greatest. However, the correlation is not starting at a time difference of 0. It starts at a negative time difference, closes to 0, and then goes positive. That is, you were expecting:

自相关(a) = ∑ -∞ <我<∞ aivt+i 其中 0 <= t <;∞

autocorrelation(a) = ∑ -∞ < i < ∞ aivt+i where 0 <= t < ∞

但是你得到的是:

自相关(a) = ∑ -∞ <我<∞ aivt+i 其中 -∞ <<∞

autocorrelation(a) = ∑ -∞ < i < ∞ aivt+i where -∞ < t < ∞

您需要做的是取相关结果的后半部分,这应该是您正在寻找的自相关.一个简单的python函数来做到这一点:

What you need to do is take the last half of your correlation result, and that should be the autocorrelation you are looking for. A simple python function to do that would be:

def autocorr(x):
    result = numpy.correlate(x, x, mode='full')
    return result[result.size/2:]

当然,您需要进行错误检查以确保 x 实际上是一个一维数组.此外,这种解释在数学上可能不是最严格的.我一直在讨论无穷大,因为卷积的定义使用它们,但这不一定适用于自相关.所以,这个解释的理论部分可能有点不稳定,但希望实际结果会有所帮助.这些 有关自相关的页面非常有用,如果您不介意涉足符号和繁重的概念,可以为您提供更好的理论背景.

You will, of course, need error checking to make sure that x is actually a 1-d array. Also, this explanation probably isn't the most mathematically rigorous. I've been throwing around infinities because the definition of convolution uses them, but that doesn't necessarily apply for autocorrelation. So, the theoretical portion of this explanation may be slightly wonky, but hopefully the practical results are helpful. These pages on autocorrelation are pretty helpful, and can give you a much better theoretical background if you don't mind wading through the notation and heavy concepts.

这篇关于如何使用 numpy.correlate 进行自相关?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆