Convolution python code | numpy convolution, np convolve

This page covers convolution python code. It covers comparison between numpy convolve using np convolve python function and convolution using brute force method.

Introduction: The convolution of two vector sequences can be implemented as multiplication of two matrices. It is used in simulation of channel impairment of time domain transmit sequence such as OFDM/OFDMA waveforms. For LTI (Linear Time Invariant) system, channel impulse response (say h[n]) and input sequence (say x[n]) can be convolved to get output sequence (say y[n]).

➨The convolution of h[n]*x[n] can be expressed as follows.


Convolution of two sequences

Convolution Python script

Following python script can be used for simulating convolution functionalities using two different methods viz. numpy "np.convolve" function and Brute-force method. Developers have to change input vectors as required for convolution of real numbers or complex numbers at the beginning of the python script.


import numpy as np
import matplotlib.pyplot as plt

#Inputs as complex numbers
#x = np.random.normal(size=3) + 1j * np.random.normal(size=3) # normal random complex vectors
#h = np.random.normal(size=4) + 1j * np.random.normal(size=4) # normal random complex vectors

#Inputs as real numbers
x = [20,31,56] #Time domain sequence say in OFDM Transmitter
h = [10,2,5,8] #Time domain channel impulse response

L = len(x) + len(h) - 1 # length of convolution output

#Method-1 : Python convolution function from NumPy module
y3 = np.convolve(h, x)
print("y3=", y3)

#Method-2 : Convolution using Brute-force method
N = len(x)
M = len(h)
y = np.zeros(L) # array filled with zeros
for i in np.arange(0,N):
  for j in np.arange(0,M):
    y[i + j] = y[i + j] + x[i] * h[j]
print("y=",y)

#Difference between NumPy module and Brute-force method
y2 = y-y3

# Initialise the subplot function using number of rows and columns
figure, axis = plt.subplots(2, 2)
# For Built in convolution
axis[0,0].plot(y3)
axis[0,0].set_title("Method # 1 : np.convolve")
# For Our own function
axis[0,1].plot(y)
axis[0,1].set_title("Method # 2 : Brute-force method")
#Difference between two
axis[1, 0].plot(y2)
axis[1, 0].set_title("Difference between both methods")
plt.show()

Inputs for real numbers

Following are the input vectors for convolution of real numbers.

x = [20,31,56] #Time domain sequence say in OFDM Transmitter
h = [10,2,5,8] #Time domain channel impulse response

Output for convolution python code of real numbers

Python convolution of real numbers

Inputs for complex random numbers

Following are the input vectors for convolution python code for complex numbers.

#x = np.random.normal(size=3) + 1j * np.random.normal(size=3) # normal random complex vectors
#h = np.random.normal(size=4) + 1j * np.random.normal(size=4) # normal random complex vectors

Output for convolution of complex numbers

Python convolution of complex numbers

In this python snippet, we have seen comparison between built-in numpy convolution function i.e. np convolve and convolution using Brute-force method for real and complex numbers.

Other useful DSP codes in Python

Useful Links to MATLAB codes

RF and Wireless tutorials