Skip to content Skip to sidebar Skip to footer

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: degrees of freedom ac

Solution 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

enter image description here

Solution 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.

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.

Solution 3:

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

Post a Comment for "Normalized Cross-correlation In Python"