DSP

12
>> type firfilt function yy = firfilt (bb,xx) xx(length(xx)+length(bb)-1) = 0; yy = filter (bb,1,xx) ; nn = 0:99; xx = cos (0.08 * pi * nn) ; bb = [1/3 1/3 1/3]; yy = firfilt (bb,xx) yy = Columns 1 through 6 0.3333 0.6562 0.9483 0.8580 0.7137 0.5246 Columns 7 through 12 0.3025 0.0615 -0.1835 -0.4169 -0.6241 -0.7921 Columns 13 through 18 -0.9103 -0.9713 -0.9713 -0.9103 -0.7921 -0.6241 Columns 19 through 24 -0.4169 -0.1835 0.0615 0.3025 0.5246 0.7137 Columns 25 through 30 0.8580 0.9483 0.9791 0.9483 0.8580 0.7137 Columns 31 through 36 0.5246 0.3025 0.0615 -0.1835 -0.4169 -0.6241 Columns 37 through 42 -0.7921 -0.9103 -0.9713 -0.9713 -0.9103 -0.7921 Columns 43 through 48

description

LAB

Transcript of DSP

Page 1: DSP

>> type firfilt

function yy = firfilt (bb,xx)

xx(length(xx)+length(bb)-1) = 0;

yy = filter (bb,1,xx) ;

nn = 0:99;xx = cos (0.08 * pi * nn) ;bb = [1/3 1/3 1/3];yy = firfilt (bb,xx)

yy =

Columns 1 through 6

0.3333 0.6562 0.9483 0.8580 0.7137 0.5246

Columns 7 through 12

0.3025 0.0615 -0.1835 -0.4169 -0.6241 -0.7921

Columns 13 through 18

-0.9103 -0.9713 -0.9713 -0.9103 -0.7921 -0.6241

Columns 19 through 24

-0.4169 -0.1835 0.0615 0.3025 0.5246 0.7137

Columns 25 through 30

0.8580 0.9483 0.9791 0.9483 0.8580 0.7137

Columns 31 through 36

0.5246 0.3025 0.0615 -0.1835 -0.4169 -0.6241

Columns 37 through 42

-0.7921 -0.9103 -0.9713 -0.9713 -0.9103 -0.7921

Columns 43 through 48

-0.6241 -0.4169 -0.1835 0.0615 0.3025 0.5246

Page 2: DSP

Columns 49 through 54

0.7137 0.8580 0.9483 0.9791 0.9483 0.8580

Columns 55 through 60

0.7137 0.5246 0.3025 0.0615 -0.1835 -0.4169

Columns 61 through 66

-0.6241 -0.7921 -0.9103 -0.9713 -0.9713 -0.9103

Columns 67 through 72

-0.7921 -0.6241 -0.4169 -0.1835 0.0615 0.3025

Columns 73 through 78

0.5246 0.7137 0.8580 0.9483 0.9791 0.9483

Columns 79 through 84

0.8580 0.7137 0.5246 0.3025 0.0615 -0.1835

Columns 85 through 90

-0.4169 -0.6241 -0.7921 -0.9103 -0.9713 -0.9713

Columns 91 through 96

-0.9103 -0.7921 -0.6241 -0.4169 -0.1835 0.0615

Columns 97 through 102

0.3025 0.5246 0.7137 0.8580 0.6150 0.3229

1.1: Frequency Response of FIR Filter

bb = [1/2,1/2]ww = -pi : pi/100 : piH = freqz (bb,1,ww);plot (ww,abs(H));grid

Page 3: DSP

-4 -3 -2 -1 0 1 2 3 40

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

1

2.1 Frequency Response of the Three-Point Averager:

bb = 1/3 * ones (1,3)ww = -pi : pi/400 : piH = freqz (bb,1,ww);subplot (2,1,1)plot (ww,abs(H));gridsubplot (2,1,2)plot (ww,angle(H));gridxlabel('Frequency')

-4 -3 -2 -1 0 1 2 3 40

0.2

0.4

0.6

0.8

1

-4 -3 -2 -1 0 1 2 3 4-4

-2

0

2

4

Frequency

Page 4: DSP

3.2 First Difference Filter:

A = 7; fi = pi/3; W = .125*pi; n = 0:49;xx = A * cos ((W*n)+fi);bb = [5 -5];yy = firfilt(xx,bb);

(a)

>> length1 = length(xx)

length1 =

50

>> length2 = length(yy)

length2 =

51

(b)

A = 7; fi = pi/3;W = .125*pi; n = 0:49;xx = A * cos ((W*n)+fi);bb = [5 -5];yy = firfilt(xx,bb)subplot(2,1,1)stem (n,xx);grid onxlabel ('0<n<49')title ('Magnitude and Phase of X')subplot(2,1,2)stem(n,yy(1:50));grid onxlabel ('0<n<49')title ('Magnitude and Phase of Y')

Page 5: DSP

0 5 10 15 20 25 30 35 40 45 50-10

-5

0

5

10

0<n<49

Magnitude and Phase of X

0 5 10 15 20 25 30 35 40 45 50-20

-10

0

10

20

0<n<49

Magnitude and Phase of Y

Amplitude of X is 6.9043.

frequency = pi/16

amplitude = 13

phase = pi/8

(f) A = 7; fi = pi/3; W = .125*pi; n = 0:49;xx = A * cos ((W*n)+fi);bb = [5 -5];yy = firfilt(xx,bb)subplot(2,1,1)plot (n,abs(xx));gridtitle('mag of X and Y')hold onplot(n,abs(yy(1:50)), '-.')hold offsubplot(2,1,2)plot (n,angle(xx));gridtitle('angle of X and Y')hold onplot (n,angle(yy(1:50)),'-.')

Page 6: DSP

hold off

0 5 10 15 20 25 30 35 40 45 500

5

10

15

20mag of X and Y

0 5 10 15 20 25 30 35 40 45 500

1

2

3

4angle of X and Y

(g)

W = .125*pi; n = 0:49;xx = exp (j*W*n);bb = [5 -5];yy = firfilt(xx,bb)subplot(2,1,1)stem (n,xx);grid onxlabel ('0<n<49')title ('Magnitude and Phase of X')subplot(2,1,2)stem(n,yy(1:50));grid onxlabel ('0<n<49')title ('Magnitude and Phase of Y')

Page 7: DSP

0 5 10 15 20 25 30 35 40 45 50-1

-0.5

0

0.5

1

0<n<49

Magnitude and Phase of X

0 5 10 15 20 25 30 35 40 45 50-2

0

2

4

6

0<n<49

Magnitude and Phase of Y

3.3 Linearity of the Filter

(a)

A = 7; fi = pi/3; W = .125*pi; n = 0:49;xx = A * cos ((W*n)+fi);xa = 2 * xx;bb = [5 -5];ya = firfilt (xa,bb)subplot(2,1,1)stem (n,xx);grid onxlabel ('0<n<49')title ('Magnitude and Phase of X')subplot(2,1,2)stem(n,yy(1:50));grid onxlabel ('0<n<49')title ('Magnitude and Phase of Y')

Page 8: DSP

0 5 10 15 20 25 30 35 40 45 50-10

-5

0

5

10

0<n<49

Magnitude and Phase of X

0 5 10 15 20 25 30 35 40 45 50-2

0

2

4

6

0<n<49

Magnitude and Phase of Y

approximate values of frequency, amplitude and phase of x[n] is given below:

frequency = pi/16

amplitude = 7

phase = pi/8

(b,c)

A = 7; fi = pi/3; W = .125*pi; n = 0:49;

xx = A * cos ((W*n)+fi);xa = 2 * xx;bb = [5 -5];ya = firfilt(xa,bb)xb = 8*cos(.25*pi*n);yb = firfilt (xb,bb)xc = xa+xbYc1 = firfilt(xc,bb)Yc2 = ya+yb;error = max(abs(Yc1-Yc2))subplot(2,1,1)plot(n,Yc1(1:50));gridsubplot(2,1,2)plot(n,Yc2(1:50));gridhold on

Page 9: DSP

plot (n,error,'r')hold off

0 5 10 15 20 25 30 35 40 45 50-100

-50

0

50

100

0 5 10 15 20 25 30 35 40 45 50-100

-50

0

50

100

error =

2.4869e-14

3.4 Time-Invariance of the Filter:

A = 7; fi = pi/3; W = .125*pi; n = 0:49;xx = A * cos ((W*n)+fi);bb = [5 -5];yy = firfilt(xx,bb)xs = A * cos ((W*(n-3))+fi);ys = firfilt(xs,bb)subplot(311)plot(n,yy(1:50));gridtitle('yy')subplot(312)plot(n,ys(1:50),'r');gridtitle('ys')subplot(313)plot(n,yy(1:50));gridhold onplot(n,ys(1:50),'r');title('yy and ys')hold off

Page 10: DSP

0 5 10 15 20 25 30 35 40 45 50-20

0

20yy

0 5 10 15 20 25 30 35 40 45 50-50

0

50ys

0 5 10 15 20 25 30 35 40 45 50-50

0

50yy and ys

3.5 Cascading Two Systems:

(a)

A = 7; fi = pi/3; W = .125*pi; n = 0:49;xx = A * cos ((W*n)+fi);w = xx.^2;b = [1 1];y = firfilt (w,b)

y =

Columns 1 through 6

12.2500 13.0848 4.1172 21.4413 54.9089 84.9152

Columns 7 through 12

93.8828 76.5587 43.0911 13.0848 4.1172 21.4413

Columns 13 through 18

54.9089 84.9152 93.8828 76.5587 43.0911 13.0848

Columns 19 through 24

4.1172 21.4413 54.9089 84.9152 93.8828 76.5587

Columns 25 through 30

43.0911 13.0848 4.1172 21.4413 54.9089 84.9152

Columns 31 through 36

Page 11: DSP

93.8828 76.5587 43.0911 13.0848 4.1172 21.4413

Columns 37 through 42

54.9089 84.9152 93.8828 76.5587 43.0911 13.0848

Columns 43 through 48

4.1172 21.4413 54.9089 84.9152 93.8828 76.5587

Columns 49 through 51

43.0911 13.0848 0.8348

(b)

A = 7; fi = pi/3; W = .125*pi; n = 0:49;xx = A * cos ((W*n)+fi);w = xx.^2;b = [1 1];y = firfilt (w,b)subplot(3,1,1)plot (n,xx);gridtitle('X')subplot(3,1,2)plot(n,abs(y(1:50)));gridtitle('Y')subplot(3,1,3)plot (n,w);grid

0 5 10 15 20 25 30 35 40 45 50-10

0

10X

0 5 10 15 20 25 30 35 40 45 500

50

100Y

0 5 10 15 20 25 30 35 40 45 500

50