# np.convolve: How to Use Numpy convolve() Method

What does convolution mean? In mathematical terms, convolution is a mathematical operator generally used in signal processing. An array in numpy acts as the signal.

## np.convolve

The np.convolve() is a built-in numpy library method that returns discrete, linear convolution of two one-dimensional vectors. The numpy convolve() method accepts three arguments which are v1, v2, and mode, and returns discrete linear convolution of v1 and v2 one-dimensional vectors.

The convolution of given two signals (arrays in case of numpy) can be defined as the integral of the first signal (array), reversed, convolved onto the second signal (array), and multiplied (with the scalar product) at the points wherever the vectors overlap.

The discrete convolution operation can be defined using the function given:

(v1 * v2) [n] = ∑ v1[m]v2[n-m]

### Syntax

```numpy.convolve (v1, v2, mode)
```

### Parameters

Numpy convolve() function takes at most three parameters:

v1: array_like, first one-dimensional input array. If it has a shape of (M,)

v2: array_like, second one-dimensional input array. If it has a shape of (N,)

mode: {‘full’, ‘same’, ‘valid’}, optional

It is an optional parameter and has three different modes, which are explained below:

• full: It is the default mode. This returns the convolution at each point of overlap, with an output shape of (M+N-1). At the endpoints of the convolution, the vectors v1 and v2 do not overlap completely, and boundary effects can be seen.
• same: In ‘same’ mode, the output sequence is of length max(M, N). Boundary effects will still be visible.
• valid: In this ‘valid’ mode, the output sequence is of length max(M,N) – min(M,N) + 1. The convolution product is only given when v1 and v2 completely overlap. Values that are outside the signal boundary do not affect.

### Return Value

The convolve() method returns discrete linear convolution of v1 and v2 one-dimensional vectors.

### Programming Example

#### Program to show the working of numpy.convolve() method

```# importing the numpy module
import numpy as np

# Making fist 1-D vector v1
v1 = np.array([3, 7])
print("First vector sequence is: ", v1)

# Making second 1-D vector v2
v2 = np.array([1, 2, 5, 7])
print("Second vector sequence is: ", v2)

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

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

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

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

printing linear convolution result between v1 and v2 using 'valid' mode:
[13 29 56]
```

#### Explanation

In the program convolve1.py, we have taken two one-dimensional input vectors named v1 and v2. Then we displayed output by displaying the linear convolution values by using each of the three different modes.

### Calculations performed in ‘full’ mode:

Since v1= [3 7] and v2= [1 2 5 7] and the operation is performed in full mode shape of the output array will be given by the formula length (M+N-1) here M=2 and N = 4. Therefore the shape of the resultant vector will be 2 + 4 – 1 = 5.

X 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]

### Calculations performed in ‘same’ mode:

Since v1= [3 7] and v2= [1 2 5 7] and the operation is performed in the same mode shape of the output array will be given by the formula max(M, N) here M=2 and N = 4. Therefore the shape of the resultant vector will be 4.

X 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]

### Calculations performed in ‘valid’ mode:

Since v1= [3 7] and v2= [1 2 5 7] and operation is performed in valid mode shape of output array will be given by the formula length max(M,N) – min(M,N) + 1, here M=2 and N = 4, therefore shape of resultant vector will be: 4 – 2 + 1 = 3.

Step1: X 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

Hence, the result is: [ 3 13 29 56]

That’s it.

## Related posts

np.polyfit

np.linalg.det

np.linalg.solve

This site uses Akismet to reduce spam. Learn how your comment data is processed.