Open In App

Get Discrete Linear Convolution of 2D sequences and Return Middle Values in Python

Improve
Improve
Like Article
Like
Save
Share
Report

In this article let’s see how to return the discrete linear convolution of two one-dimensional sequences and return the middle values using NumPy in python.

numpy.convolve method :

The numpy.convolve() Converts two one-dimensional sequences into a discrete, linear convolution. In signal processing, the convolution operator is used to describe the effect of a linear time-invariant system on a signal. The sum of two independent random variables is distributed according to the convolution of their individual distributions in probability theory. The arrays are swapped before computation if v is longer than a. In this method it takes three parameters, a, and v takes a one-dimensional input array and the third parameter is optional, with values full’, ‘valid’, ‘same’. The mode ‘same’ returns the output of length max(M, N). Boundary effects are still visible.

Syntax: numpy.convolve(a, v, mode=’full’)

Parameters:

  • a: The first one-dimensional input array.
  • v: Second one-dimensional input array.
  • mode: {‘full’, ‘valid’, ‘same’}, optional
    • full: Mode is set to ‘full’ by default. With an output shape of (N+M-1,), this returns the convolution at each point of overlap.
    • valid: The result of mode ‘valid’ is max(M, N) – min(M, N) + 1.
    • same: The mode ‘same’ produces output of maximum length (M, N).

Returns: Convolution of a and v in a discrete, linear manner.

Example 1

Here, we will create two NumPy vectors using np.array() method. The .ndim attribute is used to know the dimensions of the array, .shape attribute is used to find the shape of the vector. np.convolve() method is used to find the Convolution of a and v in a discrete, linear manner. mode is set to ‘same’ to return the middle values of the discrete linear convolution.

Python3




# importing packages
import numpy as np
 
# creating arrays using np.array() method
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
#
# # Display the arrays
print("Array 1 is :", arr1)
print("Array 2 is :", arr2)
#
# # Checking the dimensions
print("Array one dimension :", arr1.ndim)
print("Array two dimension", arr2.ndim)
#
# # Checking the shape of the arrays
print("Shape of array 1 is : ", arr1.shape)
print("Shape of array 2 is : ", arr2.shape)
 
# returns the middle values of the convolution.
print("middle values of the convolution : \n",
      np.convolve(arr1, arr2, 'same'))


Output:

Array 1 is : [1 2 3]
Array 2 is : [4 5 6]
Array one dimension : 1
Array two dimension 1
Shape of array 1 is :  (3,)
Shape of array 2 is :  (3,)
middle values of the convolution : 
 [13 28 27]

Example 2

Here, we will create two NumPy vectors using np.random.rand() method. The .ndim attribute is used to know the dimensions of the array, .shape attribute is used to find the shape of the vector. np.convolve() method is used to find the Convolution of a and v in a discrete, linear manner. mode is set to ‘same’ to return the middle values of the discrete linear convolution.

Python3




# importing packages
import numpy as np
 
# creating arrays using np.array() method
arr1 = np.random.rand(3)
arr2 = np.random.rand(4)
#
# # Display the arrays
print("Array 1 is :", arr1)
print("Array 2 is :", arr2)
#
# # Checking the dimensions
print("Array one dimension :", arr1.ndim)
print("Array two dimension", arr2.ndim)
#
# # Checking the shape of the arrays
print("Shape of array 1 is : ", arr1.shape)
print("Shape of array 2 is : ", arr2.shape)
 
# returns the middle values of the convolution.
print("middle values of the convolution : \n",
      np.convolve(arr1, arr2, 'same'))


Output:

Array 1 is : [0.12366068 0.80232848 0.49311942]

Array 2 is : [0.49370654 0.77125182 0.11638803 0.91369435]

Array one dimension : 1

Array two dimension 1

Shape of array 1 is :  (3,)

Shape of array 2 is :  (4,)

middle values of the convolution : 

 [0.49148834 0.87664621 0.58668875 0.7904762 ]



Last Updated : 13 Oct, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads