Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Experiment–2: Plotting in MATLAB

Signals and Systems Lab(EC2P002)


School of Electrical Sciences, IIT Bhubaneswar
Autumn Semester 2021

Signals and Systems Lab (EC2P002) School of Electrical Sciences, IIT Bhubaneswar 1/7
Agenda of the Experiment

In this lab session, we will learn about:

Basic plotting commands

Plotting some common signals

Plotting histograms

Saving and loading figures

Signals and Systems Lab (EC2P002) School of Electrical Sciences, IIT Bhubaneswar 2/7
Basic Commands for Plotting

1. Type help plot and help stem to learn about plot and stem commands. Define
n = 0 : 0.01 : 10 and x = cos(2πn). Plot the signal x by using the function plot(x)
and stem(x). Observe the difference between the two plots.
2. Plot the signal x by using plot(n,x). Observe the difference between the outputs of
plot(x) and plot(n,x).
3. From the same data, plot only one period of the signal x.
4. Now define y = sin(2πn) and run the command plot(n,x,n,y).
5. Define x = 2 : 20, y = x 2 , and z = x + jy. Now plot the signal z using plot(z).
Explain the numbers you see on the x-axis and y-axis.
6. Define n = 0 : 0.01 : pi/2, x = cos(2πn), and y = sin(2πn). Now plot x and y
against n in the same figure by using plot(n,x,n,y).
a. Insert legends that identify the curves for x and y as “Cosine signal” and “Sine signal”,
respectively. (Use help legend for information on legends.)
b. Plot the cosine curve as a solid red line with diamond markers.
c. Plot the sine curve as a dashed blue line without any markers.
d. Both curves must have a width of 2 points (use linewidth for this purpose).
e. The x-axis should be named “n” and the y-axis should be named “Two functions of n”
(use xlabel and ylabel).

Signals and Systems Lab (EC2P002) School of Electrical Sciences, IIT Bhubaneswar 3/7
Basic Commands for Plotting

6. Type help hold. Use hold to produce the same curve as in the previous exercise,
except that now make the width of the cosine curve double the width of the sine
curve.
7. Use help axis to learn about axes manipulation. Produce the same graph as in the
previous exercise, but plot the y axis only from -0.5 to 0.5.
8. Use the subplot function to plot the sine and cosine curves from the previous
example in two separate plots side-by-side.
9. Look up the semilogx, semilogy, and loglog commands using help. Define
x = 0.01 : 0.01 : 1 and y = x −10 . Plot y as a function of x using the following
commands: plot(x,y), semilogx(x,y), semiolgy(x,y), and loglog(x,y). Which of these
graphs are more easier to infer from?
10. Insert grids into the semilogy(x,y) graph using the grid on command.
11. Use the title() function to insert the title “Example of a Semilog Plot” into the
semilogy(x,y) graph.

Signals and Systems Lab (EC2P002) School of Electrical Sciences, IIT Bhubaneswar 4/7
Plotting of Continuous-Time Signals

Note: MATLAB cannot really plot a continuous time signal x(t); it just plots a
discrete-time sequence consisting of samples of x(t).

1. Plot the following continuous-time signals over the range of −5s ≤ t ≤ 5s with a
sampling interval of 1 ms. For each figure, include an appropriate title, insert axis
labels, and keep grids on.
a. The unit step function. 
1
 t >0
b. The signum function defined as sgn(t) = 0 t =0

−1 t < 0
c. A rectangular pulse that starts at t = 0 s and has a duration of 2 s.
d. The unit ramp function x(t) = t.
e. A square wave of period 2 s. (Use the square() function.)
f. A sawtooth wave of period 3 s. (Use the sawtooth() function.)
g. A triangular wave of period 3 s. (Use the sawtooth function with two arguments, the
second being 0.5.)
sin(πt)
h. The sinc function sinc(t) = πt (Use the sinc() function.)
i. Time scaling: Plot sinc(t), sinc(2t), and sinc(t/2) in one figure.
j. Time shifting: Plot sinc(t), sinc(t + 2), and sinc(t − 2) in one figure

2. Can you plot the unit impulse function (a.k.a. the dirac-delta function) in MATLAB?

Signals and Systems Lab (EC2P002) School of Electrical Sciences, IIT Bhubaneswar 5/7
Histograms, Skewness, and Kurtosis

1. Type help histogram. Generate a vector of normally distributed random numbers x =


randn(1,10000). Plot x, and the histogram of x.
2. How many bins were used for the histogram? (Hint: type y = histogram(x) and read
the output.)
3. What happens if you plot histogram(x,10)? What about histogram(x,1000)? (Hint:
the second argument forces the histogram to use a specified number of bins.)
4. Plot the histogram of 10,000 samples obtained from a uniform distribution on the
range [-1,1].
5. Type help skewness and help kurtosis. Find the skeweness and kurtosis of the
normally and uniformly distributed random samples from the previous exercises.

Signals and Systems Lab (EC2P002) School of Electrical Sciences, IIT Bhubaneswar 6/7
Saving and Loading

1. Learn how to use savefig to save MATLAB figure files in the .fig format

2. Learn how to use openfig to open a .fig file

3. Learn how to use saveas to save a figure in other desired formats (e.g., jpg, png)

Signals and Systems Lab (EC2P002) School of Electrical Sciences, IIT Bhubaneswar 7/7

You might also like