SciPy Convolve
“Convolution” is a mathematical operation/process that takes two functions as input and generates a third function that represents the integral of the pointwise multiplication of the two functions. In the case of arrays, convolution can be used to smooth out data, filter out noise, or find patterns in data. The Python “scipy.signal.convolve()” function performs convolution on two arrays.
This tutorial presents a detailed guide on the “scipy.signal.convolve()” function with relevant examples and the following content:
- What is the “scipy.signal.convolve()” Function in Python?
- Determining the Convolution Using the “scipy.signal.convolve()” Function.
- Determining the Convolution Using the “valid” Parameter of the “scipy.signal.convolve()” Function.
- Plotting Convolution of Input Arrays Using Matplotlib.
- Determining the Convolution Using the “scipy.ndimage.convolve()” Method.
What is the “scipy.signal.convolve()” Function in Python?
The “scipy.signal.convolve()” function in Python is used to perform convolution on two arrays.
Syntax
Parameters
In the above syntax:
- The “in1” and “in2” parameters specify the first and second input arrays having the same dimensional numbers, respectively.
- The “mode” and “method” parameters specify the output size and the method used to determine the convolution.
Return Value
The “scipy.signal.convolve()” function retrieves the subset of discrete linear convolution of the given arrays.
Example 1: Determining the Convolution Using the “scipy.signal.convolve()” Function
The below example code is used to compute the convolution:
import scipy
arr1 = numpy.array([21, 42, 44, 35])
arr2 = numpy.array([46, 37, 39, 10])
print(scipy.signal.convolve(arr1, arr2))
In the above code:
- The “numpy” and “scipy” modules are imported.
- The “numpy.array()” function initializes two arrays named as “arr1” and “arr2”.
- The “scipy.signal.convolve()” function determines the convolution of the input arrays.
Output
The convolution has been calculated successfully.
Example 2: Determining the Convolution Using the “valid” Parameter of the “scipy.signal.convolve()” Function
Let’s overview the following example code:
import scipy
arr1 = numpy.array([21, 42, 44, 35])
arr2 = numpy.array([46, 37, 39, 10])
print(scipy.signal.convolve(arr1, arr2, mode='valid'))
In this example, the “valid” mode is used, so the output only includes the elements of the convolution that do not rely on zero-padding.
Output
The convolution element that doesn’t rely on zero padding has been shown in the above output.
Example 3: Plotting Convolution of Input Arrays Using Matplotlib
The below code is used to plot the convolution of input arrays via “Matplotlib”:
import scipy
import matplotlib.pyplot as plt
arr1 = numpy.array([5, 10, 15, 25])
arr2 = numpy.array([2, 3, 7, 4])
output = scipy.signal.convolve(arr1, arr2)
plt.plot(arr1, label='Array-1')
plt.plot(arr2, label='Array-2')
plt.plot(output, label='Convolution')
plt.legend()
plt.show()
According to the above code:
- The “numpy”, “scipy” and “matplotlib.pyplot” modules are imported.
- The “scipy.signal.convolve()” function is used to determine the convolution of the given arrays.
- The “plot()” function of the “matplotlib” module is used to plot the convolution.
Output
The convolution of input arrays has been plotted successfully.
Alternative Approach: Determining the Convolution Using the “scipy.ndimage.convolve()” Method
The “scipy.ndimage.convolve()” method is used to determine the multidimensional convolution of the input arrays. Here is an example:
import numpy
arr1 = numpy.array([[2, 0, 2],[2, 2, 0],[2, 1, 0]])
arr2 = numpy.array([[1, 2, 1],[0, 0, 1],[1, 0, 2],[1, 0, 1],[1, 0, 0]])
print(ndimage.convolve(arr1, arr2, mode='nearest'))
In the above code lines, the SciPy “ndimage.convolve()” method takes the initialized arrays and the “nearest” mode as its arguments, respectively to compute the convolution.
Output
The convolution has been determined.
Conclusion
In Python, the “scipy.signal.convolve()” function of the “scipy.signal” module is used to perform convolution on two arrays. This function can also perform convolution based on various “modes” and “methods” parameter values. The “scipy.ndimage.convolve()” method, however, calculates the convolution of a multidimensional array. This Python tutorial presented a detailed overview of the SciPy “convolve()” function using appropriate examples.
Source: linuxhint.com