Sunteți pe pagina 1din 61

12/1/2011

2011

Digital Signal Processing Lab

Faiz Karobari
S.L.N. Engineering College, Raichur.
2011-12

H.K.E. SOCIETYS

SAROJINI LEELADHARAN NAIR COLLEGE OF ENGINEERING


Yermaras Camp, RAICHUR 584 101

LABORATORY CERTIFICATE
DEPARTMENT OF
ELECTRONICS AND COMMUNICATION ENGINEERING

This is to Certify that Mr./Mrs./Miss ___________________


__________________________
of
B.E.,
Vth
semester
having
USN______________ has satisfactorily conducted course of experiments in
DSP LAB practical prescribed by Visvesvaraya Technical University during the
year 2011-12.
Staff incharge

Head of the Department.

Date : __________
Roll No. :________
USN:___________
Examiners: 1._____________

2._________________

INDEX
A. LIST OF EXPERIMENT USING MATLAB

Page No.

1. Linear Convolotion of Two Sequences.

2. Impulse Response (Deconvolution) of two given Sequences.

3. Impulse Response of a given Sequences.

11

4. Difference Equation.

15

5. Autocorrelation of a given sequence.

18

6. Cross Correlation of a given sequence.

22

7. Circular Convolution of Two given Sequences.

25

8. N pt. DFT of a Given Sequence.

29

9. N pt. IDFT of a Given Sequence.

33

10. Circular Convolution using DFT and IDFT.

37

11. Linear Convolution using DFT and IDFT.

42

12. Linear Convolution using Circular Convolution.

45

13. Sampling Theorem using Spline Function.

47

14. Sampling Theorem without using Spline Function

51

15. FIR Filter Design using MATLAB

53

16. Digital Filter Design using MATLAB

56

Page | 3
DSP LAB 2011/12

B. LIST OF EXPERIMENT USING MATLAB PROCESSOR

Page No.

1. Linear Convolution Using DSP Processor

59

2. Circular Convolution Using DSP Processor

60

3. N Pt. DFT Using DSP Processor

61

4. Impluse Response Using DSP Processor

62

Page | 4
DSP LAB 2011/12

1. LINEAR CONVOLUTION OF TWO SEQUENCES


Linear Convolution relates the input, output and unit sample response of the system.
It is defined as_
y(n)

= x(n) * h(n)

Ex:
For x(n)={1,2,3,4} & h(n)={4,3,2,1}. Find the Linear Convolution between x(n) & h(n).
Soln:
x(n)={1,2,3,4}
h(n)={4,3,2,1}

input x(n)

output h(n)
4

amplitude of h(n)

amplitude of x(n)

4
3
2
1
0

1
2
samples of x(n)
input x1(n)

1
2
samples of h(n)
input y1(n)

1
2
samples of y1(n)

amplitude of y1(n)

amplitude of x1(n)

1
0.8
0.6
0.4
0.2
0
-1

-0.5
0
0.5
samples of x1(n)

3
2
1
0

Page | 5
DSP LAB 2011/12

input x(2)

output y2(n)
8

amplitude of y2(n)

amplitude of x(2)

2
1.5
1
0.5
0

0.5
1
1.5
samples of x(2)
input x(3)

2
0

1
2
3
samples of y2(n)
output y3(n)

2
4
samples of y3(n)

15

amplitude of y3(n)

amplitude of x(3)

1
2
samples of x(3)

10

y(n) = y1(n)+y2(n)+y3(n)+y4(n)
= {4,11,20,30,20,11,4}
input y(n)
30

amplitude of y(n)

25

20

15

10

3
samples of y(n)

Page | 6
DSP LAB 2011/12

MATLAB CODE:
clc;
xn =input('Enter the sequence x(n)');
hn =input('Enter the sequence h(n)');
yn =conv(xn,hn);
disp('yn');
disp(yn);
subplot(3,1,1);
n=0:1:(length(xn)-1);
stem(n,xn);
ylabel('amplitude of x(n)');
xlabel('samples of x(n)');
title('input x(n)');
subplot(3,1,2);
n1=0:1:(length(hn)-1);
stem(n1,hn);
ylabel('amplitude of h(n)');
xlabel('samples of h(n)');
title('input h(n)');
subplot(3,1,3);
n2=0:1:(length(yn)-1);
stem(n2,yn);
ylabel('amplitude of y(n)');
xlabel('samples of y(n)');
title('output y(n)');

Output :
Enter the sequence x(n)[1,2,3,4]
Enter the sequence h(n)[4,3,2,1]
yn
4 11 20 30 20 11

Page | 7
DSP LAB 2011/12

amplitude of y(n)

amplitude of h(n)

amplitude of x(n)

Plot :

input x(n)
4
2
0

0.5

1.5
samples of x(n)
input h(n)

2.5

0.5

1.5
samples of h(n)
output y(n)

2.5

3
samples of y(n)

4
2
0

40
20
0

Page | 8
DSP LAB 2011/12

2. MATLAB CODE FOR DECONVOLUTION (IMPULSE RESPONSE) OF TWO GIVEN SEQUENCE


clc;
xn =input('Enter the sequence x(n)');
yn =input('Enter the sequence y(n)');
hn =deconv(yn,xn);
disp('hn');
disp(hn);
subplot(3,1,1);
n=0:1:(length(xn)-1);
stem(n,xn);
ylabel('amplitude of x(n)');
xlabel('samples of x(n)');
title('input x(n)');
subplot(3,1,2);
n1=0:1:(length(yn)-1);
stem(n1,yn);
ylabel('amplitude of y(n)');
xlabel('samples of y(n)');
title('input y(n)');
subplot(3,1,3);
n2=0:1:(length(hn)-1);
stem(n2,hn);
ylabel('amplitude of h(n)');
xlabel('samples of h(n)');
title('output impulse response h(n)');

Output :

Enter the sequence x(n)[1,2,3,4]


Enter the sequence y(n)[4,11,20,30,20,11,4]
hn
4

Page | 9
DSP LAB 2011/12

amplitude of h(n)

amplitude of y(n)

amplitude of x(n)

Plot :

input x(n)
4
2
0

0.5

0.5

1.5
samples of x(n)
input y(n)

2.5

2.5

40
20
0

3
4
samples of y(n)
output impulse response h(n)

4
2
0

1.5
samples of h(n)

Page | 10
DSP LAB 2011/12

3. IMPULSE RESPONSE OF A GIVEN SEQUENCE

A complete characterization of an LTI system can be represented interms of its response to an unit
impulse, which is known as impulse response of the system.
OR
The impulse response is the output of a LTI system due to an impulse input applied at t=0 or n=0.
Ex1:
Find the impulse response for a given sequence x(n)={1,2,3,4} and y(n)={4,11,20,30,20,11,4}.
Soln:
x(n)=
applying ZT on both sides,
= 1 + 2 1 + 3 2 + 4 3
y(n)=
applying ZT on both sides,
= 4 + 11 1 + 20 2 + 30 3 + 20 4 + 11 5 + 4 6
w.k.t.,
Y Z
Z

HZ =X

4+11 1 +20 2 +30 3 +20 4 +11 5 +4 6


1+2 1 +3 2 +4 3

4 + 3 1 + 2 2 + 3
1 + 2 1 + 3 2 + 4 3 4 + 11 1 + 20 2 + 30 3 + 20 4 + 11 5 + 4 6
4 + 08 1 + 12 2 + 16 3
3 1 + 8 2 + 14 3 + 20 4 + 11 5 + 4 6
3 1 + 6 2 + 9 3 + 12 4
2 2 + 5 3 + 8 4 + 11 5 + 4 6
2 2 + 4 3 + 6 4 + 8 5
3 + 2 4 + 3 5 + 4 6
3 + 2 4 + 3 5 + 4 6
= 4 + 3 1 + 2 2 + 3
On applying inverse Z T
= {4,3,2,1}.
Page | 11
DSP LAB 2011/12

Ex2:
Determine the impulse response when H[Z] is given.
1

if = 10.5 1 . Calculate h(n).


Soln:
1

Given = 10.5 1

= 0.5
By Power Series Method
1+

0.5

0.25
2

Z-0.5 Z
Z-0.5
0.5
0.5-

0.25

0.25

0.25
0.125
2

0.125
2

Therefore h(n)=,1,0.5,0.125,.-

Page | 12
DSP LAB 2011/12

MATLAB CODE:
clc;
a=input('Enter the coefficients of numerator');
b=input('Enter the coefficients of denominator');
n=input('Enter the length of the sequence');
hn=impz(a,b,n);
disp('hn=');
disp(hn);
n1=0:1:n-1;
stem(n1,hn);
ylabel('amplitude of h(n)');
xlabel('samples of h(n)');
title('output impulse response h(n)');

Output :

Enter the coefficients of numerator[1]


Enter the coefficients of denominator[1,-0.5]
Enter the length of the sequence7
hn=
1.0000
0.5000
0.2500
0.1250
0.0625
0.0313
0.0156

Page | 13
DSP LAB 2011/12

Plot :

output impulse response h(n)


1

0.9

0.8

amplitude of h(n)

0.7

0.6

0.5

0.4

0.3

0.2

0.1

3
samples of h(n)

Page | 14
DSP LAB 2011/12

4. IMPLEMENTATION OF DIFFERENCE EQUATION

Solve the difference equation


3

y(n) - 4 y(n-1) + 8 y(n-2) = x(n) - 3 x(n-1)


where x(n)={1,1,1,1,1,1,1,1,1,1} and N=10 at initial conditions y(-1)=0, y(-2)=0, x(-1)=0.

Soln:
3

y(n) - 4 y(n-1) + 8 y(n-2) = x(n) - 3 x(n-1)


at n=0,
3

y(0) = 4 y(-1) - 8 y(-2) + x(0) - 3 x(-1)


y(0)=1
at n=1,
3

y(1) = 4 y(0) - 8 y(-1) + x(1) - 3 x(0)


y(1) = 1.4166
at n=2,
3

y(2) = 4 y(1) = 8 y(0) + x(2) - 3 x(1)


y(2) = 1.6042
at n=3,
3

y(3) = 4 y(2) - 8 y(1) + x(3) - 3 x(2)


y(3) = 1.6927
at n=4,
3

y(4) = 4 y(3) - 8 y(2) + x(4) - 3 x(3)


y(4) = 1.7357
at n=5,
3

y(5) = 4 y(4) - 8 y(3) + x(5) - 3 x(4)


y(5) = 1.7568
at n=6,
3

y(6) = 4 y(5) - 8 y(4) + x(6) - 3 x(5)


y(6) = 1.7673

Page | 15
DSP LAB 2011/12

at n=7,
3

y(7) = 4 y(6) - 8 y(5) + x(7) - 3 x(6)


y(7) = 1.7726
at n=8,
3

y(8) = 4 y(7) - 8 y(6) + x(8) - 3 x(7)


y(8) = 1.7752
at n=9,
3

y(9) = 4 y(8) - 8 y(7) + x(9) - 3 x(8)


y(9) = 1.7765

Page | 16
DSP LAB 2011/12

MATLAB CODE:
clc;
a=input('Enter the coefficients of x');
b=input('Enter the coefficients of y');
xn=input('Enter the sequence of x(n)');
yn=filter(a,b,xn);
disp('yn=');
disp(yn);
n=0:1:(length(yn)-1);
stem(n1,yn);
ylabel('amplitude of y(n)');
xlabel('samples of y(n)');
title('output y(n)');

Output :
Enter the coefficients of x[1,-1/3]
Enter the coefficients of y[1,-3/4,1/8]
Enter the sequence of x(n)[1,1,1,1,1,1,1,1,1,1]
yn=
1.0000 1.4167 1.6042 1.6927 1.7357 1.7568 1.7673 1.7726 1.7752 1.7765

Plot :
output y(n)

amplitude of y(n)

1.5

0.5

4
5
samples of y(n)

Page | 17
DSP LAB 2011/12

5. AUTO CORRELATION OF A GIVEN SEQUENCE AND VERIFCATION OF ITS PROPERTIES

When the signal is correlated with itself, it is called autocorrelation.


It is given by

x l x(n l)
=

For n=0, 1, 2,

Ex: Evaluate auto correlation of the sequence x(n)={1,2,1,1}


Sol:
x(n)={1,2,1,1}
x(-n)={1,1,2,1}

1
11

1 2 1
2 1 1

21

11

11

={,,,,,,}

Page | 18
DSP LAB 2011/12

VERFICATION:
Ex:
Find the auto correlation of x(n)={1,2,3,4} and verify its properties
Soln:
Given, x(n)={1,2,3,4}
x(-n)={4,3,2,1}

4
4

3
3

2
2

1
1

12

16

12

= ,,,,,,

Energy of x(n) is
3

=0
2

=1 + 22 + 32 + 42
E =30
Hence the energy E is equal to rxx (0) hence it is an energy signal

left = , ,
right = , ,
Hence rxx is symmetric about the origin (30) hence it is an even signal.
The centre index is 4.

Page | 19
DSP LAB 2011/12

MATLAB CODE:
clc;
xn =input('Enter the input signal x(n)=');
yn =fliplr(xn);
zn =conv(xn,yn);
disp('zn');
disp(zn);
subplot(3,1,1);
n=0:1:(length(xn)-1);
stem(n,xn);
ylabel('amplitude of x(n)');
xlabel('samples of x(n)');
title('input x(n)');
subplot(3,1,2);
n1=-(length(yn)-1):1:0;
stem(n1,yn);
ylabel('amplitude of y(n)');
xlabel('samples of y(n)');
title('input y(n)');
subplot(3,1,3);
n2=-(length(zn)-1)/2:1:(length(zn)-1)/2;
stem(n2,zn);
ylabel('amplitude of z(n)');
xlabel('samples of z(n)');
title('output z(n)');

Output :
Enter the input signal x(n)=[1,2,1,1]
zn
1 3 5 7 5 3 1

Page | 20
DSP LAB 2011/12

amplitude of z(n)

amplitude of y(n)

amplitude of x(n)

Plot :

input x(n)
2
1
0

0.5

1.5
samples of x(n)
input y(n)

2.5

0
-3

-2.5

-2

-1.5
samples of y(n)
output z(n)

-1

-0.5

-2

-1

0
samples of z(n)

2
1

10
5
0
-3

Page | 21
DSP LAB 2011/12

6. CROSS CORRELATION OF A GIVEN SEQUENCE


When the correlation of two different signals is performed it called cross
correlation.
It is given by

x l y(n l)
=

for n=0, 1, 2,
Ex:
Evaluate cross correlation of the fallowing sequence
x(n)={1,2,1,1} y(n)={1,1,2,1}.
Sol:
x(n)={1,2,1,1}
y(-n)={1,2,1,1}

1
11

2 1 1
2 1 1

22

11

11

={,,,,,,}

Page | 22
DSP LAB 2011/12

MATLAB CODE
clc;
xn=input('Enter the first sequence x(n)=');
yn=input('Enter the second sequence y(n)=');
yn1=fliplr(yn);
zn=conv(xn,yn1);
disp('zn=');
disp(zn);
xlen=length(xn)-1;
n1=0:1:xlen;
subplot(4,1,1);
stem(n1,xn);
xlabel('samples n');
ylabel('amplitude xn');
title('input xn');
ylen=length(yn)-1;
n2=0:1:ylen;
subplot(4,1,2);
stem(n2,yn);
xlabel('samples n');
ylabel('amplitude yn');
title('input yn');
yn1len=-(length(yn1)-1);
n3=yn1len:1:0;
subplot(4,1,3);
stem(n3,yn1);
xlabel('samples n');
ylabel('amplitude yn');
title('input yn');
zlen1=(length(zn)-1)/2;
zlen2=-(length(zn)-1)/2;
n4=zlen2:1:zlen1;
subplot(4,1,4);
stem(n4,zn);
xlabel('samples n');
ylabel('amplitude zn');
title('output zn');

Output :

Enter the first sequence x(n)=[1,2,1,1]


Enter the second sequence y(n)=[1,1,2,1]
zn=
1

Plot :
Page | 23
DSP LAB 2011/12

amplitude xn
amplitude yn

amplitude zn

amplitude yn

input xn
2
1
0

0.5

1.5
samples n
input yn

2.5

0.5

1.5
samples n
input yn

2.5

0
-3

-2.5

-2

-1.5
samples n
output zn

-1

-0.5

-2

-1

0
samples n

1
0

10
5
0
-3

Page | 24
DSP LAB 2011/12

7. CIRCULAR CONVOLUTION OF TWO GIVEN SEQUENCE


The circular convolution of the two sequence 1 (n) and 2 (n) is denoted by
y(m)= x1 n

N x2 n

x1 l x2 ((m n))N
=

for m=0,1,2, ,(N-1)

Ex:
ind the circular convolution for x(n)={1,2,3,4} and h(n)={5,6,7,8}
Soln:
y(m) = x(n) N h(n)

y m =

x n y((m n))4
=

For m=0,
3

y 0 =

x n h((n))4
=0

y(0)=x(0)h(0)+x(1)h(3)+X(2)h(2)+x(3)h(3)
=1*5+2*8+3*7+4*6
=66
For m=1,
3

y 1 =

x n h((1 n))4
=0

y(1)=x(0)h(1)+x(1)h(0)+x(2)h(3)+x(3)h(2)
=1*6+2*5+3*8+4*7
=68

Page | 25
DSP LAB 2011/12

For m=2,
3

y 2 =

x n h((2 n))4
=0

y(2)=x(0)h(2)+x(1)h(1)+x(2)h(0)+x(3)h(3)
=1*7+2*6+3*5+4*8
=66

For m=3
3

y 3 =

x n h((3 n))4
=0

=x(0)h(3)+x(1)h(2)+x(2)h(1)+x(3)h(0)
=1*8+2*7+3*6+4*5
=60

Page | 26
DSP LAB 2011/12

MATLAB CODE:
x=input('Enter the sequence of x(n)');
h=input('Enter the sequence of h(n)');
N1=length(x);
N2=length(h);
N=max(N1,N2);
N3=N1-N2;
if(N3>0);
h=[h,zeros(1,N3)];
end
if(N3<0)
x=[x,zeros(1,-N3)];
end
for n=1:N
y(n)=0;
for i=1:N
j=n-i+1;
if(j<=0)
j=N+j;
end
y(n)=y(n)+x(i)*h(j);
end
end
disp('circular convolution output is');
y
N4=0:1:N-1;
subplot(2,1,2);
stem(N4,y);
ylabel('amplitude of y');
xlabel('samples of y');
title('output y');

Output :
Enter the sequence of x(n)[1,2,3,4]
Enter the sequence of h(n)[4,3,2,1]
circular convolution output is
y=
24 22 24 30

Page | 27
DSP LAB 2011/12

Plot :

output y
30

amplitude of y

25
20
15
10
5

amplitude zn

amplitude yn

1
2
samples of y

input yn
2
1
0
-3

-2.5

-2

-1.5
samples n
output zn

-1

-0.5

-2

-1

0
samples n

10
5
0
-3

Page | 28
DSP LAB 2011/12

8. N-POINT DFT OF A GIVEN SEQUENCE


The n point DFT of a sequence x(n) is given by
x(n)

X(k)

N DFT

N1

x n ej

2
kn
N

=0

Ex:
Find the DFT of the sequence x(n)={0,1,2,3}
Soln:
x(n)={0,1,2,3}
N1

x n ej

2
kn
N

=0

For k=0,
3

0 =

x n 1
=0

= 0+1+2+3= 6
X(0) = 6

For k=1,
3

x n ej

1 =

2
1n
4

=0

=x(0) + x(1) ej

+ x(2) ej

+ x(3) ej

3
2

=(-j)+(-2)+3j
X(1) = -2+2j

Page | 29
DSP LAB 2011/12

For k=2,
3

x n ejn

2 =
=0

X(2) = 0-1+2-3
X(2) = -2

For k=3,
3

x n ej(2 )3n

3 =
=0

=x(0) + x(1) e

3
2

+ x(2) ej

+ x(3) e

9
2

=0+j-2-3j
X(3) = -2-2j

Therefore,
X(k)={6,-2+2j,-2,-2-2j}

Page | 30
DSP LAB 2011/12

MATLAB CODE:
clc;
x=input('Enter the DFT of x(n)=....');
[N,M]=size(x);
if M~=1;
x=x';
N=M;
end
X=zeros(N,1);
n=0:N-1;
for k=0:N-1
X(k+1)=exp(-j*2*pi*k*n/N)*x;
end
disp(X);
subplot(2,1,1);
k=0:N-1;
stem(k,abs(X));
xlabel('k=......');
ylabel('magnitude of X(k)');
title('magnitude of X(k)');
subplot(2,1,2);
k=0:N-1;
stem(k,angle(X)');
xlabel('k....');
ylabel('phaseof X(k)');
title('phase of X(k)');

Output :
Enter the DFT of x(n)=....[0,1,2,3]
6.0000
-2.0000 + 2.0000i
-2.0000 - 0.0000i
-2.0000 - 2.0000i

Page | 31
DSP LAB 2011/12

Plot :

magnitude of X(k)
6

magnitude of X(k)

5
4
3
2
1
0

0.5

1.5
k=......

2.5

2.5

phase of X(k)
4

phaseof X(k)

-2

-4

0.5

1.5
k....

Page | 32
DSP LAB 2011/12

9.

N-POINT IDFT OF A GIVEN SEQUENCE

The N-point IDFT of a sequence X(k) is given by


x(n)

X(k)

N IDFT

1
=

N1

X k ej

2
kn
N

=0

Ex: find the IDFT of the sequence x(n)={1,2,3,4}


Soln:
X(k)={1,2,3,4}
x(n)

X(k)

N IDFT

1
=
4

X k e

2
kn
4

=0

For n=0,
x(0)=
=
x(0)=

1
4
1
4

[X(0)+X(1)+x(2)+X(3)]
[1+2+3+4]

For n=1,
x(1)=
=
x(1)=-

1
4
1
4

[ X(0) + X(1) ej

+ x(2) ej + X(3) ej

3
2

[1+2j-3+4(-1)]
-

Page | 33
DSP LAB 2011/12

for n=2,
x(2)=
=

[ X(0) + X(1) ej + x(2) ej2 + X(3) ej ]

4
1

[1 + 2 1 + 3 + 4(10)]

x(2)=

for n=3,
x(3)=
=

[ X(0) + X(1) ej

4
1

+ x(2) ej3 + X(3) ej

9
2

[1 +2(-j) + 3 + 4(-1) ]

x(3) =-

3
2

Therefore,
x(n)= {

Page | 34
DSP LAB 2011/12

MATLAB CODE:
clc;
Xk=input('Enter the DFT of X(k)=....');
[N,M]=size(Xk);
if M~=1;
Xk=Xk.';
N=M;
end
Xn=zeros(N,1);
k=0:N-1;
for n=0:N-1
Xn(n+1)=exp(j*2*pi*k*n/N)*Xk;
end
Xn=Xn/N;
disp('X(n)=....');
disp(Xn);
subplot(2,1,1);
n=0:N-1;
stem(n,abs(Xk));
xlabel('n=......');
ylabel('magnitude of Xn');
title('magnitude of Xn');
subplot(2,1,2);
n=0:N-1;
stem(n,angle(Xk)');
xlabel('n....');
ylabel('phaseof Xk');
title('phase of Xk');

Output :
Enter the DFT of X(k)=....[6,-2+2j,-2,-2-2j]
X(n)=....
0
1.0000 + 0.0000i
2.0000 - 0.0000i
3.0000 - 0.0000i

Page | 35
DSP LAB 2011/12

Plot :

magnitude of Xn
6

magnitude of Xn

5
4
3
2
1
0

0.5

1.5
n=......

2.5

2.5

phase of Xk
4

phaseof Xk

-2

-4

0.5

1.5
n....

Page | 36
DSP LAB 2011/12

10. CIRCULAR CONVOLUTION USING DFT & IDFT


It states that,
If 1

1 ()

N DFT

and 2

2 ()

N DFT

Then
1

N DFT

1 () 2 ()

It is stated that multiplication of two DFTS is equivalent to circular


convolution of their sequences in time domain.

Ex:
Find the circular convolution of x(n)={1,2,3,4} and h(n)={4,3,2} using DFT and IDFT method.
Soln:
Given, x(n)={1,2,3,4}
h(n)={4,3,2}

finding X(k) using DIT FFT algorithm


1 (0) = 4

x(0)=1

x(2)=3

-1

1 (1) = 2

-1

2 (0) = 6

x(1)=2

x(3)=4

X(0)=10

-1

2 (1) = 2

X(1)=-2-2j

X(2)=-2

-1

x(3)=-2-2j

X(k) = {10 , -2-2j , -2 , -2-2j }


Page | 37
DSP LAB 2011/12

Finding H(k) using DIT FFT algorithm


1 (0) = 6

h(0)

h(2)

1 (1) = 2

-1

2 (0) = 3

h(1)

h(3)

H(0)=9

2 (1) = 3

-1

H(1)=2-3j

-1

H(2)=3

-1

H(3)=2+3j

H(k)={ 9 , 2-3j , 3 , 2+3j }

Y(k)=X(k)H(k)
10
2 + 2
=
2
2 2

9
90
2 3 2 + 10
=
3
6
2 + 3 2 10

Y(k)={ 90 , 2+10j , -6 , 2-10j }

Finding y(n) using DIT FFT algorithm


Y(0)=90

Y(2)=-6

-1

88

1 (1) = 96

76

2 (0) = 4

Y(1)=2+10j

Y(3)=2-10j

1 (0) = 84

-1

2 (1) = 20

-1

-1

80

116

y(0)=22

y(1)=10

1
4

1
4

y(2)=20

y(3)=29

y(n)={ 22, 10, 20, 29 }

Page | 38
DSP LAB 2011/12

MATLAB CODE :
clc;
x=input('Enter the sequence x(n)=....');
h=input('Enter the sequence h(n)=....');
N1=length(x);
N2=length(h);
N3=max(N1,N2);
N=N1-N2;
X=fft(x,N3);
disp('dft of x(n) is');
X
H=fft(h,N3);
disp('dft of h(n) is');
H
z=X.*H;
disp('dft of x & h is');
z
subplot(2,1,1);
n=0:N3-1;
stem(n,abs(z));
xlabel('n.....');
ylabel('magnitude of z');
z=ifft(z,N3);
disp('IDFT of z is.....');
z
subplot(2,1,2);
n1=0:N3-1;
stem(n1,abs(z));
xlabel('n.....');
ylabel('magnitude of z');
title('IDFT of z');

Page | 39
DSP LAB 2011/12

Output :
Enter the sequence x(n)=....[1,2,3,4]
Enter the sequence h(n)=....[2,3,4,0]

dft of x(n) is
X=
10.0000

-2.0000 + 2.0000i -2.0000

-2.0000 - 2.0000i

dft of h(n) is
H=
9.0000

-2.0000 - 3.0000i 3.0000

-2.0000 + 3.0000i

dft of x & h is
z=
90.0000

10.0000 + 2.0000i -6.0000

10.0000 - 2.0000i

IDFT of z is.....
z=
26 23 16 25

Page | 40
DSP LAB 2011/12

Plot :

100

magnitude of z

80
60
40
20
0

0.5

1.5
n.....

2.5

2.5

IDFT of z
30

magnitude of z

25
20
15
10
5
0

0.5

1.5
n.....

Page | 41
DSP LAB 2011/12

11. LINEAR CONVOLUTION USING DFT & IDFT


If x(n) and h(n) are the two sequences of length l and m respectively. then X(k) and
H(k) their DFTs of length N=L+M-1.
Y(k)=x(k)h(k)
Therefore the linear convolution of two sequence is the N point IDFT of Y(k).

Ex:
Find the linear convolution of x(n)={1,2} and h(n)={1,2,3} using DFT and IDFT method.
Soln:
Given, x(n)={1,2}
h(n)={1,2,3}
here L=2, M=3
N=L+M-1
Therefore, N=4
x(n)={1,2,0,0} and h(n)={1,2,3,0}

X(k) using DIT FFT algorithm


x(0)=1

x(2)=0

1 (0)=1

-1

1 (1)=1

x(1)=2

x(3)=0

X(0)=3

2 (0)=2

-1

2 (1)=2

X(1)=1-2j

-1

-1

X(2)=-1

X(3)=1+2j

X(K)={3 , 1-2j , -1 , 1+2j }


Page | 42
DSP LAB 2011/12

H(k) using DIT FFT algorithm


h(0)=1

h(2)=3

1 (0)=4

-1

1 (1)=--2

h(1)=2

h(3)=0

H(0)=6

H(1)=-2-2j

2 (0)=2

-1

-1

2 (1)=2

H(2)=2

-1

H(3)=-2+2j

H(k)={9 , 2-3j , 3 , 2+3j }

Y(k)=x(k)h(k)
10
2 + 2
Y(k)=
2
2 2

9
90
2 3 2 + 10
=
3
6
2 + 3 2 10

Y(k)={ 90 , 2+10j , -6 , 2-10j }

Y(n) using DIT FFT algorithm


Y0)=18

Y(2)=-2

-1

Y(1)=2

Y(3)=-6-6j

H(0)=6

20

H(1)=-2-2j

1/4

y(1)=4

H(2)=2

1/4

y(2)=7

-12

-1

y(0)=1

16

4j

-1

-1

H(3)=-2+2j

1/4

1/4

y(3)=6

y(n)={ 22 , 19 , 20 , 29 }
Page | 43
DSP LAB 2011/12

MATLAB CODE:
clc;
x=input('Enter the sequence x(n)=....');
h=input('Enter the sequence h(n)=....');
N1=length(x);
N2=length(h);
N=N1+N2-1;
X=fft(x,N);
H=fft(h,N);
Y=X.*H;
yn=ifft(Y,N);
disp('yn=...');
disp(yn);
n=0:N-1;
stem(n,yn);
xlabel('samples of yn');
ylabel('amplitude of yn');
title('Output yn');

Output :
Enter the sequence x(n)=....[2,3]
Enter the sequence h(n)=....[1,2,3]
yn=...
2

7 12

Plot :
Output yn
12

amplitude of yn

10
8
6
4
2
0

0.5

1.5
samples of yn

2.5

Page | 44
DSP LAB 2011/12

12. MATLAB CODE FOR LINEAR CONVOLUTION USING CIRCULAR CONVOLUTION.

clc;
x=input('Enter the sequence of x(n)');
h=input('Enter the sequence of h(n)');
N1=length(x);
N2=length(h);
N=N1+N2-1;
x=[x,zeros(1,N2-1)];
h=[h,zeros(1,N1-1)];
for n=1:N
y(n)=0;
for i=1:N
j=n-i+1;
if(j<=0)
j=N+j;
end
y(n)=y(n)+x(i)*h(j);
end
end
disp('Linear convolution output is');
y
N3=0:1:length(y)-1;
subplot(2,2,2);
stem(N3,y);
ylabel('amplitude of y');
xlabel('samples of y');
title('output y');

Output :

Enter the sequence of x(n)[1,2,3,4]


Enter the sequence of h(n)[5,6,7,8]
Linear convolution output is
y=
5 16 34 60 61 52 32

Page | 45
DSP LAB 2011/12

Plot :

output y
70
60
amplitude of y

50
40
30
20
10
0

3
samples of y

Page | 46
DSP LAB 2011/12

13. MATLAB CODE FOR SAMPLING THEOREM USING SPLINE FUNCTION


clc;
t=[0:0.01:1];
f=10;
x=sin(2*pi*f*t);
figure(1);
plot(t,x);
title('original signal');
fs=100;
ts=1/fs;
n=t/ts;
m=min(n):max(n);
x1=sin(2*pi*f*m*ts);
figure(2);
stem(m,x1);
title('sampled signal');
tt=m*ts;
st=0.001;
tt1=min(tt):st:max(tt);
xa=spline(tt,x1,tt1);
figure(3);
plot(tt1,xa);
title('reconstructed signal');
fs1=19;
ts1=1/fs1;
n1=t/ts1;
m1=min(n1):max(n1);
x2=sin(2*pi*f*m1*ts1);
figure(4);
stem(m1,x2);
title('under sampled signal');
ww=m1*ts1;
ww1=0.001;
ww2=[min(ww):ww1:max(ww)];
xa1=spline(ww,x2,ww2);
figure(5);
plot(ww2,xa1);

Page | 47
DSP LAB 2011/12

Plot :
Figure 1
original signal
1

0.5

-0.5

-1

0.2

0.4

0.6

0.8

80

100

Figure 2
sampled signal
1

0.5

-0.5

-1

20

40

60

Page | 48
DSP LAB 2011/12

Figure 3
reconstructed signal
1

0.5

-0.5

-1

0.2

0.4

0.6

0.8

Figure 4
under sampled signal
1

0.5

-0.5

-1

10

15

20

Page | 49
DSP LAB 2011/12

Figure 5

1
0.8
0.6
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

Page | 50
DSP LAB 2011/12

14. MATLAB CODE FOR SAMPLING THEOREM WITHOUT USING SPLINE FUNCTION
clc;
fs=input('enter the sampling frequency=');
ts=1/fs;
n2=0:1000;
x2=cos(2*pi*0.001*n2);
subplot(2,2,1);
plot(n2,x2);
title('analog signal');
xlabel('t:');
ylabel('x(t)');
n=0:100;
x=cos(2*pi*ts*n);
subplot(2,2,2);
plot(n,x);
title('sampled signal');
xlabel('n:');
ylabel('x(n)');
n1=-100:100;
t1=ts/30;
intf=6*sinc(n1*t1/ts);
subplot(2,2,3);
plot(n1,intf);
title('sinc function');
xlabel('n1');
X1=x(1);
for i=2:length(X1);
X1=[X1,zeros(1,29),x(i)];
end
length(X1);
rect=conv(X1,intf);
subplot(2,2,4);
plot(rect);
title('reconstructed signal');
xlabel('t:');
ylabel('x(t)');

Output:
Enter the Sampling Frequency=25

Page | 51
DSP LAB 2011/12

Plot:

sampled signal
1

0.5

0.5

x(n)

x(t)

analog signal
1

-0.5

-1

-0.5

200

400

600

800

-1

1000

20

40

t:
sinc function

80

100

200

250

reconstructed signal
6

x(t)

-2
-100

60
n:

-50

0
n1

50

100

-2

50

100

150
t:

Page | 52
DSP LAB 2011/12

15. FIR FILTER DESIGN USING MATLAB

MATLAB CODE:
clc;
disp('FIR FILTER DESIGN USING KAISER WINDOW');
disp('');
M=input('Enter the Length of the Filter=');
beta=input('Enter the value of beta=');
wc=input('Enter the Digital CutOff Frequency=');
wn=Kaiser(M,beta);
disp('The Kaiser Window Coefficient are as follow');
disp(wn);
hn=fir1(M-1,wc,wn);
disp('The Unit Sample Response of FIR Filter is h(n)=');
disp(hn);
freqz(hn,1,512);
grid on;
xlabel('Normalized Frequency');
ylabel('Gain in dB');
title('Frequency Response of the FIR Filter');

Page | 53
DSP LAB 2011/12

OUTPUT:
The Kaiser Window Coefficient are as follow
0.7175
0.7523
0.7854
0.8166
0.8457
0.8727
0.8973
0.9195
0.9392
0.9563
0.9706
0.9822
0.9909
0.9967
0.9996
0.9996
0.9967
0.9909
0.9822
0.9706
0.9563
0.9392
0.9195
0.8973
0.8727
0.8457
0.8166
0.7854
0.7523
0.7175

The Unit Sample Response of FIR Filter is h(n)=


Columns 1 through 16
0.0141 0.0028 -0.0142 -0.0224 -0.0117 0.0133 0.0333 0.0277 -0.0072 -0.0495
-0.0614 -0.0140 0.0895 0.2097 0.2900 0.2900
Columns 17 through 30
0.2097 0.0895 -0.0140 -0.0614 -0.0495 -0.0072 0.0277 0.0333 0.0133 -0.0117
-0.0224 -0.0142 0.0028 0.0141

Page | 54
DSP LAB 2011/12

PLOT:

Frequency Response of the FIR Filter


20
0

Gain in dB

-20
-40
-60
-80
-100

0.1

0.2

0.1

0.2

0.3

0.4
0.5
0.6
Normalized Frequency

0.7

0.8

0.9

0.8

0.9

Phase (degrees)

-200
-400
-600
-800
-1000

0.3
0.4
0.5
0.6
0.7
Normalized Frequency ( rad/sample)

Page | 55
DSP LAB 2011/12

16. DIGITAL FILTER DESIGN USING MATLAB

MATLAB CODE:
clc;
Wp=input('Enter the Pass Band Edge Frequency in rad/sec=');
Ap=input('Enter the Pass Band Ripple in dB=');
Ws=input('Enter Stop Band Edge Frequenccy in rad/sec=');
As=input('Enter Stop Band Ripple in dB=');
Fs=input('Enter the Sampling Frequency=');
[n,Wn]=buttord(Wp,Ws,Ap,As,'s');
disp('The Order of the filter is n=');
disp(n);
[num,den]=butter(n,Wn,'s');
[b,a]=bilinear(num,den,Fs);
freqz(b,a,512,Fs);
grid on;
xlabel('Frequency in Hz');
ylabel('Gain in dB');
title('Frequency Response of the Filter');

OUTPUT:
Enter the Pass Band Edge Frequency in rad/sec=1256.63
Enter the Pass Band Ripple in dB=3
Enter Stop Band Edge Frequenccy in rad/sec=2513.27
Enter Stop Band Ripple in dB=15
Enter the Sampling Frequency=2000
The Order of the filter is n=3

Page | 56
DSP LAB 2011/12

PLOT:

Frequency Response of the Filter


50

Gain in dB

0
-50
-100
-150
-200

100

200

300

400
500
600
Frequency in Hz

700

800

900

1000

100

200

300

400
500
600
Frequency (Hz)

700

800

900

1000

Phase (degrees)

-50
-100
-150
-200
-250
-300

Page | 57
DSP LAB 2011/12

17. Linear Convolution Using DSP Processor


C-Code:
#include<stdio.h>
#include<math.h>
int y[20];
main();
{
int m=6;
int n=6;
int i=0;
int x[15]={1,2,3,4,5,6,0,0,0,0,0,0}
int h[15]={1,2,3,4,5,6,0,0,0,0,0,0}
for(i=0;i<=m+n-1,i++)
{
y[i]=0;
for(j=0;j<=m+n-1,j++)
y[i]+=x[j]*h[i-j];
}
for(i=0;i<=m+n-1,i++)
printf(%d\n,y*i+);
}

Page | 58
DSP LAB 2011/12

18. Circular Convolution using DSP Processor


C-Code:
#include<stdio.h>
#include<math.h>
int m,n,x[30],h[30],y[30],I,j,temp[30],k,x2[30],a[30];
void main()
{
Printf(Enter the Length of the first sequence\n);
Scanf(%d;&m);
Printf(Enter the Length of the second sequence\n);
Scanf(%d;&n);
Printf(Enter the first sequence\n);
for(i=0;i<m;i++)
scanf(%d,&x*i+);
Printf(Enter the second sequence\n);
for(j=0;j<n;j++);
scanf(%d,&h*j+);
if(m-n!=0)
{
If (m>n)
{
for(i=0;i<m;i++);
h[i]=0;
n=m;
}
for(i=m;i<n;i++);
x[i]=0;
m=n;
}
y[0]=0;
a[0]=h[0];
for(j=0;j<n;j++);
a[j]=h[n-j];
for(i=0,i<n;i++)
y[0]+=x[i]*a[i];
for(k=1,k<n;k++)
{
y[k]=0;
for(j=1,j<n;j++)
x2[j]=a[j-1];
x2[0]=a[n-1];
for(i=0,i<n;i++)
{
a[i]=x2[i];
y[k]+=x[i]*x2[i];
}}
Printf(The Circular Convolution is\n);
for(i=0,i<n;j++)
printf(%d\t,y*i+);
}

Page | 59
DSP LAB 2011/12

19. N pt. DFT Using DSP Processor


C-Code:

#include<stdio.h>
#include<math.h>
void main(void)
{
short N=8;
short x[8]={1,3,2,4,1,6,4,7};
float pi=3.1412;
float sumre=0, simim=0;
float cosine=0, sine=0;
float out_real[8]={0,0}, out_imag[8]={0,0};
int i=0,k=0,n;
for(k=0;k<n;k++)
{
sumre=0;
sumim=0;
for(n=0;n<N;n++)
{
cosine=cos(2*pi*k*n/N);
sine=sin(2*pi*k*n/N)
sumre=sumre+x[n]*cosine;
sumim=sumim-x[n]*sine;
}
out_real[k]=sumre;
out_imag[k]=sumim;
printf(*%d+%7.3f%7.3f\n,k,out_real*k+,out_imag*k+);
}
}

Page | 60
DSP LAB 2011/12

20. Impulse Response Using DSP Processor


C-Code:
#include<stdio.h>
#include<math.h>
#define len3
float y[len]={0,0,0},sum;
main()
{
int j,k;
float b[order+1]={1};
float a[order+1]={1,1,-6};
for(j=0;j<len;j++)
{
if (j-k)>=0
sum=sum+(a[k]*y[j-k]);
}
if (j<=order)
{
y[j]=b[j]-sum;
}
else
{
y[j]=-sum;
}
Printf(Response*%d+=%f\n,j,y*j+);
}
}

Page | 61
DSP LAB 2011/12

S-ar putea să vă placă și