圆形互相关python

Lam*_*t_W 8 python signal-processing numpy matplotlib scipy

是否可以在具有numpy/scipy/matplotlib函数的1D阵列上执行循环交叉/自相关?我看过numpy.correlate()和matplotlib.pyplot.xcorr(基于numpy函数),两者似乎都无法进行循环互相关.

为了说明不同之处,我将使用[1,2,3,4]数组的示例.通过循环相关,进行周期性假设,滞后1看起来像[2,3,4,1].我发现的python函数似乎只使用零填充,即[2,3,4,0].有没有办法让这些函数进行循环关联?如果没有,是否有循环关联的标准解决方法?

War*_*ser 12

您可以使用FFT实现周期性(也称为圆形)互相关:

from numpy.fft import fft, ifft

def periodic_corr(x, y):
    """Periodic correlation, implemented using the FFT.

    x and y must be real sequences with the same length.
    """
    return ifft(fft(x) * fft(y).conj()).real
Run Code Online (Sandbox Code Playgroud)

np.correlate如果您不介意由此产生的开销,您也可以使用它来实现它np.hstack((y[1:], y)):

import numpy as np

def periodic_corr_np(x, y):
    """Periodic correlation, implemented using np.correlate.

    x and y must be real sequences with the same length.
    """
    return np.correlate(x, np.hstack((y[1:], y)), mode='valid')
Run Code Online (Sandbox Code Playgroud)

  • 根据[1]和一些[其他来源](http://mathworld.wolfram.com/Cross-Correlation.html),第一项应该共轭:`ifft(fft(x).conj() * fft(y )).real` [1] Papoulis, A. 傅里叶积分及其应用。纽约:McGraw-Hill,第 244-245 页和 252-253 页,1962 年。[Google 图书链接](https://books.google.de/books?id=txMIAQAAIAAJ) (2认同)