How to implement `scipy.signal.convolve()` in Python

SciPy is a Python-based module built on top of NumPy, offering a wide range of scientific and technical computing functionalities.

Signal processing in SciPy is a sub-module that provides functions and tools for analyzing, filtering, transforming, and manipulating signals, often represented as digital signals for tasks like convolution.

The scipy.signal.convolve() function

Convolution is a basic signal-processing operation for filtering, edge detection, and feature extraction tasks.

The scipy.signal.convolve() function is part of SciPy’s signal processing module and performs linear convolution of two 1-dimensional sequences.

Syntax

The syntax scipy.signal.convolve() is given below:

scipy.signal.convolve(in1, in2, mode='full')
Syntax for scipy.signal.convolve() function
  • in1 is a required parameter and the first input 1-dimensional array.

  • in2 is a required parameter, and the second input is a 1-dimensional array.

  • mode is an optional parameter and represents the type of convolution. It can be 'full', 'valid', or 'same'. The default value is 'full'.

Note: Make sure you have the SciPy library installed. To learn more about the SciPy installation on your system, click here.

Code

Let’s implement the function scipy.signal.convolve() with a simple code example:

import numpy as np
from scipy.signal import convolve
#The input sequences
sequence1 = np.array([1, 2, 3, 4])
sequence2 = np.array([2, 1])
#Performing convolution
result = convolve(sequence1, sequence2, mode='full')
#Printing the result
print("Result of Convolution:", result)

Code explanation

  • Line 1–2: Firstly, we import the necessary modules. numpy for numerical operations and scipy.signal.convolve from SciPy for performing convolution.

  • Line 5–6: Next, we define two input 1-dimensional sequences, sequence1 and sequence2.

  • Line 9: Then, we use the function scipy.signal.convolve() to perform the convolution of sequence1 and sequence2 using the 'full' mode. The 'full' mode returns the full convolution, including zero-padding. The result of the convolution is stored in the result variable.

  • Line 12: Finally, we print the output on the console.

Output

Upon execution, the code will use the function scipy.signal.convolve() and perform convolution of sequence1 and sequence2.

The output of the above code looks like this:

Result of Convolution: [2 5 8 11 4]

The convolution operation involves sliding the sequence2 over sequence1, multiplying the corresponding elements, and summing them up. We use zero padding to extend the sequences to avoid boundary issues during convolution.

Here, you will find the step-by-step breakdown of the convolution:

canvasAnimation-image
1 of 6

Finally, the result of the convolution operation is [2 5 8 11 4].

Note: We used a different color for the zero in the illustration to distinguish it from other digits.

Conclusion

In conclusion, the scipy.signal.convolve() method in Python's SciPy module is a strong tool for conducting 1-dimensional linear convolution and is used in signal-processing activities such as filtering and feature extraction by researchers and developers.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved