Digital Signal Processing Lab Manual
Digital Signal Processing Lab Manual
Class BCE-6A
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:
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.
Code:
Output:
>> 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
Periodic sequence
Create any signal x and then create its periodic sequence using the code provided above.
Code:
[x]= exp(n);
subplot(211);
stem(n,x);
xtilde = x' * ones(1,5);
xtilde = xtilde(:);
xtilde = xtilde';
figure,stem(xtilde);
Output:
In-Lab
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.
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.
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)
b. Compare the ending points of both signals time index vectors i.e. n1(end) and n2(end).
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 }
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.