Professional Documents
Culture Documents
Lab-2 Manual
Pre-Lab Work:
a. How to write script in M-file
i. Click on File drop down menu and go to New tab and select Script/M-file
ii. Once the mfile is open write your code and then save it by some name as
3 DSP Lab Manuals 2016, Electrical Engineering Department, COMSATS Institute of IT, Islamabad
iii. After writing the code click the play button on the m-file window to run
the code
i. In order to call a function in another file the directory of both file must be same.
ii. Before calling the function inputs needs to be defined in the script or command
window.
iii. Suppose we have a function by the name of “average” that takes the average of
three numbers n1, n2 and n3 and returns average avg, then following code must be
written in command window or script to call this function
n1 = 10; % defined n1
n2 = -5; % defined n2
n3 = 20; % defined n3
[avg] = average (n1, n2, n3); % function called
or simply,
iv. If the inputs are not defined the function call will produce an error and also
function cannot be run using the play button in m-file unlike scripts.
d. Some relevant MATLAB commands to generate different type of signals
(See help of following)
5 DSP Lab Manuals 2016, Electrical Engineering Department, COMSATS Institute of IT, Islamabad
>> n = 0:4;
>> x = [6, 11, 14, 5, 0]
It is of particular importance to mention that any operation performed on this signal i.e.
shifting, folding etc. will be performed on both the magnitudes “x” and time index “n”.
Example tutorial:
>> n = [0:10];
>> x = (0.9).^n;
>> n = [0:10];
>> x = exp((2+3j)*n);
Note that you need different plot commands for plotting real and imaginary
components.
3. Sinusoidal sequence
A matlab function “cos” (or sin) is used to generate sinusoidal sequences.
To generate x(n) = 3cos(0.1πn + π/3) + 2sin(0.5 πn), 0<= n<=10, we will need
the following script:
6 DSP Lab Manuals 2016, Electrical Engineering Department, COMSATS Institute of IT, Islamabad
>> n = [0:10];
>> x = 3*cos(0.1*pi*n+pi/3) + 2*sin(0.5*pi*n);
4. Random Sequences
A random or stochastic sequences are characterised by parameters of the
associated probability density functions or their statistical moments. In matlab, 2
types of (psuedo ) random sequences are avalable: “rand(1,N)” generates a length
N random sequence whos elements are uniformly distributed between 0 and 1.
“randn(1,N) generates a length N gaussian random sequence with mean 0 and
variance 1. Other random sequences can be generated suing transformation of the
above functions. (Check previous lecture slides)
5. Periodic sequence
A sequence is periodic if x(n) = x(n +N). To generate P periods of x(n) from one
period, we can copy x(n) P times:
In-Lab Work
Description:
In this Operation each sample is multiplied by a scalar. Use the command “*” for
scaling.
2. Signal Shifting
Description:
During a shift operation a signal changes its position in time where each sample of x(n) is
shifted by an amount k to obtain a shifted sequence y[n].
y(n) {x(n k )}
7 DSP Lab Manuals 2016, Electrical Engineering Department, COMSATS Institute of IT, Islamabad
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
c. 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.
d. 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. Folding
Description:
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.
Adding a signal in Matlab is not as easy as on paper. In order to add two sequences x1
and x2 in Matlab, both sequences have to be of same length, same is the case for
subtraction, multiplication and division.
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
8 DSP Lab Manuals 2016, Electrical Engineering Department, COMSATS Institute of IT, Islamabad
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)
Concatenate n2(1) – n1(1) number of zeros before x2
And put n2(1) = n1(1);
Lab Tasks
In-Lab Task-1: Take an exponential signal and perform scaling operation with a negative
integer as given in In-Lab Work section and plot the result
In-Lab Task-2: 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,n]=sigshift(x,n,k)
9 DSP Lab Manuals 2016, Electrical Engineering Department, COMSATS Institute of IT, Islamabad
In-Lab Task-3: Write a Matlab function “sigfold” for folding a given sequence ‘x[n]’
defined by arrays “x” and “n” by using the pseudo code given in In-Lab Work section.
Function [y,n]=sigfold(x,n)
Post-Lab Task-1: Write a Matlab function “sigadd” for adding 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)
10 DSP Lab Manuals 2016, Electrical Engineering Department, COMSATS Institute of IT, Islamabad
Post-Lab Task-2:
Let x(n) {1, 2,3, 4,5, 6, 7, 6,5, 4,3, 2,1}
Determine and plot the following sequences
a. x1 (n) 2 x(n 5) 3 x(n 4)
b. x2 (n) x(3 n) x(n) x(n 2)