Expt 01 DSP Lab

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

Lab Sheet 1

Implementation of Discrete Time Signals using MATLAB

1. A fundamental knowledge on MATLAB


2. A theoretical knowledge on discrete time signals.

1. describe discrete-time signals mathematically and generate, manipulate, and plot discrete-
time signals using MATLAB.
2. understand different operations on DT signals

1. Lab Session 1.1: Generation of DT signals using MATLAB


2. Lab Session 1.2: Operation on DT signals
3. Lab Session 1.3: In Lab Evaluation
4. Home work

Lab Session 1.1:


LS 1.1.1 Generation of DT signals using MATLAB
Example 1.1
Generate the sequence
x(n) = 2cos(2 0.05n), −10  n  10
Solution:
MATLAB codes:
n=[-10:10];
x=2*cos(2*pi*0.05*n);
stem(n,x,'k')

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)

**Note the element x(n=0)


Output:
8

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

3. Real valued exponential x(n) = an ,n; aR function [x n]= realx(a,n1,n2)


sequence n=[n1:n2];
x=a.^n;
Try the following codes
[x,n]=delta(3,0,10)
x=
0 0 0 1 0 0 0 0 0 0 0
n=
0 1 2 3 4 5 6 7 8 9 10

stem(n,x,'k'); axis([-1 12 -1 2])

1.5

0.5

-0.5

-1
0 2 4 6 8 10 12

Similarly try the following codes


a=2; n1=1; n2=10;
[x n]= realx(a,n1,n2)
stem(n,x,'k');

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.04n) +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

(b) x(n) = (0.8)n , 0  n  9 ,one periodic, plot 10 periods.

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

3. Signal Shifting y(n)={x(n-k)} function [y n]=sigshift(x,m,n0)


n=m+n0;
y=x;
end

4. Signal folding y(n)={x(-n)} function [y n] = sigfold(x,n)


y=fliplr(x); n=-fliplr(n);
% fliplr is library function.
end
>> help fliplr in command window to
know in details .

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

Try the following codes


[x1,n1] = delta(3,0,10); subplot(3,1,1);stem(n1,x1);
[x2,n2] =delta(7,0,10); subplot(3,1,2);stem(n2,x2);
[x3,n3] =sigadd(x1*2,n1,-x2*3,n2);subplot(3,1,3);stem(n3,x3);

6|Page
Example 1.5:
Let x(n) = {1,2,3,4,5,6,7,6,5,4,3,2,1}

Generate and plot the following sequence using MATLAB:


(a) x1(n)=2x(n-5)-3x(n+4) (b) x2(n)=x(3-n)+x(n)x(n-2)
Solution :
MATLAB Code:
n = -2:10; x = [1:7,6:-1:1];
% a) x1(n) = 2*x(n-5) - 3*x(n+4)
[x11,n11] = sigshift(x,n,5); [x12,n12] = sigshift(x,n,-4);
[x1,n1] = sigadd(2*x11,n11,-3*x12,n12);
subplot(2,1,1);
stem(n1,x1); title('Sequence in a')
xlabel('n'); ylabel('x1(n)');
axis([min(n1)-1,max(n1)+1,min(x1)-1,max(x1)+1])
% b) x2(n) = x(3-n) + x(n)*x(n-2)
[x21,n21] = sigfold(x,n); [x21,n21] = sigshift(x21,n21,3);
[x22,n22] = sigshift(x,n,2); [x22,n22] = sigmult(x,n,x22,n22);
[x2,n2] = sigadd(x21,n21,x22,n22);
subplot(2,1,2);
stem(n2,x2);
title('Sequence in Example b')
xlabel('n');
ylabel('x2(n)');
axis([min(n2)-1,max(n2)+1,0,40])

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 :

real part imaginary part


2 1

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

Downsampling and Upsampling.


Downsampling:
Used library function: downsample(x,k)

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:

y(n) = x(n + 4) x(n −1) + 4 x(n + 4) + 3x(n)


ILE 1.3 : Let x(n) = {1, 2, 3, 4} is one period of a periodic sequence. Plot 4 periods of the 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.

2. (a) A unit ramp DT signal r(n) is defined as,


n n0
r ( n) = 
0 n0
Develop a function to implement the above signal.
(b) Use the above sequence to plot a ramp sequence for the interval -10 to 10.
3. Generate x(n)= cos(0.25n) , − 50  n  50 . Downsample x(n) by a factor of 5 to generate
y(n) . Plot both x(n) and y(n) using subplot.
4. Let, x(n) = {1,−2,6,−5,4,6,8,10} Generate and plot the samples of the following
sequence:
5
y(n) =  nx(n − k)
k =1
Hint: Use a for loop and “sigadd” & “sigshift” functions.
5. A simple digital differentiator is given by
y(n) = x(n) − x(n − 1)
which computes a backward first-order difference of the input sequence. Implement this
differentiator on the following sequences, and plot the results. Comment on the
appropriateness of this simple differentiator.
(i) x(n) = 5 u (n) − u(n − 20) 
n 
(ii) x(n) = sin   u (n) − u(n − 100) 
 25 

12 | P a g e

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy