使用 fft 返回向后输出计算互相关

Calculating cross-correlation with fft returning backwards output

我试图通过对两组数据进行傅立叶变换并将第一个 fft 与第二个 fft 的共轭相乘,然后再转换回时间 space 来交叉关联两组数据。为了测试我的代码,我将输出与 numpy.correlate 的输出进行比较。然而,当我绘制我的代码时,(仅限于某个 window),这两个信号似乎相反 directions/are 镜像大约为零。

这是我的输出结果

我的代码:

import numpy as np
import pyplot as plt

phl_data = np.sin(np.arange(0, 10, 0.1))
mlac_data = np.cos(np.arange(0, 10, 0.1))
N = phl_data.size
zeroes = np.zeros(N-1)
phl_data = np.append(phl_data, zeroes)
mlac_data = np.append(mlac_data, zeroes)

# cross-correlate x = phl_data, y = mlac_data:
# take FFTs:
phl_fft = np.fft.fft(phl_data)
mlac_fft = np.fft.fft(mlac_data)
# fft of cross-correlation
Cw = np.conj(phl_fft)*mlac_fft
#Cw = np.fft.fftshift(Cw)
# transform back to time space:
Cxy = np.fft.fftshift(np.fft.ifft(Cw))
times = np.append(np.arange(-N+1, 0, dt),np.arange(0, N, dt))

plt.plot(times, Cxy)
plt.xlim(-250, 250)

# test against convolving:
c = np.correlate(phl_data, mlac_data, mode='same')
plt.plot(times, c)
plt.show()

(两个数据集都被N-1个零填充)

documentation to numpy.correlate 对此进行了解释:

This function computes the correlation as generally defined in signal processing texts:

c_{av}[k] = sum_n a[n+k] * conj(v[n])

和:

Notes

The definition of correlation above is not unique and sometimes correlation may be defined differently. Another common definition is:

c'_{av}[k] = sum_n a[n] conj(v[n+k])

which is related to c_{av}[k] by c'_{av}[k] = c_{av}[-k].

因此,没有唯一的定义,两个共同的定义导致相反的输出。