Professional Documents
Culture Documents
Introduction To Discrete-Time Signals: Instructions
Introduction To Discrete-Time Signals: Instructions
LAB 2
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. 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:
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
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.
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
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
𝑥[𝑛] = 𝑎𝑛, ∀ 𝑛, 𝑎 ∈ ℝ
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;
𝑥[𝑛] = 𝑒(𝜎+𝑗𝜔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);
𝑥[𝑛] = 𝐴 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);
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.
Note that the last two lines can be combined into one for compact coding.
𝑥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:
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:
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.
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)
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.
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
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.
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].
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]');
1
4