Laboratory 1: Discrete-Time Signals in MATLAB
Laboratory 1: Discrete-Time Signals in MATLAB
Laboratory 1: Discrete-Time Signals in MATLAB
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.
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.
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:
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:
For example, to generate x(n) = (0.9)n; 0 ≤ n ≤ 10, we will need the following MATLAB script:
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:
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
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:
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.
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.
For example, to generate x(n) = δ[n − 2]+ δ[n + 4]; -5 ≤ n ≤ 5, we will need the following
MATLAB script:
For example, to generate x(n) = δ[n − 2]* δ[n + 4]; -5 ≤ n ≤ 5, we will need the following
MATLAB script:
>> 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:
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:
5. Sample summation. This operation differs from signal addition operation. It adds all sample values of x(n)
between n1 and n2.
6. Sample Product: This operation also differs from signal multiplication operation. It multiplies all sample
values of x(n) between n1 and n2.
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.
2B: Let x(n) = { −1, 2, 3, 4, 5, 6, 7, 6, 5, 4, 3, −2, 1} Determine and plot the following sequences
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)
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.