• Post author:
  • Post category:NumPy / Python
  • Post last modified:March 27, 2024
  • Reading time:15 mins read
You are currently viewing NumPy convolve() Function in Python

NumPy convolve() function in Python is used to perform a 1-dimensional convolution of two arrays. Convolution is a mathematical operation that combines two functions to produce a third function. In the context of NumPy, the convolve() function is often used for operations like signal processing and filtering. In this article, I will explain the NumPy convolve() function used to return discrete the linear convolution of arr and arr1.

Advertisements

1. Quick Examples of NumPy convolve() Function

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


# Quick examples of convolve() function

import numpy as np

# Create input arrays
arr = np.array([3, 5])
arr1 = np.array([1, 2, 4, 7, 9])

# 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, the 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 return discrete linear convolution of arr and arr1 one-dimensional sequences.

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

The numpy.convolve() method in Python is used for linear convolution of two one-dimensional sequences. When the mode is set to ‘full’, the resulting array will be the full discrete linear convolution of the inputs.

In the below example, arr is a NumPy array with elements [3, 5]. arr1 is another NumPy array with elements [1, 2, 4, 7, 9]. The np.convolve(arr1, arr2, mode='full') call computes the full discrete linear convolution of arr and arr1 with the mode set to ‘full’.


# Import numpy module
import numpy as np

# Create input arrays
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("Full convolution result:\n",arr2)

Yields below output.

numpy convolve

Explain code.


# 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’

When using the numpy.convolve() method in Python with mode set to ‘same’, the resulting array will be the central part of the convolution that is the same size as the larger of the two input arrays. For example, the np.convolve(arr1, arr2, mode='same') call computes the convolution of arr1 and arr2 with the mode set to ‘same’.


# Use convolve() method in numpy when mode is ‘same’
arr2 = np.convolve(arr, arr1, mode='same')
print("Same convolution result:\n",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:
# Same convolution result:
#  [ 3 11 22 41 62]

The resulting array represents the central part of the convolution that is the same size as the larger of the two input arrays. The ‘same’ mode is often used when you want the output to have the same length as the larger input array, and it ensures that the convolution result is centered around the middle of the input arrays.

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

Using the numpy.convolve() method in Python with mode set to ‘valid’, the resulting array will only contain elements that do not rely on zero-padding. The output size is smaller than that of the ‘full’ mode. For instance, the np.convolve(arr1, arr2, mode='valid') call computes the convolution of arr1 and arr2 with the mode set to ‘valid’.

The resulting array only contains elements that do not rely on zero-padding, and its size is determined by the valid cross-correlations between the input arrays. The ‘valid’ mode is useful when you want the output to be as large as possible without including zero-padded elements


# Use convolve() method in numpy when mode is ‘valid’
arr2 = np.convolve(arr, arr1, mode='valid')
print("Valid convolution result:\n",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:
# Valid convolution result:
#  [11 22 41 62]

Frequently Asked Questions

What is the purpose of the numpy.convolve() function?

The numpy.convolve() function is used for linear convolution of two one-dimensional sequences. It combines two arrays to produce a third array, which represents the result of the convolution operation.

What does “convolution” mean in the context of signal processing?

In signal processing, convolution is a mathematical operation that combines two signals to produce a third signal. It involves sliding one signal over another, multiplying the overlapping elements at each position, and summing the results.

When should I use ‘full’, ‘valid’, or ‘same’ mode in the convolve() method?

Use 'full' when you want the complete convolution result.
Use 'valid' when you want only valid cross-correlations without zero padding.
Use 'same' when you want the output size to be the same as the larger input

Can numpy.convolve() be used for multidimensional arrays?

numpy.convolve() is designed for 1-dimensional arrays. For convolution with multidimensional arrays, you might use scipy.signal.convolve2d or similar functions in the SciPy library.

How can I interpret the output of convolve()?

The output represents the result of convolving the input arrays based on the chosen mode. Each element in the output array is obtained by sliding one array over the other, multiplying overlapping elements, and summing the results. The mode determines the size and characteristics of the output array.

What is the difference between convolution and correlation?

The main difference is that convolution involves flipping the kernel (second sequence) before the operation, while correlation does not. In signal processing, convolution is more common, but the choice depends on the specific application.

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

Malli is an experienced technical writer with a passion for translating complex Python concepts into clear, concise, and user-friendly articles. Over the years, he has written hundreds of articles in Pandas, NumPy, Python, and takes pride in ability to bridge the gap between technical experts and end-users.