[英]The normalized cross-correlation of two signals in python
I wanted to calculate the normalized cross-correlation function of two signals where "x" axes is the time delay and "y" axes is value of correlation between -1
and 1
.我想计算两个信号的归一化互相关 function ,其中“x”轴是时间延迟,“y”轴是-1
和1
之间的相关值。 so I decided to use scipy.所以我决定使用 scipy。
I use the command corr = signal.correlate(s1['Strain'], s2['Strain'], mode='full')
我使用命令corr = signal.correlate(s1['Strain'], s2['Strain'], mode='full')
where s1['Strain']
and s2['Strain']
are the pandas dataframe values but it doesn't return the normalized function with "x" axes as time delay.其中s1['Strain']
和s2['Strain']
是 pandas dataframe 值,但它不返回归一化的 ZC1C425268E68385D1AB5074C17A94F1 轴。 Here is example data这是示例数据
s1:
Strain
0 -1.587702e-22
1 -1.425868e-22
2 -1.174897e-22
3 -8.559119e-23
4 -4.949480e-23
. .
. .
. .
for s2
it looks similar.对于s2
,它看起来很相似。 I knew the sampling of both datasets, it's 4096 kHz.我知道这两个数据集的采样频率是 4096 kHz。
Thank for your help.感谢您的帮助。
First of all to get normalized coefficient (such that as lag 0, we get the Pearson correlation):首先要得到归一化系数(例如滞后 0,我们得到 Pearson 相关性):
out = correlate(x/np.std(x), y/np.std(y), 'full') / min(len(x), len(y))
Now for the lags, from the official documentation of correlate
one can read that the full output of cross-correlation is given by:现在对于滞后,从correlate
的官方文档中可以读到完整的互相关 output 由下式给出:
z[k] = (x * y)(k - N + 1)
= \sum_{l=0}^{||x||-1}x_l y_{l-k+N-1}^{*}\]
Where *
denotes the convolution, and k goes from 0 up to ||x|| + ||y|| - 2
其中*
表示卷积,k从0到||x|| + ||y|| - 2
||x|| + ||y|| - 2
||x|| + ||y|| - 2
precisely. ||x|| + ||y|| - 2
精确。 N is max(len(x), len(y))
. N 是max(len(x), len(y))
。
The lags are denoted above as the argument of the convolution (x * y)
, so they range from 0 - N + 1
to ||x|| + ||y|| - 2 - N + 1
滞后在上面表示为卷积(x * y)
的参数,因此它们的范围从0 - N + 1
到||x|| + ||y|| - 2 - N + 1
||x|| + ||y|| - 2 - N + 1
||x|| + ||y|| - 2 - N + 1
which is n - 1
with n=min(len(x), len(y))
. ||x|| + ||y|| - 2 - N + 1
这是n - 1
与n=min(len(x), len(y))
。
Also, by briefly looking at the source code, I think they swap x
and y
sometimes if convenient... (hence the min(len(x), len(y))
in the normalisation above. However this implies to change the start of our lags, therefore:另外,通过简要查看源代码,我认为他们有时会在方便的情况下交换x
和y
......(因此在上面的规范化中使用min(len(x), len(y))
。但这意味着改变开始我们的滞后,因此:
N = max(len(x), len(y))
n = min(len(x), len(y))
# if len(x) < (len(y):
lags = np.arange(-N + 1, n)
# else:
lags = np.arange(-n + 1, N)
Check this code on two time-series for which you want to plot the cross-correlation of:在您想要 plot 的互相关的两个时间序列上检查此代码:
import numpy as np
import matplotlib.pyplot as plt
from scipy.signal import correlate
def plot_xcorr(x, y):
"Plot cross-correlation (full) between two signals."
N = max(len(x), len(y))
n = min(len(x), len(y))
if N == len(y):
lags = np.arange(-N + 1, n)
else:
lags = np.arange(-n + 1, N)
c = correlate(x / np.std(x), y / np.std(y), 'full')
plt.plot(lags, c / n)
plt.show()
To calculate the time delay between two signals, we need to find the cross-correlation between two signals and find the argmax.要计算两个信号之间的时间延迟,我们需要找到两个信号之间的互相关并找到 argmax。
Assuming data_1
and data_2
are samples of two signals:假设data_1
和data_2
是两个信号的样本:
import numpy as np
import pandas as pd
correlation = np.correlate(data_1, data_2, mode='same')
delay = np.argmax(correlation) - int(len(correlation)/2)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.