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

DSP Lab 1 Handout

This document discusses discrete time signals and operations in MATLAB. It introduces unit impulse, unit step, and sinusoidal sequences. It also covers linear and time-invariant systems, impulse response computation, convolution, stability of systems, and moving average filters.

Uploaded by

M usman Usman
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)
29 views

DSP Lab 1 Handout

This document discusses discrete time signals and operations in MATLAB. It introduces unit impulse, unit step, and sinusoidal sequences. It also covers linear and time-invariant systems, impulse response computation, convolution, stability of systems, and moving average filters.

Uploaded by

M usman Usman
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/ 7

International Islamic University,

Islamabad

Digital Signal Processing LAB

EXPERIMENT # 01: Introduction to Discrete Signals and Signal


Operations in MATLAB

Name of Student: …………………………………..

Roll No.: ……………………………………………

Date of Experiment: ………………………………..

Report submitted on: ………………………………..

Marks obtained: ……………………………………

Remarks: ……………………………………………

Instructor’s Signature: ……………………………...


Digital Signal Processing Lab Page 1
Introduction to Discrete Signals and Signal Operations
in MATLAB
1. Objective
This lab focuses on the creation of discrete time signals in MATLAB and explains the required
MATLAB commands. After performing the lab, the students will be able to
• Create discrete time signals in MATLAB
• Use MATLAB commands to perform simple operations on discrete time signals
2. Resources Required
• A computer
• MATLAB 7.0 or higher
3. Introduction
Digital signal processing involves the processing of a discrete time signal (input signal) to produce
another discrete time signal (output signal) which has some desired properties. A strong
background of discrete time signals is essential for analyzing discrete time systems. Following are
some of the commonly used discrete time signals generated using MATLAB:
3.1 Unit Impulse and Unit Step sequences
A unit impulse sequence of length N can be generated in MATLAB by the following command:
N=10;
u = [1 zeros(1, N-1)]
A unit impulse sequence delayed by M samples where M < N can be generated as follows:
M=5;
ud = [zeros(1,M) 1 zeros(1,N-M- 1)];
Likewise, a unit step sequence s[n] of length N can be generated using the MATLAB command:
s = [ones(1,N)];
A delayed unit step sequence can be generated in a manner similar to that used in the generation
of a delayed unit sample sequence.
% Program P1_1
% Generation of a Unit Sample Sequence
clear all
close all
clc
% Generate a vector from -10 to 20
n = -10:20;
% Generate the unit sample sequence
u = [zeros(1,10) 1 zeros(1,20)];
% Plot the unit sample sequence
stem(n,u);
xlabel('Time index n');
ylabel('Amplitude');
title('Unit Sample Sequence');
axis([-10 20 0 1.2]); grid on;
3.2 Sinusoidal sequences
Sinusoidal sequences can be generated in MATLAB using “sin” and “cos” functions. Program
P1_2 is a MATLAB code that generates a sinusoidal signal.

Digital Signal Processing Lab Page 2


% Program P1_2
% Generation of a sinusoidal sequence
clear all; close all; clc;
n = 0:40;
f = 0.1;
phase = 0;
A = 1.5;
arg = 2*pi*f*n - phase; x = A*cos(arg);
stem(n,x,'r'); % Plot the generated sequence
axis([0 40 -2 2]);
grid;
title('Sinusoidal Sequence'); xlabel('Time index n'); ylabel('Amplitude');
3.3 Linear and Nonlinear Systems
We now investigate the linearity property of a causal system.

Consider the system given by


𝑦[𝑛] − 0.4𝑦[𝑛 − 1] + 0.75𝑦[𝑛 − 2] = 2.2403𝑥[𝑛] + 2.4908𝑥[𝑛 − 1] + 2.2403𝑥[𝑛 − 2]
MATLAB Program P1_3 is used to simulate the system, to generate three different input sequences
x1[n], x2[n], and x[n] = a · x1[n] + b · x2[n], and to compute and plot the corresponding output
sequences y1[n], y2[n], and y[n].
% Program P1_3
% Generate the input sequences
close all; clear all; clc
n = 0:40; a = 2; b = -3;
x1 = cos(2*pi*0.1*n);
x2 = cos(2*pi*0.4*n);
x = a*x1 + b*x2;
num = [2.2403 2.4908 2.2403];
den = [1 -0.4 0.75];
ic = [0 0]; % Set zero initial conditions
y1 = filter(num,den,x1,ic); % Compute the output y1[n]
y2 = filter(num,den,x2,ic); % Compute the output y2[n]
y = filter(num,den,x,ic); % Compute the output y[n]
yt = a*y1 + b*y2;
d=y-yt;% Compute the difference output d[n]
% Plot the outputs and the difference signal
subplot(3,1,1);
stem(n,y);
ylabel('Amplitude');
title('Output Due to Weighted Input: a \cdot x_{1}[n] + b \cdot x_{2}[n]');
subplot(3,1,2);
stem(n,yt);
ylabel('Amplitude');
title('Weighted Output: a \cdot y_{1}[n] + b \cdot y_{2}[n]');
subplot(3,1,3);
stem(n,d);
ylabel('Amplitude');

Digital Signal Processing Lab Page 3


xlabel('Time index n');
title('Difference Signal')
3.4 Time-Invariant and Time-Varying Systems
We next investigate the time-invariance property of a causal system.
MATLAB Program P1_4 is used to simulate the system, to generate two different input sequences
x[n] and x[n - D], and to compute and plot the corresponding output sequences y1[n], y2[n], and
the difference y1[n] - y2[n + D].
% Program P1_4
% Generate the input sequences
close all; clear all; clc
n = 0:40; D = 10; a = 3.0; b = -2;
x = a*cos(2*pi*0.1*n) + b*cos(2*pi*0.4*n);
xd = [zeros(1,D) x];
num = [2.2403 2.4908 2.2403];
den = [1 -0.4 0.75];
ic = [0 0];% Set initial conditions
% Compute the output y[n]
y = filter(num,den,x,ic);
% Compute the output yd[n]
yd = filter(num,den,xd,ic);
% Compute the difference output d[n]
d=y- yd(1+D:41+D);
% Plot the outputs
subplot(3,1,1);
stem(n,y);
ylabel('Amplitude');
title('Output y[n]');
grid;
subplot(3,1,2); stem(n,yd(1:41)); ylabel('Amplitude');
title(['Output Due to Delayed Input x[n - ', num2str(D),']']);
grid;
subplot(3,1,3);
stem(n,d);
xlabel('Time index n');
ylabel('Amplitude');
title('Difference Signal');
grid;
3.5 Impulse Response Computation
The MATLAB command y = impz(num,den,N) can be used to compute the first N samples of the
impulse response of the causal LTI discrete-time system.
MATLAB Program P1_5 given below computes and plots the impulse response of the system.
% Program P1_5
% Compute the impulse response y
clf;
N = 40;
num = [2.2403 2.4908 2.2403];
den = [1 -0.4 0.75];
y = impz(num,den,N);

Digital Signal Processing Lab Page 4


% Plot the impulse response
stem(y);
xlabel('Time index n');
ylabel('Amplitude');
title('Impulse Response');
grid;
3.6 Convolution
The convolution operation is implemented in MATLAB by the command conv, provided the two
sequences to be convolved are of finite length. For example, the output sequence of an FIR system
can be computed by convolving its impulse response with a given finite-length input sequence.

𝑦[𝑛] = ∑ ℎ[𝑘]𝑥[𝑛 − 𝑘]
𝑘=−∞

𝑦[𝑛] = ∑ ℎ[𝑛 − 𝑘]𝑥[𝑘]


𝑘=−∞

Or can be represented as
𝑦[𝑛] = ℎ[𝑛] ⊛ 𝑥[𝑛]
The following MATLAB program illustrates this approach.
% Program P1_6
close all; clear all; clc
h= [3 2 1 -2 1 0 -4 0 3]; % impulse response
x = [1 -2 3 -4 3 2 1]; % input sequence
y = conv(h,x); n = 0:14;
subplot(2,1,1); stem(n,y);
xlabel('Time index n'); ylabel('Amplitude');
title('Output Obtained by Convolution');grid;
x1 = [x zeros(1,8)];
y1 = filter(h,1,x1);
subplot(2,1,2); stem(n,y1);
xlabel('Time index n'); ylabel('Amplitude');
title('Output Generated by Filtering');grid;
3.7 Stability of LTI Systems

As indicated, an LTI discrete-time system is BIBO stable if its impulse response is absolutely sum
able. It therefore follows that a necessary condition for an IIR LTI system to be stable is that its
impulse response decays to zero as the sample index gets larger. Program P1_7 is a simple
MATLAB program used to compute the sum of the absolute values of the impulse response
samples of a causal IIR LTI system. It computes N samples of the impulse response sequence,
evaluates
𝐾

𝑆(𝐾) = ∑|ℎ[𝑛]|
𝑛=0

Digital Signal Processing Lab Page 5


for increasing values of K, and checks the value of |h[K]| at each iteration step. If the value of
|h[K]| is smaller than 10−6, then it is assumed that the sum S(K) of equation has converged and is
very close to S(∞).
% Program P1_7
% Stability test based on the sum of the absolute
% values of the impulse response samples
clear all; close all; clc
num = [1 -0.8];
den = [1 1.5 0.9];
N = 200;
h = impz(num,den,N+1);
parsum = 0;
for k = 1:N+1;
parsum = parsum + abs(h(k));
if abs(h(k)) < 10^(-6)
break;
end
end
% Plot the impulse response
n = 0:N;
stem(n,h);
xlabel('Time index n');
ylabel('Amplitude');
% Print the value of abs(h(k))
disp('Value =');
disp(abs(h(k)));
3.8 The Moving Average System
1
𝑦[𝑛] = (𝑥[𝑛 + 1] + 𝑥[𝑛] + 𝑥[𝑛 − 1])
3
Examination of above eq. reveals that the three-point smoothing filter considered here is an LTI
FIR system. Moreover, as y[n] depends on a future input sample x[n + 1], the system is non-causal.
A causal version of the three-point smoothing filter is obtained by simply delaying the output by
one sample period, resulting in the FIR filter described by
1
𝑦[𝑛] = (𝑥[𝑛] + 𝑥[𝑛 − 1] + 𝑥[𝑛 − 2]) (1)
3
𝑀−1
1
𝑦[𝑛] = ∑ 𝑥[𝑛 − 𝑘] (2)
𝑀
𝑘
This defines a causal M-point smoothing FIR filter. The system of Eq. 2 is also known as a moving
average filter. We illustrate its use in filtering high-frequency components from a signal composed
of a sum of several sinusoidal signals.
% Program P1_8
% Generate the input signal clear all; close all; clc
n = 0:100;
s1 = cos(2*pi*0.05*n); % A low frequency sinusoid
s2 = cos(2*pi*0.47*n); % A high frequency sinusoid
x = s1+s2;
% Implementation of the moving average filter

Digital Signal Processing Lab Page 6


M = input('Desired length of the filter = ');
num = ones(1,M);
y = filter(num,1,x)/M;
% Display the input and output signals
subplot(2,2,1); plot(n,s1);
axis([0, 100, -2, 2]);
xlabel('Time index n'); ylabel('Amplitude'); title('Signal # 1');
subplot(2,2,2); plot(n,s2);
axis([0, 100, -2, 2]);
xlabel('Time index n'); ylabel('Amplitude'); title('Signal # 2');
subplot(2,2,3); plot(n,x);
axis([0, 100, -2, 2]);
xlabel('Time index n'); ylabel('Amplitude'); title('Input Signal');
subplot(2,2,4); plot(n,y);
axis([0, 100, -2, 2]); xlabel('Time index n'); ylabel('Amplitude');
title('Output Signal');

LAB WORKSHEET (Lab # 1)

1 Run the Program P1_8 for M = 2 to generate the output signal with x[n] = s1[n] + s2[n] as the input.
Which component of the input x[n] is suppressed by the discrete-time system simulated by this
program?
2 If the LTI system is changed from y[n] = 0.5(x[n] + x[n - 1]) to y[n] = 0.5(x[n] - x[n - 1]), what
would be its effect on the input x[n] = s1[n] + s2[n]?
3 Run Program P1_8 for other values of filter length M, and various values of the frequencies of the
sinusoidal signals s1[n] and s2[n]. Comment on your results.
4 Use sinusoidal signals with different frequencies as the input signals and compute the output signal
for each input. How do the output signals depend on the frequencies of the input signal?
5 Run Program P1_3 and compare y[n] obtained with weighted input with yt[n] obtained by
combining the two outputs y1[n] and y2[n] with the same weights. Are these two sequences equal?
Is this system linear?
6 Consider another system described by: y[n] = x[n] x[n − 1].
Modify Program P1_3 to compute the output sequences y1[n], y2[n], and y[n] of the above system.
Compare y[n] with yt[n]. Are these two sequences equal? Is this system linear?
7 Run Program P1_4 and compare the output sequences y[n] and yd[n - 10]. What is the relation
between these two sequences? Is this system time-invariant?
8 Consider another system described by

Modify Program P1_4 to simulate the above system and determine whether this system is time-
invariant or not.

Digital Signal Processing Lab Page 7

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy