NumPy convolve() Function in Python

  • Post author:
  • Post category:NumPy / Python
  • Post last modified:February 1, 2023
Spread the love

Python numpy.convolve() function is used to return discrete, linear convolution of two one-dimensional sequences. The NumPy convolve() method accepts three arguments which are arr, arr1, and mode. The convolution operator is often seen in signal processing, where it models the effect of a linear time-invariant system on a signal. In this article, I will explain NumPy convolve() function used to return discrete the linear convolution of arr and arr1.

1. Quick Examples of Python NumPy convolve() Function

If you are in a hurry, below are some quick examples of how to use Python NumPy convolve() function.


# Below are the quick examples

# Example 1: Use convolve() method in numpy when mode is ‘full’
arr2 = np.convolve(arr, arr1, mode='full')

# Example 2: Use convolve() method in numpy when mode is ‘same’
arr2 = np.convolve(arr, arr1, mode='same')

# Example 3: Use convolve() method in numpy when mode is ‘valid’
arr2 = np.convolve(arr, arr1, mode='valid')

2. Syntax of NumPy convolve()

Following is the syntax of convolve() function.


# Syntax of numpy.convolve()
numpy.convolve(arr, arr1, mode='full')

2.1 Parameters of NumPy convolve()

The numpy.convolve() function allows the following parameters.

  • arr - array_like, first one-dimensional input array.
  • arr1 - array_like, second one-dimensional input array.
  • mode - {‘full’, ‘valid’, ‘same’}, optional.
    • full – It is the default, 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 – It is the ‘same’ mode, the output sequence is of length max(M, N). Boundary effects will still be visible.
    • valid – Mode ‘valid’ returns output of length max(M, N) – min(M, N) + 1. The convolution product is only given when arr1 and arr1 completely overlap each other. Values that are outside the signal boundary do not affect.

2.2 Return Value of convolve()

The convolve() function is used to returns discrete linear convolution of arr and arr1 one-dimensional sequences.

3. Use convolve() Method in NumPy when Mode is ‘full’


import numpy as np
 
arr = np.array([3, 5])
arr1 = np.array([1, 2, 4, 7, 9])
 
# Use convolve() method in numpy when mode is ‘full’
arr2 = np.convolve(arr, arr1, mode='full')
print(arr2)

Yields below output.


# Output:
First element: 5 * undefined (extrapolated as 0) + 3 * 1 = 3
Second element: 5*1 + 3*2 = 11
Third element: 5*2 + 3*4 = 22
Fourth element: 5*4 + 3*7 = 41
Fifth element: 5*7 + 3*9 = 62
sixth element is: 5*9 + 3* undefined (extrapolated as 0) = 45

# Output:
# [ 3 11 22 41 62 45]

4. Use convolve() Method in NumPy when Mode is ‘same’


# Use convolve() method in numpy when mode is ‘same’
arr2 = np.convolve(arr, arr1, mode='same')
print(arr2)

Yields below output.


# Output:
First element: 5 * undefined (extrapolated as 0) + 3 * 1 = 3
Second element: 5*1 + 3*2 = 11
Third element: 5*2 + 3*4 = 22
Fourth element: 5*4 + 3*7 = 41
Fifth element is : 5*7 + 3*9 = 62

# Output:
[ 3 11 22 41 62]

5. Use convolve() Method in NumPy when Mode is ‘valid’


# Use convolve() method in numpy when mode is ‘valid’
arr2 = np.convolve(arr, arr1, mode='valid')
print(arr2)

Yields below output.


# Output:
First element: 5 * undefined (extrapolated as 0) + 3 * 1 = 3
Second element: 5*1 + 3*2 = 11
Third element: 5*2 + 3*4 = 22
Fourth element: 5*4 + 3*7 = 41
Fifth element is : 5*7 + 3*9 = 62

# Output:
[11 22 41 62]

6. Conclusion

In this article, I have explained how to use numpy.convolve() function and using how to return discrete, linear convolution values with examples.

Happy Learning!!

References

Malli

I am Mallikarjuna an experienced technical writer with a passion for translating complex Python concepts into clear, concise, and user-friendly documentation. Over the years, I have written hundreds of articles in Pandas, NumPy, Python, and I take pride in my ability to bridge the gap between technical experts and end-users by delivering well-structured, accessible, and informative content.

Leave a Reply

You are currently viewing NumPy convolve() Function in Python