Sunteți pe pagina 1din 6

EEE 496 DIGITAL SIGNAL PROCESSING

Lab 2: Implementation and Analysis of Discrete-Time Systems


Objective:
To gain experience in implementing and analyzing digital systems within MATLAB. In
particular, you will look at time-domain convolution, system impulse responses, and the discrete-
time Fourier transform (DTFT).

Assessment:
- Your grade for this lab will be based on your ability to create and work with digital systems
within MATLAB, and on your reporting of the results. The report should contain any
mathematical calculations or derivations carried out, MATLAB plots of results with brief
descriptions, the MATLAB code with which you obtained your results, and answers to specific
questions below.
- Clearly label all plots and their axes (points for style will be deducted otherwise)
- DON’T COPY OTHERS BLINDLY!!
- Please attend the lab section to which you have been assigned.

Pre-lab:
- Carefully read through this lab description, so that you know what is required.
- Read through the text books (and bring them with you) so that you know how to answer the
questions.
- Familiarize yourself with the MATLAB commands that may be required for this lab – see the
list at the end of this lab description for some hints.

Instructions:

Convolution: An input signal x[n] = {2,-1,3} is applied to system with impulse response h[n] =
{1,2,2,3}. To find the response y[n], we can use the following code:

clear all;
x = [2 -1 3]; h = [1 2 2 3]; % Signals x[n] and h[n]
y = conv(x,h); % Displays convolution y[n]
nx = -1:1; nh = -2:1; % Indices of x[n] and h[n]
ns = nx(1) + nh(1); % Starting index of y[n]
ne = nx(length(nx)) + nh(length(nh)); % Ending index of y[n]
ny = ns:ne; % Index of y[n]
subplot(3,1,1);
stem(nx,x); % Plot signal x
xlabel('n')
ylabel('x[n]')
subplot(3,1,2);
stem(nh,h); % Plot signal h
xlabel('n')
ylabel('h[n]')
subplot(3,1,3);
stem(ny,y); % Plot signal y
xlabel('n')

Course teacher: Muhammad Sana Ullah (Sanam) 1


EEE 496 DIGITAL SIGNAL PROCESSING

ylabel('y[n]')

```
5

x[n]
0

-5 0 1
-1 -0.5 n 0.5
4
h[n]

0
0 1
-2 -1.5 -1 -0.5
n 0.5

10
y[n]

0
0 1 2
-3 -2
-1 n
Difference equation: A system is described by the difference equation y(n) - 0.9y(n-1) = x(n).
We will find the response of the system when input is given by x(n) = u(n)-u(n-10). To generate
stem function, we will use a user-defined function ‘stepseq.m’.

b = [1]; a = [1,-0.9]; % Coefficients of difference equation


n = -5:50; % index
x = stepseq(0,-5,50) - stepseq(10,-5,50); % Signal x[n]
y = filter(b,a,x); % Find response, y[n]
subplot(2,1,1);stem(n,x); title('Input sequence') % plot
subplot(2,1,2); stem(n,y); title('Output sequence') % plot
xlabel('n'); ylabel('y(n)'); axis([-5,50,-0.5,8])

Save the following code as stepseq.m in the same directory.


function [x,n] = stepseq(n0,n1,n2)
% Generates x(n) = u(n-n0); n1 <= n,n0 <= n2
% [x,n] = stepseq(n0,n1,n2)

if ((n0 < n1) | (n0 > n2) | (n1 > n2))


error('arguments must satisfy n1 <= n0 <= n2')
end

n = [n1:n2];
%x = [zeros(1,(n0-n1)), ones(1,(n2-n0+1))];

Course teacher: Muhammad Sana Ullah (Sanam) 2


EEE 496 DIGITAL SIGNAL PROCESSING

x = [(n-n0) >= 0];

x(n)

0.5

0
-10 0 10 20 30 40 50
n

4
y(n)

0
-5 0 5 10 15 20 25 30 35 40 45 50
n

DTFT:
clear all;
% Discrete-time Signal
Ts = 0.0002; % Sampling interval
n = -25:1:25; % index
% use any af the following 3 signals by uncommenting it

% x = exp(-1000*abs(n*Ts)); % exponential signal


% x = cos(1000*n*Ts); % sinusoidal signal
x=[0 0 0 ones(1,45) 0 0 0]; % rectangular signal

% Discrete-time Fourier Transform


K = 500; k = 0:1:K;
w = pi*k/K;
X = x * exp(-j*n'*w);
X = real(X);
w = [-fliplr(w), w(2:K+1)];
X = [fliplr(X), X(2:K+1)];

subplot(2,1,1);
stem(n*Ts*1000,x);
xlabel('n'); ylabel('x(n)')
subplot(2,1,2);plot(w/pi,X);
xlabel('Frequency in pi units'); ylabel('X(w)')
title('Discrete-time Fourier Transform')

Course teacher: Muhammad Sana Ullah (Sanam) 3


EEE 496 DIGITAL SIGNAL PROCESSING

x(n)
0.5

0
-5 -4 -3 -2 -1 0 1 2 3 4 5
n
Discrete-time Fourier Transform
60

40
X(w)

20

-20
-1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1
Frequency in pi units

Frequency response:

Find the frequency response of the system described by


y[n] +0.4y[n-1] + 0.8y[n-2] = x[n]

Use the following code:

clear all;

F = (0:399)/800; W = 2*pi*F; % frequency array


H = freqz([1 0 0], [1 0.4 0.8], W); % get frequency response
subplot(2,1,1);
plot(F,abs(H));
xlabel('Frequency in pi units'); ylabel('|H(w)|')

subplot(2,1,2);
plot(F,angle(H));
xlabel('Frequency in pi units'); ylabel('Phase of H(w)')

Course teacher: Muhammad Sana Ullah (Sanam) 4


EEE 496 DIGITAL SIGNAL PROCESSING

4
|H(w)|

0
0 0.05 0.1 0.15 0.2 0.25 0.3 0.35 0.4 0.45 0.5
Frequency in pi units

2
Phase of H(w)

-1
0 0.05 0.1 0.15 0.2 0.25 0.3 0.35 0.4 0.45 0.5
Frequency in pi units

Tasks:

1. Introduction to convolution:

1 1. a. Create the discrete-time sequence:


x[n] = u[n] – u[n-10]
where u[n] is the unit step function.
1 b. Now, convolve x[n] over and over:
a[n] = x[n]*x[n]
b[n] = a[n]*x[n]
c[n] = b[n]*x[n]

1 c. Plot a[n], b[n], and c[n] using the stem() function.

2. Convolution of signals and system impulse responses:


a. Load the supplied acoustic impulse response of a room into MATLAB
using the command:
[impr,fs,nbits] = wavread('impr.wav');

Course teacher: Muhammad Sana Ullah (Sanam) 5


EEE 496 DIGITAL SIGNAL PROCESSING

This impulse response was obtained by creating an impulsive noise at one


position in the room and recording (and digitizing) the sounds arriving at
another position in the room.
b. Plot the impulse-response waveform impr using the plot() command and
listen to it using the soundsc() command. What can you see and hear in the
impulse response?
c. Load the supplied speech signal into MATLAB using the command:
[y,fs,nbits] = wavread('oilyrag.wav');

d. Convolve the speech signal with the impulse response, and plot and listen
to the resulting signal. Describe what you see and hear, comparing it to the
original speech signal y.

3. DTFT
Plot the magnitude and phase of the frequency response of the system described by-
y[n] +0.1y[n-1] + 0.8y[n-2] = x[n] + 0.1x[n-1] + x[n-2]

4. Filtering white Gaussian noise


1 a. Create a signal 1000 samples long of white Gaussian noise. Convolve
this signal with h[n] = [ 1 2 3], then compute the DTFT of the convolved
signal
2

Potentially useful MATLAB commands:


Note that this is not an exhaustive list! You are not required to incorporate
all of these in your scripts.

help helpwin figure plot stem


<topic>
hist subplot hold on xlabel ylabel
legend title function clear close
clc zeros ones cos exp
abs conv sum repmat sqrt
mean log10 randn polyfit polyval
if for end real imag
angle unwrap phase max min
soundsc wavread wavwrite

Course teacher: Muhammad Sana Ullah (Sanam) 6

S-ar putea să vă placă și