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

Viewed 2061

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?

2 Answers

You can refer to the numpy manual,https://docs.scipy.org/doc/numpy/reference/generated/numpy.convolve.html,

Here is the example of numpy.convolve from the manual (the "mode" in numpy.correlate is the same as numpy.convolve):

>>>np.convolve([1, 2, 3], [0, 1, 0.5])

array([ 0. , 1. , 2.5, 4. , 1.5])

#Only return the middle values of the convolution. Contains boundary effects, where zeros are taken into account:

>>>np.convolve([1,2,3],[0,1,0.5], 'same')

array([ 1. , 2.5, 4. ])

#The two arrays are of the same length, so there is only one position where they completely overlap:

>>>np.convolve([1,2,3],[0,1,0.5], 'valid')

array([ 2.5])

Related