Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Return the discrete linear convolution of two one-dimensional sequences in Python
To return the discrete linear convolution of two one-dimensional sequences, use the numpy.convolve() method in Python NumPy.
The convolution operator is often seen in signal processing, where it models the effect of a linear time-invariant system on a signal. In probability theory, the sum of two independent random variables is distributed according to the convolution of their individual distributions.
Syntax
numpy.convolve(a, v, mode='full')
Parameters
The method accepts the following parameters ?
- a ? First one-dimensional input array
- v ? Second one-dimensional input array
- mode ? Optional parameter with values 'full', 'valid', 'same' (default is 'full')
Basic Example
Let's create two arrays and compute their convolution ?
import numpy as np
# Creating two numpy one-dimensional arrays
arr1 = np.array([1, 2, 3])
arr2 = np.array([0, 1, 0.5])
print("Array1:", arr1)
print("Array2:", arr2)
print("Shape of Array1:", arr1.shape)
print("Shape of Array2:", arr2.shape)
# Compute convolution
result = np.convolve(arr1, arr2)
print("\nConvolution result:", result)
Array1: [1 2 3] Array2: [0. 1. 0.5] Shape of Array1: (3,) Shape of Array2: (3,) Convolution result: [0. 1. 2.5 4. 1.5]
Understanding Convolution Modes
The mode parameter controls the size and computation of the output ?
import numpy as np
x = np.array([1, 2, 3])
h = np.array([0, 1, 0.5])
# Full mode (default) - returns full convolution
full_conv = np.convolve(x, h, mode='full')
print("Full mode:", full_conv)
# Valid mode - returns only valid (non-padded) part
valid_conv = np.convolve(x, h, mode='valid')
print("Valid mode:", valid_conv)
# Same mode - returns output of same size as largest input
same_conv = np.convolve(x, h, mode='same')
print("Same mode:", same_conv)
Full mode: [0. 1. 2.5 4. 1.5] Valid mode: [2.5] Same mode: [1. 2.5 4. ]
Convolution Mode Comparison
| Mode | Output Size | Description |
|---|---|---|
| 'full' | len(a) + len(v) - 1 | Complete convolution (default) |
| 'valid' | max(len(a), len(v)) - min(len(a), len(v)) + 1 | Only where arrays fully overlap |
| 'same' | max(len(a), len(v)) | Same size as largest input |
Practical Signal Processing Example
Here's a simple example of using convolution for signal smoothing ?
import numpy as np
# Original signal with noise
signal = np.array([1, 4, 2, 8, 3, 7, 5])
# Simple moving average filter
filter_kernel = np.array([1/3, 1/3, 1/3])
# Apply convolution for smoothing
smoothed = np.convolve(signal, filter_kernel, mode='valid')
print("Original signal:", signal)
print("Filter kernel:", filter_kernel)
print("Smoothed signal:", smoothed)
Original signal: [1 4 2 8 3 7 5] Filter kernel: [0.33333333 0.33333333 0.33333333] Smoothed signal: [2.33333333 4.66666667 4.33333333 6. 5. ]
Conclusion
NumPy's convolve() function efficiently computes discrete linear convolution between two 1D sequences. Use different modes ('full', 'valid', 'same') to control the output size based on your application needs.
