Professional Documents
Culture Documents
Created by
Hasan Ali Stationwala
B.Tech., 2nd Year Student
Electronics and Communication Engineering,
National Institute Of Technology,
Tiruchirappalli
College teacher
Madhu N. Belur, IIT Bombay
Reviewer
Prashant Dave, IIT Bombay
15 July, 2010
Edition: 4th
Year: 2007
Scilab numbering policy used in this document and the relation to the above
book is as follows.
Prb Problem (Unsolved problem) These are at the end of each chapter.
For example, Prb 2.67 means Problem 2.67 of the above book. Scilab code
having number Sec 2.6 means a scilab code whose theory is explained in
Section 2.6 of the book.
1
Contents
2
9.40 Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
3
List of Scilab Codes
4
Exa 17 Example 17, page no. 1103 . . . . . . . . . . . . . . . 45
Exa 18 Example 18, page no. 1104 . . . . . . . . . . . . . . . 45
Exa 25 Example 25, page no. 1114 . . . . . . . . . . . . . . . 46
Exa 25 Example 29, page no. 1120 . . . . . . . . . . . . . . . 46
5
List of Figures
1
Chapter 2
2
4 function h=i m p u l s e ( a , b ,%n)
5
6 %a=length ( a ) ;
7
14 // c o m p u t i n g f i r s t %a terms
15 while %index <= %a,
16 h ( 1 , %index )=−a ( 1 , 1 : %index −1) ∗( h ( 1 , %index −1: −1:1) . ’ )+b
( 1 , %index ) ;
17 %index=%index +1;
18 end
19
26 endfunction
2.66 Problem
5 // l e n g t h of response
6 %n=50;
7
3
8 %a=length ( a ) ;
9 // zero − p a d d i n g in order to make element wise summation
compatible
10 b=[b , zeros ( 1 ,%n−length ( b ) ) ] ;
11 h=[ zeros ( 1 , 1 ) ] ;
12 h ( 1 , 1 )=b ( 1 , 1 ) ;
13 %index =2;
14
15 // c o m p u t i n g f i r s t %a terms
16 while %index <= %a,
17 h ( 1 , %index )=−a ( 1 , 1 : %index −1)∗h ( 1 , %index −1: −1:1) . ’+ b
( 1 , %index ) ;
18 %index=%index +1;
19 end
20
27 plot2d3 ( h ) ;
28 t i t l e ( ’ Impulse r e s p o n s e ’ )
29 x l a b e l ( ’ i m p u l s e r e s p o n s e a f t e r a terms ’ )
30 // ( b ) .
31 // u n i t step sequence
32 s =[ones ( 1 , 1 0 0 ) ] ;
33 y=convol ( h , s ) ;
34 scf (1) ;
35 plot2d ( y ( 1 , 1 : 1 0 0 ) ) ;
36 t i t l e ( ’ unit step response ’ )
37 xlabel ( ’ convoluted response ’ )
38 // c .
39 h f=h ( 1 : 1 9 ) ;
40 y f=convol ( hf , s ) ;
41 scf (2) ;
42 plot2d ( y f ) ;
4
43 t i t l e ( ’ unit step response ’ )
44 xlabel ( ’ convoluted response ’ )
45 // p l o t with the p i l l a r s is impulse response and that
with smooth curve is unit step response
2.67 Problem
4 // s ys te m 1
5 h1 =[1 0 . 5 0 . 2 5 0 . 1 2 5 0 . 0 6 2 5 0 . 3 1 2 5 ] ;
6
7 // s ys te m 2
8 h2 =[1 1 1 1 1 ] ;
9
13 // s ys te m 3
14 a3 = [ ] ;
15 b3 = [ 0 . 2 5 0 . 5 0 . 2 5 ] ;
16 h3=i m p u l s e ( a3 , b3 , 1 0 ) ;
17
18 // s ys te m 3 is in p a r a l l e l with 1 & 2
19 h123=h12+h3 ;
20
21 // s ys te m 4
22 a4 =[ −0.9 0 . 8 1 ] ;
23 b4 =[1 1 ] ;
24 h4=i m p u l s e ( a4 , b4 , 9 1 ) ;
25
26 // o v e r a l l system response
27 h=convol ( h123 , h4 ) ;
28
5
29 plot2d3 ( h ) ;
2.6 Correlation
Correlation is a mathematical tool used to measure the degree of similarity
between two sequences. The cross-correlation function of two sequences x
and y is denoted by rxy and is defined by the relation:
∞
X
rxy (l) = x(n)y(n − l), l ∈ (−∞, ∞) (2.2)
n=−∞
6
cor(). But if length of x is smaller than y, it sends y as first argument and
x as second argument to cor(), i.e. cor() actually calculates ryx and then
corel() calculates rxy from ryx using relation above.
The function ’cor()’ calculates cross-correlation between two sequences,
with constraint that the second sequence should be smaller than the first
sequence. Inputs to the function are:
• x-First sequence (a row vector)
7 %R=length ( y )−%y+%x ; / / c a l c u l a t i n g i n d e x o f l =0 in R
8 i f ( length ( x )>=length ( y ) )
9 R=c o r ( x , y ) ; / / c a l c u l a t i n g o f R x y
10 else
11 R=c o r ( y , x ) ; / / c a l c u l a t i n g R y x
12 R=R( 1 , $ : − 1 : 1 ) ; / / r e v e r s i n g t h e s e q u e n c e for R x y
13 end
14
15 endfunction
16
17
18 // f u n c t i o n to find corelation
19
20 function R= c o r ( a , b )
7
21
25 %index =0;
26 while %index<=(length ( b ) −1) ; // c a l c u l a t i n g f i r s t
length (b) terms
27 r=a ( 1 , 1 : ( %index+1) ) . ∗ b ( 1 , $−%index : $ ) ; // s h i f t i n g
and multiplication
28 R( 1 , %index+1)=sum( r , 2 ) ; // a d d i t i o n
29 %index=%index +1;
30 end
31
32 %index =1;
33 while %index<=(length ( a )−length ( b ) ) ; // c a l c u l a t i n g
next length ( a ) terms
34 r=b ( 1 , 1 : $ ) . ∗ a ( 1 , %index +1: length ( b )+%index ) ;
35 R( 1 , %index+length ( b ) )=sum( r , 2 ) ;
36 %index=%index +1;
37 end
38
39 %index =1; / / c a l c u l a t i n g r e m a i n i n g t e r m s
40 while %index<=(length ( b ) −1) ,
41 r=b ( 1 , 1 : $−%index ) . ∗ a ( 1 , $−length ( b )+%index +1: $ ) ;
42 R( 1 , %index+length ( a ) )=sum( r , 2 ) ;
43 %index=%index +1;
44 end
45
46 endfunction
2.65 Problem
8
4 // c r o s s correlation between received signal &
transmitted signal .
5 // Auto correlation of transmitted signal
6 // d i s t a n c e between the peeks of two plots gives the
delay
7
9 // ( b )
10
11 // t r a n s m i t t e d sequence
12 x=[1 1 1 1 1 −1 −1 1 1 −1 1 −1 1 ] ;
13
14 // n o i s e sequence
15 v=grand ( 1 , 2 0 0 , ” nor ” , 0 , 0 . 0 1 ) ;
16
17 // r e c e i v e d sequence
18 y = 0 . 9 ∗ [ zeros ( 1 , 2 0 ) x zeros ( 1 , length ( v )−20−length ( x ) ) ]+v
;
19
20 // c a l c u l a t i n g r y x
21 [ r , %r]= c o r e l ( y , x , 0 , 0 ) ;
22
23 // s e l e c t i n g f i r s t 59 terms
24 r =[ r ( 1 , %r : %r+59) ] ;
25
26 // p l o t t i n g
27 plot2d3 ( r )
28 t i t l e ( ’ c r o s s c o r r e l a t i o n between r e c e i v e d s i g n a l and
transmitted signal ’ )
29 x l a b e l ( ’ r−c r o s s c o r r e l a t i o n s e q u e n c e ’ )
30
31
9
10
Figure 2.1: Problem 2.66
11
12
14
Chapter 5
Output is:
The function first finds two consecutive quantization levels within which a
particular element falls, then it checks if that element falls above or belove
corresponding decision level.
15
4
7 // l e v e l s
8 l =2ˆb ;
9
10 // s t e p size
11 s =(mx−mn) / l ;
12
13 // q u a n t i z a t i o n levels
14 q l =[mn: s : mx ] ;
15
16 // d i c e s i o n levels
17 d l =[mn+( s / 2) : s : mx ] ;
18 i n d e x =1;
19 xq = [ ] ;
20
21 while index<=length ( x )
22 count =2;
23 while count<=length ( q l )
24 i f x ( i n d e x )>q l ( count )
25 count=count +1;
26 continue
27 else
28 i f x ( i n d e x )<=d l ( count −1)
29 xq ( 1 , i n d e x )=q l ( count −1) ;
30 else
31 xq ( 1 , i n d e x )=q l ( count ) ;
32 end
33 end
34 break ;
35 end
36 i n d e x=i n d e x +1;
37 end
38
39 endfunction
16
5.14 Problem
Scilab code Prb 5.14 quantize.sce
4 b=5; / / n o o f b i t s
5 f =1/100; / / f r e q u e n c y of signal
6 // g e n e r a t i n g signal
7 n=[0:99];
8 x=[];
9 x=sin (2∗ %pi∗n∗ f ) ;
10
11 // q u a n t i z i n g
12 xq=q u a n t i z e ( x , b ,1 , −1) ;
13
26 // p l o t of xq
27 plot2d3 ( xq ) ;
17
5.4 Filter Design by placing poles and zeros
Digital filters designed in this section are all two poles and two zeros systems.
Systems designed are of the form:
b0 (1 − e−jω0 z −1 )(1 − ejω0 z −1 )
H(z) = (5.1)
(1 − ae−jω1 z −1 )(1 − aejω1 z −1 )
Here ω0 is the location of the zero on the unit circle and ω1 is the frequency
where magnitude response is one. A pole is placed at this frequency.
5.43 Problem
8 a=poly ( 0 , ”a” ) ;
9 // d e n o m i n a t o r polynomial when z=e ˆ− j f 1
10 den1=1+a ˆ4+2∗( a ˆ2 ) ∗ cos (2∗ f 2 ) −4∗a ∗( cos ( f 2 ) ) ˆ2−4∗a ˆ3∗( cos
( f 2 ) ) ˆ2+4∗( a ˆ2 ) ∗( cos ( f 2 ) ) ˆ 2 ;
11
15 // g a i n square in terms of a
16 bsq=den1 /(2+2∗ cos ( f 2 ) −6∗cos ( f 1 ) ∗ cos ( f 2 ) +4∗( cos ( f 1 ) ) ˆ2 ) ;
17 mag=mag3∗mag3 ;
18
19 // c a l c u l a t i n g value of a
20 a=roots ( bsq ∗(2+2∗ cos ( f 3 ) −6∗cos ( f 1 ) ∗ cos ( f 3 ) +4∗( cos ( f 1 ) )
ˆ 2)−mag∗denm ) ;
18
21
22 // c a l c u l a t i n g value of b0
23 b0=sqrt ( horner ( bsq , a ( 1 ) ) ) ;
24
25 // d e s i g n i n g system
26 z=poly ( 0 , ” z ” ) ;
27 n=b0 ∗( z−%eˆ ( %i∗ f 1 ) ) ∗( z−%eˆ(−%i∗ f 1 ) ) ;
28 d=(z−a ( 1 ) ∗%eˆ ( %i∗ f 2 ) ) ∗( z−a ( 1 ) ∗%eˆ(−%i∗ f 2 ) ) ;
29 h=s y s l i n ( ”d” , n ( 1 ) , d ) ;
30
31 // e v a l u a t i n g system response
32 n=[0:100];
33 w=%eˆ ( %i∗2∗ %pi∗n /100) ;
34 r e s=horner ( h ,w) ;
35
19
Chapter 8
20
Scilab code Sec 8.1
1 // f u n c t i o n to compute dft of a sequence
2 // i n p u t : x ( a row vector )
3 // o u t p u t : X ( a row vector )
4
5 function X=mydft ( x )
6
7 %n=length ( x ) ;
8 w=%eˆ(−%i∗2∗ %pi/%n) ;
9 k=0;
10
11 // f o r m a t i o n of the W matrix
12 while k<%n
13 n=0;
14 while n<%n
15 W( n+1,k+1)=wˆ ( k∗n ) ;
16 n=n+1;
17 end
18 k=k+1;
19 end
20
21 X=x∗W;
22
23 endfunction
21
• x-the sequnce (a row vector)
10 // f o r m a t i o n of l cross m matrix
11 while %m<m
12 %l=0;
13 while %l<l
14 XX( %l+1,%m+1)=x ( %l+1+%m∗ l ) ;
15 %l=%l+1;
16 end
17 %m=%m+1;
18 end
19 %l=0;
20
22
27 %l=%l+1;
28 end
29 %m= [ ] ;
30 %m=0;
31
39 // r e a d i n g elaments rowise
40 while %l<l
41 %m=0;
42 while %m<m
43 X(%m+1+%l∗m)=XX( %l+1,%m+1) ;
44 %m=%m+1;
45 end
46 %l=%l+1;
47 end
48
51 endfunction
23
• x: Input sequence (a row vector)
Output is:
5 function X=r 2 f f t ( x )
6
7 i f length ( x )==2
8 X( 1 , 1 )=x ( 1 )+x ( 2 ) ;
9 X( 1 , 2 )=x ( 1 )−x ( 2 ) ;
10 else / / d i v i d e s e q u e n c e i n t o t w o
11 n = [ 0 : ( length ( x ) / 2 ) −1];
12 x1=x ( 1 : 2 : $ ) ;
13 x2=x ( 2 : 2 : $ ) ;
14 X1=r 2 f f t ( x1 ) ;
15 X2=r 2 f f t ( x2 ) ;
16 W=%eˆ(−%i∗2∗ %pi∗n/ length ( x ) ) ; // phase factor
17 // r e c o m b i n e two sequences
18 Y1=X1+(W. ∗ X2) ;
19 Y2=X1−(W. ∗ X2) ;
20 X=[Y1 Y2 ] ;
21 end
22
23 endfunction
8.36 Problem
Scilab code Prb 8.36 (a)
24
2
3 x=zeros ( 1 , 6 4 ) ;
4 x ( 1 , 1 : 1 6 ) =1;
5 X=mydft ( x ) ;
6 x1=zeros ( 1 , 6 4 ) ;
7 x1 ( 1 , 1 : 8 ) =1;
8 X1=mydft ( x1 ) ;
9 x2 =[x zeros ( 1 , 6 4 ) ] ;
10 X2=mydft ( x2 ) ;
11 scf (1) ;
12 plot2d3 ( abs (X) ) ;
13 scf (2) ;
14 plot2d3 ( abs (X1) ) ;
15 scf (3) ;
16 plot2d3 ( abs (X2) ) ;
3 a =[0 0 . 9 ∗ 0 . 9 ] ;
4 b=[1];
5 y=i m p u l s e ( a , b , 1 2 8 ) ;
6 scf (1) ;
7 Y=mydft ( y ) ;
8 plot2d3 (Y)
9 n=[0:127];
10 w=0.8ˆ( −n ) . ∗ y ;
11 W=mydft (w) ;
12 scf (2) ;
13 plot2d3 (W) ;
14 w=0.5ˆ( −n ) . ∗ y ;
15 W=mydft (w) ;
16 scf (3) ;
17 plot2d3 (W) ;
18
25
19 // the difference between the two curves ( fig 2 and fig
3) is that of amplitude because pole is placed much
closer to unit c i r c l e in fig 2
26
Chapter 9
In this section functions are defined to get impulse response through direct
form 1 implementation and to get lattice, lattice ladder coefficients from
system function, and vice-versa.
These functions are:
27
• N: The numerator of system function (a polynomial in ’zin’ where
zin=z −1 )
Output is:
6 b=c o e f f (N) ;
7 a=c o e f f (D) ;
8 y=zeros ( 1 , l ) ;
9 w=[b zeros ( 1 , l −length ( b ) ) ] ;
10 h ( 1 )=w( 1 ) ;
11 i n d e x =2;
12 while index<=length ( a )
13 h ( 1 , i n d e x )=w( i n d e x )−a ( 2 : i n d e x ) ∗h ( 1 , index −1: −1:1) . ’ ;
14 i n d e x=i n d e x +1;
15 end
16
17 while i n d e x <=l ,
18 h ( i n d e x )=−a ( 1 , 2 : $ ) ∗h ( 1 , $ : −1: $−length ( a ) +2) . ’+w( 1 ,
index ) ;
19 i n d e x=i n d e x +1;
20 end
21
22 endfunction
28
9.2.4 Function “lattfir()”
In lattice ladder representation of FIR filters, system function Hm (z) is rep-
resented as:
Hm (z) = Am (z) (9.1)
Where Am (z), by definition, is given by
Am (z) − Km Bm (z)
Am−1 (z) = 2
(9.3)
1 − Km
Output is:
4 function k= l a t t f i r (H)
5
6 x=poly ( 0 , ”x” ) ;
7 H=horner (H, x ) ; // c o n v e r s i o n of polynomial in unknown
variable into polynomial in x
8 m=length ( c o e f f (H) ) −1;
9 A(m, 1 )=H;
10 alpha (m, : ) =c o e f f (A(m, 1 ) ) ; / / mˆ t h row of alpha
comprise of c o e f f i c i e n t s of A m
29
11 k (m)=alpha (m,m+1) ;
12 i n d e x =1;
13
25 endfunction
4 f n ( 1 , 1 ) =1;
5 g n ( 1 , 1 ) =1;
6 f n 1=zeros ( 1 , length ( k ) +1) ;
7 g n 1=zeros ( 1 , length ( k ) +1) ;
8 i =1
30
9
25 endfunction
31
• H: The system function(a fractional polynomial in z −1 )
9 // calculation of l a t t i c e c o e f f i c i e n t s
10 A( n , 1 )=H( 3 ) ;
11 alpha ( n , : ) =c o e f f (A( n , 1 ) ) ;
12 k ( n )=alpha ( n , n+1) ;
13 i n d e x =1;
14
26 temp = [ ] ;
27 temp=alpha ( n−i n d e x +1,n−i n d e x +2: −1:1) ;
28 B( n−i n d e x +1 ,1)=i n v c o e f f ( temp ) ;
32
29 m=length ( c o e f f (H( 2 ) ) ) ;
30 C(m, 1 )=H( 2 ) ;
31 gama (m, : ) =c o e f f (C(m, 1 ) ) ;
32 v (m)=gama (m,m) ;
33 i n d e x =1;
34
35 // c a l c u l a t i o n of ladder c o e f f i c i e n t s
36 while m−index >=1
37 C(m−index , 1 )=C(m−i n d e x +1 ,1)−v (m−i n d e x +1)∗B(m−index
,1) ;
38 temp=c o e f f (C(m−index , 1 ) ) ;
39 gama (m−index , 1 : m−i n d e x )=temp ( 1 :m−i n d e x ) ;
40 v (m−i n d e x )=gama (m−index ,m−i n d e x ) ;
41 i n d e x=i n d e x +1;
42 end
43
44 endfunction
Output is:
33
7 function h=l l i m p u l s e ( k , v , l )
8
9 N=length ( k )+1
10 f n ( 1 ,N) =1;
11 f n 1 ( 1 , 1 :N) =0;
12 g n ( 1 , 1 ) =1;
13 g n 1 ( 1 , 1 :N) =0;
14 i =1;
15
16 while i<=l
17 i n d e x =1;
18 while index<N
19 f n ( 1 ,N−i n d e x )=f n ( 1 ,N−i n d e x +1)−k (N−i n d e x ) ∗ g n 1
( 1 ,N−i n d e x ) ;
20 g n ( 1 ,N−i n d e x +1)=g n 1 ( 1 ,N−i n d e x )+k (N−i n d e x ) ∗ f n
( 1 ,N−i n d e x ) ;
21 i n d e x=i n d e x +1;
22 end
23 f n ( 1 ,N) =0;
24 g n ( 1 , 1 )=f n ( 1 , 1 ) ;
25 h ( i )=g n ( 1 , 1 : length ( v ) ) ∗v ;
26 g n 1=g n ;
27 f n
28 g n
29 i=i +1;
30 end
31
32 endfunction
9.40 Problem
34
4 z i n=poly ( 0 , ” z i n ” ) ;
5 N=(1 −0.08∗ z i n ∗%eˆ ( %i∗%pi /4 ) ) ∗(1 −0.08∗ z i n ∗%eˆ(−%i∗%pi /4 )
) ∗(1 −0.625∗ z i n ) ∗(1+0.25∗ z i n ) ;
6 D=(1 −0.08∗ z i n ∗%eˆ ( %i∗%pi /3 ) ) ∗(1 −0.08∗ z i n ∗%eˆ(−%i∗%pi /3 )
) ∗(1 −0.5∗ z i n ) ∗(1+0.333∗ z i n ) ;
7 H=N/D;
8
9 // ( a ) .
10 k1= l a t t f i r (N) ;
11
12 // ( b ) .
13 [ k2 v2 ]= l a t t l a d d (1 /D) ;
14
15 // ( c ) .
16 [ k3 v3 ]= l a t t l a d d (H) ;
17
18 // ( e ) .
19 h=l l i m p u l s e ( k3 , v3 , 2 5 ) ;
20 ha=l a t t i m p u l s e ( k1 ) ;
21 hb=l l i m p u l s e ( k2 , v2 , 2 5 ) ;
22
23 // ( f ) .
24 h f=convol ( ha , hb ) ;
25
26 scf (1) ;
27 plot2d3 ( h ) ;
28 scf (2) ;
29 plot2d3 ( h f ) ;
35
Chapter 10
36
• bpfir(): To design bandpass Fir filter
Inputs to the function are:
6 n = [ 1 : 1 :%n / 2 ] ;
7 hd=2∗(omegc /(2∗ %pi ) ) ∗ sin ( n∗omegc ) . / ( n∗omegc ) ;
8
9 s e l e c t wtype ,
10 case 1 then / / r e c t a n g l e w i n d o w
11 w=ones ( 1 , (%n−1) /2 ) ;
12 case 2 then / / h a n n i g
13 w=.5+.5∗ cos (2∗ %pi∗n / (%n−1) ) ;
14 case 3 then / / h a m m i n g
15 w=.54+.46∗ cos (2∗ %pi∗n / (%n−1) ) ; ;
16 case 4 then / / b l a c k m a n n
37
17 w=.42+.5∗ cos (2∗ %pi∗n / (%n−1) ) +.08∗ cos (4∗ %pi∗n / (%n
−1) ) ; ;
18 end
19
20 h=hd . ∗w;
21 hc =[h ( $ : − 1 : 1 ) omegc/%pi h ] ;
22
23 endfunction
6 n = [ 1 : 1 :%n / 2 ] ;
7 hd=−2∗(omegc /(2∗ %pi ) ) ∗ sin ( n∗omegc ) . / ( n∗omegc ) ;
8
9 s e l e c t wtype ,
10 case 1 then / / r e c t a n g l e w i n d o w
11 w=ones ( 1 , (%n−1) /2 ) ;
12 case 2 then / / h a n n i g
13 w=.5+.5∗ cos (2∗ %pi∗n / (%n−1) ) ;
14 case 3 then / / h a m m i n g
15 w=.54+.46∗ cos (2∗ %pi∗n / (%n−1) ) ; ;
16 case 4 then / / b l a c k m a n n
17 w=.42+.5∗ cos (2∗ %pi∗n / (%n−1) ) +.08∗ cos (4∗ %pi∗n / (%n
−1) ) ; ;
18 end
19
20 h=hd . ∗w;
21 hc =[h ( $ : − 1 : 1 ) 1−omegc/%pi h ] ;
22
23 endfunction
38
Scilab code Sec 10.2
1 // f u n c t i o n to generate band pass f i r
f i l t e r c o e f f i c i e n t s
2 / / i n p u t s : %n , o m e g c 1 , o m e g c 2 , w t y p e all scalars , wtype
integer in 1 −4
3
6 n = [ 1 : 1 :%n / 2 ] ;
7 hd=2∗(omegc2 /(2∗ %pi ) ) ∗ sin ( n∗omegc2 ) . / ( n∗omegc2 ) −2∗(
omegc1 /(2∗ %pi ) ) ∗ sin ( n∗omegc1 ) . / ( n∗omegc1 ) ;
8
9 s e l e c t wtype ,
10 case 1 then / / r e c t a n g l e w i n d o w
11 w=ones ( 1 , (%n−1) /2 ) ;
12 case 2 then / / h a n n i g
13 w=.5+.5∗ cos (2∗ %pi∗n / (%n−1) ) ;
14 case 3 then / / h a m m i n g
15 w=.54+.46∗ cos (2∗ %pi∗n / (%n−1) ) ; ;
16 case 4 then / / b l a c k m a n n
17 w=.42+.5∗ cos (2∗ %pi∗n / (%n−1) ) +.08∗ cos (4∗ %pi∗n / (%n
−1) ) ; ;
18 end
19
20 h=hd . ∗w;
21 hc =[h ( $ : − 1 : 1 ) ( omegc2−omegc1 ) /%pi h ] ;
22
23 endfunction
6 n = [ 1 : 1 :%n / 2 ] ;
39
7 hd=−2∗(omegc2 /(2∗ %pi ) ) ∗ sin ( n∗omegc2 ) . / ( n∗omegc2 ) +2∗(
omegc1 /(2∗ %pi ) ) ∗ sin ( n∗omegc1 ) . / ( n∗omegc1 ) ;
8
9 s e l e c t wtype ,
10 case 1 then / / r e c t a n g l e w i n d o w
11 w=ones ( 1 , (%n−1) /2 ) ;
12 case 2 then / / h a n n i g
13 w=.5+.5∗ cos (2∗ %pi∗n / (%n−1) ) ;
14 case 3 then / / h a m m i n g
15 w=.54+.46∗ cos (2∗ %pi∗n / (%n−1) ) ; ;
16 case 4 then / / b l a c k m a n n
17 w=.42+.5∗ cos (2∗ %pi∗n / (%n−1) ) +.08∗ cos (4∗ %pi∗n / (%n
−1) ) ;
18 end
19
20 h=hd . ∗w;
21 hc =[h ( $ : − 1 : 1 ) 1 −((omegc2−omegc1 ) /%pi ) h ] ;
22
23 endfunction
40
• butterd(): Function to design digital Butterworth filter using Bilinear
Transformation.
7 Ep=(1/( r i p p b ˆ 2) ) −1;
8 Es =(1/( r i p s b ˆ 2 ) ) −1;
9 n=log ( Es/Ep) /(2∗ log ( omegs /omegp ) ) ; / / o r d e r of f i l t e r
10 n=b i n 2 d e c ( d e c 2 b i n ( n ) ) +1; / / a p p r o x i m a t i o n o f f i l t e r to
integer
11 omegc=omegs / ( Es ˆ ( 1 / ( 2 ∗ n ) ) ) ; // cut − o f f frequency
12
13 endfunction
41
• rip sb: stop band ripple (a scalar)
7 Es =(1/( r i p s b ˆ 2 ) ) −1;
8 n=log ( Es ) /(2∗ log ( omegs /omegc ) ) ; / / o r d e r of f i l t e r
9 n=b i n 2 d e c ( d e c 2 b i n ( n ) ) +1; / / a p p r o x i m a t i o n of order to
integer
10
11 endfunction
Output is:
7 n = [ 0 : 1 :%n− 1 ] ;
42
8 p=omegc∗%eˆ ( %i∗%pi / 2 ) ∗%eˆ ( %i∗%pi ∗(2∗ n+1) /(2∗%n) ) ; //
poles of f i l t e r
9 a=poly ( 0 , ” s ” ) ;
10 G=1;
11 i n d e x =1;
12
18 endfunction
43
15 end
16
17 b=poly ( 0 , ” z ” ) ;
18 s =(2) ∗( b−1) / ( b+1) ; // b i l i n e a r transformation
19 H=horner (G, s ) ;
20
21 endfunction
44
Solved examples from book’s
appendix
1 // Example Problem no . 16
2
3 f p =250;
4 f s =1000;
5 omegp=2∗%pi∗ f p / f s ;
6 hr= l p f i r ( 1 1 , omegp , 1 )
7 hhan= l p f i r ( 1 1 , omegp , 2 )
8 hham= l p f i r ( 1 1 , omegp , 3 )
9 h b l a c k= l p f i r ( 1 1 , omegp , 4 )
1 // Example Problem no . 17
2
3 f c =250;
4 f s =1000;
5 omegp=2∗%pi∗ f p / f s ;
6 hr=h p f i r ( 7 , omegp , 1 )
7 hhan=h p f i r ( 7 , omegp , 2 )
8 hham=h p f i r ( 7 , omegp , 3 )
9 h b l a c k=h p f i r ( 7 , omegp , 4 )
45
1 // Example Problem no . 18
2
3 f c 1 =100;
4 f c 2 =200;
5 f s =1000;
6 omegc1=2∗%pi∗ f c 1 / f s ;
7 omegc2=2∗%pi∗ f c 2 / f s ;
8 hr=b p f i r ( 9 , omegc1 , omegc2 , 1 )
9 hhan=b p f i r ( 9 , omegc1 , omegc2 , 2 )
10 hham=b p f i r ( 9 , omegc1 , omegc2 , 3 )
11 h b l a c k=b p f i r ( 9 , omegc1 , omegc2 , 4 )
4 r i p p b =10ˆ( −1.25/20) ;
5 r i p s b =10ˆ( −15/20) ;
6 fpb =200;
7 f s b =300;
8 f s =2000;
9 omegs=2∗%pi∗ f s b / f s ;
10 omegp=2∗%pi∗ fpb / f s ;
11 omegs pw=2∗tan ( omegs /2 ) ; / / p r e w a r p e d f r e q u e n c y
12 omegp pw=2∗tan ( omegp / 2) ; / / p r e w a r p e d f r e q u e n c y
13 [ n , omegc]= b u t t o r d c ( omegp pw , r i p p b , omegs pw , r i p s b ) ; //
order and cut − o f f frequency
14 s y s f u n=b u t t e r d ( n , omegc ) // d i g i t a l system
4 f l =200;
46
5 f u =300;
6 f s =1000;
7 omegl=2∗%pi∗ f l / f s ;
8 omegu=2∗ f u ∗%pi/ f s ;
9 omegl pw=2∗tan ( omegl / 2) ; / / prewrped frequency
10 omegu pw=2∗tan ( omegu / 2) ; / / prewrped frequency
11 G=b u t t e r c ( 1 , 1 , 1 ) ; / / a n a l o g lowpass f i l t e r with cut − o f f
1 order 1
12 a=poly ( 0 , ” s ” ) ;
13 s =(aˆ2+(omegu pw∗ omegl pw ) ) / ( a ∗( omegu pw−omegl pw ) ) ; //
Transformation
14 G=horner (G, s ) // a n a l o g band − p a s s f i l t e r
47