Tuesday, November 26, 2024
Google search engine
HomeLanguagesDiscrete Linear Convolution of Two One-Dimensional Sequences and Get Where they Overlap...

Discrete Linear Convolution of Two One-Dimensional Sequences and Get Where they Overlap in Python

In this article, we will be looking at the approach to returning the discrete linear convolution of two one-dimensional sequences and getting where they overlap in Python.

Numpy np.convolve()

To return the discrete linear convolution of two one-dimensional sequences, the user needs to call the numpy.convolve() method of the Numpy library in Python.The convolution operator is often seen in signal processing, where it models the effect of a linear time-invariant system on a signal. In probability theory, the sum of two independent random variables is distributed according to the convolution of their individual distributions.

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

Parameters:

  • a: First one-dimensional input array.
  • v: Second one-dimensional input array.
  • mode{‘full’, ‘valid’, ‘same’}:
    •  full’: By default, the mode is ‘full’. This returns the convolution at each point of overlap, with an output shape of (N+M-1,). At the end-points of the convolution, the signals do not overlap completely, and boundary effects may be seen.
    •  same’: Mode ‘same’ returns output of length max(M, N). Boundary effects are still visible.
    • ‘valid’:  Mode ‘valid’ returns output of length max(M, N) – min(M, N) + 1. The convolution product is only given for points where the signals overlap completely. Values outside the signal boundary have no effect.

Returns: out: Discrete, linear convolution of a and v.

Example:

In this example, we created two arrays of 5 data points each, then we have simply gotten the dimension and the shape of each array, further with the use of the np.convolve() method we pass both the arrays with the mode value to default as parameters to return the discrete linear convolution of two one-dimensional sequences and getting where they overlap in python.

Python3




import numpy as np
  
# Creating two numpy One-Dimensional
# array using the array() method
arr1 = np.array([1, 2, 3, 4, 5])
arr2 = np.array([0, 0.5, 1, 1.5])
  
# Display the arrays
print("Array1:->\n", arr1)
print("\nArray2:->\n", arr2)
  
# Check the Dimensions of both the arrays
print("\nDimensions of Array1:->\n", arr1.ndim)
print("\nDimensions of Array2:->\n", arr2.ndim)
  
# Check the Shape of both the arrays
print("\nShape of Array1:->\n", arr1.shape)
print("\nShape of Array2:->\n", arr2.shape)
  
# To return the discrete linear convolution
# of two one-dimensional sequences,
# use the numpy.convolve() method in Python Numpy
print("\nResult:->\n", np.convolve(arr1, arr2))


Output:

Array1:->
 [1 2 3 4 5]

Array2:->
 [0.  0.5 1.  1.5]

Dimensions of Array1:->
 1

Dimensions of Array2:->
 1

Shape of Array1:->
 (5,)

Shape of Array2:->
 (4,)

Result:->
 [ 0.   0.5  2.   5.   8.  11.  11.   7.5]

Example:

In this example, we created two arrays of 5 data points each, then we have simply gotten the dimension and the shape of each array, further with the use of the np.convolve() method we pass both the arrays with the mode value to valid as parameters to return the discrete linear convolution of two one-dimensional sequences and getting where they overlap in python.

Python3




import numpy as np
  
# Creating two numpy One-Dimensional
# array using the array() method
arr1 = np.array([1, 2, 3, 4, 5])
arr2 = np.array([0, 0.5, 1, 1.5])
  
# Display the arrays
print("Array1:->\n", arr1)
print("\nArray2:->\n", arr2)
  
# Check the Dimensions of both the arrays
print("\nDimensions of Array1:->\n", arr1.ndim)
print("\nDimensions of Array2:->\n", arr2.ndim)
  
# Check the Shape of both the arrays
print("\nShape of Array1:->\n", arr1.shape)
print("\nShape of Array2:->\n", arr2.shape)
  
# To return the discrete linear convolution of
# two one-dimensional sequences, use the
# numpy.convolve() method in Python Numpy
print("\nResult:->\n", np.convolve(arr1, arr2,
                                   mode='valid'))


Output:

Array1:->
 [1 2 3 4 5]

Array2:->
 [0.  0.5 1.  1.5]

Dimensions of Array1:->
 1

Dimensions of Array2:->
 1

Shape of Array1:->
 (5,)

Shape of Array2:->
 (4,)

Result:->
 [5. 8.]

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments