What is the numpy.convolve() Method in Python?

Topics Covered

NumPy is a very potent library offered by Python that enables us to carry out intricate and computationally intensive computations. One such computation is called Convolution.

In general, convolution means the formation of the third function by any sort of combination of two functions. It is a very common and useful topic when it comes to signal analysis/processing. In Signal Processing, the operation of combining two signals from a third signal is called a convolution operation.

The NumPy library offers a function convolve(), which allows us to find the discrete and linear convolution of two one-dimensional arrays/vectors.

The general formula for convolution is:

Syntax for NumPy convolve()

The syntax for NumPy convolve() function is:

Parameters for NumPy convolve()

The parameters that NumPy convolve() functions takes are:

  • a1: This mandatory parameter represents the first one-dimensional vector.
  • a2: This mandatory parameter represents the second one-dimensional vector.
  • mode: This optional parameter represents the different modes for the convolution operation. These modes are full, same, valid.
  • full: This returns the convolution at each point of overlap, with an output shape of (M+N-1); M and N are the dimensions of the first and second array, respectively.
  • same: This returns the output sequence of length max(M, N)
  • valid: This returns the output sequence is of length max(M,N) – min(M,N) + 1.

Return Type for NumPy convolve()

As discussed earlier, the NumPy convolve() function returns a discrete and linear convolution of the two input arrays/vectors.

Examples for NumPy convolve()

  • Using NumPy convolve() with "full" mode

    As discussed above, the full mode ensures that the vector returned will have the convolution as (M+N-1); M and N are the dimensions of the first and second array respectively.

    Output

    Explanation

    The first vector is reversed, and then some calculations are performed.

    The calculations for the "full" mode of convolution are:

    IndexCalculation
    First Element7 x undefined (usually 0) + 3 x 1 = 3
    Second Element7x1 + 3x2 = 13
    Third Element7x2 + 3x5 = 29
    Fourth Element7x5 + 3x7 = 56
    Fifth Element7x7 + 3 x undefined (usually 0) = 49
  • Using NumPy convolve() with "same" mode

    The same mode ensures that the output vector is of length max(M, N); M and N are the dimensions of the first and second array, respectively.

    Output

    Explanation

    Here's the set of calculations performed for same mode:

    IndexCalculation
    First Element7 x undefined (usually 0) + 3 x 1 = 3
    Second Element7x1 + 3x2 = 13
    Third Element7x2 + 3x5 = 29
    Fourth Element7x5 + 3x7 = 56
  • Using NumPy convolve() with "valid" mode

    This mode returns the output sequence of length max(M,N) – min(M,N) + 1.

    Output

    Explanation

    The first vector is reversed, and then some calculations are performed.

    Here's the set of calculations performed for valid mode:

    IndexCalculation
    First Element7 x undefined (usually 0) + 3 x 1 = 3
    Second Element7x1 + 3x2 = 13
    Third Element7x2 + 3x5 = 29
  • NumPy convolve() using all the modes

In this example, we will see all the different modes (same, full, and valid).

Output

Conclusion

  • In this article, we learned about the NumPy convolve() function, which allows us to find the discrete and linear convolution of two one-dimensional arrays/vectors.
  • We also discussed the different modes that come with NumPy convolve(); full, same, and valid.
  • We discussed the different modes in-depth with their code examples too.