You are on page 1of 11

>> type firfilt

function yy = firfilt (bb,xx)


xx(length(xx)+length(bb)-1) = 0;
yy = filter (bb,1,xx) ;
nn
xx
bb
yy

=
=
=
=

0:99;
cos (0.08 * pi * nn) ;
[1/3 1/3 1/3];
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

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 : pi
H = freqz (bb,1,ww);
plot (ww,abs(H));grid

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

-3

-2

-1

2.1 Frequency Response of the Three-Point Averager:

bb = 1/3 * ones (1,3)


ww = -pi : pi/400 : pi
H = freqz (bb,1,ww);
subplot (2,1,1)
plot (ww,abs(H));grid
subplot (2,1,2)
plot (ww,angle(H));grid
xlabel('Frequency')

1
0.8
0.6
0.4
0.2
0
-4

-3

-2

-1

-3

-2

-1

4
2
0
-2
-4
-4

0
Frequency

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 on
xlabel ('0<n<49')
title ('Magnitude and Phase of X')
subplot(2,1,2)
stem(n,yy(1:50));grid on
xlabel ('0<n<49')
title ('Magnitude and Phase of Y')

Magnitude and Phase of X


10
5
0
-5
-10

10

15

10

15

20

25
30
35
0<n<49
Magnitude and Phase of Y

40

45

50

40

45

50

20
10
0
-10
-20

20

25
0<n<49

30

35

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));grid
title('mag of X and Y')
hold on
plot(n,abs(yy(1:50)), '-.')
hold off
subplot(2,1,2)
plot (n,angle(xx));grid
title('angle of X and Y')
hold on
plot (n,angle(yy(1:50)),'-.')

hold off

mag of X and Y
20
15
10
5
0

10

15

20

25

30

35

40

45

50

35

40

45

50

angle of X and Y
4
3
2
1
0

10

15

20

25

30

(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 on
xlabel ('0<n<49')
title ('Magnitude and Phase of X')
subplot(2,1,2)
stem(n,yy(1:50));grid on
xlabel ('0<n<49')
title ('Magnitude and Phase of Y')

Magnitude and Phase of X


1
0.5
0
-0.5
-1

10

15

10

15

20

25
30
35
0<n<49
Magnitude and Phase of Y

40

45

50

40

45

50

6
4
2
0
-2

20

25
0<n<49

30

35

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 on
xlabel ('0<n<49')
title ('Magnitude and Phase of X')
subplot(2,1,2)
stem(n,yy(1:50));grid on
xlabel ('0<n<49')
title ('Magnitude and Phase of Y')

Magnitude and Phase of X


10
5
0
-5
-10

10

15

10

15

20

25
30
35
0<n<49
Magnitude and Phase of Y

40

45

50

40

45

50

6
4
2
0
-2

20

25
0<n<49

30

35

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+xb
Yc1 = firfilt(xc,bb)
Yc2 = ya+yb;
error = max(abs(Yc1-Yc2))
subplot(2,1,1)
plot(n,Yc1(1:50));grid
subplot(2,1,2)
plot(n,Yc2(1:50));grid
hold on

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

100
50
0
-50
-100

10

15

20

25

30

35

40

45

50

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));grid
title('yy')
subplot(312)
plot(n,ys(1:50),'r');grid
title('ys')
subplot(313)
plot(n,yy(1:50));grid
hold on
plot(n,ys(1:50),'r');
title('yy and ys')
hold off

yy
20
0
-20

10

15

20

25

30

35

40

45

50

30

35

40

45

50

30

35

40

45

50

ys
50
0
-50

10

15

20

25
yy and ys

50
0
-50

10

15

20

25

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

43.0911

13.0848

4.1172

21.4413

76.5587

43.0911

13.0848

84.9152

93.8828

76.5587

21.4413

54.9089

84.9152

Columns 7 through 12
93.8828

76.5587

Columns 13 through 18
54.9089

84.9152

93.8828

Columns 19 through 24
4.1172

21.4413

54.9089

Columns 25 through 30
43.0911

13.0848

Columns 31 through 36

4.1172

93.8828

76.5587

43.0911

13.0848

4.1172

21.4413

76.5587

43.0911

13.0848

84.9152

93.8828

76.5587

Columns 37 through 42
54.9089

84.9152

93.8828

Columns 43 through 48
4.1172

21.4413

54.9089

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);grid
title('X')
subplot(3,1,2)
plot(n,abs(y(1:50)));grid
title('Y')
subplot(3,1,3)
plot (n,w);grid

X
10
0
-10

10

15

20

25

30

35

40

45

50

Y
100
50
0

10

15

20

25

30

35

40

45

50

10

15

20

25

30

35

40

45

50

50

You might also like