.convolve()
The .convolve
function from the scipy.signal
module is used to compute the convolution of two input arrays. Convolution is a mathematical process that combines two sequences to produce a third sequence, representing the way one sequence is modified by the other. It is frequently used in signal processing, data analysis, and machine learning tasks, such as filtering signals or image processing.
This technique has a wide array of uses, including in the context of neural networks and signal filtering.
Syntax
scipy.signal.convolve(in1, in2, mode='full', method='auto')
Parameters
in1
: The first input array (array-like). It is the sequence to be convolvedin2
: The second input array (array-like). It is the other sequence to be convolved.mode
(Optional): Defines the size of the output:'full'
: Returns the full convolution (default).'valid'
: Outputs only the parts that are fully overlapped.'same'
: The output size is the same as the first input array.
method
(Optional): Specifies the convolution method:'auto'
(default): Chooses the optimal method automatically.'direct'
: Uses the direct method of convolution.'fft'
: Uses Fast Fourier Transform for the convolution.
This function returns the result of the convolution as an ndarray, which is the convolved output array.
Example
Here’s an example demonstrating the use of .convolve()
:
import numpy as npfrom scipy.signal import convolve# Define the signal and the kernelsignal = [1, 2, 3, 4]kernel = [0.5, 1.0, 0.5]# Perform convolution with 'full' modefull_result = convolve(signal, kernel, mode='full')print("Full mode result:", full_result)# Perform convolution with 'same' modesame_result = convolve(signal, kernel, mode='same')print("Same mode result:", same_result)# Perform convolution with 'valid' modevalid_result = convolve(signal, kernel, mode='valid')print("Valid mode result:", valid_result)
The code above generates this output:
Full mode result: [0.5 2. 4. 6. 5.5 2. ]Same mode result: [2. 4. 6. 5.5]Valid mode result: [4. 6.]
In this example, convolution is used to apply a smoothing kernel to the input signal, which can be useful for filtering noise or processing time-series data.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn SciPy on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Course
Learn Python 3
Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.With CertificateBeginner Friendly23 hours