In Python, we use the numpy.convolve()
method to calculate the combination of two one-dimensional vectors discretely and linearly.
numpy.convolve(v1, v2, mode)
v1
: This is the 1st input array.v2
: This is the 2nd input array.mode
(optional): There are three different modes:
full
: This is the default mode, which returns the convolution at each overlap point with an output shape of (v1+v2-1,). The signals don’t wholly overlap at the convolution’s endpoints, and boundary effects can be noticed.same
: This returns the output with the maximum length (v2, v1). The effects of boundaries can still be seen.valid
: This produces a length of max(v2, v1) - min(v2, v1) + 1. Only points where the signals overlap are provided with the convolution product. Outside of the signal boundary, values have no effect.The numpy.convolve()
method returns discrete, linear convolution of two one-dimensional vectors
The following code shows how to use Python’s numpy.convolve()
method.
# import numpyimport numpy as np# create arrays using np.arrayv1 = np.array(range(1,10,2))v2 = np.array(range(5,15,3))# compute discrete, linear convolution# and store the result in result# By default mode='full'result = np.convolve(v1, v2)print(result)# using mode='same' argumentresult = np.convolve(v1, v2, mode='same')print(f"using mode='same' argument: {result}")# using mode='valid' argumentresult = np.convolve(v1, v2, mode='valid')print(f"using mode='valid' argument: {result}")
numpy
library.v1
and v2
using range()
method.np.convolve()
method is used to calculate the discrete, linear convolution of two one-dimensional vectors (v1 & v2) The result is stored in a new variable called result
Note: By default, the mode argument is set to
full.
Free Resources