Simulating Amplitude Modulation using Python
In communication labs in our colleges we all generate amplitude modulated signals using CROs. But the same can be performed by using Python and few of its additional libraries and the end result can be equally dope.
At first let’s revise the formulas needed to generate amplitude modulated signal
Now to simulate the signals in python first we have to import 2 python libraries: numpy and matplotlib
Then we have to take carrier amplitude, carrier frequency, message amplitude, message frequency and modulation index as inputs.
The built-in input function in python returns string value. So we have to convert them to integer or float. Now float is preferred because amplitude or frequency can be in decimal too.
The time function in analog communication is continuous function. To replicate its behavior we will use linspace function which will provide large number of discrete points which will act almost similar to continuous function. The linspace function will generate evenly spaced numbers within a given interval. The first argument of linspace is starting point, second argument is ending point and third argument is number of breakpoints between the given interval.
Now we will create our carrier, modulator(message), and product function. Here we will use sin , cos and pi from numpy.
Here comes the plotting part. We will utilize matplotlib library functions. The subplot function creates more than one plot in the canvas. The plot function plots the given function. In plot function we can pass color names or their acronyms to plot the graph in any color we wish. The ‘g’ , ‘r’ stands for green and red respectively. The xlabel and ylabel prints the x-axis and y-axis variable names. And the title function prints the title of the over all plot.
Now we can customize the plot as much we want. We can change the space between plots, font, size etc. And if we want to save the picture we can easily do so by savefig function where dpi means dots per inch which in today’s world, can be inter-changeably used with ppi(pixel per inch). More of such functions can be found in official matplotlib documentation.
Thus we can create simple amplitude modulation plot in python just using two external libraries only.
The full code is here: