0% found this document useful (0 votes)
28 views

Abdul Wahab DSP Lab 2

Uploaded by

zimalabdi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Abdul Wahab DSP Lab 2

Uploaded by

zimalabdi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

EEE324 Digital Signal Processing

Abdul Wahab
Name
FA20-BEE-010
Registration Number
BEE-6A
Class

Instructor’s Name Dr. Fawad Zaman

Lab Assessment

Post Lab Total


Pre-Lab In-Lab Data
Data Analysis Writing Style
Presentation

1
Lab # 02:
Introduction to Discrete Time Signal Processing on
MATLAB

Introduction:
This is the basic and introductory lab about the Discrete Time Signal
processing and their representation on MATLAB. It is a high level
programming language which is used to solve the Complex
Engineering problem.

Lab Tasks
Task 1: Generate a Continuous time cosine signal and
plot it.
CODE:
%% lab 2 task 1
%cosine signal
t = 0:0.01:10; % time variable
w1 = 0.1*2*pi; % frequency
A = 1.5; % amplitude
2
g1 = A*cos(w1*t); % cosine formula
subplot(3,1,1);
plot(t,g1,'LineWidth',3); % plotting of continuous time signal
grid on;
w2 = 0.5*2*pi; % frequency
A = 2.5; % amplitude
g2 = A*cos(w2*t); % cosine formula
subplot(3,1,2);
plot(t,g2,'LineWidth',3); % plotting of continuous time signal
grid on;
a = g1 + g2; % sum of two signal
subplot(3,1,3);
plot(t,a,'LineWidth',3); % result is plotted.
grid on;

Result:

Figure 1: Result of the Continuous Signal.

Discription:

3
In the 1st wave the period is higher and frequency is lower. In 2nd wave the period
is less than the 1st one but the frequency is higher. In 3rd wave when both the
sinusoidal signals sum up then the period matches to 2nd wave almost, but the
frequency also varies from one to another point.

Task-2: Generate a Discrete time exponential signal and plot


it.
CODE:
%exponential signal
clc
clear all
close all
n = 0:10; % discrete time range
w1 = 2.5*(pi/3); % value of omega
A1 = 10; % value for the amplitute
x1 = A1*exp(w1*n); % implementation of formula
subplot(3,1,1); % division into subplots
stem(n,x1,'LineWidth',2); % for the graph of discrete signal
title('discrete exponential'); % graph title
xlabel('n') % x-axis for independent variable
ylabel('z') % y-axis for the dependent variable
grid on;
w2 = 2.5*(pi/4);
A2 = 3; % value of amplitude changes
x2 = A2*exp(w2*n);
subplot(3,1,2);
stem(n,x2,'LineWidth',2);
title('discrete exponential');
xlabel('n')
ylabel('z')
grid on;
w3 = 2.5*(pi/2);
A3 = 5; % value of te amplitude changes again
x3 = A3*exp(w3*n);
subplot(3,1,3)
4
stem(n,x3,'LineWidth',2);
title('discrete exponential');
xlabel('n')
ylabel('z')
grid on;
Result:

Figure 2: to represent the graph of DT exponentially growing signal.

Discription:
There is no effect on the signal by changing the value of amplitude. So it is independent on all
the values of amplitude.

Task 3:

Write a MATLAB program for the ‘running average’, a running total is a sequence of partial
sumof a given sequence/signal. For example, the running totals of the signal {a, b, c, …}are a,
a+b, a+b+c, ... Use that program to find the running total of the discrete time signal of length
5
N=100.Write your program so that it is flexible. That is, you should be able to invoke your
program from the command window as follows:
>> y=running_averagel(x)
where x is the input signal, and y is the running total of that signal.

Code:
N = 100;
running_total = 0;
for i = 1:N
running_total=running_total+i;
stem(running_total,'LineWidth',2),
hold on
xlabel('x');
ylabel('running total');
title('Running Total');
grid on
end
Result:

Figure 3:Moving average of the numbers we want to find.

6
Task 4:

Code:
n = 1000;
sum = 0;
for i = 1:1000
sum=sum+i;
end
mean = sum/1000;
sum2 = 0;
sum3 = 0;
for j = 1:n
sum2 = (j - mean)^2;
sum3 = sum3+sum2;
end
varience = (1/n).*sum3;

Result:

7
Task 5:
Can you explain what the following program does:
L = length(x);
for i = 1: L
if x (i) < 0
x (i) = -1;
end
end

Explanation:
The code takes an input array x and assigns the length of the array to the variable L. The code
then iterates over each element in the array x using for loop. For each element, the code checks if
it is less than zero using if statement. If the element is less than zero, the code assigns a value of -
1 to that element using the assignment operator =.
In other words, the code replaces any negative values in the input array x with -1.

Task 6:
Generate a step sequence u [n] as described in In-Lab section, use it to
generate an impulse as δ [n] = u[n] – u[n-1].

Code:
n = -7:1:30;
u1 = (n>=0)*1;
subplot(3,1,1)
stem(n,u1,'linewidth',2)
xlabel('n')
ylabel('Amplitude')
title('Step')
grid on
u2 = ((n-1)>=0)*1;
subplot(3,1,2)
stem(n,u2,'linewidth',2)
xlabel('n')
ylabel('Amplitude')
title('Step')
grid on
u = u1 - u2;
8
subplot(3,1,3)
stem(n,u,'linewidth',2)
xlabel('n')
ylabel('Amplitude')
title('Impulse')
grid on

Result:

Figure 4: Result of impulse in difference of unit step and delay.

Description:
Here we observe that when an impulse signal is represented in the form of unit step and the
advance shift is added then we observe only result at the 0 due to difference remaining singal
observing point are cancel out.

9
Task 7:
Generate an impulse sequence δ [n] as described in In-Lab section, use it to generate step
sequence.
𝑈𝑈[𝑛𝑛] = Σ𝛿𝛿[𝑛𝑛 − 𝑘𝑘] 𝑘𝑘

Code:
n = -10:1:10;
u1 = (n==0)*1;
subplot(2,1,1)
stem(n,u1,'linewidth',2)
xlabel('n')
ylabel('Amplitude')
title('Impulse')
grid on
k = 7;
for i = 1:k
u = ((n-i)>=0)*1;
subplot(2,1,2)
stem(n,u,'linewidth',2);
hold on
xlabel('n');
ylabel('Amplitude');
title('Step');
grid on
end

10
Result:

Figure 5:show the result of step function in sum of impulses.

Task-8:

Generate the following sequences using step and impulse sequence.


a. x[n] = u[n] – u[n-10]
b. x[n] = an u[n]
c. Σan δ[n-k] , k = -10 to 10

Code:
% a part

n = -20:2:30;
u1 = (n>=0)*1; %definig the signal
figure(1)
subplot(3,1,1)
stem(n,u1,'linewidth',2) %graph of discrete signal
11
xlabel('n')
ylabel('Amplitude')
title('u[n]')
grid on
u2 = ((n-10)>=0)*1; %advance shift of signal
subplot(3,1,2)
stem(n,u2,'linewidth',2)
xlabel('n')
ylabel('Amplitude')
title('u[n-10]')
grid on
x1 = u1 - u2; %difference of two-unit step signal
subplot(3,1,3)
stem(n,x1,'linewidth',2)
xlabel('n')
ylabel('Amplitude')
title('x[n] = u[n] – u[n-10]')
grid on

Result:

Figure 6: Response of unit step and advance of unit step and difference between them.
12
Code:

% b part
n2 = -7:1:7;
a = 10;
x2 = a.^n2.*(n2>=0)*1; %defining o the input
figure(2)
stem(n2,x2,'linewidth',2)
xlabel('n')
ylabel('Amplitude')
title('x[n] = a^n u[n]')
grid on

Result:

Figure 7:result of step function when it multiplies with any constant with increasing power
of the that constant.

13
Code:
% c part
n3 = -12:1:12
for k=-10:10
x3 = a.^n3.*((n3-k)>=0)*1; %defining the signal
figure(3)
stem(n3,x3,'linewidth',2);
hold on
xlabel('n')
ylabel('Amplitude')
title('Σan δ[n-k] , k = -10 to 10')
grid on
end

Result:

Figure 8:Summation of the impulse signal with the advance shift up to the value of k.

14
Critical Analysis:
In this lab we learned the following points

• How to generate analogue and digital signal, as we first define


the axis of time and then we plot our function, and then we use
plot and stem command to generate respective signal.
• We came to know about some fundamental sequences like
impulse and step sequence.
• We use some commands of MATLAB like average, variance,
mean which we didn’t use in previous course.
• We came to know about different statements like while, for,
switch etc.
We learn to use arithmetical operations on vectors like addition,
subtraction, division etc. I also revise the concepts of how to make
vectors, arrays, allocating memory to variables, conditional
statements and loops in MATLAB.
Now I am able to easily plot discrete time graph and easily handle
vectors and arrays.

15

You might also like