Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does the mode 'same' in np.correlate() work?

Tags:

python

numpy

I am trying to understand the mode "same" in the function np.correlate(). I saw the definition in some website, but I can't understand how it does the computation to get the final array.

How from this: np.correlate([2, 1], [1, 1], 'same') we get this: array([2, 3])

Can someone make a computation example?

like image 833
Salvador Vigo Avatar asked Nov 27 '25 03:11

Salvador Vigo


1 Answers

The mode='same' just implies that you will have a result equal to the size of the largest input array. It is a subset of the full cross correlation (there is a mode='full' option as well). In your example, we have:

  2 1 
1 1
------
0+2+0 = 2

(dot product, zero fill where appropriate, then "slide")

  2 1
  1 1
-----
  2+1 = 3

Hence the answer of [2,3].

The full cross correlation would continue sliding.

  2 1
    1 1
-------
  0+1+0 = 1

So, the full cross correlation would be [2,3,1].

Note: These keywords are derived from the MATLAB implementations of these functions

like image 80
Matt Messersmith Avatar answered Nov 28 '25 16:11

Matt Messersmith