Lab 2
Lab 2
Lab 2
Name
Registration Number
Instructor’s Name
Lab # 02 Introduction to Discrete Time Signal Processing on MATLAB
Objective
This lab allows students to understand the basic concept of the discrete-time signals and their
representation on MATLAB. Furthermore, students will be able to perform signal shifting and
folding operations in MATLAB followed by arithmetic operations like adding, subtracting or
multiplying signals of different lengths.
Pre-Lab:
MATLAB works with three types of windows on your computer screen. These are the
‘Command window’, the Figure window and the Editor window. The Figure window only
pops up whenever you plot something. The Editor window is used for writing and editing
MATLAB programs (called M-files) and can be invoked in Windows from the pull-down menu
after selecting File |New| M-file. In UNIX, the Editor window pops up when you type in
the command window: edit filename (‘filename’ is the name of the file you want to create).
The ‘ Command window’ is the main window in which you communicate with the
MATLAB interpreter. The MATLAB interpreter displays a command >> indicating that
i t is ready to accept commands from you.
>> z=3 + 4i % note that you do not need the ‘*’ after 4
You can also define the imaginary number with any other variables you like. Try the following:
>> img = sqrt (-1)
>> z = 3+4*img
>> exp(pi*img)
Array Indexing
In MATLAB, all arrays (vectors) are indexed starting with 1, i.e., y(1) is the first element of
the array y.
Note that the arrays are indexed using parenthesis (.) and not square brackets [.] as in
C/C++. To create an array having as elements the integers -5 through 5, just enter:
>> x= [-5,-4,-3,-2,-1,0,1,2,3,4,5]
The : notation above creates a vector in steps of 1. We can visualize the array ‘x’ and
its indexes by the following table:
array ‘x’ -5 -4 -3 -2 -1 0 1 2 3 4 5
indexes of ‘x’ 1 2 3 4 5 6 7 8 9 10 11
ans=1 3 5
>> jj=20 : -2 : 0
Note that MATLAB’s data files have the extension .mat. To retrieve the data that was saved
in the vector x, type:
The vector x is loaded in memory. To see the contents of memory use the command whos:
>> whos
The command whos gives a list of all the variables currently in memory, along with their
dimension. In our case, x contained 8193 samples.
To clear up memory after loading a file, you may type clear all when done.That is very
important, because if you do not clear all the variables in memory, you may run into problems
with other programs that you will write that use the same variables.
In-Lab:
A signal is a function that conveys some useful information about the behavior or attributes of
some phenomenon. Signals are mathematically represented as a function of one or more
independent variables i.e. y =ƒ( x1, x2 ,..,xN ) where x1, x2 , .., xN are the independent variables that
could be time or space and ‘y’ is the dependent variable that in this case represent the signal values.
In this lab, we will mostly be dealing with signals which are the function of time only and in case
of discrete signals, we will replace time with the variable ‘n’ which will only take discrete values.
1. Signals in MATLAB
Signals are divided into two major types.
MATLAB representation
Output:
In MATLAB, signals are represented by using two
variables i.e. y=sin (t) where variable ‘t’ represents the
time axis and ‘y’ represents the signal values.
>> t=0:0.01:7;
>> y=sin(t);
>> plot(y)
MATLAB representation:
>> n=0:7;
>> y=sin(n);
>> stem(n,y)
>> n=-5:5;
>> x=[n==0];
>> n=-5:5;
>> x=[(n-2)==0];
>> n=-5:5;
The 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 script:
Example tutorial:
>> n = [0:10];
>> x = (0.9).^n;
For example, to generate x(n) = exp[(2+j3)n], 0<= n<=10, we will need the following
script:
>> n = [0:10];
>> x = exp((2+3j)*n);
Note that you need different plot commands for plotting real and imaginary components.
Generate a matrix containing P rows of x(n) values. Concatenate P rows into a long row
vector using the construct (:).
Have to use matrix transposition operator (‘) to provide the same effect on rows:
>> xtilde = x' * ones(1,P); %P columns of x; x is a row vector
y (n) {x(n k )}
In Matlab, this operation is not so simple, both “x” and “n” need to be processed separately in
order to get the shifted signal x[n-k]. Perform following steps to in Matlab to perform the signal
shift.
i) Check whether k is positive or negative?
ii) If k is positive it means shift is towards right i.e. x[n-k] then do following
a. As signal is shifting towards right time axes “n” should be extended towards right
side by “k” samples i.e. if n is ending at n2 then it should now end at n2 + k.
b. Since the signal has now moved towards right meaning initial “k” samples in “x”
are empty so concatenate “k” zeroes in the beggining of the sequence x.
iii) If k is negative it means shift is towards left i.e. x[n+k] then do following
a. As signal is shifting towards left time axes “n” should be extended towards left side
by “k” samples i.e. if n is starting at n1 then it should now start at n1 - k.
b. Since the signal has now moved towards left meaning last “k” samples in “x” are
empty so concatenate “k” zeroes in the end of the sequence x.
3.3. Folding
Folding operation is also termed as flipping a signal where each sample is of x(n) is mirrored
around n=0 to obtain a folded sequence y(n) = x[-n].
Note that in Matlab a built-in function “fliplr(m) can be used to flip any sequence.
We know that signals can have different starting and ending times, here we want to modify
time indexes such that both signals start from the lowest time index and end at highest time index
and accordingly concatenate zeros or at the start or end of the signal. Suppose we want to
add/multiply two signals x1[n] specified by x1 and n1 in Matlab and and x2[n] specified by x2
and n2 in Matlab. Following two steps can be followed to make the lengths of x1 & x2 and making
n1 and n2 same as well.
a. Compare the starting points of both signals time index vectors i.e. n1(1) and n2(1).
If n1(1) is smaller than n2(1)
>> y=running_averagel(x)
where x is the input signal, and y is the running total of that signal.
Task-2: Write a program to compute and analyze the variance and mean of a signal x. The
variance σ is defined to be:
𝑁
1
𝜎 = ∑(𝑥𝑖 − 𝑥̅ )
𝑁
1=1
where ‘ 𝑥
̅’ is the mean value of the signal x. For signal x, use all the integers from 1 to
1000.
Task-3: Take an exponential signal and perform scaling operation with a negative integer as given
in In-Lab Work section and plot the result.
Task-4: Write a Matlab function “sigshift” for producing a delay of ‘k’ in a given sequence ‘x[n]’
defined by arrays “x” and “n” by using the pseudo code given in In-Lab Work section. Your
function should yield y[n] = x[n-k].
Function [y,n1]=sigshift(x,n,k)
Task-2: Generate an impulse sequence δ [n] as described in In-Lab section, use it to generate a
step sequence
u [n] = ∑ δ[n − k]
𝑘
Task-3: Generate the following sequences using step and impulse sequence.
Task-4: Write a Matlab function “sigadd” for addition of two sequences x1[n] and x2[n] by using
the pseudo code given in In-Lab Work section.
Function [y,n]=sigadd(x1,n1,x2,n2)