简体   繁体   English

"Python中的归一化互相关"

[英]Normalized Cross-Correlation in Python

I have been struggling the last days trying to compute the degrees of freedom of two pair of vectors (x and y) following reference of Chelton (1983) which is:最近几天,我一直在努力计算两对向量(x 和 y)的自由度,参考 Chelton(1983),即:

degrees of freedom according to Chelton(1983)<\/a>根据 Chelton (1983) 的自由度<\/a>

and I can't find a proper way to calculate the normalized cross correlation function using np.correlate, I always get an output that it isn't in between -1, 1.而且我找不到使用 np.correlate 计算归一化互相关函数的正确方法,我总是得到一个不在 -1、1 之间的输出。

Is there any easy way to get the cross correlation function normalized in order to compute the degrees of freedom of two vectors?是否有任何简单的方法可以使互相关函数归一化以计算两个向量的自由度?

"

Nice Question.好问题。 There is no direct way but you can "normalize" the input vectors before using np.correlate like this and reasonable values will be returned within a range of [-1,1]:没有直接的方法,但您可以在使用np.correlate之前“规范化”输入向量,并且合理的值将在 [-1,1] 的范围内返回:

Here i define the correlation as generally defined in signal processing textbooks.在这里,我定义了信号处理教科书中通常定义的相关性。

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

CODE: If a and b are the vectors:代码:如果 a 和 b 是向量:

a = (a - np.mean(a)) / (np.std(a) * len(a))
b = (b - np.mean(b)) / (np.std(b))
c = np.correlate(a, b, 'full')

References:参考:

https://docs.scipy.org/doc/numpy/reference/generated/numpy.correlate.html https://docs.scipy.org/doc/numpy/reference/generated/numpy.correlate.html

https://en.wikipedia.org/wiki/Cross-correlation https://en.wikipedia.org/wiki/Cross-correlation

在此处输入图片说明

Normalized cross correlation formula: Source : https://anomaly.io/detect-correlation-time-series/ 归一化互相关公式:来源: https//anomaly.io/detect-correlation-time-series/ 归一化互相关公式

Python code: 'a' and 'b' are series to find a correlation and it's type are pandas.Dataframe Python代码:'a'和'b'是用于查找关联的系列,它的类型是pandas.Dataframe

np.sum((a*b))/(np.sqrt((np.sum(a**2))*(np.sum(b**2))))

The function numpy.corrcoef does this directly, as computing the covariance matrix of x and y and then normalizing it by the standard deviation of x and the standard deviation of y.函数 numpy.corrcoef 直接执行此操作,即计算 x 和 y 的协方差矩阵,然后通过 x 的标准偏差和 y 的标准偏差对其进行归一化。

https://numpy.org/doc/stable/reference/generated/numpy.corrcoef.html#numpy.corrcoef https://numpy.org/doc/stable/reference/generated/numpy.corrcoef.html#numpy.corrcoef

This is the Pearson correlation coefficient and will have a range of +/-1.这是 Pearson 相关系数,范围为 +/-1。

# MATLAB -> xcorr(a, b, 'normalized');
# In MATLAB normalized cross-correlation calculated like this and I tried to implement 
# in Python.

import numpy as np
a = [1, 2, 3, 4]
b = [2, 4, 6, 8]
norm_a = np.linalg.norm(a)
a = a / norm_a
norm_b = np.linalg.norm(b)
b = b / norm_b
c = np.correlate(a, b, mode = 'full')

a = np.dot(abs(var1),abs(var2),'full')

b = np.correlate(var1,var2,'full')

c = b/a

这是我的想法:但它会将其标准化为 0-1

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

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