【发布时间】:2021-11-19 03:46:44
【问题描述】:
我正在尝试解决一个相关问题,我需要找到在信号序列中找到模式序列的位置。在某些时候,我能够找到正确的解决方案,只是开始尝试优化代码,而我完成的代码没有保存。现在互相关函数无法正确求解,我不知道为什么。我已经多次重启内核了。
这里是代码和包含信号和模式的文本文件的链接。
https://drive.google.com/file/d/1tBzHMUfmcx_gGR0arYPaQ5GB9MybXKRv/view?usp=sharing https://drive.google.com/file/d/1TeSe9t8TeVHEp2BxKXYz6Ndlpah--yLg/view?usp=sharing
import numpy as np
import matplotlib.pyplot as plt
patron = np.loadtxt('patron.txt', delimiter=',', skiprows=1)
senal = np.loadtxt('señal.txt', delimiter=',', skiprows=1)
Fs=100
ts = np.arange(0,len(senal))
plt.figure(figsize=(20,8))
plt.subplot(3,1,1)
plt.plot(ts,patron)
plt.subplot(3,1,2)
plt.plot(ts,senal)
corr = np.correlate(senal,patron,"same")
print(np.where(corr == np.amax(corr))) #this should be where correlation reaches its maximum value, and where the functions are most "similar"
plt.subplot(3,1,3)
plt.plot(ts,corr, 'r')
我怎么知道我做对了?我绘制了移动了 799 个位置的“senal”序列(代码正确时的值):
np.roll(senal,799)
plt.plot(senal)
这导致了这个图表。当它在索引 799 处产生最大相关性时,它看起来非常直观:
【问题讨论】:
标签: python numpy jupyter-notebook jupyter cross-correlation