numpy.convolve#
- numpy.convolve(a,v,mode='full')[source]#
Returns the discrete, linear convolution of two one-dimensional sequences.
The convolution operator is often seen in signal processing, where itmodels the effect of a linear time-invariant system on a signal[1]. Inprobability theory, the sum of two independent random variables isdistributed according to the convolution of their individualdistributions.
Ifv is longer thana, the arrays are swapped before computation.
- Parameters:
- a(N,) array_like
First one-dimensional input array.
- v(M,) array_like
Second one-dimensional input array.
- mode{‘full’, ‘valid’, ‘same’}, optional
- ‘full’:
By default, mode is ‘full’. This returns the convolutionat each point of overlap, with an output shape of (N+M-1,). Atthe end-points of the convolution, the signals do not overlapcompletely, and boundary effects may be seen.
- ‘same’:
Mode ‘same’ returns output of length
max(M,N). Boundaryeffects are still visible.- ‘valid’:
Mode ‘valid’ returns output of length
max(M,N)-min(M,N)+1. The convolution product is only givenfor points where the signals overlap completely. Values outsidethe signal boundary have no effect.
- Returns:
- outndarray
Discrete, linear convolution ofa andv.
See also
scipy.signal.fftconvolveConvolve two arrays using the Fast Fourier Transform.
scipy.linalg.toeplitzUsed to construct the convolution operator.
polymulPolynomial multiplication. Same output as convolve, but also accepts poly1d objects as input.
Notes
The discrete convolution operation is defined as
\[(a * v)_n = \sum_{m = -\infty}^{\infty} a_m v_{n - m}\]It can be shown that a convolution\(x(t) * y(t)\) in time/spaceis equivalent to the multiplication\(X(f) Y(f)\) in the Fourierdomain, after appropriate padding (padding is necessary to preventcircular convolution). Since multiplication is more efficient (faster)than convolution, the function
scipy.signal.fftconvolveexploits theFFT to calculate the convolution of large data-sets.References
[1]Wikipedia, “Convolution”,https://en.wikipedia.org/wiki/Convolution
Examples
Note how the convolution operator flips the second arraybefore “sliding” the two across one another:
>>>importnumpyasnp>>>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 takeninto 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 thereis only one position where they completely overlap:
>>>np.convolve([1,2,3],[0,1,0.5],'valid')array([2.5])