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

EE-384: Digital Signal Processing Spring 2023

Laboratory 1: Discrete-time Signals in MATLAB


Instructor: Mr Ammar Naseer EE UET New Campus

Aims
We begin with the concepts of signals in discrete time. A number of important types of signals and their
operations are introduced. The emphasis in this chapter is on the representations and implementation
of signals using MATLAB.

Pre-Lab:
Discrete time Signals
Signals are broadly classified into analog and discrete signals. An analog signal will be denoted by x(t), 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 x(n), in which the variable n 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 n = 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 n. Therefore a correct
representation of x(n) would require two vectors, one each for x and n. For example, a sequence x(n) =
[2 1 -1 0 1 4 3 7] can be represented in MATLAB by

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 n = 0). An arbitrary
infinite-duration sequence cannot be represented in MATLAB due to the finite memory limitations.

Type of Sequences

Page 1
Laboratory 2: Discrete-time Signals in MATLAB 2.2

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

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
𝛿(n) over a finite interval. However, the logical relation n==0 is an elegant way of implementing 𝛿(n).

To implement,

1 𝑛 = 𝑛0
𝛿(𝑛 − 𝑛0 ) = {
0 𝑛 ≠ 𝑛0

over the n1 < n0 < n2 interval, we will use the following MATLAB function

For example, to generate x(n) = δ[n − 2]; -5 ≤ n ≤ 5, we will need the following MATLAB script:

>> [x,n] = impseq(-5,5,2);


>> stem(n,x)

Unit Step Sequence

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

over the n1 < n0 < n2 interval, we will use the following MATLAB function
Laboratory 2: Discrete-time Signals in MATLAB 2.3

For example, to generate x(n) = u[n + 1]; -5 ≤ n ≤ 5, we will need the following MATLAB script:

>> [x,n] = stepseq(-5,5,-1);


>> stem(n,x)

Real-valued exponential sequence

In MATLAB an array operator "." is required to implement a real exponential sequence.

For example, to generate x(n) = (0.9)n; 0 ≤ n ≤ 10, we will need the following MATLAB script:

>> n = [0:10]; x = (0.9).^n;


>> stem(n,x)

Complex-valued exponential sequence

Where 𝜎 produces an attenuation (if < 0) or amplification (if > 0) and 𝜔0 is the frequency in radians. A
MATLAB function exp is used to generate exponential sequences.

For example, to generate x(n) = exp[(2+j3)n], 0 ≤ n ≤ 10, we will need the following MATLAB script:

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


>> stem(n,x)

Sinusoidal sequence

where A is an amplitude and 𝜃0 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𝜋𝑛 + ) + 2sin (0.5𝜋𝑛).
3
0<n<10, we will need the following MATLAB script:
Laboratory 2: Discrete-time Signals in MATLAB 2.4

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


>> stem(n,x)

Periodic Sequence

A sequence x(n) is periodic if x(n) = x(n+N); ∀ n. The smallest integer N that satisfies this relation is called
the fundamental period. We will use 𝑥̃(n) to denote a periodic sequence. To generate P periods of 𝑥̃ (n)
from one period {x(n), 0<n<N1}, we can copy x(n) P times:

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

But an elegant approach is to use MATLABs powerful indexing capabilities. First we generate a matrix
containing P rows of x(n) values. Then we can concatenate P 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
>> xtilde = xtilde'; % long row vector

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

Operation on sequence

Here we briefly describe basic sequence operations and their MATLAB equivalents.

1. Signal addition: This is a sample-by-sample addition given by


{𝑥1 [𝑛]} + {𝑥2 [𝑛]} = {𝑥1 [𝑛] + 𝑥2 [𝑛]}
It is implemented in MATLAB by the arithmetic operator +. However, the lengths of x1(n) and
x2(n) 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 x1(n) and x2(n) so that they have the same position vector n (and hence the same
length). This requires careful attention to MATLABs indexing operations. In particular, logical
operation of intersection &, relational operations like ;= and ==, and the find function are
required to make x1(n) and x2(n) of equal length. The following function, called the sigadd
function, demonstrates these operations.
Laboratory 2: Discrete-time Signals in MATLAB 2.5

For example, to generate x(n) = δ[n − 2]+ δ[n + 4]; -5 ≤ n ≤ 5, we will need the following
MATLAB script:

>> [x1,n1] = impseq(-5,5,4);


>> [x2,n2] = impseq(-5,5,-2);
>> [x,n] = sigadd(x1,n1,x2,n2);
>> stem(n,x)

2. Signal Multiplications: This is a sample-by-sample (or "dot") multiplication) given by


{𝑥1 [𝑛]} . {𝑥2 [𝑛]} = {𝑥1 [𝑛]𝑥2 [𝑛]}
It is implemented in MATLAB by the array operator ” * ”. Once again, the similar restrictions
apply for the “ .* operator as for the + operator”. Therefore we have developed the sigmult
function, which is similar to the sigadd function.

For example, to generate x(n) = δ[n − 2]* δ[n + 4]; -5 ≤ n ≤ 5, we will need the following
MATLAB script:

>> [x1,n1] = impseq(-5,5,4);


>> [x2,n2] = impseq(-5,5,-2);
>> [x,n] = sigmult(x1,n1,x2,n2);
Laboratory 2: Discrete-time Signals in MATLAB 2.6

>> stem(n,x)

3. Shifting In this operation, each sample of x(n) is shifted by an amount k to obtain a shifted
sequence y(n).
𝑦[𝑛] = {𝑥[𝑛𝑘]}
If we let m = nk, then n = m + k and the above operation is given by
𝑦[𝑚 + 𝑘] = {𝑥[𝑚]}
Hence this operation has no effect on the vector x, but the vector n is changed by adding k to
each element. This is shown in the function sigshift.

For example, to generate x(n) = δ[n − 2]; -3 ≤ n ≤ 7, we can also write the following MATLAB
script:

>> [x,n] = impseq(-5,5,0);


>> stem(n,x)
>> [y,n] = sigshift(x,n,2);
>> stem(n,y)

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

For example, to generate x(n) = δ[n + 2]; -5 ≤ n ≤ 5, we can also write the following MATLAB
script:

>> [x,n] = impseq(-5,5,2);


>> stem(n,x)
>> [y,n] = sigfold(x,n);
>> stem(n,y)
Laboratory 2: Discrete-time Signals in MATLAB 2.7

5. Sample summation. This operation differs from signal addition operation. It adds all sample values of x(n)
between n1 and n2.

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

6. Sample Product: This operation also differs from signal multiplication operation. It multiplies all sample
values of x(n) between n1 and n2.

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


7. Signal energy. The energy of a sequence x(n) is given by

where superscript * denotes the operation of complex conjugation. The energy of a finite-duration
sequence x(n) can be computed in MATLAB using

Main Lab
2A: Generate and plot each of the following sequences over the indicated interval.

a) x[n] = 3δ[n − 3] − δ[n − 6], 0 ≤ 𝑛 ≤ 10


b) x[n] = n{u[n] − u[n − 10]} + 10𝑒 −0.3(𝑛−10) {𝑢[𝑛 − 10] − 𝑢[𝑛 − 20]}, 0 ≤ 𝑛 ≤ 20
c) 𝑥̃[𝑛] = {… 5, 4, 3, 2, 1, 5, 4, 3, 2, 1, 5, 4, 3, 2, 1, … . } −10 ≤ 𝑛 ≤ 9

2B: Let x(n) = { −1, 2, 3, 4, 5, 6, 7, 6, 5, 4, 3, −2, 1} Determine and plot the following sequences

a) 𝑥1 [𝑛] = 2𝑥[𝑛 − 5] − 3𝑥[𝑛 + 4]


b) 𝑥2 [𝑛] = 4𝑥[5 − 𝑛] − 3𝑥[𝑛 + 𝑙𝑎𝑠𝑡 𝑑𝑖𝑔𝑖𝑡 𝑜𝑓 𝑦𝑜𝑢𝑟 𝑠𝑡𝑢𝑑𝑒𝑛𝑡 𝐼𝐷]

2C: Generate the complex-valued signal

𝑥[𝑛] = 𝑒 (0.1+0.3𝑗)𝑛 −10 ≤ 𝑛 ≤ 10

and plot its magnitude, phase, the real part, and the imaginary part in four separate subplots.
Laboratory 2: Discrete-time Signals in MATLAB 2.8

Rubrics
Rubrics No participation Average Excellent
(0 points) (1 point) (2 points)
R1: Attendance T> 15min
07<T<15 min T< 7min
and Ethics Or Absent
Absent or Axis label or title printscreen or
R3: Results no manual submitted of graph not snippet tool used
defined (Code not visible)

No Unsatisfactory Poor Fair Excellent


participation (1 point) (2 points) (3 points) (4 points)
(0 points)
Absent or Most of the
Most of the lab
R2: Code Matlab Code Major flaws in Fair amount of code code
task not
Function not code function implemented implemented
implemented
implemented with minor flaws
Good Excellent
(5 points) (6 points)
All task All task
implemented
R2: Code implemented
but after
Function rectification
with proper
from instructor understanding

Rule violation Any rule violation would be given -1 mark each time
To be observed each time and would be deducted from the total marks earned.

R1 and R2 would be evaluated individually for each student

R3 would be evaluated in terms of group

You might also like