Top Banner
12/1/2011 2011 Faiz Karobari S.L.N. Engineering College, Raichur. 2011-12 Digital Signal Processing Lab
61
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: DSP LAB

12/1/2011 2011

Faiz Karobari

S.L.N. Engineering College, Raichur.

2011-12

Digital Signal Processing Lab

Page 2: DSP LAB

H.K.E. SOCIETY’S

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._________________

Page 3: DSP LAB

Page | 3 DSP LAB – 2011/12

INDEX

A. LIST OF EXPERIMENT USING MATLAB Page No.

1. Linear Convolotion of Two Sequences. 5

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

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 4: DSP LAB

Page | 4 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 5: DSP LAB

Page | 5 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}

-1 -0.5 0 0.5 10

0.2

0.4

0.6

0.8

1

am

plit

ude o

f x1(n

)

samples of x1(n)

input x1(n)

0 1 2 30

1

2

3

4

am

plit

ude o

f y1(n

)

samples of y1(n)

input y1(n)

0 1 2 30

1

2

3

4

am

plit

ude o

f x(n

)

samples of x(n)

input x(n)

0 1 2 30

1

2

3

4

am

plit

ude o

f h(n

)

samples of h(n)

output h(n)

Page 6: DSP LAB

Page | 6 DSP LAB – 2011/12

y(n) = y1(n)+y2(n)+y3(n)+y4(n)

= {4,11,20,30,20,11,4}

0 1 2 30

1

2

3

am

plit

ude o

f x(3

)

samples of x(3)

input x(3)

0 2 4 60

5

10

15

am

plit

ude o

f y3(n

)

samples of y3(n)

output y3(n)

0 0.5 1 1.5 20

0.5

1

1.5

2

am

plit

ude o

f x(2

)

samples of x(2)

input x(2)

0 1 2 3 40

2

4

6

8

am

plit

ude o

f y2(n

)

samples of y2(n)

output y2(n)

0 1 2 3 4 5 60

5

10

15

20

25

30

am

plit

ude o

f y(n

)

samples of y(n)

input y(n)

Page 7: DSP LAB

Page | 7 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 4

Page 8: DSP LAB

Page | 8 DSP LAB – 2011/12

Plot :

0 0.5 1 1.5 2 2.5 30

2

4

am

plit

ude o

f x(n

)

samples of x(n)

input x(n)

0 0.5 1 1.5 2 2.5 30

2

4

am

plit

ude o

f h(n

)

samples of h(n)

input h(n)

0 1 2 3 4 5 60

20

40

am

plit

ude o

f y(n

)

samples of y(n)

output y(n)

Page 9: DSP LAB

Page | 9 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 3 2 1

Page 10: DSP LAB

Page | 10 DSP LAB – 2011/12

Plot :

0 0.5 1 1.5 2 2.5 30

2

4

am

plit

ude o

f x(n

)

samples of x(n)

input x(n)

0 1 2 3 4 5 60

20

40

am

plit

ude o

f y(n

)

samples of y(n)

input y(n)

0 0.5 1 1.5 2 2.5 30

2

4

am

plit

ude o

f h(n

)

samples of h(n)

output impulse response h(n)

Page 11: DSP LAB

Page | 11 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.,

H Z =Y Z

X Z

=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 12: DSP LAB

Page | 12 DSP LAB – 2011/12

Ex2:

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

if 𝐻 𝑍 =1

1−0.5𝑍−1 . Calculate h(n).

Soln:

Given 𝐻 𝑍 =1

1−0.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 13: DSP LAB

Page | 13 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 14: DSP LAB

Page | 14 DSP LAB – 2011/12

Plot :

0 1 2 3 4 5 60

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

1

am

plit

ude o

f h(n

)

samples of h(n)

output impulse response h(n)

Page 15: DSP LAB

Page | 15 DSP LAB – 2011/12

4. IMPLEMENTATION OF DIFFERENCE EQUATION

Solve the difference equation

y(n) - 3

4 y(n-1) +

1

8 y(n-2) = x(n) -

1

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:

y(n) - 3

4 y(n-1) +

1

8 y(n-2) = x(n) -

1

3 x(n-1)

at n=0,

y(0) = 3

4 y(-1) -

1

8 y(-2) + x(0) -

1

3 x(-1)

y(0)=1

at n=1,

y(1) = 3

4 y(0) -

1

8 y(-1) + x(1) -

1

3 x(0)

y(1) = 1.4166

at n=2,

y(2) = 3

4 y(1) =

1

8 y(0) + x(2) -

1

3 x(1)

y(2) = 1.6042

at n=3,

y(3) = 3

4 y(2) -

1

8 y(1) + x(3) -

1

3 x(2)

y(3) = 1.6927

at n=4,

y(4) = 3

4 y(3) -

1

8 y(2) + x(4) -

1

3 x(3)

y(4) = 1.7357

at n=5,

y(5) = 3

4 y(4) -

1

8 y(3) + x(5) -

1

3 x(4)

y(5) = 1.7568

at n=6,

y(6) = 3

4 y(5) -

1

8 y(4) + x(6) -

1

3 x(5)

y(6) = 1.7673

Page 16: DSP LAB

Page | 16 DSP LAB – 2011/12

at n=7,

y(7) = 3

4 y(6) -

1

8 y(5) + x(7) -

1

3 x(6)

y(7) = 1.7726

at n=8,

y(8) = 3

4 y(7) -

1

8 y(6) + x(8) -

1

3 x(7)

y(8) = 1.7752

at n=9,

y(9) = 3

4 y(8) -

1

8 y(7) + x(9) -

1

3 x(8)

y(9) = 1.7765

Page 17: DSP LAB

Page | 17 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 :

0 1 2 3 4 5 6 7 8 90

0.5

1

1.5

2

am

plit

ude o

f y(n

)

samples of y(n)

output y(n)

Page 18: DSP LAB

Page | 18 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 1 2 1

1 1 2 1 1

2 1 2 4 2

1 1 1 2 1

1 1 1 2 1

𝒓𝒙𝒙={𝟏,𝟑,𝟓,𝟕,𝟓,𝟑,𝟏}

Page 19: DSP LAB

Page | 19 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 3 2 1

1 4 3 2 1

2 8 6 4 2

3 12 9 6 3

4 16 12 8 4

𝐫𝐱𝐱= 𝟒,𝟏𝟏,𝟐𝟎,𝟑𝟎,𝟐𝟎,𝟏𝟏,𝟒

Energy of x(n) is

𝐸 = x 2

3

𝑛=0

=12 + 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 20: DSP LAB

Page | 20 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 21: DSP LAB

Page | 21 DSP LAB – 2011/12

Plot :

0 0.5 1 1.5 2 2.5 30

1

2

am

plit

ude o

f x(n

)

samples of x(n)

input x(n)

-3 -2.5 -2 -1.5 -1 -0.5 00

1

2

am

plit

ude o

f y(n

)

samples of y(n)

input y(n)

-3 -2 -1 0 1 2 30

5

10

am

plit

ude o

f z(n

)

samples of z(n)

output z(n)

Page 22: DSP LAB

Page | 22 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 2 1 1

1 1 2 1 1

2 2 4 2 2

1 1 2 1 1

1 1 2 1 1

𝒓𝒙𝒚={𝟏,𝟒,𝟔,𝟔,𝟓,𝟐,𝟏}

Page 23: DSP LAB

Page | 23 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 4 6 6 5 2 1

Plot :

Page 24: DSP LAB

Page | 24 DSP LAB – 2011/12

0 0.5 1 1.5 2 2.5 30

1

2

samples n

am

plit

ude x

n

input xn

0 0.5 1 1.5 2 2.5 30

1

2

samples n

am

plit

ude y

n

input yn

-3 -2.5 -2 -1.5 -1 -0.5 00

1

2

samples n

am

plit

ude y

n

input yn

-3 -2 -1 0 1 2 30

5

10

samples n

am

plit

ude z

n

output zn

Page 25: DSP LAB

Page | 25 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 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) h(n)

y m = x n

𝑛=−∞

y((m − n))4

For m=0,

y 0 = x n

3

𝑛=0

h((−n))4

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,

y 1 = x n

3

𝑛=0

h((1 − n))4

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

N

N

Page 26: DSP LAB

Page | 26 DSP LAB – 2011/12

For m=2,

y 2 = x n

3

𝑛=0

h((2 − n))4

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

y 3 = x n

3

𝑛=0

h((3 − n))4

=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 27: DSP LAB

Page | 27 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 28: DSP LAB

Page | 28 DSP LAB – 2011/12

Plot :

-3 -2.5 -2 -1.5 -1 -0.5 00

1

2

samples n

am

plit

ude y

n

input yn

-3 -2 -1 0 1 2 30

5

10

samples n

am

plit

ude z

n

output zn

0 1 2 30

5

10

15

20

25

30

am

plit

ude o

f y

samples of y

output y

Page 29: DSP LAB

Page | 29 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) N DFT X(k)

𝑋 𝑘 = x n

N−1

𝑛=0

e−j 2πN

kn

Ex:

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

Soln:

x(n)={0,1,2,3}

𝑋 𝑘 = x n

N−1

𝑛=0

e−j 2πN

kn

For k=0,

𝑋 0 = x n

3

𝑛=0

∗ 1

= 0+1+2+3= 6

X(0) = 6

For k=1,

𝑋 1 = x n

3

𝑛=0

e−j 2π4 1∗n

=x(0) + x(1) e−j π

2 + x(2) e−j π + x(3) e−j

2

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

X(1) = -2+2j

Page 30: DSP LAB

Page | 30 DSP LAB – 2011/12

For k=2,

𝑋 2 = x n

3

𝑛=0

e−jπn

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

X(2) = -2

For k=3,

𝑋 3 = x n

3

𝑛=0

e−j(π2

)3n

=x(0) + x(1) e−j

2 + x(2) e−j 3π + x(3) e

−j 9π

2

=0+j-2-3j

X(3) = -2-2j

Therefore,

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

Page 31: DSP LAB

Page | 31 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 32: DSP LAB

Page | 32 DSP LAB – 2011/12

Plot :

0 0.5 1 1.5 2 2.5 30

1

2

3

4

5

6

k=......

magnitude o

f X

(k)

magnitude of X(k)

0 0.5 1 1.5 2 2.5 3-4

-2

0

2

4

k....

phaseof

X(k

)

phase of X(k)

Page 33: DSP LAB

Page | 33 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) N IDFT X(k)

𝑥 𝑛 = 1

𝑁 X k

N−1

𝑛=0

ej 2πN

kn

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

Soln:

X(k)={1,2,3,4}

x(n) N IDFT X(k)

𝑥 𝑛 = 1

4 X k

3

𝑛=0

ej

2π4 kn

For n=0,

x(0)= 1

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

= 1

4 [1+2+3+4]

x(0)= 𝟓

𝟐

For n=1,

x(1)= 1

4 [ X(0) + X(1) ej

π

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

2 ]

= 1

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

x(1)=- 𝟏

𝟐 -

𝟏

𝟐𝒋

Page 34: DSP LAB

Page | 34 DSP LAB – 2011/12

for n=2,

x(2)= 1

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

= 1

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

x(2)=− 𝟏

𝟐

for n=3,

x(3)= 1

4 [ X(0) + X(1) ej

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

2 ]

= 1

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

x(3) =- 𝟏

𝟐 +

𝟏

𝟐𝒋

Therefore,

x(n)= { 𝟓

𝟐 ,−

𝟏

𝟐 −

𝟏

𝟐𝒋 , −

𝟏

𝟐 ,−

𝟏

𝟐 +

𝟏

𝟐𝒋 }

Page 35: DSP LAB

Page | 35 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 36: DSP LAB

Page | 36 DSP LAB – 2011/12

Plot :

0 0.5 1 1.5 2 2.5 30

1

2

3

4

5

6

n=......

magnitude o

f X

n

magnitude of Xn

0 0.5 1 1.5 2 2.5 3-4

-2

0

2

4

n....

phaseof

Xk

phase of Xk

Page 37: DSP LAB

Page | 37 DSP LAB – 2011/12

10. CIRCULAR CONVOLUTION USING DFT & IDFT

It states that,

If 𝑥1 𝑛 N DFT 𝑋1(𝑘)

and 𝑥2 𝑛 N DFT 𝑋2(𝑘)

Then

𝑥1 𝑛 𝑥2 𝑛 N DFT 𝑋1(𝑘) 𝑋2(𝑘)

It is stated that multiplication of two DFT’S 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

x(0)=1 𝐹1(0) = 4 X(0)=10

x(2)=3 -1 𝐹1(1) = −2 -1 X(1)=-2-2j

x(1)=2 𝐹2(0) = 6 X(2)=-2

x(3)=4 -1 𝐹2(1) = −2 -1 x(3)=-2-2j

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

N

Page 38: DSP LAB

Page | 38 DSP LAB – 2011/12

Finding H(k) using DIT FFT algorithm

h(0) 𝐹1(0) = 6 H(0)=9

h(2) -1 𝐹1(1) = 2 - H(1)=2-3j

h(1) 𝐹2(0) = 3 -1 H(2)=3

h(3) -1 𝐹2(1) = 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𝑗

92 − 3𝑗

32 + 3𝑗

=

902 + 10𝑗−6

2 − 10𝑗

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

Finding y(n) using DIT FFT algorithm

Y(0)=90 𝑃1(0) = 84 88 1

4 y(0)=22

Y(2)=-6 -1 𝑃1(1) = 96 76 1

4 y(1)=10

Y(1)=2+10j 𝑃2(0) = 4 -1 80 1

4 y(2)=20

Y(3)=2-10j -1 𝑃2(1) = 20𝑗 -1 116 1

4 y(3)=29

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

Page 39: DSP LAB

Page | 39 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 40: DSP LAB

Page | 40 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 41: DSP LAB

Page | 41 DSP LAB – 2011/12

Plot :

0 0.5 1 1.5 2 2.5 30

20

40

60

80

100

n.....

magnitude o

f z

0 0.5 1 1.5 2 2.5 30

5

10

15

20

25

30

n.....

magnitude o

f z

IDFT of z

Page 42: DSP LAB

Page | 42 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 DFT’s 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 𝐹1(0)=1 X(0)=3

x(2)=0 -1 𝐹1(1)=1 X(1)=1-2j

x(1)=2 𝐹2(0)=2 -1 X(2)=-1

x(3)=0 -1 𝐹2(1)=2 -1 X(3)=1+2j

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

Page 43: DSP LAB

Page | 43 DSP LAB – 2011/12

H(k) using DIT FFT algorithm

h(0)=1 𝐹1(0)=4 H(0)=6

h(2)=3 -1 𝐹1(1)=--2 H(1)=-2-2j

h(1)=2 𝐹2(0)=2 -1 H(2)=2

h(3)=0 -1 𝐹2(1)=2 -1 H(3)=-2+2j

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

Y(k)=x(k)h(k)

Y(k)=

10−2 + 2𝑗

−2−2 − 2𝑗

92 − 3𝑗

32 + 3𝑗

=

902 + 10𝑗−6

2 − 10𝑗

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

Y(n) using DIT FFT algorithm

Y0)=18 16 H(0)=6 1/4 y(0)=1

Y(2)=-2 -1 20 H(1)=-2-2j 1/4 y(1)=4

Y(1)=2 -12 -1 H(2)=2 1/4 y(2)=7

Y(3)=-6-6j -1 4j -1 H(3)=-2+2j 1/4 y(3)=6

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

Page 44: DSP LAB

Page | 44 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 9

Plot :

0 0.5 1 1.5 2 2.5 30

2

4

6

8

10

12

samples of yn

am

plit

ude o

f yn

Output yn

Page 45: DSP LAB

Page | 45 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 46: DSP LAB

Page | 46 DSP LAB – 2011/12

Plot :

0 1 2 3 4 5 60

10

20

30

40

50

60

70

am

plit

ude o

f y

samples of y

output y

Page 47: DSP LAB

Page | 47 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 48: DSP LAB

Page | 48 DSP LAB – 2011/12

Plot :

Figure 1

Figure 2

0 0.2 0.4 0.6 0.8 1-1

-0.5

0

0.5

1original signal

0 20 40 60 80 100-1

-0.5

0

0.5

1sampled signal

Page 49: DSP LAB

Page | 49 DSP LAB – 2011/12

Figure 3

Figure 4

0 0.2 0.4 0.6 0.8 1-1

-0.5

0

0.5

1reconstructed signal

0 5 10 15 20-1

-0.5

0

0.5

1under sampled signal

Page 50: DSP LAB

Page | 50 DSP LAB – 2011/12

Figure 5

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1-1

-0.8

-0.6

-0.4

-0.2

0

0.2

0.4

0.6

0.8

1

Page 51: DSP LAB

Page | 51 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 52: DSP LAB

Page | 52 DSP LAB – 2011/12

Plot:

-100 -50 0 50 100-2

0

2

4

6sinc function

n1

0 50 100 150 200 250-2

0

2

4

6reconstructed signal

t:

x(t

)

0 20 40 60 80 100-1

-0.5

0

0.5

1sampled signal

n:

x(n

)

0 200 400 600 800 1000-1

-0.5

0

0.5

1analog signal

t:

x(t

)

Page 53: DSP LAB

Page | 53 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 54: DSP LAB

Page | 54 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 55: DSP LAB

Page | 55 DSP LAB – 2011/12

PLOT:

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1-1000

-800

-600

-400

-200

0

Normalized Frequency ( rad/sample)

Phase (

degre

es)

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1-100

-80

-60

-40

-20

0

20

Normalized Frequency

Gain

in d

B

Frequency Response of the FIR Filter

Page 56: DSP LAB

Page | 56 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 57: DSP LAB

Page | 57 DSP LAB – 2011/12

PLOT:

0 100 200 300 400 500 600 700 800 900 1000-300

-250

-200

-150

-100

-50

0

Frequency (Hz)

Phase (

degre

es)

0 100 200 300 400 500 600 700 800 900 1000-200

-150

-100

-50

0

50

Frequency in Hz

Gain

in d

B

Frequency Response of the Filter

Page 58: DSP LAB

Page | 58 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 59: DSP LAB

Page | 59 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 60: DSP LAB

Page | 60 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 61: DSP LAB

Page | 61 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+); } }