numpy中多维数组的自相关 [英] Autocorrelation of a multidimensional array in numpy
问题描述
我有一个二维数组,即序列的数组,它也是数组.对于每个序列,我想计算自相关,这样对于(5,4)数组,我将获得5个结果或维数为(5,7)的数组.
I have a two dimensional array, i.e. an array of sequences which are also arrays. For each sequence I would like to calculate the autocorrelation, so that for a (5,4) array, I would get 5 results, or an array of dimension (5,7).
我知道我可以循环浏览第一个维度,但这很慢,这是我的最后选择.还有另一种方法吗?
I know I could just loop over the first dimension, but that's slow and my last resort. Is there another way?
谢谢!
基于选择的答案以及mtrw的评论,我具有以下功能:
Based on the chosen answer plus the comment from mtrw, I have the following function:
def xcorr(x):
"""FFT based autocorrelation function, which is faster than numpy.correlate"""
# x is supposed to be an array of sequences, of shape (totalelements, length)
fftx = fft(x, n=(length*2-1), axis=1)
ret = ifft(fftx * np.conjugate(fftx), axis=1)
ret = fftshift(ret, axes=1)
return ret
请注意,在我的代码中length是一个全局变量,因此请务必声明它.我也没有将结果限制为实数,因为我还需要考虑复数.
Note that length is a global variable in my code, so be sure to declare it. I also didn't restrict the result to real numbers, since I need to take into account complex numbers as well.
推荐答案
使用我对您关于答案具有维度(5,7)的陈述感到困惑,所以也许有些我不理解的重要事情.
I'm a little confused by your statement about the answer having dimension (5, 7), so maybe there's something important I'm not understanding.
在mtrw的建议下,它没有环绕的填充版本:
At the suggestion of mtrw, a padded version that doesn't wrap around:
import numpy
from numpy.fft import fft, ifft
data = numpy.arange(5*4).reshape(5, 4)
padding = numpy.zeros((5, 3))
dataPadded = numpy.concatenate((data, padding), axis=1)
print dataPadded
##[[ 0. 1. 2. 3. 0. 0. 0. 0.]
## [ 4. 5. 6. 7. 0. 0. 0. 0.]
## [ 8. 9. 10. 11. 0. 0. 0. 0.]
## [ 12. 13. 14. 15. 0. 0. 0. 0.]
## [ 16. 17. 18. 19. 0. 0. 0. 0.]]
dataFT = fft(dataPadded, axis=1)
dataAC = ifft(dataFT * numpy.conjugate(dataFT), axis=1).real
print numpy.round(dataAC, 10)[:, :4]
##[[ 14. 8. 3. 0. 0. 3. 8.]
## [ 126. 92. 59. 28. 28. 59. 92.]
## [ 366. 272. 179. 88. 88. 179. 272.]
## [ 734. 548. 363. 180. 180. 363. 548.]
## [ 1230. 920. 611. 304. 304. 611. 920.]]
必须有一种更有效的方法来执行此操作,尤其是因为自相关是对称的,而我没有利用它.
There must be a more efficient way to do this, especially because autocorrelation is symmetric and I don't take advantage of that.
这篇关于numpy中多维数组的自相关的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!