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

查看:93
本文介绍了如何使用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)av的倒数进行卷积,并给出由指定模式裁剪的结果. 卷积的定义,C(t)= ∑ -∞<我< ∞ a i v t + i 其中-∞< t < ∞,允许从-∞到∞的结果,但是显然不能存储无限长的数组.因此必须对其进行裁剪,这就是该模式的用处.共有3种不同的模式:完全,相同和&有效:

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:

  • 完全"模式为每个tv都重叠的每个t返回结果.
  • 相同"模式返回的结果的长度与最短向量(av)的长度相同.
  • 仅当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)= ∑ -∞<我< ∞ a i v t + i 其中0< = t< ∞

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

但是您得到的是:

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

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天全站免登陆