Expt 01 DSP Lab
Expt 01 DSP Lab
Expt 01 DSP Lab
1. describe discrete-time signals mathematically and generate, manipulate, and plot discrete-
time signals using MATLAB.
2. understand different operations on DT signals
Output:
1|Page
Example 1.2
Generate a sequence x(n) = [2 1,-1,3,1,4,3,7] represented in MATLAB.
Solution:
MATLAB codes:
n=[-3 -2 -1 0 1 2 3 4];
x=[2 1 -1 3 1 4 3 7];
stem(n,x)
-2
-3 -2 -1 0 1 2 3 4
2|Page
Used custom function in this section:
Table 1.1
Name Mathematical definition MATLAB function
1. Unit sample function 1, n = 0 function [x n]= delta(n0,n1,n2)
(n) = n=[n1:n2];
0, n 0 x=[n-n0]==0;
2. Unit step function 1, n 0 function [x n]= u(n0,n1,n2)
u (n ) = n=[n1:n2];
0, n 0 x=[n-n0]>=0;
1.5
0.5
-0.5
-1
0 2 4 6 8 10 12
3|Page
Example 1.3:
Generate and plot the following sequences over the indicated intervals.
(a) x (n) = 2(n+2) - (n-4) ; -5 ≤ n≤ 5.
(b) x(n) = cos(0.04n) +0.2w(n) , 0 ≤ n ≤ 50 , where w(n) is the Gaussian random
sequence with zero mean and unit variance.
Solution :
MATLAB Code:
%for part a
n=[-5:5];
x=2*delta(-2,-5,5)-delta(4,-5,5);
subplot(2,1,1);stem(n,x);
title('sequence in part a.');xlabel('n');ylabel('x(n)');
%for part b
n=[0:50];
x=cos(0.04*pi*n)+0.2*randn(size(n));
subplot(2,1,2);stem(n,x);
title('sequence in part b.');xlabel('n');ylabel('x(n)');
Output:
sequence in part a.
2
1
x(n)
-1
-5 -4 -3 -2 -1 0 1 2 3 4 5
n
sequence in part b.
2
1
x(n)
-1
-2
0 5 10 15 20 25 30 35 40 45 50
n
4|Page
Example 1.4:
Generate and plot the samples (use the stem function) of the following sequence using MATLAB:
10
(a) x1 (n ) = (m + 1)(n − 2m ) − (n − 2m − 1) ; 0 n 25
m =0
Solution :
MATLAB Code:
% (a)
n = [0:25];
x1 = zeros(1,26);
for m=0:10,
x1 = x1 + (m+1).*(delta(2.*m,0,25)-delta(2.*m+1,0,25));
end
subplot(2,1,1);
stem(n,x1,'k');
title('Sequence in Problem a')
% (b)
n=[0:9]';
x=(0.8).^n
N=10;
y = repmat(x,N,1); %repmat is a library function
m=0:1:size(y,1)-1;
subplot(2,1,2);
stem(m,y,'k')
title('Sequence in Problem b')
Output:
5|Page
LS 1.2 Operation on DT Sequences
Used custom function in this section:
Table 1.2
Name Mathematical definition MATLAB function
1. Signal addition {x1(n)}+{x2(n)}= function [y n]=sigadd(x1,n1,x2,n2)
{x1(n)+x2(n)} n=min(min(n1),min(n2)):max(max(n1),max(n2));
y1=zeros(1,length(n));y2=y1;
y1(find((n>=min(n1))&(n<=max(n1))==1))=x1;
y2(find((n>=min(n2))&(n<=max(n2))==1))=x2;
y=y1+y2;
end
2. Signal {x1(n)}.{x2(n)}= {x1(n).x2(n)} function [y n]=sigmult(x1,n1,x2,n2)
multiplication n=min(min(n1),min(n2)):max(max(n1),max(n2));
y1=zeros(1,length(n));y2=y1;
y1(find((n>=min(n1))&(n<=max(n1))==1))=x1;
y2(find((n>=min(n2))&(n<=max(n2))==1))=x2;
y=y1.*y2;
end
5 Decompositio 1
xe (n) = [ x(n) + x(−n)] function [xe, xo, m] = evenodd(x,n)
n to even and 2
odd part 1 m = -fliplr(n);
xo (n) = [ x(n) − x(−n)] m1 = min([m,n]); m2 = max([m,n]); m = m1:m2;
2
nm = n(1)-m(1); n1 = 1:length(n);
x1 = zeros(1,length(m)); x1(n1+nm) = x; x = x1;
xe = 0.5*(x + fliplr(x)); xo = 0.5*(x -
fliplr(x));
end
6|Page
Example 1.5:
Let x(n) = {1,2,3,4,5,6,7,6,5,4,3,2,1}
Output :
Sequence in a
10
0
x1(n)
-10
-20
-6 -4 -2 0 2 4 6 8 10 12 14 16
n
Sequence in b
40
30
x2(n)
20
7 | P a g e 10
0
-8 -6 -4 -2 0 2 4 6 8 10 12
n
Example 1.6: Real and Imaginary part of complex sequence
Generate the complex valued signal,
x (n ) = e ( −0.1+ j0.3) n , -10≤ n ≤ 10
and plot it’s magnitude ,phase, the real part and the imaginary part in four separate subplots .
Solution:
MATLAB Code:
n = [-10:1:10]; alpha = -0.1+0.3j;
x = exp(alpha*n);
subplot(2,2,1);stem(n,real(x));title('real part');xlabel('n')
subplot(2,2,2);stem(n,imag(x));title('imaginary part');xlabel('n')
subplot(2,2,3);stem(n,abs(x));title('magnitude part');xlabel('n')
subplot(2,2,4);stem(n,(180/pi)*angle(x));
title('phase part');xlabel('n')
Output :
0 0
-2 -1
-4 -2
-10 -5 0 5 10 -10 -5 0 5 10
n n
magnitude part phase part
3 200
100
2
0
1
-100
0 -200
-10 -5 0 5 10 -10 -5 0 5 10
n n
8|Page
Example 1.7: Decomposing a sequence in even and odd components:
−0.1 n
Use the function evenodd(x,n )to decompose x(n) = 1.2e into even and odd
components . Show original sequence, even part and odd part in subplots.
Solution:
MATLAB Code:
n = [0:10];
x=1.2*exp(-0.1*pi*n);
[xe,xo,m] = evenodd(x,n);
subplot(3,1,1); stem(n,x); title('Original Signal')
xlabel('n'); ylabel('x(n)'); axis([-10,10,0,1.2])
subplot(312); stem(m,xe); title('Even Part')
xlabel('n'); ylabel('xe(n)'); axis([-10,10,0,1.2])
subplot(313); stem(m,xo); title('Odd Part')
xlabel('n'); ylabel('xe(n)'); axis([-10,10,-0.6,0.6])
Output:
Rectangular pulse
1
x(n)
0.5
0
-10 -8 -6 -4 -2 0 2 4 6 8 10
n
Even Part
1
xe(n)
0.5
0
-10 -8 -6 -4 -2 0 2 4 6 8 10
n
Odd Part
0.5
xe(n)
-0.5
-10 -8 -6 -4 -2 0 2 4 6 8 10
n
9|Page
Example 1.8:
Use downsample(x,k) function to plot the original and downsampled sequence in subplots,
where,
x(n) = {1 2 3 4 5 6 7 8 9 10 11 12}
and the downsampling factor is 3.
Solution:
MATLAB Code:
x = [ 1 2 3 4 5 6 7 8 9 10 11 12];
k = 3;
z=downsample(x,k)
n=[0:length(x)-1];
subplot(2,1,1)
stem(n,x)
title('Original sequence.')
m=[0:length(z)-1];
subplot(2,1,2)
stem(m,z)
title('Downsampled sequence.')
Output:
Original sequence.
15
10
0
0 2 4 6 8 10 12
Downsampled sequence.
10
0
0 0.5 1 1.5 2 2.5 3
10 | P a g e
Upsampling:
Used library function:
upsample(x,k)
Example 1.9:
Use upsample(x,k) function to plot the original and downsampled sequence in subplots, where,
x(n) = {2 3 4 5 6} and the upsampling factor is 3.
Solution: MATLAB Code:
x = [ 2 3 4 5 6];
k = 3;
z=upsample(x,k);
n=[0:length(x)-1];
subplot(2,1,1)
stem(n,x)
title('Original sequence.')
m=[0:length(z)-1];
subplot(2,1,2)
stem(m,z)
title('Oversampled sequence.')
stem(m,z)
title('Original sequence.')
stem(m,z)
Output:
title('Original sequence.')
Original sequence.
6
0
0 0.5 1 1.5 2 2.5 3 3.5 4
Oversampled sequence.
6
0
0 2 4 6 8 10 12 14
11 | P a g e
Lab Session 1.3 : In Lab Evaluation
In this section, students will solve the following problems in lab hour
themselves.
ILE 1.1 : Using the evenodd function, decompose the following sequences into their even and
odd components. Plot these components using the stem function.
x(n) = e0.1n [u(n +5) − u(n −10)]
ILE 1.2 :Let x(n) = {1, −2, 4,6, −5,8,10} . Generate and plot the samples of the following sequence:
Home Work:
1. Generate and plot the samples(use the stem function ) of the following sequence using
MATLAB : x1 (n) = 3x(n + 2) + x(n − 4) − 2 x(n) where, x(n) = {1,−2,6,−5,4,6,8,10}
Hint: Use “sigadd” & “sigshift” functions.
12 | P a g e