NumPy convolve()

The numpy.convolve() function computes the discrete, linear convolution of two one-dimensional sequences. Convolution is commonly used in signal processing to model the effect of a linear time-invariant system on a signal.

Syntax

</>
Copy
numpy.convolve(a, v, mode='full')

Parameters

ParameterTypeDescription
a(N,) array_likeFirst one-dimensional input array.
v(M,) array_likeSecond one-dimensional input array.
mode{‘full’, ‘valid’, ‘same’}, optionalDetermines the size of the output array.

Modes in Convolution

ModeDescription
'full'Returns the convolution at each point of overlap with an output size of (N+M-1). Includes boundary effects.
'same'Returns an output of length max(N, M), keeping the center of the convolution.
'valid'Returns only values where the input arrays fully overlap, with an output size of max(N, M) – min(N, M) + 1.

Return Value

Returns an ndarray containing the discrete, linear convolution of the input arrays.


Examples

1. Basic Convolution Using Default Mode (‘full’)

In this example, we perform a simple convolution using two one-dimensional arrays.

</>
Copy
import numpy as np

# Define two input sequences
a = np.array([1, 2, 3])
v = np.array([0, 1, 0.5])

# Compute the convolution using default mode ('full')
result = np.convolve(a, v)

# Print the result
print("Convolution result (full mode):", result)

Output:

Convolution result (full mode): [0.  1.  2.5  4.  1.5]