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

College of Engineering and Computing

EEE 472 Digital Signal


Processing
Lab Manual
Dr. Kahtan Aziz

Lab-1

1. Discrete-Time Signals in the Time Domain


1.1

Introduction

Digital signal processing is concerned with the processing of a discrete-time signal, called the input
signal, to develop another discrete-time signal, called the output signal, with more desirable
properties. In certain applications, it may be necessary to extract some key properties of the original
signal using specific digital signal processing algorithms. It is also possible to investigate the
properties of a discrete-time system by observing the output signals for specific input signals. It is
thus important to learn first how to generate in the time domain same basic discrete-time signals in
MATLAB and perform elementary operations on them, which are the main objectives of this first
exercise. A secondary objective is to learn the application of some basic MATLAB commands and
how to apply them in simple digital signal processing problems

Exercise 1.1 Unit Sample and Unit Step Sequences


A unit sample sequence u[n] of length N can be generated using the MATLAB command
u = [1 zeros (1, N -1)];
A unit sample sequence ud[n] of length N and delayed by M samples, whereM < N, can be
ngenerated using the MATLAB command
ud = [zeros(1,M) 1 zeros(1,N - M - 1)];
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 can be used to generate and plot a unit sample sequence.

% Program P1_1
% Generation of a Unit Sample Sequence
clf;
% 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]);

Questions:
Q1.1 Run Program P1 1 to generate the unit sample sequence u[n] and display it.
Q1.2 What are the purposes of the commands clf, axis, title, xlabel, and ylabel?
Q1.3 Modify Program P1 1 to generate a delayed unit sample sequence ud[n] with a
delay of 11 samples. Run the modified program and display the sequence generated.
Q1.4 Modify Program P1 1 to generate a unit step sequence s[n]. Run the modified
program and display the sequence generated.

Exercise 1.2 Exponential Signals


Another basic discrete-time sequence is the exponential sequence. Such a sequence can be
Generated using the MATLAB operators. ^ and exp.

Program P1 2 given below can be employed to generate a complex-valued exponential


Sequence.
% Program P1_2
% Generation of a complex exponential sequence
clf;
c = -(1/12)+(pi/6)*i;
K = 2;
n = 0:40;
x = K*exp(c*n);
subplot(2,1,1);
stem(n,real(x));
xlabel(Time index n);ylabel(Amplitude);
title(Real part);
subplot(2,1,2);
stem(n,imag(x));
xlabel(Time index n);ylabel(Amplitude);
title(Imaginary part);
Questions:
Q1.6 Run Program P1 2 and generate the complex-valued exponential sequence.
Q1.7 Which parameter controls the rate of growth or decay of this sequence? Which
Parameter controls the amplitude of this sequence?
Q1.9 What are the purposes of the operators real and imag?
Q1.10 What is the purpose of the command subplot?

Program P1 3 given below can be employed to generate a real-valued exponential


sequence.
% Program P1_3
% Generation of a real exponential sequence
clf;
n = 0:35; a = 1.2; K = 0.2;
x = K*a.^+n;
stem(n,x);
xlabel(Time index n);ylabel(Amplitude);

Questions:
Q1.11 Run Program P1 3 and generate the real-valued exponential sequence.
Q1.12 Which parameter controls the rate of growth or decay of this sequence? Which
Parameter controls the amplitude of this sequence?
Q1.13 What is the difference between the arithmetic operators ^ and .^?

LAB-2
Discrete Time Fourier Analysis
Matlabs function fft is used l for computing the Discrete Fourier Transform of a signal.
The following code examples will help you to understand the details of using the fft function.
8 point DFT of the sequence x(n) =(1,1,1,1,0,0,0,0] can be computed as follows
x=[1 1 1 1 0 0 0 0];
fft(x); or fft(x, 8);

Exercise 2.1: The typical syntax for computing the FFT of a signal is fft(x, N) where x is the signal,
x[n], you wish to transform, and N is the number of points in the FFT. N must be at least as large as
the number of samples in x[n]. To demonstrate the effect of changing the value of N,
% P_2.1 DFT to demonstrate the effect of changing the value of N
% synthesizes a cosine wave with 30 samples at 10 samples per period.
n = [0:29];
x = cos(2*pi*n/10);
% define N
N1= 64;
% finds the fft of x
X1=fft(x,N1);

% the abs function finds the magnitude of the transform

X1abs=abs(X1);
%The frequency scale begins at 0 and extends to N - 1 for an N-point FFT.
F1 = [0 : N1 - 1]/N1;
subplot(3,1,1)
plot(F1,X1,'-x'),title('N = 64'),axis([0 1 0 20])

Questions:
2.1. Run the program and observe the plot
2.2 Modify the program with N= 128 and N=256 , then observe the effect of
changing N

Exercise 2_2: In the above example the length of x[n] was limited to 3 periods in length.

Now, let's choose a large value for N (for a transform with many points), and vary the number of
repetitions of the fundamental period.

% P2_2 With repetitions of the fundamental period.

n = [0:29];
x1 = cos(2*pi*n/10); % 3 periods
x2 = [x1 x1]; % 6 periods
N = 2048;
X1 = abs(fft(x1,N));
X2 = abs(fft(x2,N));
F = [0:N-1]/N;
subplot(3,1,1)
plot(F,X1),title('3 periods'),axis([0 1 0 50])
subplot(3,1,2)
plot(F,X2),title('6 periods'),axis([0 1 0 50])

Questions:
2.1. Run the program and observe the plot
2.2 Modify the program with 9 periods, then observe plot

LAB -3
Digital Filters
y = filter(num,den,x) generates an output vector y of the same length as the
Specified input vector x with zero initial conditions, that is, y[-1] y[-2] = ... =
y[-N] = 0.

Exercise 3.1
% Program P3_1
% Simulation of an M-point Moving Average Filter
% Generate the input signal
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
M = input(Desired length of the filter = );
num = ones(1,M);
y = filter(num,1,x)/M;
% Display the input and output signals
clf;
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);
axis;

Questions:

Q3.1 Run the above program forM = 2to generate the output signal with x[n] = s1[n]
+ s2[n] as the inpu
Q3.1 Run Program P2 1 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.t.

LAB-4
Circular Convolution
Aim : To write a MATLAB program to obtain the circular convolution of two sequences

Theory: A convolution operation that contain a circular shift is called the circular
convolution . Circular convolution of two sequences x2 (n) and x2 (n) each having a length of
N is givenby
N 1

x1 ( n)( N ) x2 ( n) x1 ( m)x2 (( n m)) N


m 0

The circular convolution is also an N- Point sequence. If the length of the sequence are not equal,
sufficient number of zeros must be padded to it
Algorithm
1. Get the length of the input sequence
1. If the length of the sequence is not equal add zeros
2. Calculate the circular convolution
Example
cconv([3 2 1],[1 2 3 2 1],5)
where the number '5' as input indicates N=5 point circular convolution. So the number of outputs will
be 5
Let see how to achieve the result.
For circular convolution flip h=[3 2 1] as h'= 1 2 3 and find the sum of products as shown below
Step 1:
12321
0 0 1 2 3 ( need to add two zeros since N=5 and size of h is only 3.
________
1x0+2x0+3x1+2x2+1x3 = 3 + 4+3 = 10

Step 2: (Circularly shift the second row by 1 digit to the left


12321
01230
__________
0+2+6+6+0 = 14

Step 3:
12321
12300
_________
1+4+9+0+0 = 14
Step4:
12321
23001
________
2+6+0+0+1=9
Step 5:
12321
30012
__________
3+0+0+2+2=7
So the output is
[ 7 9 14 14 10]

program1( using cconv function)


% Circular Convolution
X1=input('enter first sequence= ');
N1=length(X1);% Gets the length of the input sequence
X2= input('enter second sequence= ');
N2=length(X2);
mode = input('enter mode = ');
y= cconv(X1, X2,mode)

program- 2 ( using the algorithm )


X1=input('enter first sequence= ');
N1=length(X1);% Gets the length of the input sequence
X2= input('enter second sequence= ');
N2=length(X2);
N=max(N1,N2);
X1=[X1,zeros(1, (N-N1))];
X2=[X2, zeros(1, (N-N2))];
for n=0:(N-1);
y(n+1)=0;
for i=0:N-1
j=mod(n-i,N);
y(n+1)=y(n+1)+X1(i+1)*X2(j+1)
end
end

Exercise
For the input given below verify the results for progam 1 & program 2. Also verify the
same with your own calculation

Inputs
1.

X1= [ 1 1 2]
X2=[ 1 2 3 4]
N=4

2.

X1=[ 3 2 1]
X2=[1 2 3 2 1]
N=5

You might also like