Normalized Cross-Correlation in Python

回眸只為那壹抹淺笑 提交于 2019-12-23 07:28:19

问题


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:

degrees of freedom according to Chelton(1983)

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.

Is there any easy way to get the cross correlation function normalized in order to compute the degrees of freedom of two vectors?


回答1:


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]:

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 = (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://en.wikipedia.org/wiki/Cross-correlation




回答2:


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

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

c = b/a

This is my idea: but it will normalize it 0-1



来源:https://stackoverflow.com/questions/53436231/normalized-cross-correlation-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!