0% found this document useful (0 votes)
118 views

Digital Signal Processing Lab Manual

Here are the steps to perform the tasks in the lab: 1. Signal Scaling To scale a signal x by a scalar a, use x = a*x 2. Signal Shifting To shift a signal x by k samples: a) Check if k is positive or negative b) If positive, concatenate k zeros to x and extend n by k samples c) If negative, concatenate k zeros to end of x and reduce n by k samples 3. Folding To fold a signal x: a) Flip x using fliplr(x) b) Flip the time index n using fliplr(n) c) Multiply the flipped n by -1

Uploaded by

ali khan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
118 views

Digital Signal Processing Lab Manual

Here are the steps to perform the tasks in the lab: 1. Signal Scaling To scale a signal x by a scalar a, use x = a*x 2. Signal Shifting To shift a signal x by k samples: a) Check if k is positive or negative b) If positive, concatenate k zeros to x and extend n by k samples c) If negative, concatenate k zeros to end of x and reduce n by k samples 3. Folding To fold a signal x: a) Flip x using fliplr(x) b) Flip the time index n using fliplr(n) c) Multiply the flipped n by -1

Uploaded by

ali khan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

EEL-325 DIGITAL SIGNAL PROCESSING

Digital Signal Processing


EEL-325

Name Muhammad Ali Khan

Registration Number 01-132172-007

Class BCE-6A

Instructor name Ma’am Tooba Khan


EEL-325 DIGITAL SIGNAL PROCESSING

Lab # 02
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:

How to make a function in MATLAB


A function in MATLAB is like 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 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) . Body of the function: Write code to
perform a certain a task end
i A function may be written in a separate M-file. Also, it is also possible to write more then
one functions in a single M-file.
ii Each function must have a distinct name and the file must also be saved with the same
name. If an M-file having the function with a name “average” is saved by the name of
“avg”, then this function will not work properly when called in another file.

Calling a function in MATLAB?


1. In order to call a function in another file the directory of both files must be same.
2. Before calling the function, inputs need to be defined in the script file or command
window.
3. 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 may be used to in
command window or script to call this function
EEL-325 DIGITAL SIGNAL PROCESSING

or simply,
[avg] = average (10,-5, 20); % function called directly
4. If the inputs are not defined the function call will produce an error message. Functions
cannot be run using the play button in m-file unlike scripts.

Some relevant MATLAB commands to generate different type of signals


(See help of following)

Elementary Matrices and Matrix Manipulation


I Ones Pi Rand Randm zeros
Elementary functions
Cos Exp Imag real

Generating a signal in MATLAB

A signal x[n] 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 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”.
Some other type of signals
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:
>> n = [0:10];
>> x = (0.9).^n;
Create a function (M-file) to generate an exponential sequence. Use the previous examples as
your guide.
EEL-325 DIGITAL SIGNAL PROCESSING

Code:

Output:

Complex-valued exponential Sequence


A built-in Matlab function “exp” is used to generate exponential sequences.
For example, to generate x (n)=exp [(2+ j 3)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.

Sinusoidal sequence
A built-in Matlab function “cos” (or sin) is used to generate sinusoidal sequences.
To generate x (n)=3 cos (0.1 πn+ π / 3)+2 sin(0.5 πn), 0≤n≤10, we will need the following script:
>> n = [0:10];
>> x = 3*cos(0.1*pi*n+pi/3) + 2*sin(0.5*pi*n);

Random Sequences
EEL-325 DIGITAL SIGNAL PROCESSING

A random or stochastic sequence are characterized by parameters of the associated probability


density functions or their statistical moments. In MATLAB, 2 types of (pseudo) 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 using transformation of the above
functions.

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 capabilities:
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 column vector
>> xtilde = xtilde'; %long row vector

Create any signal x and then create its periodic sequence using the code provided above.

Code:

function [x] = exp(n)


x = 3*cos(0.1*pi*n+pi/3) + 2*sin(0.5*pi*n);
end
n = [0:10];
EEL-325 DIGITAL SIGNAL PROCESSING

[x]= exp(n);
subplot(211);
stem(n,x);
xtilde = x' * ones(1,5);
xtilde = xtilde(:);
xtilde = xtilde';
figure,stem(xtilde);

Output:

In-Lab

Important Signal Operations in MATLAB:

1. Signal Scaling

In this Operation each sample is multiplied by a scalar. Use the command “*” for scaling.
2. Signal Shifting

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 )
EEL-325 DIGITAL SIGNAL PROCESSING

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. 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 ]
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 must 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 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
x2[n] specified by x2 and n2.
EEL-325 DIGITAL SIGNAL PROCESSING

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)

i If n1(1) is smaller than n2(1)

 Concatenate n2(1)-n1(1) number of zeros before x2

 And put n2(1)=n1(1)

ii 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).

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

ii 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(end)=n2(end)

c. Now make a universal time index n=n1(1): n1(end) or n2(1): n2(end)

d. Add the magnitude vectors by x=x1+x2 or multiply them as x1.*x2 etc.

Lab Tasks

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
Code:
n = [0:10];
a=-3;
x= a*cos(1*pi*n+pi/3)
x1= cos(1*pi*n+pi/3)
subplot(221);
stem(n,x1);
subplot(222);
EEL-325 DIGITAL SIGNAL PROCESSING

stem(n,x);
Output:

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)
Code:
function [x] = exp(n,k)
x =[zeros(1,k),n];
end
close all;
n=[1,2,3,4];
subplot(221);
stem(n);
k=4;
x=sig(n,k);
subplot(222);
stem(x);

Output:
EEL-325 DIGITAL SIGNAL PROCESSING

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)
Code:
function [x] = sig(n)
x =exp(0.25.*-n);
end n=-4:4 ;
x1 =exp(0.25.*n);
subplot(2,2,1)
stem(n,x1)
[x]=sig(n);
subplot(2,2,2)
stem(n,x) ;
Output:

Task-4: 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)

Code:

Output:
EEL-325 DIGITAL SIGNAL PROCESSING

Task-5: Using the functions defined in the above tasks perform the following and determine the
outputs

For x ( n )={1,2,11,5,8,9,7,10,2,4,6,7,1 }

a. x 1 ( n )=5 x ( n−5 )−8 x (n+ 4)


b. x 2 ( n )=8 x (−(n−5) )+ x ( n )∗x (n−2)

Code:
a)

b)

c.

Output:
EEL-325 DIGITAL SIGNAL PROCESSING

a)

b)

Conclusion:
In this lab we performed signal shifting and folding operations in MATLAB, also we
performed arithmetic operations like adding, subtracting or multiplying signals of different
lengths. In the end of this lab we can make a function in MATLAB. I performed all the task
successfully.

You might also like