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

Regd. No.

: 2019-EE-076 Name: Muhammad Hassan Bashir Section: B

LAB 2

Introduction to Discrete-Time Signals

Instructions:

 All tasks must be completed in this lab session for full credit.
 Students must show the output and MATLAB code for each task to the instructor
before proceeding to the next task.

References:

1. Digital Signal Processing using MATLAB: A problem solving companion, Vinay K.


Ingle and John G. Proakis, 4th Edition, Cengage Learning, 2016
2. Discrete Time Signal Processing, Alan. V. Oppenheim and Ronald. W. Schafer, 3rd
Edition, Prentice Hall, 2010
3. Digital Signal Processing: Principles, Algorithms, and Applications, John G.
Proakis and Dimitris G. Manolakis, 4th Edition, Prentice Hall, 2007

1. DISCRETE-TIME SIGNALS

Signals are broadly classified into analog and discrete signals. An analog signal will be denoted by 𝑥(𝑡), in
which the variable t can represent any physical quantity but we will assume that it represents time in seconds. A
discrete signal will be denoted by 𝑥[𝑛], in which the variable 𝑛 is integer-valued and represents discrete
instances in time. Therefore, it is also called a discrete-time signal, which is a number sequence and will be
denoted by one of the following notations:

𝑥[𝑛] = {𝑥[𝑛]} = {… , 𝑥(−1), 𝑥(0), 𝑥(1), … }

where the up-arrow indicates the sample at 𝑛 = 0.

In MATLAB, we can represent a finite-duration sequence by a row vector of appropriate values. However, such
a vector does not have any information about sample position 𝑛. Therefore, a correct representation of 𝑥[𝑛]
would require two vectors, one each for x and n. For example, a sequence 𝑥[𝑛] = {2, 1, −1, 0, 1, −3, 2, 10} can
be represented in MATLAB by

>> n = [-4, -3, -2, -1 0, 1, 2, 3]; x = [2, 1, -1, 0, 1, -3, 2, 10];

Most of the content in this lab manual is taken from the second chapter of ref. [1].
Generally, we will use the x-vector representation alone when the sample position information is not required or
when such information is trivial (e.g., when the sequence begins at 𝑛 = 0). An arbitrary infinite-duration
sequence cannot be represented in MATLAB, due to the finite memory limitations.

1.1 TYPES OF SEQUENCES


We use several elementary sequences in digital signal processing for analysis purposes. Their definitions and
MATLAB representations follow.

1.1.1 Unit sample sequence:


1, 𝑛 = 0,
𝛿[𝑛] = { = {… , 0, 0, 1, 0, 0, … }
0, 𝑛 ≠ 0,

In MATLAB, the function zeros(1,N) generates a row vector of N zeros, which can be used to implement 𝛿[𝑛]
over a finite interval. However, the logical relation 𝑛 == 0 is an elegant way of implementing 𝛿[𝑛]. For
example, to implement

1, 𝑛 = 𝑛0,
𝛿[𝑛 − 𝑛0 ] = {
0, 𝑛 ≠ 𝑛0

over the 𝑛1 ≤ 𝑛0 ≤ 𝑛2 interval, we will use the following MATLAB function.

function [x, n] = impseq (n0, n1, n2)


% Generates x(n) = delta(n - n0); n1 <= n <= n2
% [x, n] = impseq (n0, n1, n2)
%
n = n1 : n2;
x = (n-n0) == 0;

1.1.2 Unit step sequence:


1, 𝑛 ≥ 0,
𝑢[𝑛] = { = {… , 0, 0, 1, 1, 1, … }
0, 𝑛 < 0,

In MATLAB, the function ones(1,N) generates a row vector of N ones. It can be used to generate 𝑢[𝑛] over a
finite interval. Once again, an elegant approach is to use the logical relation 𝑛 >= 0. To implement

1, 𝑛 ≥ 𝑛0,
𝑢[𝑛 − 𝑛0 ] = {
0, 𝑛 < 𝑛0

over the 𝑛1 ≤ 𝑛0 ≤ 𝑛2 interval, we will use the following MATLAB function.

function [x, n] = stepseq (n0, n1, n2)


% Generates x(n) = u(n - n0); n1 <= n <= n2
% [x, n] = stepseq (n0,n1,n2)
%
n = n1 : n2; x = (n - n0) >= 0;
2
1.1.3 Real-valued exponential sequence:

𝑥[𝑛] = 𝑎𝑛, ∀ 𝑛, 𝑎 ∈ ℝ

In MATLAB, an array operator “.ˆ” is required to implement a real exponential sequence. For example, to
generate 𝑥[𝑛] = 0.9𝑛, 0 ≤ 𝑛 ≤ 10, we will need the following MATLAB script.

>> n = 0 : 10;
>> x = (0.9).ˆn;

1.1.4 Complex-valued exponential sequence:

𝑥[𝑛] = 𝑒(𝜎+𝑗𝜔0𝑛), ∀ 𝑛,

where 𝜎 produces an attenuation (if < 0) or amplification (if > 0) and 𝜔0 is the angular frequency in radians. A
MATLAB function “exp” is used to generate exponential sequences. For example, to generate 𝑥[𝑛] = 𝑒(2+3𝑗𝑛),
0 ≤ 𝑛 ≤ 10, we will need the following MATLAB script.

>> n = 0 : 10;
>> x = exp((2+3j)*n);

1.1.5 Sinusoidal sequence:

𝑥[𝑛] = 𝐴 cos(𝜔0𝑛 + 𝜃) , ∀ 𝑛,

where 𝐴 is an amplitude and 𝜃 is the phase in radians. A MATLAB function cos (or sin) is used to generate
sinusoidal sequences. For example, to generate [𝑛] = 3 cos(0.1𝜋𝑛 + 𝜋/3) + 2 sin(0.5𝜋𝑛), 0 ≤ 𝑛 ≤ 10, we
will need the following MATLAB script.

>> n = 0 : 10;
>> x = 3*cos(0.1*pi*n+pi/3) + 2*sin(0.5*pi*n);

1.1.6 Periodic sequence:


A sequence 𝑥[𝑛] is periodic if 𝑥[𝑛] = 𝑥[𝑛 + 𝑁], ∀ 𝑛. The smallest integer 𝑁 that satisfies this relation is called
the fundamental period. We will use 𝑥̃[𝑛] to denote a periodic sequence. To generate 𝑃 periods of 𝑥̃[𝑛] from
one period {𝑥[𝑛], 0 ≤ 𝑛 ≤ 𝑁 − 1}, we can copy 𝑥[𝑛] 𝑃 times:

>> xtilde = [x, x, ..., x];

But an elegant approach is to use MATLAB’s powerful indexing capabilities. First we generate a matrix
containing 𝑃 rows of 𝑥[𝑛] values. Then we can concatenate 𝑃 rows into a long row vector using the construct
(:). However, this construct works only on columns. Hence we will have to use the matrix transposition operator
’ to provide the same effect on rows.

>> xtilde = x’ * ones(1, P); % P columns of x; x is a row vector


>> xtilde = xtilde(:); % Long column vector
3
>> xtilde = xtilde’; % Long row vector

Note that the last two lines can be combined into one for compact coding.

1.2 OPERATIONS ON SEQUENCES

1.2.1 Signal Addition:


This is a sample-by-sample addition given by

𝑥1[𝑛] + 𝑥2[𝑛].

It is implemented in MATLAB by the arithmetic operator “+”. However, the lengths of 𝑥1[𝑛]and 𝑥2[𝑛] must be
the same. If sequences are of unequal lengths, or if the sample positions are different for equal length
sequences, then we cannot directly use the operator +. We have to first augment 𝑥1[𝑛]and 𝑥2[𝑛] so that they
have the same position vector 𝑛 (and hence the same length). This requires careful attention to MATLAB’s
indexing operations. In particular, logical operation of intersection “&”, relational operations like “<=” and
“==”, and the find function are required to make 𝑥1[𝑛]and 𝑥2[𝑛] of equal length.

Task 1

 Complete the following function, sigadd, for the addition of two sequences.
 Also write the commands to plot the signals 𝑥1[𝑛], 𝑥2[𝑛] and 𝑦[𝑛] = 𝑥1[𝑛] + 𝑥2[𝑛] in the top, middle
and bottom sections respectively of the same figure window using the subplot command. Note that all
plots should have the same range of temporal index values. Use stem command for plotting.
 Initialize the two sequences and call the function in the command window using the following piece of
code:

% Function for addition of two signals


function [y,n] = sigadd(x1,n1,x2,n2)
% Implements y(n) = x1(n)+x2(n)
% -----------------------------
% [y,n] = sigadd(x1,n1,x2,n2)
% y = sum sequence over n, which includes n1 and n2
% x1 = first sequence over n1
% x2 = second sequence over n2 (n2 can be different from n1)
%
n = min(min(n1),min(n2)):max(max(n1),max(n2)); % Duration of y(n)
y1 = zeros(1,length(n)); y2 = y1; % Initialization
y1(find((n>=min(n1))&(n<=max(n1))==1))=x1; % x1 with duration of y
y2(find((n>=min(n2))&(n<=max(n2))==1))=x2; % x2 with duration of y
y = y1+y2; % Sequence addition

%Function Call For given input and plot of signals


n1 = -2 : 3;
x1 = [1, 2, 0, -2, 2, 1];
n2 = -5 : 4;
x2 = [2, 3, 0, -1, 5, 0, -2, 4, 1, -4];
[y, n] = sigadd (x1, n1, x2, n2)
%To match the size of both signal for same range of temporal index values
y1 = zeros(1,length(n)); y2 = y1;
y1(find((n>=min(n1))&(n<=max(n1))==1))=x1;
y2(find((n>=min(n2))&(n<=max(n2))==1))=x2;
4
%Plot of 1st input signal (x1[n])
subplot(3,1,1); stem(n,y1);
title('1st Input Signal')
xlabel('n'); ylabel('x1(n)');
%Plot of 2nd input signal (x2[n])
subplot(3,1,2); stem(n,y2);
title('2nd Input Signal')
xlabel('n'); ylabel('x2(n)');
%Plot of Resultant Signal (x1[n] + x2[n])
subplot(3,1,3); stem(n,y);
title('Resultant Signal')
xlabel('n'); ylabel('y(n)');

5
1.2.2 Signal Multiplication:
This is a sample-by-sample multiplication given by

𝑥1[𝑛]𝑥2[𝑛].

It is implemented in MATLAB by the arithmetic operator “.∗”. Once again, similar restrictions apply for the .∗
operator as for the + operator.

Task 2

 Complete the following function, sigmult, for the multiplication of two sequences.
 Also write the commands to plot the signals 𝑥1[𝑛], 𝑥2[𝑛] and 𝑦[𝑛] = 𝑥1[𝑛]𝑥2[𝑛] in the top, middle and
bottom sections respectively of the same figure window using the subplot command. Note that all plots
should have the same range of temporal index values. Use stem command for plotting.
 Initialize the two sequences and call the function in the command window using the following piece of
code:

% Function for multiplication of two signals


function [y,n] = sigmult(x1,n1,x2,n2)
% Implements y(n) = x1(n)*x2(n)
% -----------------------------
% [y,n] = sigmult(x1,n1,x2,n2)
% y = product sequence over n, which includes n1 and n2
% x1 = first sequence over n1
% x2 = second sequence over n2 (n2 can be different from n1)
n = min(min(n1),min(n2)):max(max(n1),max(n2)); % Duration of y(n)
y1 = zeros(1,length(n)); y2 = y1; %
y1(find((n>=min(n1))&(n<=max(n1))==1))=x1; % x1 with duration of y
y2(find((n>=min(n2))&(n<=max(n2))==1))=x2; % x2 with duration of y
y = y1 .* y2; % Sequence multiplication

%Function Call For given input and plot of signals


n1 = -2 : 3;
x1 = [1, 2, 0, -2, 2, 1];
n2 = -5 : 4;
x2 = [2, 3, 0, -1, 5, 0, -2, 4, 1, -4];
[y, n] = sigmult (x1, n1, x2, n2)
%To match the size of both signal for same range of temporal index values
y1 = zeros(1,length(n)); y2 = y1;
y1(find((n>=min(n1))&(n<=max(n1))==1))=x1;
y2(find((n>=min(n2))&(n<=max(n2))==1))=x2;
%Plot of 1st input signal (x1[n])
subplot(3,1,1); stem(n,y1);
title('1st Input Signal')
xlabel('n'); ylabel('x1(n)');
%Plot of 2nd input signal (x2[n])
subplot(3,1,2); stem(n,y2);
title('2nd Input Signal')
xlabel('n'); ylabel('x2(n)');
%Plot of Resultant Signal (x1[n] + x2[n])
subplot(3,1,3); stem(n,y);
title('Resultant Signal')
xlabel('n'); ylabel('y(n)');

6
1.2.3 Scaling:
In this operation, each sample is multiplied by a scalar α. An arithmetic operator (∗) is used to implement the
scaling operation in MATLAB.

1.2.4 Shifting:
In this operation, each sample of 𝑥[𝑛] is shifted by an amount 𝑘 to obtain a shifted sequence 𝑦[𝑛] = 𝑥[𝑛 − 𝑘].
If we let 𝑚 = 𝑛 − 𝑘, then 𝑛 = 𝑚 + 𝑘 and the above operation is given by 𝑦[𝑚 + 𝑘] = 𝑥[𝑚]. Hence this
operation has no effect on the vector 𝑥, but the vector 𝑛 is changed by adding 𝑘 to each element. This is shown
in the function sigshift.

7
function [y, n] = sigshift (x, m, k)
% Implements y(n) = x (n - k)
%
% [y, n] = sigshift (x, m, k)
%
n = m + k;
y = x;

1.2.5 Folding:
In this operation, each sample of 𝑥[𝑛] is flipped around 𝑛 = 0 to obtain a folded sequence 𝑦[𝑛], i.e., 𝑦[𝑛] =
𝑥[−𝑛]. In MATLAB, this operation is implemented by the fliplr (x) function for sample values and by -fliplr (n)
function for sample positions, as shown in the sigfold function.

function [y, n] = sigfold (x, n)


% Implements y(n) = x(-n)
%
% [y, n] = sigfold (x, n)
%
y = fliplr (x);
n = -fliplr (n);

Task 3
Consider the discrete-time signal 𝑦[𝑛] = 2𝑥[𝑛] − 3𝑥[𝑛 − 2] + 5𝑥[−𝑛], where 𝑥[𝑛] = {−2, −1, 0, 1, 2}.

 Write a MATLAB script file that uses the functions defined above for signal addition, signal shifting
and signal folding to construct the above signal.
 Using stem command, subplot the signal 𝑥[𝑛], 𝑥[−𝑛], 𝑥[𝑛 − 2] and 𝑦[𝑛] in a 2x2 grid on the same
figure window. Write your code in the space below.
%Input Signal
n = 0 : 4;
x = [-2, -1, 0, 1, 2];
%y1[n] = 2*x[n]
y1 = 2*x;
%y2[n] = 3*x[n-2]
[y2,n2] = sigshift(x,n,2);
y2 = 3*y2;
%y3[n] = 5*x[-n]
[y3,n3] = sigfold(x,n);
y3 = 5*y3;
%y[n] = 2*x[n] - 3*x[n-2] + 5*x[-n]
[y4 n4] = sigsub(y1,n,y2,n2);
[y n5] = sigadd(y4,n4,y3,n3)

%Plot of signal (x[n])


subplot(2,2,1); stem(n,x);
title('Input Signal')
xlabel('n'); ylabel('x[n])');
%Plot of signal (x[-n])
8
subplot(2,2,2); stem(n3,y3);
title('Time Reversal of Input Signal')
xlabel('n'); ylabel('x[-n]');
%Plot of Signal (x[n-2])
subplot(2,2,3); stem(n2,y2);
title('Input Signal Delayed by 2')
xlabel('n'); ylabel('x[n-2]');
%Plot of Signal (y[n])
subplot(2,2,4); stem(n5,y);
title('Output Signal')
xlabel('n'); ylabel('y(n)');

9
Task 4
 Write a MATLAB function that computes the even and odd part of a signal 𝑥[𝑛], i.e., 𝑥𝑒[𝑛] and 𝑥𝑜[𝑛]
respectively. Using stem command, subplot the signal 𝑥[𝑛], 𝑥𝑒[𝑛] and 𝑥𝑒[𝑛] in a 3x1 grid on the same
figure window.
 Call the function for 𝑥[𝑛] = 𝑢[𝑛], −10 ≤ 𝑛 ≤ 10.

Write your MATLAB function in the space below.


%Input Signal
[x n] = stepseq(0,-10,10);
%Time Reverse of Input Signal
[x1 n1] = sigfold(x,n);
%Even Part of Input Signal
[xe ne] = sigadd(x,n,x1,n1);
xe = (1/2)*xe;
%Odd Part of Input Signal
[xo no] = sigsub(x,n,x1,n1);
xo = (1/2)*xo;

%Plot of input signal (x[n])


subplot(3,1,1); stem(n,x);
title('Input Signal')
xlabel('n'); ylabel('x(n)');
%Plot of odd part of input signal (xo[n])
subplot(3,1,2); stem(no,xo);
title('Odd Part of Input Signal')
xlabel('n'); ylabel('xo(n)');
%Plot of even part of input Signal (xe[n])
subplot(3,1,3); stem(ne,xe);
title('Even Part of Input Signal')
xlabel('n'); ylabel('xe(n)');

1
0
1.2.6 Sample Summation:
This operation differs from signal addition operation. It adds all sample values of 𝑥[𝑛] between 𝑛1 and 𝑛2, i.e.,
𝑛2

∑ 𝑥[𝑛] = 𝑥[𝑛1] + ⋯ + 𝑥[𝑛2].


𝑛=𝑛1

It is implemented by the MATLAB function sum(x(n1 : n2)).

1.2.7 Sample Product:


This operation also differs from signal multiplication operation. It multiplies all sample values of 𝑥[𝑛] between
𝑛1 and 𝑛2, i.e.,
𝑛2

𝖦 𝑥[𝑛] = 𝑥[𝑛1] × … × 𝑥[𝑛2].


𝑛=𝑛1

It is implemented by the MATLAB function prod(x(n1 : n2)).

1.2.8 Signal Energy:


The energy of a sequence 𝑥[𝑛] is given by
∞ ∞
̅[𝑛̅] = ∑|𝑥[𝑛]|2 ,
𝐸𝑥 = ∑ 𝑥[𝑛]𝑥
−∞ −∞

where the overbar denotes the operation of complex conjugation.

Task 5
Write a MATLAB function that computes the signal energy and prints it on the command window. Call the
function for 𝑥[𝑛] = {−2, −1, 0, 1, 2}. Write your MATLAB function in the space below.
%Function to Calculate Energy of Signal
function [y] = sigenergy(x)
% Implements y(n) = x(-n)
% -----------------------
% [y,n] = sigfold(x,n)
%
y = sum(abs(x).^2);

%Function Call
x = [-2, -1, 0, -1, 2];
E = sigenergy(x)

1
1
Exercise: Generate the following sequences using the operations and functions discussed in
this manual. Plot signal samples using the stem function. Choose suitable sample position
vector for 𝑥3 [𝑛] and 𝑥4 [𝑛].
1. 𝑥1 [𝑛] = 3𝛿[𝑛 + 2] + 2𝛿[𝑛 − 1] + 𝛿[𝑛 + 3] − 4𝛿[𝑛 − 8], −10 ≤ 𝑛 ≤ 10.

[x1, n1] = impseq(-2,-10,10);


x1 = 3*x1;
[x2, n2] = impseq(1,-10,10);
x2 = 2*x2;
[x3, n3] = impseq(-3,-10,10);
[x4, n4] = impseq(8,-10,10);
[y5, n5] = sigadd(x1,n1,x2,n2);
[y6, n6] = sigsub(x3,n3,x4,n4);
[y7, n7] = sigadd(y5,n5,y6,n6);
stem(n7,y7);
title('Plot of x1[n] Vs n')
xlabel('n'); ylabel('x1[n]');

2. 𝑥2 [𝑛] = ∑𝟓𝒌 𝟓𝒆
|𝒌|/𝟐
𝛿[𝑛 − 3𝑘], −20 ≤ 𝑛 ≤ 20.

n2 = [-20:20];
x2 = zeros(1,length(n2));
for k = -5:5
x2 = x2 + exp(-abs(k))*impseq(3*k,-20,20);
end
stem(n2,x2);
title('Plot of x2[n] Vs n')
xlabel('n'); ylabel('x2[n]');

1
2
3. 𝑥3 [𝑛] = 5𝑢[𝑛 + 2] − 2𝑢[𝑛 − 1] + 𝑢[𝑛 + 3] + 2𝑢[𝑛 − 8].

x3 = 5*stepseq(-2,-10,10) - 2*stepseq(1,-10,10) + stepseq(-3,-10,10) + 2*stepseq(8,-10,10);


n3 = [-10:10];
stem(n3,x3);
title('Plot of x3[n] Vs n')
xlabel('n'); ylabel('x3[n]');

1
3
4. 𝑥4 [𝑛] = 𝑒0.2𝑛(𝑢[𝑛 + 10] − 𝑢[𝑛 − 20]).
n4 = [-15:25];
x4 = exp(0.2*n4).*(stepseq(-10,-15,25) - stepseq(20,-15,25));
stem(n4,x4);
title('Plot of x4[n] Vs n')
xlabel('n'); ylabel('x4[n]');

5. 𝑥5 [𝑛] = 10(cos(0.495𝜋𝑛) + cos(0.505𝜋𝑛)), −200 ≤ 𝑛 ≤ 200


n5 = [-200:200];
x5 = 10*(cos(0.495*pi*n5) + cos(0.505*pi*n5));
stem(n5,x5);
title('Plot of x5[n] Vs n')
xlabel('n'); ylabel('x5[n]');

1
4

You might also like