Numpy Convolve For Different Modes in Python

In this article, we will discuss the Numpy convolve function in Python. The convolution operator is a mathematical operator primarily used in signal processing.

The convolution of two signals is defined as the integral of the first signal(reversed) sweeping over (“convolved onto”) the second signal. And multiplied (with the scalar product) at each position of overlapping vectors. An array in numpy is a signal. Thus, the numpy convolve function performs convolutions over single-dimensional arrays.

Syntax of Numpy convolve

numpy.convolve(avmode='full')

Parameters:

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

The return value of numpy convolve

Returns the discrete, linear convolution of two one-dimensional arrays i.e, of ‘a’ and ‘v’.

Numpy convolve in Python when mode is ‘full’

import numpy as np

a = np.array([3, 7])
print("First vector sequence is: ", a)

v = np.array([1, 2, 5, 7])
print("Second vector sequence is: ", v)

print("\nprinting linear convolution result between v1 and v2 using default 'full' mode:")
print(np.convolve(a, v))

Output:

First vector sequence is: [3 7]
Second vector sequence is: [1 2 5 7]

printing linear convolution result between v1 and v2 using default 'full' mode:
[ 3 13 29 56 49]

Explanation:

The above example shows the most basic use of Numpy convolve() method. Firstly, we define two single-dimensional arrays as ‘a’ and ‘v’ using the numpy.array() function.

Then, we pass ‘a’ and ‘v’ as parameters to the convolve function. Since the mode is not mentioned, it takes the default value i.e., ‘full’. And calculates the convolution for the default value of mode as a= [3 7] and v= [1 2 5 7], and the operation is performed in full mode. Hence, the shape of the output array is: length (M+N-1) here M=2 and N = 4. Therefore the shape of the resultant vector will be 2 + 4 – 1 = 5. ‘a’ is reversed from [3 7] to [7 3], and then we perform the multiplication operation as:

First element: 7 * undefined (extrapolated as 0) + 3 * 1 = 3 
Second element: 7*1 + 3*2 = 13 
Third element: 7*2 + 3*5 = 29 
Fourth element: 7*5 + 3*7 = 56 
Fifth element is: 7*7 + 3 * undefined (extrapolated as 0) = 49 
Hence, the result is: [3 13 29 56 49]

Numpy convolve when mode is ‘same’

import numpy as np

a = np.array([3, 7])
print("First vector sequence is: ", a)

v = np.array([1, 2, 5, 7])
print("Second vector sequence is: ", v)

print("\nprinting linear convolution  result between a and v using 'same' mode:")
print(np.convolve(a, v, mode='same'))

Output:

First vector sequence is: [3 7]
Second vector sequence is: [1 2 5 7]

printing linear convolution result between a and v using 'same' mode:
[ 3 13 29 56]

Explanation:

Firstly, we define two single-dimensional arrays as ‘a’ and ‘v’ using the numpy.array() function. Then, we pass ‘a’ and ‘v’ as parameters to the convolve function. The optional parameter mode(which by default is ‘full’) is set to ‘same’. Thus, the Numpy convolve function with mode = ‘same’ computes the convolution as a= [3 7] and v= [1 2 5 7], and the operation is performed in the same mode.

The shape of the output array is max(M, N) here, M=2 and N = 4. Therefore the shape of the resultant vector will be 4. ‘a’ is reversed from [3 7] to [7 3], and then we perform the multiplication operation as: (does not calculate convolution for the nonoverlapping points at the end of the array. Hence the fifth element is not calculated)

  • First element: 7 * undefined (extrapolated as 0) + 3 * 1 = 3
  • Second element: 7*1 + 3*2 = 13
  • Third element: 7*2 + 3*5 = 29
  • Fourth element: 7*5 + 3*7 = 56
  • Hence, the result is: [3 13 29 56].

Convolution when mode is ‘valid’

import numpy as np

a = np.array([3, 7])
print("First vector sequence is: ", a)

v = np.array([1, 2, 5, 7])
print("Second vector sequence is: ", v)

print("\nprinting linear convolution  result between a and v using 'valid' mode:")
print(np.convolve(a, v, mode='valid'))

Output:

First vector sequence is: [3 7]
Second vector sequence is: [1 2 5 7]

printing linear convolution result between a and v using 'valid' mode:
[ 3 13 29 56 ]

Explanation:

Just like the above two cases, first, we defined the arrays. numpy.convolve() function uses these two arrays as arguments with mode set to ‘valid’. This computes the convolution as : a= [3 7] and v= [1 2 5 7] and operation valid mode.

The shape of the output array will be given by the formula length max(M, N) – min(M, N) + 1, here M=2 and N = 4, therefore the shape of the resultant vector will be: 4 – 2 + 1 = 3. ‘a’ is reversed from [3 7] to [7 3], and then we perform the multiplication operation as ( convolution product is given only for overlapping values. Since they do not overlap while calculating the first and fifth element, hence they are not given as the output)

Second element: 7*1 + 3*2 = 13 
Third element: 7*2 + 3*5 = 29
Fourth element: 7*5 + 3*7 = 56
Hence, the result is: [3 13 29 ]

Must Read

Exploring numpy.ones Function in Python | np.ones
Best Ways to Normalize Numpy Array
NumPy.ndarray object is Not Callable: Error and Resolution

Conclusion

In this article, we have explicitly discussed about the Numpy convolve function in Python. We have also provided examples with detailed explanations for different modes while computing convolutions of one-dimensional arrays. Refer to this article to gain clear knowledge of how the Numpy Convolve works.

However, if you have any doubts or questions, do let me know in the comment section below. I will try to help you as soon as possible.

Happy Pythoning!

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments