Scipy Convolve – Complete Guide

In this Python tutorial, we will learn about the “Scipy Convolve” and additionally we will cover the following topics using some examples.

  • Scipy Convolve 3d
  • Scipy Convolve 2d
  • Scipy Convolve 1d
  • Scipy Convolve fft
  • Scipy Convolve gaussian
  • Scipy Convolve stride
  • Scipy Convolve along axis
  • Scipy Convolve kernel

Scipy Convolve 1d

The Scipy has a method convolve1d() within module scipy.ndimage that computes the one-dimensional convolution on a specified axis with the provided weights.

The syntax is given below.

scipy.ndimage.convolve1d(input, weights, axis=- 1, output=None, mode='reflect', cval=0.1, origin=1)

Where parameters are:

  • input(array): It is array data as input.
  • weights(ndarray): It is the one-dimensional sequence of numbers.
  • axis(int): This parameter represents the axis of the given array on which to calculate the convolution.
  • output(dtype, array): Used to specify the data type of returned array as output.
  • mode(): It is used to control the boundaries of the input array when we want to extend its boundary. The modes are wrap, constant, nearest, mirror and reflect.
  • cval(scalar): It is used to fill the past edges of the input array when the mode is set to constant.
  • origin(int): It is used to put the filter on the given input array, if the value is zero then put the filter in the center. If the value is positive, put the filter on the left side, and if the value is negative, put the filter on the right side.

Let’s understand with an example by following the below steps:

Import the required libraries using the below python code.

from scipy import ndimage

Create an array with several values and weights using the below Python code.

a = [4,6,3,5,6,0,9]
w = [2,6]

Now pass the above-created array and weights to the method convolve1d.

ndimage.convolve1d(a,weights=w)
Scipy Convolve 1d
Scipy Convolve 1d

Read: Scipy Optimize – Helpful Guide

Scipy Convolve 2d

The Scipy has a method convolve() withing module scipy.ndimage that computes the multi-dimensional convolution on a specified axis with the provided weights.

The syntax is given below.

scipy.ndimage.convolve(input, weights, axis=- 1, output=None, mode='reflect', cval=0.1, origin=1)

Where parameters are:

  • input(array): It is array data as input.
  • weights(ndarray): The array of weights must have a dimension equal to the input array.
  • output(dtype, array): Used to specify the data type of returned array as output.
  • mode(): It is used to control the boundaries of the input array when we want to extend its boundary. The modes are wrap, constant, nearest, mirror, and reflect.
  • cval(scalar): It is used to fill the past edges of the input array when the mode is set to constant.
  • origin(int): It is used to put the filter on the given input array, if the value is zero then put the filter in the center. If the value is positive, put the filter on the left side, and if the value is negative, put the filter on the right side.

Let’s understand with an example by following the below steps:

Import the required libraries using the below python code.

from scipy import ndimage
import numpy as np

Create a two-dimensional array with several values and weights using the below Python code.

array_data = np.array([(5,4,6,8),(1,0,7,4)])
weight = np.array([[0,0,1],[1,1,1]])

Now pass the above-created two-dimensional array and weights to the method convolve.

ndimage.convolve(array_data,weights=weight,cval=0.1,mode='constant')
Scipy Convolve 2d
Scipy Convolve 2d

Read: Scipy Rotate Image

Scipy Convolve 3d

The Scipy has a method convolve() withing module scipy.ndimage that computes the multi-dimensional convolution. Here we will use the same method with a 3d array that we have used in the above sub-section.

The syntax is given below.

scipy.ndimage.convolve(input, weights, axis=- 1, output=None, mode='reflect', cval=0.1, origin=1)

Where parameters are:

  • input(array): It is array data as input.
  • weights(ndarray): The array of weights must have a dimension equal to the input array.
  • output(dtype, array): Used to specify the data type of returned array as output.
  • mode(): It is used to control the boundaries of the input array when we want to extend its boundary. The modes are wrap, constant, nearest, mirror, and reflect.
  • cval(scalar): It is used to fill the past edges of the input array when the mode is set to constant.
  • origin(int): It is used to put the filter on the given input array, if the value is zero then put the filter in the center. If the value is positive, put the filter on the left side, and if the value is negative, put the filter on the right side.

Let’s understand with an example by following the below steps:

Import the required libraries using the below python code.

from scipy import ndimage
import numpy as np

Create a three-dimensional array with several values and weights using the below code.

array_data = np.array([[[3,18], [46, 80]], [[0, 0], [62, 86]],[[86,34],[22,19]]])
weight = np.array([[[0,0,1],[1,1,1],[0,1,0]]])

Now pass the above-created three-dimensional array and weights to the method convolve.

ndimage.convolve(array_data,weights=weight,cval=0.1,mode='constant')
Scipy Convolve 3d
Scipy Convolve 3d

Read: Scipy Constants

Scipy Convolve fft

The Scipy has a method fftconvolve() in module scipy.signal that convolved n-dimensional array using the method FFT (Fast Fourier Transform).

The syntax is given below.

scipy.signal.fftconvolve(in1, in2, mode='full', method='auto')

Where parameters are:

  • in1(array_data): It is used to input the first signal in the form of an array.
  • in2(array_data): It is used to input the second signal in the form of an array, the dimension must be the same as the first input array.
  • mode: It is used to specify the string that determines output size. The mode can be same, full and valid.
  • method: It is used to specify the method that computes the convolution. The method can be auto, direct and fft.

Let’s understand with an example by following the below steps:

Import the required library using the below python code.

from scipy.signal import fftconvolve
import scipy.signal
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

Generate a random noise signal and apply the method fftconvolveI() using the below code.

random_gen = np.random.default_rng()
s = random_gen.standard_normal(800)
autocorrelation = fftconvolve(s, s[::-1], mode='full')

Let’s plot the above-convolved signal using the below code.

fig, (orig_axes, mag_axes) = plt.subplots(2, 1)
orig_axes.plot(s)
orig_axes.set_title('It is noise')
mag_axes.plot(np.arange(-len(s)+1,len(s)), autocorrelation)
mag_axes.set_title('It is autocorrelation')
fig.tight_layout()
fig.show()
Scipy Convolve fftconvolve
Scipy Convolve fftconvolve

This is how to use the method fftconvolve() using Scipy in Python.

Read: Scipy Stats

Scipy Convolve gaussian

The Scipy has a method gaussian_filter within a module scipy.ndimage that apply gaussian to the multi-dimensional array.

The syntax is given below.

scipy.ndimage.gaussian_filter(input, sigma, order=0, output=int, mode='nerest', cval=1.1, truncate=3.1)

Where parameters are:

  • input(array_data): It is array data as input.
  • sigma(sequence of scalar): Gaussian kernel standard deviation.
  • order(int): It is used to specify the order of the filter.
  • output(dtype, array): Used to specify the data type of returned array as output.
  • mode(): It is used to control the boundaries of the given array as input when we want to extend its boundary. The modes are wrap, constant, nearest, mirror and reflect.
  • cval(scalar): It is used to fill the past edges of the given array as input when the mode is set to constant.

The method gaussian_filter returns gaussian_filter of type ndarray.

Let’s understand with an example by following the below steps:
Import the required libraries using the python below code.

from scipy.ndimage import gaussian_filter
import numpy as np

Create an array containing values using the below code.

array_data = np.arange(100, step=4).reshape((5,5))
array_data

Now, apply the Gaussian filter on the array data using the below code.

gaussian_filter(array_data, sigma=1)
Scipy Convolve Gaussian
Scipy Convolve Gaussian

This is how to apply a gaussian filter to an array of data using Scipy in the Python program.

Read: Scipy Misc + Examples

Scipy Convolve along axis

Here we will use the same method convolve1d() of Scipy to convolve the given array on a specified axis.

To know about the method convolve() please refer to the above sub-section.

Let’s take an example by following the below steps:

Import the required libraries using the below python code.

from scipy import ndimage
import numpy as np

Create a two-dimensional array with several values and weights using the below python code.

array_data = np.array([(4,5,8,6),(0,1,4,7)])
weight = [0,1,1]

Pass the above-created two-dimensional array and weights to the method convolve without specifying axis parameters.

ndimage.convolve1d(array_data,weights=weight)

Again run the above code with an axis equal to 0 using the below code.

ndimage.convolve1d(array_data,weights=weight,axis=0)
Scipy Convolve along axis
Scipy Convolve along axis

Look at the result of both codes, one without an axis and the second with an axis.

Read: Scipy Signal – Helpful Tutorial

Scipy Convolve kernel

The weights are a group of values that is called kernel in convolution which slides over the given array as input to transform that data. Here we will use the same Scipy method convolve() that we have learned in the above sub-section.

Let’s understand through an example by following the below steps:

Import the required libraries using the below python code.

from scipy import ndimage
import numpy as np

Create a two-dimensional array with several values and weights or kernels using the below code.

array_data = np.array([(1,4,8,6),(4,7,0,4)])
kernel = np.array([[1,0,1],[1,1,0]])

Pass the above-created two-dimensional array and kernel to the method convolve and see the result.

ndimage.convolve(array_data,weights=kernel)

Create a new kernel using the below code.

kernel_2 = np.array([[0,0,1],[0,1,1]])

Again, pass the new kernel to a method and see the result using the code below.

ndimage.convolve(array_data,weights=kernel_2)
Scipy Convolve kernel
Scipy Convolve kernel

This is how to use the different kernels to achieve different transformations on the same data.

Also, check the following Python SciPy tutorials.

So, in this tutorial, we have learned about the “Scipy Convolve” and covered the following topics.

  • Scipy Convolve 3d
  • Scipy Convolve 2d
  • Scipy Convolve 1d
  • Scipy Convolve fft
  • Scipy Convolve gaussian
  • Scipy Convolve stride
  • Scipy Convolve along axis
  • Scipy Convolve kernel