How to plot a signal
What is a signal?
The signal is defined as data values that are represented by the signal vector at specified points in time. Let’s create a signal plot in Matlab and then in Python.
MATLAB and Python are powerful programming languages for various purposes, including numerical and scientific analysis. Both of these languages can display the data graphically.
MATLAB code
We will create a Continuous Time (CT) signal, often used in analog systems. Let’s say we have a CT signal, for a time interval (0–20). Here is a signal defined as a function of time . Let’s look at the code to understand it:
time=0:0.01:20;signal=2*time.*cos(time);# Now Plottingh1=figure;plot(time,signal,'color','r');xlabel('Time');ylabel('CT Signal');title('Signal Plot');grid;
Explanation
Line 1–2: We declare our axes i.e., time and signal.
Line 6: We plot the data values using the
plot()function. An important thing to note here is thattimeis independent variable andsignalis a dependent variable. We also specify the color of the plotted signal.Line 9: We use the
gridfunction to display the grid line on the plot.
Example
Let’s create a sinusoidal signal, which is an example of a continuous-time signal, to understand the concept better. For example, we have a CT signal, for a time interval (0-10). Here is a signal defined as a function of time .
% time interval valuest = 0:0.01:10;% the signal valuesx = sin(2*pi*t);% Plotting the signalh1=figure;plot(t, x, 'color', 'b')xlabel('Time')ylabel('CT signal')title('Sinusoidal Signal')grid;
Explanation
-
Line 2: We declare x-axis ie., time.
-
Line 5: We declare y-axis ie., signal.
-
Line 9: We plot the data values using the
plot()function. We also specify the color of the plotted signal.
Python code
Let's try to plot the same signal in Python:
# Importing pyplot and numpy from matplotlibfrom matplotlib import pyplot as pltimport numpy as np# Setting the figure sizeplt.rcParams['figure.figsize'] = (10,6)# Defining variablesinterval=0.01time=np.arange(0,20,interval)signal=2*time*np.cos(time)# Plottingplt.plot(time,signal,color="red")plt.grid()plt.margins(x=0)plt.xlabel('Time')plt.ylabel('CT Signal')plt.title('Signal Plot');plt.show()
Explanation
Line 2–3: Importing some important libraries required for data arrangement and plotting i.e.,
matplotlibandnumpy.Line 9–11: We define the variables i.e.,
timeandsignal.Line 14: Plotting the graph by using
plt.plot()function.Line 20: The function
plt.show()is used to display the plot.
Second signal
Let’s create a sinusoidal signal, which is an example of a continuous-time signal, to understand the concept better. For example, we have a CT signal, for a time interval (0-10). Here is a signal defined as a function of time .
# Importing pyplot and numpy from matplotlibfrom matplotlib import pyplot as pltimport numpy as np# Setting the figure sizeplt.rcParams['figure.figsize'] = (10,6)# Defining variablesinterval=0.01time=np.arange(0,10,interval)signal=np.sin(2*np.pi*time)# Plottingplt.plot(time,signal,color="red")plt.grid()plt.margins(x=0)plt.xlabel('Time')plt.ylabel('CT Signal')plt.title('Signal Plot');plt.show()
Explanation
-
Line 2–3: Importing some important libraries i.e.,
matplotlibandnumpy. -
Line 9–11: Defining axes i.e.,
timeandsignal. -
Line 14: Plotting the graph by using plt.plot() function.
-
Line 20: The function
plt.show()is used to display the plot.
Free Resources