Vous êtes sur la page 1sur 53

Scilab Codes for

Digital Signal Processing


by Proakis and Manolakis1

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

1 Funded by a grant from the National Mission on Education through ICT,


http://spoken-tutorial.org/NMEICT-Intro
Book Details

Authors: Proakis and Manolakis

Title: Digital Signal Processing: Principle, algorithms and applications

Publisher: Dorling Kindersley India Pvt. Ltd.


(licensees of Prentice Education in South Asia)

Edition: 4th

Year: 2007

Place: New Delhi

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.

Exa Example (Solved example)

Sec Section (Particular section of the above book)

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

List of Scilab Codes 4

2 Impulse Response and Correlation 2


2.4 Impulse response . . . . . . . . . . . . . . . . . . . . . . . . 2
2.66 Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2.67 Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.6 Correlation . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.65 Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8

5 Quantization and Filter Design by placing poles and zeros 15


5.1 Function “quantize()” . . . . . . . . . . . . . . . . . . . . . 15
5.14 Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
5.4 Filter Design by placing poles and zeros . . . . . . . . . . . 18
5.43 Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18

8 The discrete fourier transform (DFT) and the fast fourier


transform (FFT) 20
8.1 Function “mydft()” . . . . . . . . . . . . . . . . . . . . . . . 20
8.2 Function “divdft()” . . . . . . . . . . . . . . . . . . . . . . . 21
8.3 Function “r2fft()” . . . . . . . . . . . . . . . . . . . . . . . . 23
8.36 Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24

9 Implementation of FIR and IIR filters 27


9.2 Function “direct1()”, ”lattfir()” and ”lattimpulse()” . . . . . 27
9.2.1 Function ”direct()” . . . . . . . . . . . . . . . . . . . 27
9.2.4 Function “lattfir()” . . . . . . . . . . . . . . . . . . . 29
9.3 Function ”lattimpulse()” . . . . . . . . . . . . . . . . . . . . 30
9.4 Function “lattladd()” and ”llimpulse()” . . . . . . . . . . . . 31

2
9.40 Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34

10 FIR and IIR Filter Design 36


10.2 FIR filter design by window method . . . . . . . . . . . . . . 36
10.3 IIR fiter design by Butterworth filter design and Bi-linear
Transformation . . . . . . . . . . . . . . . . . . . . . . . . . 40

Solved examples from book’s appendix 45

3
List of Scilab Codes

Sec 2.4 Impulse.sce . . . . . . . . . . . . . . . . . . . . . . . . 2


Prb 2.66 Impulse.sce . . . . . . . . . . . . . . . . . . . . . . . . 3
Prb 2.67 Problem 2.67 . . . . . . . . . . . . . . . . . . . . . . . 5
Sec 2.6 Correlation . . . . . . . . . . . . . . . . . . . . . . . . 7
Prb 2.65 Problem 2.65 . . . . . . . . . . . . . . . . . . . . . . . 8
Sec 5.14 quantize.sce . . . . . . . . . . . . . . . . . . . . . . . . 15
Prb 5.14 Problem 5.14 . . . . . . . . . . . . . . . . . . . . . . . 17
Prb 5.43 5.43.sce . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Sec 8.1 mydft.sce . . . . . . . . . . . . . . . . . . . . . . . . . 20
Sec 8.2 divdft.sce . . . . . . . . . . . . . . . . . . . . . . . . . 22
Sec 8.3 r2fft.sce . . . . . . . . . . . . . . . . . . . . . . . . . . 24
Prb 8.36 Problem 8.36 . . . . . . . . . . . . . . . . . . . . . . . 24
Prb 8.36 Problem 8.36 . . . . . . . . . . . . . . . . . . . . . . . 25
Sec 9.2 direct1.sce . . . . . . . . . . . . . . . . . . . . . . . . 28
Sec 9.2 lattfir.sce . . . . . . . . . . . . . . . . . . . . . . . . . 29
Sec 9.2 lattimpulse.sce . . . . . . . . . . . . . . . . . . . . . . 30
Sec 9.3 lattladd.sce . . . . . . . . . . . . . . . . . . . . . . . . 32
Sec 9.3 llimpulse.sce . . . . . . . . . . . . . . . . . . . . . . . 33
Prb 9.40 Problem . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Sec 10.2 Low pass FIR filter . . . . . . . . . . . . . . . . . . . . 37
Sec 10.2 High pass FIR filter . . . . . . . . . . . . . . . . . . . 38
Sec 10.2 Bandpass FIR filter . . . . . . . . . . . . . . . . . . . 38
Sec 10.2 Bandstop FIR filter . . . . . . . . . . . . . . . . . . . 39
Sec 10.3 buttordc.sce . . . . . . . . . . . . . . . . . . . . . . . 41
Sec 10.3 buttord.sce . . . . . . . . . . . . . . . . . . . . . . . . 42
Sec 10.3 butterc.sce . . . . . . . . . . . . . . . . . . . . . . . . 42
Sec 10.3 butterd.sce . . . . . . . . . . . . . . . . . . . . . . . . 43
Exa 16 Example 16, page 1098 . . . . . . . . . . . . . . . . . 45

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

2.1 Problem 2.66 . . . . . . . . . . . . . . . . . . . . . . . . . . 11


2.2 Problem 2.66 . . . . . . . . . . . . . . . . . . . . . . . . . . 12
2.3 Problem 2.66 . . . . . . . . . . . . . . . . . . . . . . . . . . 13
2.4 Problem 2.65 . . . . . . . . . . . . . . . . . . . . . . . . . . 14

1
Chapter 2

Impulse Response and


Correlation

2.4 Impulse response


When unit impulse is fed as input, the output of the system is called as the
impulse response of the system. It is usually denoted by h(n). This function
calculates the impulse response of system described by following difference
equation:

y(n) = a1 y(n−1)+a2 y(n−2)+a3 y(n−3)...+b0 x(n)+b1 x(n−1)+b2 x(n−2)...


(2.1)
This function takes following parameters as arguments:

• a-array of a1 , a2 ... (a row vector)

• b-array of b0 , b1 , b2 ... (a row vector)

• n-size of impulse response (a scalar)

it is assumed that size of a is greater than size of b

Scilab code Sec 2.4


1 // f u n c t i o n to cala impulse response of
given diff eq .
2 // i n p u t s a , b ( row vectors ) and %n ( s c a l a r )
3

2
4 function h=i m p u l s e ( a , b ,%n)
5

6 %a=length ( a ) ;
7

8 // zero − p a d d i n g in order to make element wise summation


compatible
9 [ b ] = [ b , zeros ( 1 ,%n−length ( b ) ) ] ;
10 h=[ zeros ( 1 , 1 ) ] ;
11 h ( 1 , 1 )=b ( 1 , 1 ) ;
12 %index =2;
13

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

20 // c o m p u t a t i o n of terms after f i r s t %a terms


21 while %index <= %n,
22 h ( 1 , %index )=−a ( 1 , 1 : $ ) ∗h ( 1 , $ : −1: $−%a+1) . ’+ b ( 1 , %index ) ;
23 %index=%index +1;
24 end
25

26 endfunction

2.66 Problem

Scilab code Prb 2.66


1 // Problem 2.66 Digital Signal
Processing by Proakis Manolakis Fourth Edition
2 a =[ −0.8 , 0 . 6 4 ] ;
3 b=[0.866];
4

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

21 // c o m p u t a t i o n of terms after f i r s t %a terms


22 while %index <= %n,
23 h ( 1 , %index )=−a ( 1 , 1 : $ ) ∗h ( 1 , $ : −1: $−%a+1) . ’+ b ( 1 , %index
);
24 %index=%index +1;
25 end
26

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

Scilab code Prb 2.67


1 // Problem 2.67 Digital Signal
Processing by Proakis and Manolakis Fourth Edition
2 // f i l e to be executed :− impulse . sce
3

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

10 // system 1 &2 are in se ri es


11 h12=convol ( h1 , h2 ) ;
12

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=−∞

The cross-correlation function of two sequences y and x is denoted by ryx


and is defined by the relation:

X
ryx (l) = y(n)x(n − l), l ∈ (−∞, ∞) (2.3)
n=−∞

From equations (1) and (2) it can be deduced that:

rxy (l) = ryx (−l) (2.4)

Inputs to the function ’corel()’ are:

• x-First sequence (a row vector).

• y-Second sequence (a row vector).

• %x-Index of n=0 for first sequence (a scalar).

• %y-Index of n=0 for second sequence (a scalar).

The output of the function are:

• r-Sequence of cross-correlation between x and y (a row vector).

• %r-Index of l=0 for above sequence(a scalar).

The cross-correlation is actually calculated using function cor(). This func-


tion calculates cross-correlation of two sequences subjected to constrain that
the second sequence(y) is smaller than first(x). If length of x is greater than
or equal to y corel() sends x as first argument and y as second argument to

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)

• y-Second sequence (a row vector)


The output of the function is:
• r-The cross-correlation sequence (a row vector)
The lth term of cross-correlation of two sequences is calculated first by
shifting second sequence by l units.

Scilab code Sec 2.6


1 // f u n c t i o n to calculate correlation between signals
2 // i n p u t s : −x , y two row vectors , %x , % y two scalars
3 // o u t p u t s : −R row v e c t o r , %R scalar
4

5 function [ R,%R]= c o r e l ( x , y , %x, %y)


6

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

22 R=[ zeros ( 1 , length ( a )+length ( b ) −1) ] ;


23 %r=length ( b ) −1;
24

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

Scilab code Prb 2.65


1 // Problem 2.65 d i g i t a l signal
processing by Proakis and Manolakis Fourth edition
2 // f i l e to be executed :− corel . sce
3 // the delay ’d ’ can be estimated by calculating

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

32 // Peak of this function is at n =20 which is expected


33 // when value of variance is increased in line no 13 (
fourth argument of function grand () ) the peak
becomes less dominant
34 // hence the value of delay can be estimated properly
only if variance of noice is kept low .

9
10
Figure 2.1: Problem 2.66

11
12

Figure 2.2: Problem 2.66


13

Figure 2.3: Problem 2.66


Figure 2.4: Problem 2.65

14
Chapter 5

Quantization and Filter Design


by placing poles and zeros

5.1 Function “quantize()”


In the code presented here, the given sequence is quantized to desired number
of bits. Inputs to the function ’quantize()’ are:

• x-the sequence to be quantized (a row vector)

• b-no. of bits (a scalar)

• max-maximum value in given sequence (a scalar)

• min- minimum value in given sequence (a scalar)

Output is:

• xq-the quantized sequence (a row vector).

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.

Scilab code Sec 5.14


1 // f u n c t i o n to quantize given sequence
2 // i n p u t s : x , ( a row vector ) ; b , mx , mn ( all scalars )
3 // o u t p u t : xq , ( a row vector )

15
4

5 function xq=q u a n t i z e ( x , b , mx,mn)


6

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

1 // Problem 5.14 Digital Signal Processing by Proakis


Manolakis Fourth Edition
2 // f i l e to be executed : quantize . sci
3

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

14 // c a l c u l a t i n g total harmonic distruction for 4 ,6 ,8 ,16


bits
15 y=[ q u a n t i z e ( x , 4 ) ; q u a n t i z e ( x , 6 ) ; q u a n t i z e ( x , 8 ) ; q u a n t i z e ( x
,16) ] ;
16 w=%eˆ(−5∗%i∗2∗ %pi∗n /100) ;
17 c=f ∗(w∗y ’ ) ;
18 ysq=y∗y ’ ;
19 i =1;
20 while i <=4
21 p ( 1 , i )=f ∗( ysq ( i , i ) ) ;
22 THD=1−2∗( r e a l ( c ( 1 , i ) ) ˆ2+imag ( c ( 1 , i ) ) ˆ 2) /p ( 1 , i )
23 i=i +1;
24 end
25

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

Scilab code Prb 5.43


1 // Problem 5.43 Digital Signal
Processing by Proakis Manolakis Fourth Edition
2

3 f 1=%pi ∗ 1 2 0 / 5 0 0 ; / / freq where zero is present


4 f 2=%pi ∗ 4 0 0 / 5 0 0 ; / / freq where pole is present , magnitude
=1
5 f 3=%pi ∗ 3 0 0 / 5 0 0 ; / / s o m e intermidiate frequency
6 mag3 = 0 . 5 ; / / m a g n i t u d e at f3
7

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

12 // d e n o m i n a t o r polynomial when z=e ˆ− j f 3


13 denm=1+a ˆ4+2∗( a ˆ2 ) ∗ cos (2∗ f 3 ) −4∗a ∗( cos ( f 2 ) ) ∗( cos ( f 3 ) ) −4∗
a ˆ3∗( cos ( f 2 ) ) ∗( cos ( f 3 ) ) +4∗(a ˆ 2) ∗( cos ( f 2 ) ) ˆ 2 ;
14

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

36 plot2d3 ( sqrt ( r e a l ( r e s ) ˆ2+imag ( r e s ) ˆ 2) )

19
Chapter 8

The discrete fourier transform


(DFT) and the fast fourier
transform (FFT)

In this section functions are defined to calculate discrete fourier transform of


given sequence.
These functions are:-

• mydft()-This function calculates the dft by its definition.

• divdft()-Calculation of dft by divide and conquer approach.

• r2fft()-Calculation of dft by radix 2 method.

8.1 Function “mydft()”


This function calculates the dft of a sequence by its definition and therefore
not the efficient manner to calculate the dft.
The input to the function is:

• x:-The sequence of which the dft is to be calculated (a row vector)

The output of the function is:

• X:-The dft of the sequence (a row vector)

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

8.2 Function “divdft()”


This function calculates the dft by divide and conquer approach.The length
of the sequence should be compulsorily m times l. The actual sequence is
first arranged column-wise in ’l’ rows and ’m’ columns to form a matrix, then
the ’m’ point dft of each row is calculated. Each element of this calculated
dft is multiplied by an appropriate phase factor and again ’l’ point dft of
each column of resultant matrix is calculated using function mydft(). The
matrix obtained when read row-wise gives the dft.
This concept is explained in detail in Digital Signal Processing by Proakis
and Manolakis Fourth Edition.
The inputs to the function are:

21
• x-the sequnce (a row vector)

• l-no of columns (a scalar)

• m-no of rows (a scalar)

The output of the function is:

• X-DFT of given sequence (row vector)

Scilab code Sec 8.2


1 // f u n c t i o n to calculate fft by divide
and conquer approach
2 // i n p u t s : x ( a row vector ) and m, l ( scalars )
3 // o u t p u t : X ( a row vector )
4

5 function X=d i v d f t ( x , l ,m)


6

7 w=%eˆ(−%i∗2∗ %pi / ( l ∗m) ) ;


8 %m=0;
9

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

21 // dft ’ s of row vectors and multiplying with phase


factor
22 while %l<l
23 %m= [ ] ;
24 %m= [ 0 :m− 1 ] ;
25 XX( %l + 1 , : )=mydft (XX( %l + 1 , : ) ) ;
26 XX( %l + 1 , : )=XX( %l + 1 , : ) . ∗wˆ ( %l∗%m) ;

22
27 %l=%l+1;
28 end
29 %m= [ ] ;
30 %m=0;
31

32 // dft ’ s of column vectors


33 while %m<m
34 XX( : ,%m+1)=(mydft (XX( : ,%m+1) . ’ ) ) . ’ ;
35 %m=%m+1;
36 end
37 %l=0;
38

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

49 X=X ’ ; // c o n v e r s i o n from column vector to row vector


50

51 endfunction

8.3 Function “r2fft()”


This function calculates the dft of sequence of length 2r̂ only. If the original
sequence is not of appropriate length it has to be made so by zero padding.
This algorithm is also based on divide and conquer approach.The incoming
sequence is divided into two comprising of odd and even terms.these two
new sequences is again divided into two.This continues till we get a two
point sequence.The two point dft of these sequences is calculated which is
simply the sum and difference of two points.These two point dft‘s are clubbed
together to form the dft of original sequence.
Input to the function is:

23
• x: Input sequence (a row vector)

Output is:

• X: The dft of the input sequence (a row vector)

Scilab code Sec 8.3


1 // f u n c t i o n to calculate dft by radix 2 method
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=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)

1 // Problem 8.36 Digtal Signal Processing by Proakis


Manolakis Fourth Edition

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) ) ;

Scilab code Prb 8.36 (c)

1 // Problem 8.36 Digtal Signal Processing by Proakis


Manolakis Fourth Edition
2

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

Implementation of FIR and IIR


filters

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:

• direct1(): to calculate impulse response through direct form 1 imple-


mentation

• lattfir(): to get lattice coefficients of FIR filter

• lattimpilse(): to get the impulse response from lattice structure

• lattladd(): to get lattice ladder coefficients of an IIR filter

• llimpulse(): to get impulse response of an IIR through lattice ladder


implementation

9.2 Function “direct1()”, ”lattfir()” and ”lattim-


pulse()”
9.2.1 Function ”direct()”
Inputs to the function are:

27
• N: The numerator of system function (a polynomial in ’zin’ where
zin=z −1 )

• D: The denominator (a polynomial in ’zin’ where zin=z −1 )

• l: length of impulse response (a scalar)

Output is:

• h: The impulse response (a row vector)

Scilab code Sec 9.2


1 // f u n c t i o n which takes the system
polynomial as numerator and denominator
2 // and computes impulse reponse by direct form 1
implementation
3

4 function h=d i r e c t 1 (N, D, l )


5

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) = 1 + Σαm (k)z −k (9.2)

Here m represents the degree of polynomial. Lower order polynomials in A


can be calculated using recursive formula

Am (z) − Km Bm (z)
Am−1 (z) = 2
(9.3)
1 − Km

Where Km is the lattice coefficient and is equal to coefficient of z −m in Am .


Bm is just the reverse polynomial of Am .
Input to the function is:

• H: The system function of FIR filter (a polynomial in ’zin’ where


zin=z −1 )

Output is:

• k: The lattice coefficients(a column vector)

Scilab code Sec 9.2


1 // f u n c t i o n to get l a t t i c e parameters
for given f i r f i l t e r
2 // i n p u t : H, a polynomial
3

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

14 while m−index >=1


15 temp = [ ] ;
16 temp=alpha (m−i n d e x +1,m−i n d e x +2: −1:1) ;
17 B(m−i n d e x +1 ,1)=i n v c o e f f ( temp ) ; / / c a l c B m which is
just the reverse polynomial of A m
18 A(m−index , 1 ) =(A(m−i n d e x +1 ,1)−k (m−i n d e x +1)∗B(m−i n d e x
+1 ,1) ) /(1−k (m−i n d e x +1) ˆ 2 ) ;
19 temp1=c o e f f (A(m−index , 1 ) ) ;
20 alpha (m−index , 1 : m−i n d e x +1)=temp1 ( 1 :m−i n d e x +1) ;
21 k (m−i n d e x )=alpha (m−index ,m−i n d e x +1) ;
22 i n d e x=i n d e x +1;
23 end
24

25 endfunction

9.3 Function ”lattimpulse()”


following are the input parameters:

• k: The lattice coefficients

It gives the following as output:

• h: The impulse reponse (a row vector)

Scilab code Sec 9.2


1 // f u n c t i o n to find the response of
system given l a t t i c e parameters
2 function h=l a t t i m p u l s e ( k )
3

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

10 while i<=length ( k )+1


11 i n d e x =2;
12 while index<=length ( k )+1
13 f n ( 1 , i n d e x )=f n ( 1 , index −1)+(k ( index −1)∗ g n 1 ( 1 ,
index −1) ) ;
14 g n ( 1 , i n d e x ) =(k ( index −1)∗ f n ( 1 , index −1) )+g n 1 ( 1 ,
index −1) ;
15 i n d e x=i n d e x +1;
16 end
17 h ( i )=f n ( 1 , $ ) ;
18 f n 1=f n
19 g n 1=g n
20 f n ( 1 , 1 ) =0;
21 g n ( 1 , 1 ) =0;
22 i=i +1;
23 end
24

25 endfunction

9.4 Function “lattladd()” and ”llimpulse()”


The lattice coefficients of all poles system are similar to that of all zero system
but system is implemented in just reverse order. A pole zero system can be
synthesized from an all pole system by taking out, put as weighted sum of g
functions of lattice structure of all zero system. These weights are calculated
as follows:
The System function of pole zero system is represented as:
CM (z)
H(z) = (9.4)
AN (z)
Where M and N represents degree of numerator and denominator respec-
tively. Lower order polynomials of C are computed by formula:
Cm−1 = Cm − vm Bm (9.5)
Here vm represents ladder coefficient of mth stage. It is equal to coefficient
of z −m in Cm .
Following are the input parameters:

31
• H: The system function(a fractional polynomial in z −1 )

It gives the following as output:

• k: The lattice coefficients (a column vector)

• v: The ladder coefficients (a column vector)

Scilab code Sec 9.3


1 // f u n c t i o n to get l a t t i c e ladder
parameters for given i i r f i l t e r
2 // i n p u t : H, a f r a c t i o n a l polynomial in z ˆ −1
3

4 function [ k , v]= l a t t l a d d (H)


5 x=poly ( 0 , ”x” ) ;
6 H=horner (H, x ) ;
7 n=length ( c o e f f (H( 3 ) ) ) −1;
8

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

15 while n−index >=1


16 temp = [ ] ;
17 temp=alpha ( n−i n d e x +1,n−i n d e x +2: −1:1) ;
18 B( n−i n d e x +1 ,1)=i n v c o e f f ( temp ) ;
19 A( n−index , 1 ) =(A( n−i n d e x +1 ,1)−k ( n−i n d e x +1)∗B( n−
i n d e x +1 ,1) ) /(1−k ( n−i n d e x +1) ˆ 2 ) ;
20 temp1=c o e f f (A( n−index , 1 ) ) ;
21 alpha ( n−index , 1 : n−i n d e x +1)=temp1 ( 1 : n−i n d e x +1) ;
22 k ( n−i n d e x )=alpha ( n−index , n−i n d e x +1) ;
23 i n d e x=i n d e x +1;
24 end
25

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

Input parameters are:

• k: Lattice coefficients(a column vector)

• v: Ladder coefficients(a column vector)

• l: Length of impulse response

Output is:

• h: The impulse response

Scilab code Sec 9.3


1 // f u n c t i o n to calcuate impulse
response of an l a t t i c e ladder f i l t e r
2 // i n p u t s : k , v ( column vectors ) , l ( scalar )
3 // f n and g n represents present states of l a t t i c e
functions
4 // f n 1 and g n 1 represents previous states of l a t t i c e
functions
5 // n ˆ th term in above arrays represnts nˆ th stage
6

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

Scilab code Prb 9.40


1 // Problem 9.40 Digital Signal
Processing by Proakis and Manolakis Fourth Edition
2 // f i l e s to be executed : l a t t f i r . sci , lattladd . sci ,
llimpulse .
3

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

FIR and IIR Filter Design

10.2 FIR filter design by window method


In this section codes are presented to design FIR low-pass, high-pass,band-
pass, and band-stop filters. The desired impulse response is calculated by
taking inverse fourier transform of expected spectrum. The resultant IIR is
converted into FIR by multiplying it by suitable window. The resultant FIR
is made causal by shifting it by (n-1)/2 units. Where n is the length of the
FIR coefficients
The functions are:
• lpfir(): To design lowpass FIR filter
Inputs to the function are:

– n: Length of filter (a scalar-odd integer)


– omegac: Cut-off frequency(a scalar)
– wtype: Window type 1 for rectangle, 2 for hanning, 3 for ham-
ming, 4 for blackmann

• hpfir(): To design highpass FIR filter


Input to the function are:

– n: Length of filter(a scalar-odd integer)


– omegac: Cut-off frequency(a scalar)
– wtype: Window type 1 for rectangle, 2 for hanning, 3 for ham-
ming, 4 for blackmann

36
• bpfir(): To design bandpass Fir filter
Inputs to the function are:

– n: Length of filter (a scalar-odd integer)


– omegac1: First cut-off frequency (a scalar)
– omegac2: Second cut-off frequency (a scalar)
– wtype: Window type 1 for rectangle, 2 for hanning, 3 for ham-
ming, 4 for blackmann

• bsfir(): To design bandstop FIR filter


Inputs to the function are:

– n: Length of filter (a scalar)


– omegac1: First cut-off frequency (a scalar)
– omegac2: Second cut-off frequency (a scalar)
– wtype: Window type 1 for rectangle, 2 for hanning, 3 for ham-
ming, 4 for blackmann

Scilab code Sec 10.2


1 // f u n c t i o n to generate low 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 , w t y p e all scalars , wtype integer in
1 −4
3

4 function hc= l p f i r (%n, omegc , wtype )


5

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

Scilab code Sec 10.2


1 // f u n c t i o n to generate high 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 , w t y p e all scalars , wtype integer in
1 −4
3

4 function hc=h p f i r (%n, omegc , wtype )


5

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

4 function hc=b p f i r (%n, omegc1 , omegc2 , wtype )


5

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

Scilab code Sec 10.2


1 // f u n c t i o n to generate band stop 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

4 function hc=b s f i r (%n, omegc1 , omegc2 , wtype )


5

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

10.3 IIR fiter design by Butterworth filter


design and Bi-linear Transformation
Functions defined in this section are used to design Butterworth filters in
continuous time and discrete time. A Butterworth filter can be completely
defined if its pass band and stop band ripple,and its passband and stop
band edge frequencies are known. First the order and cutt-off frequency of
filteris calculated using these four parameters. Once these are known position
of poles in s-plane can be obtained. This completes design procedure in
continuous time. After this the digital filter can be obtained by applying
Bilinear Tranformation.
The functions which are used for these purpose are:
• buttordc(): Function to calculate order of filter and its cut-off frequency

• buttord(): Function to calculate order of filter

• butterc(): Function to design analog Butterworth filter

40
• butterd(): Function to design digital Butterworth filter using Bilinear
Transformation.

Inputs to the function are:

• omegp: Pass band edge frequency (a scalar)

• rip pb: Pass band ripple (a scalar)

• omegs: Stopband edge frequency (a scalar)

• rip sb: Stop band ripple (a scalar)

The out put of the function are:

• n: Order of filter (a scalar-integer)

• omegc: Cut-off frequency(a scalar)

Scilab code Sec 10.3


1 // B u t t e r w o r t h f i l t e r design
2 // f u n c t i o n to calculate order and cut − o f f frequency of
f i l t e r
3 // I n p u t s : omegp , r i p p b , omegs , r i p sb all scalars
4

5 function [ n , omegc]= b u t t o r d c ( omegp , r i p p b , omegs , r i p s b )


6

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

Inputs to the function are:

• omegp: cut-off frequency (a scalar)

• omegs: stopband edge frequency (a scalar)

41
• rip sb: stop band ripple (a scalar)

following are the output parameters of the function

• n: order of filter (a scalar)

Scilab code Sec 10.3


1 // B u t t e r w o r t h f i l t e r design
2 // f u n c t i o n to calculate order of f i l t e r when cut − o f f
frequency is known
3 // I n p u t : omegc , omegs , rip sb all scalars
4

5 function n=b u t t o r d ( omegc , omegs , r i p s b )


6

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

Inputs to the function are:

• n: order of filter (a scalar-integer)

• omegp: cut-off frequency (a scalar-prewarped frequency)

Output is:

• G: system function in s domain (a polynomial in s)

Scilab code Sec 10.3


1 // B u t t e r w o r t h f i l t e r design
2 // f u n c t i o n to find transfer function given the order
and cut − o f f frequency
3 // I p u t : %n , o m e g c both scalars
4

5 function G=b u t t e r c (%n, omegc )


6

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

13 while index<=%n / / g e n e r a t i n g the system function


14 G=G/ ( a−p ( i n d e x ) ) ;
15 i n d e x=i n d e x +1;
16 end
17

18 endfunction

Inputs to the function are:


• n: order of filter (a scalar-integer)

• omegp: cut-off frequency(a scalar-prewarped frequency)


Output is:
• H: system function in Z domain (a polynomial in ’z’,in fractional form)

Scilab code Sec 10.3


1 // B u t t e r w o r t h f i l t e r design
2 // f u n c t i o n to find transfer function given the order
and cut − o f f frequency
3 // I n p u t : %n , o m e g c both scalars
4

5 function H=b u t t e r d (%n, omegc )


6 n = [ 0 : 1 :%n− 1 ] ;
7 p=omegc∗%eˆ ( %i∗%pi / 2 ) ∗%eˆ ( %i∗%pi ∗(2∗ n+1) /(2∗%n) ) ; //
poles of f i l t e r
8 a=poly ( 0 , ” s ” ) ;
9 G=1;
10 i n d e x =1;
11

12 while index<=%n / / g e n e r a t i n g system function


13 G=G/ ( a−p ( i n d e x ) ) ;
14 i n d e x=i n d e x +1;

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

Scilab code Exa 16 Example 16, page no. 1098

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 )

Scilab code Exa 17 Example 17, page no. 1103

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 )

Scilab code Exa 18 Example 18, page no. 1104

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 )

Scilab code Exa 25 Example 25, page no. 1114

1 // S o l v e d Example No . 2 5 Digital Signal Processing


Proakis Manolakis Fourth Edition
2 // F i l e s to be executed : b u t t o r d c . sce , butterd . sce
3

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

Scilab code Exa 25 Example 29, page no. 1120

1 // S o l v e d Example No . 2 9 Digital Signal Processing by


Proakis Manolakis Fourth Edition
2 // F i l e to be executed : butterc . sce
3

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

Vous aimerez peut-être aussi