You are on page 1of 10

1 DSP Lab Manuals 2016, Electrical Engineering Department, COMSATS Institute of IT, Islamabad

Digital Signal Processing

Lab-2 Manual

Basic Operations on Discrete-Time


Sequences
2 DSP Lab Manuals 2016, Electrical Engineering Department, COMSATS Institute of IT, Islamabad

Basic Operations on Discrete-Time


Sequences
Objective:
By the end of this lab students will be able to perform signal shifting and folding
operations in Matlab, in addition students will be able to perform arithmetic operations
like adding, subtracting or multiplying signals of different lengths.

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

or just write following in Command Window to open the M-file


>> script

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

b. How to make a function in Matlab

A function in Matlab is similar to a function in C/C++. Once a function is made it can be


called in any other function/script just like C/C++. Note that script is just a code but
function is more like a separate object or entity. Just like C/C++ there is a particular
syntax for making a function as given below.

i. function command is used to define a function as

function [output1 output2 output3] = func_name(input1 input2 input3)


.
.
Code
.
.
end

ii. Each function is defined in a separate M-file


iii. Each function has a distinct name and file is also saved with the same name as
function. If an M-file having the function with a name “average” is saved by the name
of “avg”, then this function cannot be called in another file.
4 DSP Lab Manuals 2016, Electrical Engineering Department, COMSATS Institute of IT, Islamabad

c. How to call a functions in Matlab

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,

[avg] = average (10,-5, 20); % function called directly

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

e. Generating a signal in Matlab

A signal x[n] in Matlab in matlab is defined using two arrays


or sequences, one array representing the magnitudes “x” and
the other array defining the time indexes i.e. the signal given
in the figure (on right side) will be defined as

>> 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”.

f. Some other type of signals

1. Real-valued exponential Sequence

 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;

 Create a function (M-file) to generate an exponential sequence. Use the


previous examples as your guide.

2. Complex-valued exponential Sequence

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 script:

>> 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:

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

An elegant approach is to use matlab’s indexing capabilites:


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


>> xtilde = xtilde(:); %long coloumn vector
>> xtilde = xtilde'; %long row vector

In-Lab Work

Important Signal Operations in Matlab:


1. Signal Scaling

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].

In Matlab, folding operation can be performed in three steps


i) Flip the magnitude sequence “x”
ii) Flip the time index “n”
iii) Now multiply the flipped time vector “n” with a minus

Note that in Matlab a built-in function “fliplr(m) can be used to flip any sequence.

4. Add/Subtract/Multiply two signals

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.

In signal processing signal elements are added/subtracted or multiplied corresponding to


their time index i.e. a signal element at time -1 will be added to the signal element of the
other signal at the same time.

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);

Else-If n2(1) is smaller than n1(1)


 Concatenate n1(1) – n2(1) number of zeros before x1
 And put n1(1) = n2(1);
b. Compare the ending points of both signals time index vectors i.e. n1(end) and
n2(end).
If n1(end) is greater than n2(end)
 Concatenate n1(end) – n2(end) number of zeros at the end of
x2
 And put n2(end) = n1(end);

Else-If n2(end) is greater than n1(end)


 Concatenate n2(end) – n1(end) number of zeros at the end of x1
 And put n1(1) = n2(1);
c. Now make a universal time index n = n1(1):n2(1)
d. Add the magnitude vectors by as x = x1+x2 or multiply them as x = x1.*x2 etc.

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)

You might also like