简体   繁体   English

正弦波自相关不正确

[英]Incorrect autocorrelation of sine wave

I'm generating a basic sine wave as follows: 我正在生成一个基本的正弦波,如下所示:

import numpy as np
x = np.linspace(-10, 10, 200)
x = np.sin(x)

Which looks correct when plotted: 绘制时看起来正确:

import matplotlib.pyplot as plt    
plt.plot(x)
plt.show()

在此处输入图片说明

But when I try to calculate and plot the normalized autocorrelation of this signal, I get some strange results: 但是,当我尝试计算并绘制此信号的归一化自相关时,会得到一些奇怪的结果:

autocorr = np.correlate(x, x, mode='full')    
autocorr /= autocorr[autocorr.argmax()]  # Normalize autocorrelation

plt.plot(autocorr)
plt.show()

在此处输入图片说明

Given a sine wave is perfectly periodic, I'm expecting the autocorrelation values to be 1.0 when in phase. 给定正弦波是完全周期性的,我期望同相时自相关值为1.0。 However, my autocorrelation peaks get smaller with more time steps 但是,随着时间的增加,我的自相关峰变小

Is there an error in my autocorrelation calculation? 我的自相关计算中有错误吗? Or is there another correlation function I should be using? 还是我应该使用另一个相关函数?

That looks right. 看起来不错。 The reason you don't get repeating peaks at 1.0 is because correlate treats the signal as zero outside the data. 之所以没有在1.0处重复出现峰值,是因为correlate将数据之外的信号视为零。 You can confirm this by zero-padding the input signal and looking at its correlation, which will match what you see above. 您可以通过对输入信号进行零填充并查看其相关性(与您在上面看到的相匹配)来确认这一点。 Try the following: it'll produce the same autocorrelation as your plot above. 尝试以下方法:它会产生与上面的图相同的自相关。

x0 = np.hstack([np.zeros_like(x), x, np.zeros_like(x)])
ac0 = np.correlate(x0, x0, mode='full')
ac0 /= ac0.max()
plt.plot(ac0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM