Digital Signal Processing
Lab 1
Introduction to MATLAB and Discrete-
Time Signals
Name: Muhammad Hunzilah Ahmad
Session: BSEE 2018-2022(Electronics)
Date: 15th October, 2021
Instructors: Dr. Sufi Tabassum Gul
Engr. Javeria Ayub
Engr. Ayesha Ashraf
Pakistan Institute of Engineering and Applied Sciences, Islamabad
Page |0
Task 1:
Write a MATLAB program to generate the following sequences and plot them using the
function stem.
MATLAB Code:
a) Unit Sample Sequence s[n]
%Plot the unit sample sequence
I = [zeros(1,5) 1 zeros(1,5)]
t = -5 : 1 : 5;
stem (t,I)
b) Unit Step Sequence u[n]
%Plot the unit step sequence
I = [zeros(1,5) 1 ones(1,5)]
t = -5 : 1 : 5;
stem (t,I)
c) Unit Ramp Sequence r[n]
%Plot the ramp sequence
t = 0 : 1 : 10;
I = t
stem (t,I)
Results:
a) Unit Sample Sequence s[n]
Figure 1 unit sample sequence
Page |1
b) Unit Step Sequence u[n]
Figure 2 unit step sequence
c) Unit Ramp Sequence r[n]
Figure 3 unit ramp sequence
Task 2:
Using the function sawtooth and square, write a MATLAB program to generate sawtooth
wave and square wave. The input parameters specified by the user are the desired length L of
the sequence, peak value A, and period N. Also, duty cycle for square wave. Using this
program, generate the first 100 samples of the sequences with sampling rate of 20 kHz, peak
value 7, period of 13 and 60% duty cycle for square wave.
Page |2
MATLAB Code:
a) Sawtooth wave
desired_length = input (' Enter desired_length =')
Peak_value = input (' Enter Peak_value =')
period = input (' Enter period =')
frequency = 1/period;
sampling_rate = input (' Enter sampling rate ');
t = 0: 1/sampling_rate :desired_length;
x = Peak_value * sawtooth(2*pi*frequency*t);
plot (t,x)
b) Square wave
desired_length = input (' Enter desired_length =')
Peak_value = input (' Enter Peak_value =')
period = input (' Enter period =')
Duty_cycle = input(' Enter Duty_cycle =')
frequency = 1/period;
sampling_rate = input (' Enter sampling rate ');
t = 0: 1/sampling_rate :desired_length;
x = Peak_value * square (2*pi*frequency*t, Duty_cycle );
plot (t,x)
Results:
a) Sawtooth wave
Figure 4 sawtooth wave
Page |3
a) Square wave
Figure 5 square wave
Task 3:
Write a program to generate and plot the complex exponential equation
𝜋⁄ )𝑛
2.5 𝑒 (−0.4+𝑗 5 for 0 ≤ n ≤ 100.
Show real and imaginary plots.
MATLAB Code:
% Generation of a complex exponential equation
n=0: 0.5 :100;
power = -0.4+(pi/5)*i;
x=2.5* exp(power*n);
subplot(2,1,1)
stem(n,real(x));
title('Real part');
xlabel('Time ');
ylabel('Real Part');
subplot(2,1,2)
stem(n,imag(x));
title('Imaginary part');
xlabel('Time ');
ylabel('Imaginary Part');
Page |4
Results:
Figure 6 complex exponential equation using plot function
Figure 7 complex exponential equation using stem function
Task 4:
Write a MATLAB program to generate and display five sample sequences of random
sinusoidal signal of length 31.
𝑥[𝑛] = 𝐴. 𝑐𝑜𝑠(𝜔𝑜 𝑛 + 𝜑)
Page |5
Where input data specified by the user are the desired length L, amplitude A, angular
frequency 𝜔𝑜 and the phase 𝜑, where 0 ≤ 𝜔𝑜 ≤ 𝜋 and 0 ≤ 𝜑 ≤ 2𝜋.
Generate sequences for:
Part A 𝝎𝒐 𝝋
a) 1.5 0 0
b) 1.5 0.1π 0
c) 1.5 1.2 π 0
d) 2 1.8 π 0
e) 2 1.2 π 0.1 π
f) 2 1.8 π 0.1 π
MATLAB Code:
desired_length = input (' Enter desired_length =')
Amplitude = input (' Enter Amplitude =')
angular_frequency = input (' Enter angular_frequency =')
Phase = input (' Enter Phase ');
n=0: desired_length;
x_n= Amplitude*cos(angular_frequency*n+Phase)
stem (n,x_n)
Results:
Part A 𝝎𝒐 𝝋
a) 1.5 0 0
Figure 8 sample sequences of given sinusoidal signal
Page |6
Part A 𝝎𝒐 𝝋
b) 1.5 0.1π 0
Figure 9 sample sequences of given sinusoidal signal
Part A 𝝎𝒐 𝝋
c) 1.5 1.2 π 0
Figure 10 sample sequences of given sinusoidal signal
Page |7
Part A 𝝎𝒐 𝝋
d) 2 1.8 π 0
Figure 11 sample sequences of given sinusoidal signal
Part A 𝝎𝒐 𝝋
e) 2 1.2 π 0.1 π
Figure 12 sample sequences of given sinusoidal signal
Page |8
Part A 𝝎𝒐 𝝋
f) 2 1.8 π 0.1 π
Figure 13 sample sequences of given sinusoidal signal
Conclusion:
Given tasks are done by writing MATLAB codes. Codes and figures of results are attached
under each task.
Page |9