Comsats: University Islamabad

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

COMSATS University Islamabad

Department of Electrical Engineering (Wah Campus)


Digital Signal Processing Lab Manual

LAB # 2: Formation of Discrete-Time Signals in the Time Domain


over MATLAB

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 some 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.

Background Review

R2.1 A discrete-time signal is represented as a sequence of numbers, called samples. A sample value of a
typical discrete-time signal or sequence {x[n]} is denoted as x[n] with the argument n being an integer in the
range −∞ and ∞. For convenience, the sequence {x[n]} is often denoted without the curly brackets.

R2.2 The discrete-time signal may be a finite length or an infinite length sequence. A finite length (also
called finite duration or finite extent) sequence is defined only for a finite time interval:
N1 ≤ n ≤ N2, (2.1)
where −∞ < N1 and N2 < ∞ with N2 ≥ N1. The length or duration N of the finite length sequence is
N = N2 − N1 + 1. (2.2)
R2.3 A sequence ˜x[n] satisfying

(2.3)
is called a periodic sequence with a period N where N is a positive integer and k is any integer.

R2.2 The energy of a sequence x[n] is defined by

(2.2)

The energy of a sequence over a finite interval −K ≤ n ≤ K is defined by

(2.5)

1
COMSATS University Islamabad
Department of Electrical Engineering (Wah Campus)
Digital Signal Processing Lab Manual

R2.5 The average power of an aperiodic sequence x[n] is defined by

(2.6)
The average power of a periodic sequence ˜x[n] with a period N is given by

(2.7)
R2.6 The unit sample sequence, often called the discrete-time impulse or the unit impulse, denoted by δ[n],
is defined by

(2.8)
The unit step sequence, denoted by μ[n], is defined by

(2.9)
R2.7 The exponential sequence is given by

(2.10)
Where A and α are real or complex numbers. By expressing

we can rewrite

(2.11)
R2.8 The real sinusoidal sequence with a constant amplitude is of the form
x[n] = Acos(ωon + φ), (2.12)
where A, ωo, and φ are real numbers. The parameters A, ω o, and φ in Eqs. (2.11) and (2.12) are called,
respectively, the amplitude, the angular frequency, and the initial phase of the sinusoidal sequence x[n].
fo = ωo/2π is the frequency.
R2.9 The complex exponential sequence of Eq. (2.11) with σo = 0 and the sinusoidal sequence of Eq. (2.12)
are periodic sequences if ω oN is an integer multiple of 2π, that is,
ωoN = 2πr, (2.13)

2
COMSATS University Islamabad
Department of Electrical Engineering (Wah Campus)
Digital Signal Processing Lab Manual

where N is a positive integer and r is any integer. The smallest possible N satisfying this
condition is the period of the sequence.
R2.10 The product of two sequences x[n] and h[n] of length N yields a sequence y[n], also of length N, as
given by
y[n] = x[n] · h[n]. (2.12)
The addition of two sequences x[n] and h[n] of length N yields a sequence y[n], also of length N, as given
by
y[n] = x[n] + h[n]. (2.15)
The multiplication of a sequence x[n] of length N by a scalar A results in a sequence y[n] of length N as
given by
y[n] = A · x[n]. (2.16)
The time-reversal of a sequence x[n] of infinite length results in a sequence y[n] of infinite length as defined
by
Y[n] = x[−n]. (2.17)
The delay of a sequence x[n] of infinite length by a positive integer M results in a sequence y[n] of infinite
length given by
y[n] = x[n −M]. (2.18)
If M is a negative integer, the operation indicated in Eq. (2.18) results in an advance of the sequence x[n]. A
sequence x[n] of length N can be appended by another sequence g[n] of length M resulting in a longer
sequence y[n] of length N +M given by
{y[n]} = {{x[n]} , {g[n]}} . (2.19)
MATLAB Commands Used
The MATLAB commands you will encounter in this exercise are as follows:
Operators and Special Characters
: . + - * / ; %
Elementary Matrices and Matrix Manipulation
i ones pi zeros
Elementary Functions
cos exp imag real
Two-Dimensional Graphics
axis grid legend plot
stairs stem title xlabel ylabel

3
COMSATS University Islamabad
Department of Electrical Engineering (Wah Campus)
Digital Signal Processing Lab Manual

General Purpose Graphics Functions


clf subplot
For additional information on these commands, see the MATLAB Reference Guide [Mat05] or type help
command name in the Command window. A brief explanation of the MATLAB functions used here can be
found in Appendix B.
Generation of Sequences
The purpose of this section is to familiarize you with the basic commands in MATLAB for signal generation
and for plotting the generated signal. MATLAB has been designed to operate on data stored as vectors or
matrices. For our purposes, sequences will be stored as vectors. Therefore, all signals are limited to being
causal and of finite length. The steps to follow to execute the programs listed in this book depend on the
platform being used to run the MATLAB.
Exercise 1.1 Unit Sample and Unit Step Sequences
Two basic discrete-time sequences are the unit sample sequence and the unit step sequence of Eqs. (2.8) and
(2.9), respectively. 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, where M < N, can be generated using
the MATLAB command
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 E2_1 can be used to generate and plot a unit sample sequence.
% Program E2_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);

4
COMSATS University Islamabad
Department of Electrical Engineering (Wah Campus)
Digital Signal Processing Lab Manual

xlabel(’Time index n’); ylabel(’Amplitude’);


title(’Unit Sample Sequence’);
axis([-10 20 0 1.2]);
Questions:
Q2.1 Run Program E2_1 to generate the unit sample sequence u[n] and display it.
Q2.2 What are the purposes of the commands clf, axis, title, xlabel, and ylabel?
Q2.3 Modify Program E2_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.
Q2.2 Modify Program E2_1 to generate a unit step sequence s[n]. Run the modified program and display the
sequence generated.
Q2.5 Modify Program E2_1 to generate a delayed unit step sequence sd[n] with an advance of 7 samples.
Run the modified program and display the sequence generated.
Exercise 2.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 E2_2 given below can be employed to generate a complex-
valued exponential sequence.
% Program E2_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’);
Program E2_3 given below can be employed to generate a real-valued exponential sequence.

5
COMSATS University Islamabad
Department of Electrical Engineering (Wah Campus)
Digital Signal Processing Lab Manual

% Program E2_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:
Q2.6 Run Program E2_2 and generate the complex-valued exponential sequence.
Q2.7 Which parameter controls the rate of growth or decay of this sequence? Which parameter controls the
amplitude of this sequence?
Q2.8 What will happen if the parameter c is changed to (1/12)+(pi/6)*i?
Q2.9 What are the purposes of the operators real and imag?
Q2.10 What is the purpose of the command subplot?
Q2.11 Run Program E2_3 and generate the real-valued exponential sequence.
Q2.12 Which parameter controls the rate of growth or decay of this sequence? Which parameter controls the
amplitude of this sequence?
Q2.13 What is the difference between the arithmetic operators ^ and .^?
Q2.12 What will happen if the parameter a is less than 1? Run Program E2_3 again with the parameter a
changed to 0.9 and the parameter K changed to 20.
Q2.15 What is the length of this sequence and how can it be changed?
Q2.16 You can use the MATLAB command sum(s.*s) to compute the energy of a real sequence s[n] stored
as a vector s. Evaluate the energy of the real-valued exponential sequences x[n] generated in Questions Q2.11
and Q2.12.
Exercise 2.3 Sinusoidal Sequences
Another very useful class of discrete-time signals is the real sinusoidal sequence of the form of Eq. (2.12).
Such sinusoidal sequences can be generated in MATLAB using the trigonometric operators cos and sin.
Program E2_2 is a simple example that generates a sinusoidal signal.
% Program E2_2
% Generation of a sinusoidal sequence
n = 0:40;
f = 0.1;

6
COMSATS University Islamabad
Department of Electrical Engineering (Wah Campus)
Digital Signal Processing Lab Manual

phase = 0;
A = 1.5;
arg = 2*pi*f*n - phase;
x = A*cos(arg);
clf; % Clear old graph
stem(n,x); % Plot the generated sequence
axis([0 40 -2 2]);
grid;
title(’Sinusoidal Sequence’);
xlabel(’Time index n’);
ylabel(’Amplitude’);
axis;
Questions:
Q2.17 Run Program E2_2 to generate the sinusoidal sequence and display it.
Q2.18 What is the frequency of this sequence and how can it be changed? Which parameter controls the
phase of this sequence? Which parameter controls the amplitude of this sequence? What is the period of this
sequence?
Q2.19 What is the length of this sequence and how can it be changed?
Q2.20 Compute the average power of the generated sinusoidal sequence.
Q2.21 What are the purposes of the axis and grid commands?
Q2.22 Modify Program E2_2 to generate a sinusoidal sequence of frequency 0.9 and display it. Compare this
new sequence with the one generated in Question Q2.17. Now, modify Program E2_2 to generate a sinusoidal
sequence of frequency 1.1 and display it. Compare this new sequence with the one generated in Question
Q2.17. Comment on your results.
Q2.23 Modify the above program to generate a sinusoidal sequence of length 50, frequency 0.08, amplitude
2.5, and phase shift 90 degrees and display it. What is the period of this sequence?
Q2.22 Replace the stem command in Program E2_2 with the plot command and run the program again. What
is the difference between the new plot and the one generated in Question Q2.17?
Q2.25 Replace the stem command in Program E2_2 with the stairs command and run the program again.
What is the difference between the new plot and those generated in Questions Q2.17 and Q2.22?

7
COMSATS University Islamabad
Department of Electrical Engineering (Wah Campus)
Digital Signal Processing Lab Manual

Exercise Random Signals


A random signal of length N with samples uniformly distributed in the interval (0,1) can be generated by
using the MATLAB command
x = rand(1,N);
Likewise, a random signal x[n] of length N with samples normally distributed with zero mean and unity
variance can be generated by using the following MATLAB command
x = randn(1,N);
Questions:
Q2.26 Write a MATLAB program to generate and display a random signal of length 100 whose elements are
uniformly distributed in the interval [−2, 2].
Q2.27 Write a MATLAB program to generate and display a Gaussian random signal of length 75 whose
elements are normally distributed with zero mean and a variance of 3.
Q2.28 Write a MATLAB program to generate and display five sample sequences of a random sinusoidal
signal of length 31
{X[n]} = {A · cos(ωon + φ)}, (2.20)
where the amplitude A and the phase φ are statistically independent random variables with uniform
probability distribution in the range 0 ≤ A ≤ 4 for the amplitude and in the range 0 ≤ φ ≤ 2π for the phase.
Simple Operations on Sequences
As indicated earlier, the purpose of digital signal processing is to generate a signal with more desirable
properties from one or more given discrete-time signals. The processing algorithm consists of performing a
combination of basic operations such as addition, scalar multiplication, time-reversal, delaying, and product
operation (see R2.10). We consider here three very simple examples to illustrate the application of such
operations.
Exercise 2.5 Signal Smoothing
A common example of a digital signal processing application is the removal of the noise component from a
signal corrupted by additive noise. Let s[n] be the signal corrupted by a random noise d[n] resulting in the
noisy signal x[n] = s[n] + d[n]. The objective is to operate on x[n] to generate a signal y[n] which is a
reasonable approximation to s[n]. To this end, a simple approach is to generate an output sample by averaging
a number of input samples around the sample at instant n. For example, a three-point moving average
algorithm is given by

8
COMSATS University Islamabad
Department of Electrical Engineering (Wah Campus)
Digital Signal Processing Lab Manual

y[n] = 1
3 (x[n − 1] + x[n] + x[n + 1]). (2.21)

Program E2_5 implements the above algorithm.


% Program E2_5
% Signal Smoothing by Averaging
clf;
R = 51;
d = 0.8*(rand(R,1) - 0.5); % Generate random noise
m = 0:R-1;
s = 2*m.*(0.9.^m); % Generate uncorrupted signal
x = s + d’; % Generate noise corrupted signal
subplot(2,1,1);
plot(m,d’,’r-’,m,s,’g--’,m,x,’b-.’);
xlabel(’Time index n’);ylabel(’Amplitude’);
legend(’d[n] ’,’s[n] ’,’x[n] ’);
x1 = [0 0 x];x2 = [0 x 0];x3 = [x 0 0];
y = (x1 + x2 + x3)/3;
subplot(2,1,2);
plot(m,y(2:R+1),’r-’,m,s,’g--’);
legend(’y[n] ’,’s[n] ’);
xlabel(’Time index n’);ylabel(’Amplitude’);
Questions:
Q2.29 Run Program E2_5 and generate all pertinent signals.
Q2.30 What is the form of the uncorrupted signal s[n]? What is the form of the additive noise d[n]?
Q2.31 Can you use the statement x = s + d to generate the noise-corrupted signal? If not, why not?
Q2.32 What are the relations between the signals x1, x2, and x3, and the signal x?
Q2.33 What is the purpose of the legend command?
Exercise 2.6 Generation of Complex Signals
More complex signals can be generated by performing the basic operations on simple signals. For example,
an amplitude modulated signal can be generated by modulating a high-frequency sinusoidal signal xH[n] =
cos(ωHn) with a low-frequency modulating signal xL[n] cos(ωLn). The resulting signal y[n] is of the form

9
COMSATS University Islamabad
Department of Electrical Engineering (Wah Campus)
Digital Signal Processing Lab Manual

y[n] = A(1 + m · xL[n])xH[n] = A(1 + m · cos(ωLn)) cos(ωHn),


where m, called the modulation index , is a number chosen to ensure that (1 + m · xL[n]) is positive for all
n. Program E2_6 can be used to generate an amplitude modulated signal.
% Program E2_6
% Generation of amplitude modulated sequence
clf;
n = 0:100;
m = 0.4;fH = 0.1; fL = 0.01;
xH = sin(2*pi*fH*n);
xL = sin(2*pi*fL*n);
y = (1+m*xL).*xH;
stem(n,y);grid;
xlabel(’Time index n’);ylabel(’Amplitude’);
Questions:
Q2.32 Run Program P1 6 and generate the amplitude modulated signal y[n] for various values of the
frequencies of the carrier signal xH[n] and the modulating signal xL[n], and various values of the modulation
index m.
Q2.35 What is the difference between the arithmetic operators * and .* ?
As the frequency of a sinusoidal signal is the derivative of its phase with respect to time, to generate a swept-
frequency sinusoidal signal whose frequency increases linearly with time, the argument of the sinusoidal
signal must be a quadratic function of time. Assume that the argument is of the form an2 + bn (i.e. the angular
frequency is 2an + b). Solve for the values of a and b from the given conditions (minimum angular frequency
and maximum angular frequency). Program E2_7 is an example program to generate this kind of signal.
% Program E2_7
% Generation of a swept frequency sinusoidal sequence
n = 0:100;
a = pi/2/100;
b = 0;
arg = a*n.*n + b*n;
x = cos(arg);
clf;
stem(n, x);

10
COMSATS University Islamabad
Department of Electrical Engineering (Wah Campus)
Digital Signal Processing Lab Manual

axis([0,100,-1.5,1.5]);
title(’Swept-Frequency Sinusoidal Signal’);
xlabel(’Time index n’);
ylabel(’Amplitude’);
grid; axis;
Questions:
Q2.36 Run Program E2_7 and generate the swept-frequency sinusoidal sequence x[n].
Q2.37 What are the minimum and maximum frequencies of this signal?
Q2.38 How can you modify the above program to generate a swept sinusoidal signal with a minimum
frequency of 0.1 and a maximum frequency of 0.3?
Workspace Information
The commands who and whos can be used to get information about the variables stored in the workspace
and their sizes created in running various MATLAB programs at any time.
Questions:
Q2.39 Type who in the Command window. What information is displayed in the Command window as a
result of this command?
Q2.20 Type whos in the Command window. What information is displayed in the Command window as a
result of this command?
Other Types of Signals
Exercise 2.7 Square wave and Sawtooth Signals
MATLAB functions square and sawtooth can be used to generate sequences of the types shown in Figures
2.1 and 2.2, respectively.

Figure 2.1 Square wave sequences.


Question:
11
COMSATS University Islamabad
Department of Electrical Engineering (Wah Campus)
Digital Signal Processing Lab Manual

Q2.21 Write MATLAB programs to generate the square wave and the sawtooth wave sequences of the types
shown in Figures 2.1 and 2.2. Using these programs, generate and plot the sequences.

Figure 2.2 Sawtooth wave sequences.

12

You might also like