Digital Signal Processing - A Computer Based Approach - S. K. Mitra
Digital Signal Processing - A Computer Based Approach - S. K. Mitra
Digital Signal Processing - A Computer Based Approach - S. K. Mitra
Created by
Sanjeev Irny
B.Tech
Instrumentation Engineering
Shri Gurugobind Singhji Institute of Engg. & Tech.
College Teacher
Dr. B. M. Patre
Cross-Checked by
Author: S. K. Mitra
Edition: 3
Year: 2008
ISBN: 978-0-07-066756-3
1
Scilab numbering policy used in this document and the relation to the
above book.
For example, Exa 3.51 means solved example 3.51 of this book. Sec 2.3 means
a scilab code whose theory is explained in Section 2.3 of the book.
2
Contents
6 z Transform 42
3
14 Applications of Digital Signal Processing 84
4
List of Scilab Codes
5
Exa 4.6 Order of Analog filter . . . . . . . . . . . . 26
Exa 4.7 Order of Analog Chebyshev Filter . . . . . . 27
Exa 4.8 Order of Analog Lowpass Elliptic Filter . . 28
Exa 4.16 Design of Analof Butterworth HP Filter . . 28
Exa 5.1 DFT computation . . . . . . . . . . . . . . 30
Exa 5.2 DFT of sinusoidal sequence . . . . . . . . . 30
Exa 5.3 DFT computation . . . . . . . . . . . . . . 31
Exa 5.4 IDFT Computation . . . . . . . . . . . . . . 32
Exa 5.5 DFT computation . . . . . . . . . . . . . . 34
Exa 5.7 Cicular convolution computation . . . . . . 34
Exa 5.8 Cicular convolution computation . . . . . . 35
Exa 5.10 Generating symmetric parts . . . . . . . . . 36
Exa 5.11 Cicular convolution computation . . . . . . 37
Exa 5.12 Linear Convolution using DFT . . . . . . . 38
Exa 5.14 DFT computationusing single DFT . . . . . 39
Exa 5.15 DFT computationusing single DFT of shorter
length . . . . . . . . . . . . . . . . . . . . . 39
Exa 6.1 z Transform of causal exponential sequence . 42
Exa 6.2 z transform of anticausal sequence . . . . . . 42
Exa 6.3 z Transform . . . . . . . . . . . . . . . . . . 43
Exa 6.4 z Transform . . . . . . . . . . . . . . . . . . 43
Exa 6.5 Z transform of causal sequence . . . . . . . 43
Exa 6.9 z Transform . . . . . . . . . . . . . . . . . . 44
Exa 6.10 Rational form of z Transform from its zero
and pole locations . . . . . . . . . . . . . . 44
Exa 6.11 Inverse z Transform . . . . . . . . . . . . . . 45
Exa 6.12 Inverse z Transform . . . . . . . . . . . . . . 45
Exa 6.13 Proper fraction of Rational z Transform . . 46
Exa 6.14 Inverse z Transform by partial fraction expan-
sion . . . . . . . . . . . . . . . . . . . . . . 47
Exa 6.15 residue computation using coefficient match-
ing approach . . . . . . . . . . . . . . . . . 47
Exa 6.16 Inverse z Transform by power series expansion 48
Exa 6.17 Coefficients of rational form . . . . . . . . . 49
Exa 6.18 Inverse z Transform using long division . . . 49
Exa 6.19 Inverse z Transform using long division . . . 50
Exa 6.20 Inverse z Transform . . . . . . . . . . . . . . 50
Exa 6.22 z Transform . . . . . . . . . . . . . . . . . . 50
6
Exa 6.23 z Transform . . . . . . . . . . . . . . . . . . 51
Exa 6.24 sum of sequences of non overlapping ROC . 51
Exa 6.25 z Transform . . . . . . . . . . . . . . . . . . 52
Exa 6.26 z Transform . . . . . . . . . . . . . . . . . . 52
Exa 6.27 Inverse z Transform . . . . . . . . . . . . . . 53
Exa 6.28 Enlargement of ROC by pole zero cancellation 54
Exa 6.30 Convolution . . . . . . . . . . . . . . . . . . 54
Exa 6.31 Convolution . . . . . . . . . . . . . . . . . . 55
Exa 6.33 Transfer Function of Moving Average Filter 56
Exa 6.34 Transfer function determination . . . . . . . 57
Exa 7.1 Bounded real function . . . . . . . . . . . . 58
Exa 7.2 Transfer function determination . . . . . . . 59
Exa 7.6 FIR Transfer function . . . . . . . . . . . . 60
Exa 8.1 Analysis of Cascaded lattice digital filter struc-
ture . . . . . . . . . . . . . . . . . . . . . . 61
Exa 8.6 Factorization of FIR Transfer Function . . . 61
Exa 8.7 Factorization of IIR Transfer Function . . . 62
Exa 8.10 Cascaded lattice realization of IIR digital Trans-
fer Function . . . . . . . . . . . . . . . . . . 62
Exa 8.12 Gray Markel method of realization . . . . . 63
Exa 8.18 Cascaded lattice realization . . . . . . . . . 64
Exa 9.1 Computating ripple values . . . . . . . . . . 66
Exa 9.2 conversion of bandedged frequencies to Nor-
malized digital frequencies . . . . . . . . . . 66
Exa 9.3 Design of HP Digital Filter . . . . . . . . . 67
Exa 9.6 Changing passband edge frequencies to LP
IIR digital frequencies . . . . . . . . . . . . 68
Exa 9.7 Design of HP IIR Digital Filter from LP Dig-
ital Filter . . . . . . . . . . . . . . . . . . . 69
Exa 9.12 Minimum order of Type 2 Chebyshev HP IIR
digital filter . . . . . . . . . . . . . . . . . . 70
Exa 10.1 Kaiser formula . . . . . . . . . . . . . . . . 71
Exa 10.2 Bellenger formula . . . . . . . . . . . . . . . 72
Exa 10.3 Hermann formula . . . . . . . . . . . . . . . 72
Exa 10.4 Order Estimation . . . . . . . . . . . . . . . 73
Exa 10.6 Filter length estimation for window based de-
sign . . . . . . . . . . . . . . . . . . . . . . 74
Exa 10.7 Order Estimation . . . . . . . . . . . . . . . 75
7
Exa 10.8 Kaiser window . . . . . . . . . . . . . . . . 75
Exa 11.3 Reconstruction of Transfer function from Im-
pulse response coeeficients . . . . . . . . . . 77
Exa 11.11 Cascaded lattice Filter structure . . . . . . . 78
Exa 12.3 Signal to Quantisation Noise Ratio . . . . . 80
Exa 13.1 Up sampling operation . . . . . . . . . . . . 81
Exa 13.2 Down sampling operation . . . . . . . . . . 82
Exa 13.6 Decimator Computation complexity . . . . . 82
Exa 14.1 Effect of DFT length . . . . . . . . . . . . . 84
Exa 14.2 Effect of DFT length . . . . . . . . . . . . . 85
8
Chapter 2
1 //EXAMPLE 2 . 1
2 // Ensemble a v e r a g i n g
3 clear ;
4 clc ;
5 n = 1:50;
6 clf () ;
7 figure (0)
8 a = gca () ;
9 a . x_location = ” o r i g i n ” ;
10 a . y_location = ” o r i g i n ” ;
11 for i =1: length ( n )
12 s ( i ) =2* n ( i ) *((0.9) ^ n ( i ) ) ;
13 d ( i ) =( -0.1) ^ n ( i ) ; // a r b i t r a r y n o i s e
signal .
14 end
15
16 M = length ( n ) ;
17
18 for i =1: M
9
19 d ( i ) =( -0.1) ^ i ;
20 S = sum ( d ) ;
21 end
22 Eav =( s + S / M ) ’; // Ensemble a v e r a g e .
23 disp ( Eav , ’ The o u t p u t o f Ensemble a v e r a g i n g is ’)
24 plot2d3 (n , s )
25 plot (n ,s , ’ r . ’ )
26 xtitle ( ’ Ensemble a v e r a g i n g ’ , ’ n ’ , ’ Eav−s ’ ) ;
27 a . children . children . thickness =2;
28 a . children . children . foreground =2;
1 //EXAMPLE 2 . 3 , B a s i c o p s on u n e q u a l l e n g t h s e q u e n c e
2 clear ;
3 clc ;
4 c =[3.2 41 36 -9.5 0];
10
5 disp (c , ’ c = ’ ) ;
6 g =[ -21 1.5 3];
7 disp (g , ’ g = ’ ) ;
8 a = length ( g ) ;
9 b = length ( c ) ;
10 i =0;
11 while (i <b - a )
12 g (b - i ) =0;
13 i = i +1;
14 end
15 w4 = g .* c ;
16 disp ( w4 , ’ The p r o d u c t o f two s e q u e n c e s i s = ’ ) ;
17 w5 = c + g ;
18 disp ( w5 , ’ The a d d i t i o n o f two s e q u e n c e s i s = ’ ) ;
1 //EXAMPLE 2 . 5 , C o n j u g a t e −A n t i s y m m e t r i c & C o n j u g a t e −
symmetric p a r t s o f Sequence
2 clc ;
3 clear ;
4 g =[0 , 1+ %i *4 , -2+ %i *3 , 4 - %i *2 , -5 - %i *6 , - %i *2 , 3];
5 disp (g , ’ g = ’ )
6 g1 = conj ( g ) ; // C o n j u g a t e o f g ;
7 disp ( g1 , conj ( g ) ) ;
8 a = length ( g ) ;
9 for i =1: a
10 g2 (1 , i ) = g1 (a - i +1) ;
11 end
12
13 gcs =( g + g2 ) /2 // C o n j u g a t e −Symmetric p a r t
14 disp ( gcs , ’ The C o n j u g a t e s y m m e t r i c p a r t i s = ’ ) ;
15 gcas =( g - g2 ) /2; // C o n j u g a t e −A n t i s y m m e t r i c p a r t
16 disp ( gcas , ’ The C o n j u g a t e a n t i s y m m e t r i c p a r t i s = ’ ) ;
11
Scilab code Exa 2.6 Energy Signal
1 //EXAMPLE 2 . 6 , Energy S i g n a l
2
3 clear ;
4 clc ;
5 n = -5:5;
6 for i =1: length ( n )
7 if ( n ( i ) >=1)
8 h ( i ) =1/ n ( i ) ;
9 else
10 h ( i ) =0;
11 end
12 end
13
14 Sum =0;
15 N =1:10000;
16 for i =1: length ( N )
17 h ( i ) =(1/ N ( i ) ) ^2;
18 end
19
20 Energy = sum ( h ) ;
21
22 if ( Energy < %inf ) then
23 disp ( ’ Energy S i g n a l ’ ) ;
24 disp ( Energy , ’ Energy o f s i g n a l = ’ ) ;
25 else
26 if ( Energy / length ( N ) < %inf ) then
27 disp ( ’ Power S i g n a l ’ ) ;
28
29 else
30 disp ( ’ N i e t h e r Energy n o r Power S i g n a l ’ ) ;
31 end
32 end
12
Scilab code Exa 2.7 Power Signal
1 //EXAMPLE 2 . 9 , G e n e r a t i o n o f a S q u a r e wave s e q u e n c e :
2 clc ;
3 clear ;
4 clf () ;
5 a = gca () ;
6 figure (0) ;
7 a . x_location = ” o r i g i n ” ;
8 x =[0:1:80];
9 y1 = sin ( x *.05* %pi ) ;
13
10 y2 = sin ( x *.15* %pi ) ;
11 y3 = sin ( x *.25* %pi ) ;
12 y4 = y1 + y2 /3+ y3 /5;
13 plot2d3 (x , y4 ,2)
14 plot (x , y4 , ’ r . ’ )
15 xtitle ( ’ Approximate S q u a r e wave ’ , ’ x ’ , ’ y4 ’ ) ;
16 a . children . children . thickness =3;
1 //EXAMPLE 2 . 1 6 ,
2 clear ;
3 clc ;
4 // Given i n p u t s e q u e n c e = [ 3 4 5 ]
5 x =[0 3 4 5 0];
6 disp ([3 4 5] , ’ I n p u t s e q u e n c e = ’ )
7 // d e t e r m i n i n g median f i l t e r
8 // f i r s t s e q u e n c e
9 for k =2:4
10 if x ( k ) >x (k -1) & x ( k +1) >x (k -1) & x ( k +1) >x ( k )
11 y (k -1) = x ( k ) ;
12 else
13 x (k -1) >x ( k +1) & x ( k ) >x ( k +1) & x ( k ) >x (k -1)
14 y (k -1) = x (k -1) ;
15 end
16 end
17 disp (y ’ , ’ The Median F i l t e r o f t h e g i v e n i n p u t i s = ’ )
;
1 //EXAMPLE 2 . 2 0 , P a s s i v e o r l o s s l e s s s y s t e m .
2 clear ;
14
3 clc ;
4 a = input ( ” any v a l u e o f a l e s s t h a n o r e q u a l t o one ” )
5 n = -10:1:10;
6 x=n;
7 y=a*n;
8 S =0;
9 for i =1: length ( n )
10 S = S + y ^2;
11 end
12
13 if a <1 then
14 disp ( ’ t h e s y s t e m i s p a s s i v e ’ )
15 else
16
17 a ==1
18 disp ( ’ t h e s y s t e m i s l o s s l e s s ’ )
19
20 end
1 //EXAMPLE 2 . 2 2 , i m p u l s e r e s p o n s e o f a c c u m u l a t o r
2
3 clear ;
4 clc ;
5 d =[1];
6 t = -1:.01:1;
7 h =0;
8 clf () ;
9 figure (0) ;
10 a = gca () ;
11 a . x_location = ” o r i g i n ” ;
12
13 for i =1: length ( t )
14 if t ( i ) <0
15
15 h =0;
16 else
17 h=d;
18 plot2d3 (i -101 , h )
19 plot (i -101 , h , ’ . r ’ )
20 xtitle ( ’ I m p u l s e R e s p o n s e o f a c c u m u l a t o r ’ , ’ t ’
, ’Y ’ ) ;
21 a . children . children . thickness =1;
22 a . children . children . foreground =2;
23 end
24 end
25 disp (h , ’ The i m p u l s e r e s p o n s e o f Accumulator i s = ’ )
1 //EXAMPLE 2 . 2 6 , c o n v o l u t i o n o f x & h
2 x =[ -2 0 1 -1 3];
3 disp (x , ’ x = ’ ) ;
4 h =[1 2 0 -1];
5 disp (h , ’ h = ’ ) ;
6 n =0:7;
7 y = convol (x , h ) ;
8 disp (y , ’ The c o n v o l u t i o n o f t h e two i n p u t s i s : ’ )
1 //EXAMPLE 2 . 2 7 , c o n v o l u t i o n o f an e x p o n e n t i a l
sequence
2 clear ;
3 clc ;
4 n =0:.5:5
5 c =0.5;
6 b =0.4;
16
7 clf () ;
8 figure (0) ;
9 a = gca () ;
10 a . x_location = ” o r i g i n ” ;
11 x = c^n;
12 subplot (2 ,2 ,1) ;
13 plot2d3 (n ,x ,2) ;
14 plot (n ,x , ’ . r ’ ) ;
15 xtitle ( ’ ’ , ’ n ’ , ’ x ’ ) ;
16 h = b^n ;
17 subplot (2 ,2 ,2) ;
18 plot2d3 (n ,h ,2)
19 plot (n ,h , ’ . r ’ )
20 xtitle ( ’ ’ , ’ n ’ , ’ h ’ ) ;
21 N =0:.5:10;
22 y = convol ( x , h ) ;
23 subplot (2 ,2 ,3) ;
24 plot2d3 (N ,y ,2)
25 plot (N ,y , ’ . r ’ )
26 xtitle ( ’ c o n v o l ( x , h ) ’ , ’ n ’ , ’ y ’ ) ;
27 disp (y , ’ C o n v o l u t i o n o f t h e two e x p o n e n t i a l s e q u e n c e s
is =’)
1 //EXAMPLE 2 . 2 8 , g r a p h i c a l r e p r e s e n t a t i o n o f
convolution of x & h .
2 clear ;
3 clc ;
4 x =[ -2 0 1 -1 3];
5 disp (x , ’ x ’ ) ;
6 h =[1 2 0 -1];
7 disp (h , ’ h ’ ) ;
8 n =0:7;
9 y = convol (x , h ) ;
17
10 disp (y , ’ c o n v o l u t i o n = ’ ) ;
11 clf () ;
12 figure (0) ;
13 a = gca () ;
14 a . x_location = ” o r i g i n ” ;
15 a . y_location = ” o r i g i n ” ;
16 plot2d3 (n , y )
17 plot (n ,y , ’ r . ’ )
18 xtitle ( ’ c o n v o l u t i o n ’ , ’ n ’ , ’Y ’ ) ;
19 a . children . children . thickness =2;
20 a . children . children . foreground =2;
1 // Example 2 . 2 9 , C o n v o l u t i o n u s i n g T a b u l a r method .
2 clear ;
3 clc ;
4 x =[ -2 0 1 -1 3];
5 h =[1 2 0 -1];
6 q = length ( x ) ;
7 w = length ( h ) ;
8 z = q +w -1;
9 y0 =0;
10 for i =1: z ;
11 y ( i ) =0;
12 for k =1: i ;
13 if k > q
14 x ( k ) =0;
15 else
16 if (i - k +1) >w
17 h (i - k +1) =0;
18 else
19 y ( i ) = y ( i ) + x ( k ) * h (i - k +1) ;
20 end
21 end
18
22 end
23 end
24 disp (y ’ , ’ The C o n v o l u t i o n o f t h e two s e q u e n c e s is =’)
1 //EXAMPLE 2 . 3 0
2 // C o n v o l u t i o n o f two s i d e d s e q u e n c e s
3 clear ;
4 clc ;
5 g =[3 -2 4]; // o r i g i n a t i n g a t n=−1
6 h =[4 2 -1]; // o r i g i n a t i n g a t n=0
7 q = length ( g ) ;
8 w = length ( h ) ;
9 z = q +w -1;
10 y0 =0;
11 for i =1: z ;
12 y ( i ) =0;
13 for k =1: i ;
14 if k > q
15 g ( k ) =0;
16 else
17 if (i - k +1) >w
18 h (i - k +1) =0;
19 else
20 y ( i ) = y ( i ) + g ( k ) * h (i - k +1) ;
21 end
22 end
23 end
24 end
25 n = -1: z -2;
26 disp (y , ’ The C o n v o l u t i o n o f t h e two s e q u e n c e s i s = ’ )
27 clf () ;
28 a = gca () ;
29 figure (0) ;
19
30 a . x_location = ” o r i g i n ” ;
31 plot2d3 (n ,y ,2) ;
32 plot (n ,y , ’ r . ’ ) ;
33 xtitle ( ’ c o n v o l u t i o n ’ , ’ n ’ , ’ y ’ ) ;
1 //EXAMPLE 2 . 3 1 , S t a b i l i t y f o r c a u s a l s y s t e m .
2 // h [ i ]= i m p u l s e r e s p o n s e o f LTI s y s t e m .
3 clear ;
4 clc ;
5 n = -5:0.001:5;
6 a =0.6;
7
8 for i =1: length ( n )
9 if ( n ( i ) <0)
10 h ( i ) =0;
11 else
12 h ( i ) = abs ( a ^ n ( i ) ) ;
13
14 end
15 end
16 S = sum ( h ) ;
17 if (S < %inf )
18 disp ( ’ BIBO s t a b l e s y s t e m ’ ) ;
19 else
20 disp ( ’ BIBO u n s t a b l e s y s t e m ’ ) ;
21
22 end
1 //EXAMPLE2. 3 2 S t a b i l i t y f o r a n t i −C a u s a l s y s t e m .
20
2 // h [ i ]= i m p u l s e r e s p o n s e o f LTI s y s t e m .
3 clear ;
4 clc ;
5 n = -5:1/1000:5;
6 a =5;
7 for i =1: length ( n )
8 if ( n ( i ) > -1)
9 h ( i ) =0;
10 else
11 h(i)=a^n(i);
12 S = sum ( h ) ;
13 end
14 end
15
16 if (S < %inf )
17 disp ( ’ BIBO s t a b l e s y s t e m ’ ) ;
18 else
19 disp ( ’ BIBO u n s t a b l e s y s t e m ’ ) ;
20
21 end
1 //EXAMPLE 2 . 3 3 , s t a b i l i t y o f f i n i t e i m p u l s e r e s p o n s e
.
2 // h [ i ]= i m p u l s e r e s p o n s e o f LTI s y s t e m .
3 clear ;
4 clc ;
5 n = -5:1/100:5;
6 a = input ( ’ v a l u e o f a ’ ) ;
7 N1 = input ( ’ l o w e r l i m i t ’ ) ;
8 N2 = input ( ’ u p p e r l i m i t ’ ) ;
9 for i =1: length ( a )
10 if ( n ( i ) < N1 & n ( i ) > N2 )
11 h ( i ) =0;
21
12 else
13 h(i)=a^n(i);
14 S = sum ( h ) ;
15 end
16 end
17
18 if (S < %inf )
19 disp ( ’ BIBO s t a b l e s y s t e m ’ ) ; // a s l o n g a s
N1 , N2!= % i n f
20 else
21 disp ( ’ BIBO u n s t a b l e s y s t e m ’ ) ;
22
23 end
1 //EXAMPLE 2 . 4 6 , C r o s s c o r e l a t i o n Computation .
2 // Given two f i n i t e l e n g t h s e q u e n c e . x [ n ] , y [ n ] :
3 clear ;
4 clc ;
5 x =[1 3 -2 1 2 -1 4 4 2];
6 disp (x , ’ x ’ ) ;
7 y =[2 -1 4 1 -2 3];
8 disp (y , ’ y ’ ) ;
9 // C r o s s c o r e l a t i o n r x y [ n ] :
10
11 rxy = convol (x , mtlb_fliplr ( y ) ) ;
12 disp ( rxy , ’ The C r o s s −C o r e l a t i o n O p e r a t i o n o f t h e
Inputs i s =’)
22
Chapter 3
1 //EXAMPLE 3 . 5
2 //DTFT o f u n i t s a m p l e s e q u e n c e
3 clc ;
4 clear ;
5 // a = 0 . 5 ;
6 n =0:9;
7 x = [1 , zeros (1 ,9) ];
8 disp (x , ’ x [ n ] = ’ )
9
10 K = 4;
11 k = 0:4/1000:4;
12 W = k *2* %pi / K ;
13 X = ( x ) * exp ( %i *n ’* W ) ;
14 disp (X , ’DTFT, x [ n ] −−> ’ )
15 X_mag = abs ( X ) ;
16 X_phase = phasemag ( X ) ; // no p h a s e e x i s t s
17
18 figure (0) ;
19 plot2d3 ( mtlb_fliplr ( W ) , X_mag ) ;
23
20 xtitle ( ’ Magnitude p l o t ’ , ’W −−−> ’ , ’ X mag −−−> ’ ) ;
21 figure (1) ;
22 plot2d3 ( mtlb_fliplr ( W ) , X_phase ) ;
23 xtitle ( ’ z e r o p h a s e p l o t ’ , ’W −−−> ’ , ’ X p h a s e −−−> ’ ) ;
1 //EXAMPLE 3 . 6
2 // D e t e r m i n e DTFT o f s e q u e n c e
3 //PROGRAM REQUIRES MAXIMA SCILAB TOOLBOX
4
5 clc ;
6 clear ;
7 // S y m b o l i c c a l c u l a t i o n
8 Syms n w a ;
9 x1 =( a ^ n ) * exp ( - %i * n * w ) ;
10 X1 = nusum ( x1 ,n ,0 , %inf ) ;
11 disp ( X1 , ’DFT, X = ’ ) ;
12
13 // Given :
14 a =0.5;
15 n =0:9;
16 // x [ n ]= a ˆn∗u [ n ]
17 for i = 0:9
18 x ( i +1) = a ^ i ;
19 end
20 // The DTFT o f t h e s e q u e n c e
21 K = 4;
22 k = 0:4/1000:4;
23 W = k *6* %pi / K ;
24 X = (x ’) * exp ( %i *n ’* W ) ;
25 X_mag = abs ( X ) ;
26 [ X_phase , db ] = phasemag ( X ) ;
27
28 clf () ;
24
29 a = gca () ;
30 figure (0) ;
31 // Note %pi ˜ 3 . 1 4
32 plot2d3 ( mtlb_fliplr ( W ) , X_mag ) ;
33 xtitle ( ’ Magnitude r e s p o n s e ’ , ’W −−−> ’ , ’ A m p l i t u d e −−−>
’ );
34 figure (1) ;
35 plot2d3 ( mtlb_fliplr ( W ) , X_phase ) ;
36 xtitle ( ’ Phase r e s p o n s e ’ , ’W −−−> ’ , ’ Phase i n d e g r e e s
−−−> ’ ) ;
1 //EXAMPLE 3 . 1 2
2 // x [ n ]=(( −1) ˆ n ) ∗ ( a ˆ n ) ∗u [ n ] . . . . . g i v e n a = 0 . 5 ;
3
4 clc ;
5 clear ;
6
7 a =0.5;
8 n =0:9;
9 for i = 0:9
10 x ( i +1) = ( a * exp ( - %i * %pi ) ) ^ i ;
11 end
12
13 // The DTFT o f t h e s e q u e n c e
14 K = 4;
15 k = 0:4/1000:4;
16 W = k *6* %pi / K ;
17 X = (x ’) * exp ( %i *n ’* W ) ;
18 X_mag = abs ( X ) ;
19 X_phase = phasemag ( X ) ;
20
21 //PLOTTING GRAPHS FOR THE INTERVAL OF 0 TO 6∗ %pi
22 clf () ;
25
23 a = gca () ;
24 figure (0) ;
25 plot2d3 ( mtlb_fliplr ( W ) , X_mag ) ;
26 xtitle ( ’ Magnitude r e s p o n s e ’ , ’W’ , ’ A m p l i t u d e ’ ) ;
27 figure (1) ;
28 plot2d3 ( mtlb_fliplr ( W ) , X_phase ) ;
29 xtitle ( ’ Phase r e s p o n s e ’ , ’W’ , ’ X phase , d e g r e e s ’ ) ;
1 //EXAMPLE 3 . 1 0
2 // DTFT o f a s e q u e n c e
3 clc ;
4 clear ;
5 syms a n M w ;
6 x=a^n;
7 X = nusum ( x *( exp ( - %i * w * n ) ) ,n ,0 ,M -1)
8 disp ( limit ( X ) , ’ The DTFT o f t h e g i v e n s e q u e n c e , X = ’
)
1 //EXAMPLE 3 . 1 2
2 // x [ n ]=(( −1) ˆ n ) ∗ ( a ˆ n ) ∗u [ n ] . . . . . g i v e n a = 0 . 5 ;
3
4 clc ;
5 clear ;
6
7 a =0.5;
8 n =0:9;
9 for i = 0:9
10 x ( i +1) = ( a * exp ( - %i * %pi ) ) ^ i ;
11 end
26
12
13 // The DTFT o f t h e s e q u e n c e
14 K = 4;
15 k = 0:4/1000:4;
16 W = k *6* %pi / K ;
17 X = (x ’) * exp ( %i *n ’* W ) ;
18 X_mag = abs ( X ) ;
19 X_phase = phasemag ( X ) ;
20
21 //PLOTTING GRAPHS FOR THE INTERVAL OF 0 TO 6∗ %pi
22 clf () ;
23 a = gca () ;
24 figure (0) ;
25 plot2d3 ( mtlb_fliplr ( W ) , X_mag ) ;
26 xtitle ( ’ Magnitude r e s p o n s e ’ , ’W’ , ’ A m p l i t u d e ’ ) ;
27 figure (1) ;
28 plot2d3 ( mtlb_fliplr ( W ) , X_phase ) ;
29 xtitle ( ’ Phase r e s p o n s e ’ , ’W’ , ’ X phase , d e g r e e s ’ ) ;
1 //EXAMPLE 3 . 1 3
2
3 clc ;
4 clear ;
5 a =0.5;
6 n =0:9;
7 for i = 0:9
8 x ( i +1) = a ^ i ;
9 end
10 // The DTFT o f t h e s e q u e n c e
11 K = 4;
12 k = 0:4/1000:4;
13 W = k *6* %pi / K ;
14 X1 = (x ’) * exp ( %i *n ’* W ) ;
27
15 X = %i * diff ( X1 ) ;
16 X = [X ,0] + X1 ;
17
18 X_mag = abs ( X ) ;
19 [ X_phase , db ] = phasemag ( X ) ;
20
21 clf () ;
22 a = gca () ;
23 figure (0) ;
24 plot2d3 ( mtlb_fliplr ( W ) , X_mag ) ;
25 xtitle ( ’ Magnitude r e s p o n s e ’ , ’W’ , ’ X mag ’ ) ;
26 figure (1) ;
27 plot2d3 ( mtlb_fliplr ( W ) , X_phase ) ;
28 xtitle ( ’ Phase r e s p o n s e ’ , ’W’ , ’ X p h a s e ’ ) ;
1 //EXAMPLE 3 . 1 4
2 //ENERGY OF LP DISCREET TIME SIGNAL
3 //PROGRAM REQUIRES MAXIMA SCILAB TOOLBOX
4 clc ;
5 clear ;
6 syms n wc w ;
7 wc = input ( ” t h e v a l u e o f wc ( l e s s t h a n %pi )= ” ) ;
8 n = -5:0.05:5;
9
10 for i =0: length ( n )
11 hlp ( i +1) = ( wc / %pi ) * sinc (( wc * i ) / %pi ) ;
12 E ( i +1) =( abs ( hlp ( i +1) ) ) ^2;
13 end
14
15 Energy = sum ( E ) ;
16 if ( Energy < %inf ) then
17 disp ( ’ The f i l t e r i s Energy S i g n a l ’ ) ;
18 else
28
19 if ( Energy / length ( N ) < %inf ) then
20 disp ( ’ Power S i g n a l ’ ) ;
21 else
22 disp ( ’ N i e t h e r Energy n o r Power S i g n a l ’ ) ;
23 end
24 end
25 disp ( Energy , ’ t h e e n e r g y i s = ’ ) ;
1 //EXAMPLE 3 . 1 5
2 //ENERGY OF A SIGNAL x [ n ]= a ˆ n∗u [ n ]
3 clc ;
4 clear ;
5 a =0.5;
6 n =0:0.1:9.9;
7 // x [ n ]= a ˆ n∗u [ n ]
8 for i = 0: length ( n )
9 x ( i +1) = a ^ i ;
10 E =( abs ( x ) ) ^2;
11 end
12 Energy = sum ( E ) ;
13 disp ( Energy , ’ Energy o f t h e s i g n a l = ’ ) ;
29
Chapter 4
1 //EXAMPLE 4 . 5
2 // d e t e r m i n e r i p p l e v a l u e s i n db ;
3 clc
4 clear ;
5 ap = 0.01 // Peak p a s s b a n d r i p p l e i n dB
6 as = 70 // min . s t o p b a n d a t t e u a t i o n i n dB
7 dp = 1 -10^ -( ap /20) ;
8 ds = 10^ -( as /20) ;
9 disp ( dp , ’ dp = ’ ) ;
10 disp ( ds , ’ d s = ’ ) ;
1 //EXAMPLE 4 . 6
2 // Order o f LP f i l t e r
3 clc ;
30
4 clear ;
5 ap = 1 // Peak p a s s b a n d r i p p l e i n dB
6 as = 40 // min . s t o p b a n d a t t e u a t i o n i n dB
7 wp = 1000 // Hz
8 ws = 5000 // Hz
9 k = wp / ws ;
10 disp (1/ k , ’ 1/ k = ’ ) ;
11 k1 = 1/( sqrt ((10^(0.1* as ) -1) /(10^(0.1* ap ) -1) ) ) ;
12 disp (1/ k1 , ’ 1/ k1 = ’ ) ;
13 N = ceil ( log10 ( sqrt ((10^(0.1* as ) -1) /(10^(0.1* ap ) -1) ) )
/ log10 (1/ k ) ) ;
14 disp (N , ’ o r d e r o f t h e f i l t e r i s : ’ ) ;
1 //EXAMPLE 4 . 7
2 // D e t e r m i n e t h e o r d e r o f Analog Chebyshev LP f i l t e r .
3 clc ;
4 clear ;
5 ap = 1 //dB
6 as = 40 //dB
7 wp = 1000 // Hz
8 ws = 5000 // Hz
9 k = wp / ws ;
10 disp (1/ k , ’ 1/ k = ’ ) ;
11 k1 = 1/( sqrt (((10^(0.1* as ) ) -1) /((10^(0.1* ap ) ) -1) ) ) ;
12 disp (1/ k1 , ’ 1/ k1 = ’ ) ;
13 N = acosh (1/ k1 ) / acosh (1/ k ) ;
14 disp (N , ’N = ’ ) ;
15 disp ( ’ S i n c e o r d e r o f t h e f i l t e r i s a l w a y s an i n t e g e r
, ’ );
16 disp ( ceil ( N ) , ’ Order o f t h e f i l t e r i s , N = ’ ) ;
31
Scilab code Exa 4.8 Order of Analog Lowpass Elliptic Filter
1 //EXAMPLE 4 . 8
2 // D e t e r m i n e t h e o r d e r o f Analog E l l i p t i c LP f i l t e r .
3 clc ;
4 clear ;
5 ap = 1 //dB
6 as = 40 //dB
7 Fp = 1000 // Hz
8 Fs = 5000 // Hz
9 wp = Fp *2* %pi ;
10 ws = Fs *2* %pi ;
11
12
13 k1 = 1/( sqrt ((10^(0.1* as ) -1) /(10^(0.1* ap ) -1) ) ) ;
14 disp (1/ k1 , ’ 1/ k1 = ’ ) ;
15 k = wp / ws ;
16 k2 = sqrt (1 - ( k * k ) ) ;
17 disp ( k2 , ” k2 = ” ) ;
18 po = (1 - sqrt ( k2 ) ) /(2*(1 + sqrt ( k2 ) ) ) ;
19 disp ( po , ’ po = ’ ) ;
20 p = po +2* po ^5 + 15* po ^9 + 150* po ^13;
21 disp (p , ’ p = ’ ) ;
22 N = (2* log10 (4/ k1 ) ) / log10 (1/ p ) ;
23 disp (N , ’N = ’ ) ;
24 disp ( ’ S i n c e o r d e r o f t h e f i l t e r i s a l w a y s an i n t e g e r
, ’ );
25 disp ( ceil ( N ) , ’ Order o f t h e f i l t e r i s , N = ’ ) ;
1 //EXAMPLE 4 . 1 6
2 // D e s i g n a n a l o g b u t t e r w o r t h High p a s s filter
3 clc ;
4 clear ;
32
5 wp =4000;
6 ws =1000;
7 ap =0.1;
8 as =40;
9
10 Ap =1; // a s s u m p t i o n
11 As =(2* %pi * wp ) * Ap /(2* %pi * ws ) ;
12
13 N = ceil ( log10 ( sqrt ((10^(0.1* as ) -1) /(10^(0.1* ap ) -1) ) )
/ log10 ( As / Ap ) ) ;
14 disp (N , ’ o r d e r o f t h e f i l t e r i s : ’ ) ;
15
16 Ac = As /((10^(0.1* as ) -1) ^(1/( N *2) ) ) ;
17 disp ( Ac , ’ c u t o f f f r e q u e n c y = ’ )
18
19 // [ hs , p o l e , z e r o , g a i n ]= a n a l p f (N, ’ b u t t ’ , Ac ) ;
20
21 s = %s ;
22 hs =1/(( s + 1) *( s ^2 + 0.61803* s + 1) *( s ^2 + 1.61803* s
+ 1) ) ;
23 Hs = horner ( hs , s / Ac ) ;
24 H1 = numer ( Hs ) /0.0976514;
25 H2 = denom ( Hs ) /0.0976514;
26 disp ( H1 / H2 , ’ t h e low p a s s t r a n s f e r f u n c t i o n i s , HLP( s )
= ’ );
27 Hs = horner ( hs , Ac / s ) ;
28 H1 = numer ( Hs ) ;
29 H2 = denom ( Hs ) ;
30 disp ( H1 / H2 , ’ t h e High p a s s t r a n s f e r f u n c t i o n i s , HHP( s
) = ’ );
33
Chapter 5
1 //EXAMPLE 5 . 1
2 //DETERMINE DFT OF GIVEN SEQUENCE
3 clc ;
4 clear ;
5 N = input ( ” l e n g t h o f s e q u e n c e = ” ) ;
6 x = [1 , zeros (1 ,N -1) ];
7 disp (x , ’ The s e q u e n c e i s , x = ’ ) ;
8 X = dft (x ,0) ;
9 disp (X , ’DFT o f t h e s e q u e n c e i s X = ’ ) ;
10 m = input ( ” v a l u e o f some i n t e m e d i a t e ( mth ) p o i n t =
”);
11 y = [ zeros (1 ,m -1) ,1 , zeros (1 ,N - m ) ];
12 Y = dft (y ,0) ;
13 disp (Y , ’DFT o f t h e s e q u e n c e i s Y = ’ ) ;
34
1 //EXAMPLE 5 . 2
2 //DFT o f s i n u s o i d a l s e q u e n c e
3 clc ;
4 clear ;
5
6 N = input ( ” input value of N ”);
7 r = input ( ” input r value ”) ;
8 n = 0: N -1;
9 x = cos (2* %pi * r * n / N )
10 X = dft (x , -1)
11 //X e x i s i t s o n l y a t n={r , N−r } where X = N/2
12 clf () ;
13 a = gca () ;
14 a . x_location = ” o r i g i n ” ;
15 a . y_location = ’ o r i g i n ’ ;
16 plot2d3 (n ,X ,2) ;
17 a . thickness =1;
18 plot (n ,X , ’ r . ’ ) ;
19
20 xtitle ( ’DFT ’ , ’K −−> ’ , ’X [ K ] −−> ’ ) ;
21 X = disp (X , ’DFT o f x−−> ’ ) ;
1 //EXAMPLE 5 . 3
2 //DETERMINE DFT OF GIVEN SEQUENCE
3
4 clc ;
5 clear ;
6 N = input ( ” l e n g t h o f s e q u e n c e , N = ”);
7 M = input ( ”M p o i n t DFT = ” ) ;
8 if M > N
9 x = [ ones (1 , N ) , zeros (1 ,M - N ) ];
10 disp (x , ’ t h e s e q u e n c e i s : ’ ) ;
11 for n =0: M -1
35
12 for k =0: M -1
13 W ( n +1 , k +1) = exp ( -( %i *2* %pi * k / M ) * n ) ;
14 end
15 end
16 X = W *x ’;
17 disp (X , ’DFT i s , X = ’ ) ;
18 else
19 disp ( ’ i n v a l i d c o m p u t a t i o n ’ ) ;
20 end
21
22
23 n =0: M -1;
24 clf () ;
25 figure (0)
26 a = gca () ;
27 plot2d3 (n ,x ,2) // p l o t t i n g t h e s e q u e n c e
28 plot (n ,x , ’ r . ’ ) ;
29 a . x_location = ’ o r i g i n ’ ;
30 a . y_location = ’ o r i g i n ’ ;
31 poly1 = a . children (1) . children (1) ;
32 poly1 . thickness = 2.5;
33 xtitle ( ’ o r i g i n a l s e q u e n c e ’ , ’ n ’ , ’ x [ n ] ’ ) ;
34
35 figure (1)
36 a = gca () ;
37 plot2d3 (n , abs ( X ) ,2) // p l o t t i n g a b s o l u t e v a l u e o f
DFT o f s e q u e n c e
38 plot (n , abs ( X ) , ’ r . ’ ) ;
39 a . x_location = ’ o r i g i n ’ ;
40 a . y_location = ’ o r i g i n ’ ;
41 poly1 = a . children (1) . children (1) ;
42 poly1 . thickness = 2.5;
43 xtitle ( ’ m a g n i t u d e p l o t ’ , ’M’ , ’ A b s o l u t e v a l u e ’ ) ;
36
1 //EXAMPLE 5 . 4
2 //DETERMINE IDFT OF GIVEN SEQUENCE
3 clc ;
4 clear ;
5 K = input ( ” value of K ”);
6 disp ( ’ i n p u t M > K ’ ) ;
7 M = input ( ” value of M ”);
8 k1 = 0: K -1;
9 V1 = k1 ./ K ; //DFT
10 k =0: M -1;
11
12 N = length ( V1 ) ;
13 V = [ V1 , zeros (1 ,M - N ) ];
14 v = dft (V ,1) ; //IDFT
15
16 clf () ;
17 subplot (1 ,2 ,1)
18
19 a = gca () ;
20 plot2d3 (k , real ( v ) ,2) ;
21 plot (k , real ( v ) , ’ r . ’ ) ;
22 a . x_location = ’ o r i g i n ’ ;
23 a . y_location = ’ o r i g i n ’ ;
24 poly1 = a . children (1) . children (1) ;
25 poly1 . thickness = 2;
26 xtitle ( ’ r e a l p a r t ’ , ’N ’ , ’ v ’ ) ;
27
28 subplot (1 ,2 ,2)
29 a = gca () ;
30 plot2d3 (k , imag ( v ) ,2)
31 plot (k , imag ( v ) , ’ r . ’ ) ;
32 a . x_location = ’ o r i g i n ’ ;
33 a . y_location = ’ o r i g i n ’ ;
34 poly1 = a . children (1) . children (1) ;
35 poly1 . thickness = 2;
36 xtitle ( ’ i m a g i n a r y p a r t ’ , ’N ’ , ’ v ’ ) ;
37 v = disp ( v ) ;
37
Scilab code Exa 5.5 DFT computation
1 //EXAMPLE 5 . 5
2 //DFT c o m p u t a t i o n
3 clc ;
4 clear ;
5
6 N = 16 ;
7 r = 3 ;
8 n = 0: N -1;
9 x = cos (2* %pi * r * n / N )
10 X = fft (x , -1) //DFT o f t h e s e q u e n c e
11 clf () ;
12 a = gca () ;
13 plot2d3 (n ,X ,2) ;
14 plot (n ,X , ’ r . ’ )
15 a . x_location = ’ o r i g i n ’ ;
16 a . y_location = ’ o r i g i n ’ ;
17 poly1 = a . children (1) . children (1) ;
18 poly1 . thickness = 3;
19 xtitle ( ’DFT ’ , ’ k ’ , ’X ’ ) ;
20 X = disp ( real ( X ) , ’ X = ’ ) ;
1 //EXAMPLE 5 . 7
2 // C i r c u l a r c o n v o l u t i o n
3 clear ;
4 clc ;
5 g = [1 2 0 1];
6 disp (g , ’ g [ n ] = ’ ) ;
7 h = [2 2 1 1];
38
8 disp (h , ’ h [ n ] = ’ ) ;
9 G = fft (g , -1) ;
10 H = fft (h , -1) ;
11 Y = G .* H ;
12 yc = fft (Y ,1) ;
13 n1 = 0: length ( yc ) -1;
14 yl = convol (g , h ) ;
15 n2 = 0: length ( yl ) -1;
16
17 clf () ;
18 subplot (2 ,1 ,1)
19 a = gca () ;
20 plot2d3 ( n1 , yc ,2) ;
21 plot ( n1 , yc , ’ r . ’ ) ;
22 a . x_location = ’ o r i g i n ’ ;
23 a . y_location = ’ o r i g i n ’ ;
24 poly1 = a . children (1) . children (1) ;
25 poly1 . thickness = 3;
26 xtitle ( ’ c i r c u l a r c o n v o l u t i o n ’ , ’ n ’ , ’ yc ’ ) ;
27
28 subplot (2 ,1 ,2)
29 a = gca () ;
30 plot2d3 ( n2 , yl ,2) ;
31 plot ( n2 , yl , ’ r . ’ ) ;
32 a . x_location = ’ o r i g i n ’ ;
33 a . y_location = ’ o r i g i n ’ ;
34 poly1 = a . children (1) . children (1) ;
35 poly1 . thickness = 3;
36 xtitle ( ’ l i n e a r c o n v o l u t i o n ’ , ’ n ’ , ’ y l ’ ) ;
37
38 disp ( real ( yc ) ,” c i r c u l a r c o n v o l u t i o n , yc = ” ) ;
39 disp ( yl , ” l i n e a r c o n v o l u t i o n , y l = ” ) ;
39
1 //EXAMPLE 5 . 8
2 // C i c u l a r c o n v o l u t i o n
3 clc ;
4 clear ;
5 g = [1 2 0 1];
6 disp (g , ’ g [ n ] = ’ ) ;
7 h = [2 2 1 1];
8 disp (h , ’ h [ n ] = ’ ) ;
9 G = fft (g , -1) ;
10 H = fft (h , -1) ;
11 Y = G .* H ;
12 yc = fft (Y ,1) ; //IDFT o f Y
13 disp ( yc , ” c i r c u l a r c o n v o l u t i o n , yc = ” )
14 n =0:3;
15 clf () ;
16 figure (0) ;
17 a = gca () ;
18 plot2d3 (n , yc ,2) ;
19 plot (n , yc , ’ r . ’ ) ;
20 a . x_location = ’ o r i g i n ’ ;
21 a . y_location = ’ o r i g i n ’ ;
22 poly1 = a . children (1) . children (1) ;
23 poly1 . thickness = 3;
24 xtitle ( ’ C i r c u l a r c o n v o l u t i o n ’ , ’ n ’ , ’ yc ’ ) ;
1 //EXAMPLE 5 1 0
2 // c o n j u g a t e s y m m e t r i c & a n t i −s y m m e t r i c p a r t s o f
complex s e q u e n c e
3 clear ;
4 clc ;
5 un =[1+ %i *4 , -2+ %i *3 ,4 - %i *2 , -5 - %i *6];
6 disp ( un , ’ u [ n ] = ’ ) ;
7 u1 = conj ( un ) ;
40
8 disp ( u1 , ’ u ∗ [ n ] = ’ ) ;
9 // modulo −4 c i r c u l a r l y t i m e r e v e r s e d v e r s i o n :
10 disp ( pmodulo (0 ,4) , ’ u[<−0>4] = ’ ) ;
11 disp ( pmodulo ( -1 ,4) , ’ u[<−1>4] = ’ ) ;
12 disp ( pmodulo ( -2 ,4) , ’ u[<−2>4] = ’ ) ;
13 disp ( pmodulo ( -3 ,4) , ’ u[<−3>4] = ’ ) ;
14 un1 =[ u1 ( pmodulo ( -0 ,4) +1) , u1 ( pmodulo ( -1 ,4) +1) , u1 (
pmodulo ( -2 ,4) +1) , u1 ( pmodulo ( -3 ,4) +1) ];
15 disp ( un1 , ’ u∗[<−n >4] = ’ ) ;
16 disp (0.5*( un + un1 ) , ’ u c s [ n ] = ’ ) ;
17 disp (0.5*( un - un1 ) , ’ uca [ n ] = ’ ) ;
1 //EXAMPLE 5 . 1 1
2 // C i r c u l a r c o n v o l u t i o n u s i n g DFT
3 clc ;
4 clear ;
5 g = [1 2 0 1];
6 disp (g , ’ g [ n ] = ’ ) ;
7 h = [2 2 1 1];
8 disp (h , ’ h [ n ] = ’ ) ;
9 M =4;
10 for n =0: M -1
11 for k =0: M -1
12 W ( n +1 , k +1) = exp ( -( %i *2* %pi * k / M ) * n ) ;
13 end
14 end
15 G = W *g ’;
16 H = W *h ’;
17 disp (G , ’DFT i s , G = ’ ) ;
18 disp (H , ’DFT i s , H = ’ ) ;
19
20 Y = G .* H ;
21 y =(1/4) * conj ( W ) *( Y ) ;
41
22 disp ( real ( y ) , ’ C i r c u l a r c o n v o l u t i o n = ’ ) ;
1 //EXAMPLE 5 . 1 2
2 // L i n e a r c o n v o l u t i o n u s i n g C i r c u l a r c o n v o l u t i o n
3 clc ;
4 clear ;
5 g = [1 2 0 1];
6 disp (g , ’ g [ n ] = ’ ) ;
7 h = [2 2 1 1];
8 disp (h , ’ h [ n ] = ’ ) ;
9
10 // l i n e a c o n v o l u t i o n l e n g t h = 4+4−1 = 7
11 // a p p e n d i n g t h e two s i g n a l s w i t h z e r o s
12 g = [g , zeros (1 ,3) ]
13 h = [h , zeros (1 ,3) ]
14 G = fft (g , -1) ;
15 H = fft (h , -1) ;
16 Y = G .* H ; // e l e m e n t w i s e m u l t i p l i c a t i o n
17 y = fft (Y ,1) ; //IDFT
18
19 // P l o t t i n g l i n e a r c o n v o l u t i o n
20 n =0:6;
21 figure (0) ;
22 clf () ;
23 a = gca () ;
24 a . x_location = ’ o r i g i n ’ ;
25 a . y_location = ’ o r i g i n ’ ;
26 plot2d3 (n ,y ,2) ;
27 plot (n ,y , ’ r . ’ ) ;
28 poly1 = a . children (1) . children (1) ;
29 poly1 . thickness = 2;
30 xtitle ( ’ L i n e a r c o n v o l u t i o n ’ , ’ n ’ , ’ y ’ ) ;
31 disp (y , ” l i n e a r c o n v o l u t i o n , y = ” ) ;
42
Scilab code Exa 5.14 DFT computationusing single DFT
1 //EXAMPLE 5 . 1 4
2 //DFT o f two r e a l s e q u e n c e s u s i n g one DFT
3 clear ;
4 clc ;
5 g = [1 2 0 1];
6 disp (g , ’ g [ n ] = ’ ) ;
7 h = [2 2 1 1];
8 disp (h , ’ h [ n ] = ’ ) ;
9 x = g + %i .* h ;
10 disp (x , ’ x [ n ] = ’ ) ;
11 X = fft (x , -1) ;
12 disp (X , ’ The DFT, X [ k ] = ’ ) ;
13 X1 = conj ( X ) ;
14 disp ( X1 , ’X∗ [ k ] = ’ ) ;
15
16 for i =0:3;
17 a ( i +1) = pmodulo ( -i ,4) ;
18 X2 ( i +1) = X1 ( a ( i +1) +1) ;
19 end
20
21 X3 = conj ( X2 ’) ;
22 disp ( X3 , ’X∗[<4−k >4] = ’ ) ;
23 disp (0.5*( X + X3 ) , ’G[ k ] = ’ ) ;
24 disp (( X - X3 ) /(2* %i ) , ’H [ k ] = ’ ) ;
Scilab code Exa 5.15 DFT computationusing single DFT of shorter length
1 //EXAMPLE 5 . 1 5
2 //DFT c o m p u t a t i o n u s i n g DFT o f s h o r t e r l e n g t h
sequences
43
3
4 clc ;
5 clear ;
6 v = [1 2 2 2 0 1 1 1];
7 disp (v , ’ Length −8 r e a l s e q u e n c e v [ n ] = ’ )
8 for i =1:4
9 g ( i ) = v (2* i -1) ;
10 h ( i ) = v (2* i ) ;
11 end
12 G = fft (g , -1) ;
13 H = fft (h , -1) ;
14 M = length ( v ) ;
15 // f o r n =0:M−1
16 for k =0: M -1
17 W (1 , k +1) = exp ( -( %i *2* %pi * k / M ) *1) ;
18 end
19 // end
20 G =[ G (1) G (2) G (3) G (4) G (1) G (2) G (3) G (4) ] ;
21 H =[ H (1) H (2) H (3) H (4) H (1) H (2) H (3) H (4) ] ;
22 V = G + W .* H ;
23 disp (V , ’ DFt , V [ k ] = ’ ) ;
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40 // f o r k = 0 : 3
44
41 // V1 ( k +1) = G( k +1) + ( exp ( −2∗ %pi ∗ %i ∗ k / 8 ) ) ∗H( k +1)
;
42 // end
43
44 // f o r k = 4 : 7
45 // V2 ( k ) = G( k −3) + ( exp ( −2∗ %pi ∗ %i ∗ k / 8 ) ) ∗H( k −3) ;
46 // end
47 // d i s p ( [ V1 , z e r o s ( 1 , 3 ) ]+V2 )
45
Chapter 6
z Transform
1 //EXAMPLE 6 . 1
2 //Z−T r a n s f o r m o f c a u s a l s e q u e n c e
3 clc ;
4 clear ;
5 syms n a z ;
6 x = a^n;
7 X = nusum ( x *(1/ z ) ^n ,n ,0 , %inf ) ;
8 limit ( X ) ;
9 disp ( ’ X = ’ ,X ) ;
10 disp ( ’ ROC = | z | > | a | ’ )
1
2 clc ;
3 clear ;
4 syms n a z ;
5 x = a^n;
46
6 X = nusum ( - x *(1/ z ) ^n ,n , - %inf , -1) ;
7 limit ( X ) ;
8 disp ( ’ X = ’ ,X ) ;
9 disp ( ’ ROC = | z | < | a | ’ )
1 //EXAMPLE 6 . 3
2 //Z−T r a n s f o r m
3 clc ;
4 clear ;
5 syms n a z M N ;
6 x = a^n;
7 X = nusum ( x *(1/ z ) ^n ,n ,M ,N -1) ;
8 limit ( X ) ;
9 disp (X , ’ X = ’ ) ;
1 //EXAMPLE 6 . 4
2 //Z−T r a n s f o r m
3 clc ;
4 clear ;
5 syms n z ;
6 x = ( -0.6) ^ n ;
7 X = nusum ( x *((1/ z ) ^ n ) ,n ,0 , %inf ) ;
8 limit ( X ) ;
9 disp (X , ’ X = ’ ) ;
47
1 //EXAMPLE 6 . 9
2 //Z−T r a n s f o r m o f c a u s a l s e q u e n c e
3 clc ;
4 clear ;
5 // z=%z ;
6 syms n a z M N ;
7 x = a^n;
8 X = nusum ( x *(1/ z ) ^n ,n , -M , N ) ;
9 limit ( X ) ;
10 disp (X , ’ X = ’ ) ;
1 //EXAMPLE 6 . 9
2 // D e t e r m i n a t i o n o f ROC
3 clc ;
4 clear ;
5 z = %z ;
6 a =2* z ^4+16* z ^3+44* z ^2+56* z +32;
7 b =3* z ^4+3* z ^3 -15* z ^2+18* z -12;
8 [ h1 , g1 ]= factors ( a ) ;
9 [ h2 , g2 ]= factors ( b ) ;
10 disp ( h1 , ’ h1 = ’ ) ;
11 disp ( h2 , ’ h2 = ’ ) ;
12 c=a/b;
13 disp (c , ’ f u n c t i o n i s = ’ ) ;
14 plzr ( c ) ;
Scilab code Exa 6.10 Rational form of z Transform from its zero and pole locations
1 //EXAMPLE 6 . 1 0
2 //Z−t r a n s f o r m from p o l e −z e r o locations
3 clc ;
48
4 clear ;
5 z = %z ;
6 // u s i n g t h e p o l e & z e r o l o c a t i o n s p r o v i d e d
7 num =( z -0.21) *( z -3.14) *( z -( -0.3+ %i *0.5) ) *( z -( -0.3 - %i
*0.5) ) ;
8 den =( z +0.45) *( z -0.67) *( z -(0.81+ %i *0.72) ) *( z -(0.81 - %i
*0.72) ) ;
9 k =2.2;
10 Gz =( num / den ) ;
11 disp ( k * Gz , ’ Gz = ’ ) ;
1 //EXAMPLE 6 . 1 1
2 // I n v e r s e Z−t r a n s f o r m
3 clc ;
4 clear ;
5 syms n z1 ;
6 z = %z ;
7 num = z ; // g i v e n | z | > 1 ;
8 den = (z -1) ^2;
9
10
11 // Power s e r i e s e x p a n s i o n
12 x = ldiv ( num , den ,20) ;
13 disp (x , ’ x = ’ ) ;
14 disp ( ’ x = n∗u [ n ] ’ ) ;
1 //EXAMPLE 6 . 1 2
2 // I n v e r s e Z−t r a n s f o r m
3 clear ;
49
4 clc ;
5 z = %z ;
6 num = 0.5* z ;
7 den = z ^2 -z + 0.25;
8 func = num / den ;
9 v = factors ( den ) ;
10 disp (v , ’ f a c t o r s a r e = ’ ) ;
11 h1 = ldiv ( num , den ,10) ;
12 disp ( h1 , ’ h = ’ ) ;
13 // u s i n g t h e p r o p e r t y o f z−t r a s n f o r m
14 disp ( ’ o b s e r v i n g v ( 1 ) & v ( 2 ) we c o n c l u d e h=n
∗ ( 0 . 5 ) ˆn ’ )
1 //EXAMPLE 6 . 1 3
2 // D e t e r m i n i n g p r o p e r f r a c t i o n
3 clc ;
4 clear ;
5 z = %z ;
6 num = 2* z ^3 + 0.8* z ^2 + 0.5* z +0.3;
7 den = ( z ^3 + 0.8* z ^2 + 0.2* z ) ;
8 func = num / den ;
9 disp ( func , ’ t h e p o l y n o m i a l f u n c t i o n i s H = ’ )
10
11 if degree ( num ) >= degree ( den )
12 disp ( ’ An i m p r o p e r f r a c t i o n ’ ) ;
13 else disp ( ’A p r o p e r f r a c t i o n ’ ) ;
14 end
15
16 disp ( ’ d e c o m p o s i n g t h e f r a c t i o n we g e t . . . . . ’ );
17
18 H1 = func -( -3.5* z + 1.5) / z ;
19 disp ( H1 , ’ H1 = ’ ) ;
20 disp ( ’ H1 i s a P r o p e r f r a c t i o n ’ )
50
Scilab code Exa 6.14 Inverse z Transform by partial fraction expansion
1 //EXAMPLE 6 . 1 4
2 // I n v e r s e Z−t r a n s f o r m
3 clear ;
4 clc ;
5 z = %z ;
6 num = z *( z +2) ;
7 den =( z -0.2) *( z +0.6) ;
8 H = num / den ;
9 elts = factors ( den ) ;
10 disp ( elts ) ;
11 // s o l v i n g P a r t i a l F r a c t i o n s , we g e t :
12 Hz = 2.75/(1 -(0.2) / z ) - 1.75/(1+(0.6) / z ) ;
13 disp ( Hz ) ;
14 // d i s p ( h = 2 . 7 5 ∗ ( 0 . 2 ˆ n ) − 1 . 7 5 ∗ ( 0 . 6 ˆ n ) ∗u ( n ) ) ;
15 h1 = ldiv (2.75* z ,( z -(0.2) ) ,10)
16 disp ( h1 /2.75 , ’ h1 = ’ ) ;
17 h1 = ldiv (1.75* z ,( z +(0.6) ) ,10)
18 disp ( h1 /1.75 , ’ h2 = ’ ) ;
19 disp ( ’ t h e i n v e r s e z−t r a n s f o r m i s : ’ )
20 disp ( ’ h = 2 . 7 5 ∗ ( 0 . 2 ˆ n ) ∗u ( n ) − 1 . 7 5 ∗ ( − 0 . 6 ˆ n ) ∗u ( n ) ’ )
Scilab code Exa 6.15 residue computation using coefficient matching approach
1 //EXAMPLE 6 . 1 5
2 // s o l v i n g f o r c o e f f i c i e n t s ;
3 clear ;
4 clc ;
5 z = %z ;
6 num = z *( z +2) ;
51
7 den =( z -0.2) *( z +0.6) ;
8 H = num / den ;
9 disp ( ’ t h e f a c t o r s a r e : ’ ) ;
10 elts = factors ( den ) ;
11 disp ( elts ) ;
12 // c o e f f a r e :
13 disp ( ’ The c o e f f i c i e n t s a r e p1 , p2 : ’ ) ;
14 p1 = horner (( z +2) /( z +0.6) ,0.2) ;
15 disp ( p1 , ’ p1 = ’ ) ;
16 p2 = horner (( z +2) /( z -0.2) , -0.6) ;
17 disp ( p2 , ’ p2 = ’ ) ;
1 //EXAMPLE 6 . 1 6
2 // P a r t i a l f r a c t i o n e x p a n s i o n
3
4 clc ;
5 clear ;
6 z = %z ;
7 num = z ^3;
8 den = 18* z ^3 + 3* z ^2 - 4* z - 1;
9 elts = factors ( den ) ;
10 disp ( elts , ’ t h e f a c t o r s a r e : ’ ) ;
11 func = num / den ;
12 // t h e p a r t i a l f r a c t i o n g i v e s :
13 p1 = horner ((1/(1+0.3333333/ z ) ^2) ,0.5) ;
14 disp ( p1 , ’ p1 = ’ ) ;
15 p2 = horner (1/((1 -0.5/ z ) ) , -0.3333333) ;
16 disp ( p2 , ’ p2 = ’ ) ;
17 p3 = horner (0.6/((1 -0.5/ z ) ) , -0.3333333) ;
18 disp ( p3 , ’ p3 = ’ ) ;
19 disp ( ’ p a r t i a l f r a c t i o n g i v e s : ’ ) ;
20 disp ( p1 * z / elts (1) , ’ h1 = ’ ) ;
21 disp ( p3 * z / elts (3) , ’ h2 = ’ ) ;
52
22 disp ( p2 * z ^2/( elts (2) * elts (2) ) , ’ h3 = ’ ) ;
1 //EXAMPLE 6 . 1 6
2 // C o e f f i c i e n t s o f R a t i o n a l form
3
4 clc ;
5 clear ;
6 z = %z ;
7 num = 18* z ^3;
8 den = 18* z ^3 + 3* z ^2 - 4* z - 1;
9 disp ( coeff ( num ) /18 , ’ t h e Numerator p o l y n o m i a l
c o e f f i c i e n t s a r e : ’ );
10 disp ( coeff ( den ) /18 , ’ t h e d e n o m i n a t o r p o l y n o m i a l
c o e f f i c i e n t s a r e : ’ );
1 //EXAMPLE 6 . 1 8
2 // I n v e r s e Z−t r a n s f o r m u s i n g power s e r i e s expansion
3 clc ;
4 clear ;
5 z = %z ;
6 Xnum = z ;
7 Xden =( z -1) ^2;
8 xn = ldiv ( Xnum , Xden ,15) ;
9 disp ( xn , ’ The f u n c t i o n i s = ’ ) ;
10 disp ( ’ Thus , xn = n∗u ( n ) ’ ) ;
53
Scilab code Exa 6.19 Inverse z Transform using long division
1 //EXAMPLE 6 . 1 9
2 // I n v e r s e Z−t r a n s f o r m u s i n g Long d i v i s i o n method
3 clc ;
4 clear ;
5 z = %z ;
6 Hnum = z ^2 + 2* z ;
7 Hden = z ^2 + 0.4* z -0.12;
8 hn = ldiv ( Hnum , Hden ,20) ;
9 disp ( hn , ’ The f u n c t i o n i s , hn = ’ ) ;
1 //EXAMPLE 6 . 2 0
2 // I n v e r s e Z−t r a n s f o r m u s i n g power s e r i e s e x p a n s i o n
3 clc ;
4 clear ;
5 z = %z ;
6 Hnum = z ^2 + 2* z ;
7 Hden = z ^2 + 0.4* z -0.12;
8 hn = ldiv ( Hnum , Hden ,20) ;
9 disp ( hn , ’ The i m p u l s e r e s p o n s e i s , hn = ’ ) ;
1 // Example 6 . 2 2
2 //MAXIMA SCILAB TOOLBOX REQUIRED FOR THIS PROGRAM
3 //Z t r a n s f o r m o f r n . c o s ( w o n )
4 clc ;
5 clear ;
6 syms r wo n z ;
7 x1 =( r ^ n ) * exp ( %i * wo * n ) ;
54
8 X1 = nusum ( x1 *( z ^ - n ) ,n ,0 , %inf ) ;
9 x2 =( r ^ n ) * exp ( - %i * wo * n ) ;
10 X2 = nusum ( x2 *( z ^ - n ) ,n ,0 , %inf ) ;
11 X =( X1 + X2 ) /2 ;
12 disp (X , ’X( z )= ’ ) ;
13 disp ( ’ROC : | z |> r ’ ) ;
1 // Example 6 . 2 3
2 //MAXIMA SCILAB TOOLBOX REQUIRED FOR THIS PROGRAM
3 //Z t r a n s f o r m o f w( n ) =( ( − 0 .5 ) ˆ ( n−2) + ( 0 . 2 ) ˆ ( n −1) ) ∗
u(n)
4
5 syms n z ;
6 w1 = 4*( -0.5) ^ n
7 W1 = nusum ( w1 ,n ,0 , %inf ) ;
8 disp ( W1 , ’ROC = | z |> 0 . 5 ,W1 = ’ ) ;
9 w2 = 5*(0.2) ^ n
10 W2 = nusum ( w2 ,n ,0 , %inf ) ;
11 disp ( W2 , ’ ROC = | z |> 0 . 2 , W2 = ’ ) ;
12 disp ( W1 + W2 , ’ The Z−T r a n s f o r m i s = ’ ) ;
13 disp ( ’ROC = | z | > 0 . 5 ’ ) ;
1 // Example 6 . 2 4
2 //MAXIMA SCILAB TOOLBOX REQUIRED FOR THIS PROGRAM
3 //Z t r a n s f o r m o f v ( n ) =(a ) ˆ ( n ) ∗u ( n ) − ( b ) ˆ ( n ) ∗u(−n
−1)
4 clc ;
5 clear ;
6 disp ( ’ a s s u m i n g | b | > a ’ );
55
7 syms a n ;
8 x1 = a ^ n ;
9 X1 = nusum ( x1 ,n ,0 , %inf ) ;
10 x1 = b ^ n ;
11 X1 = nusum ( x2 ,n , - %inf , -1) ;
12 Vz = X1 + X2 ;
13 disp ( Vz , ’ The Z−t r a n s f o r m i s = ’ ) ;
14 disp ( ’ROC = | a | < | z | < | b | ’ ) ;
1 // Example 6 . 2 5
2 //Z t r a n s f o r m o f Vz , d0 ∗ v [ n ] + d1 ∗ v [ n −1] = p0 ∗d [ n ]
+ p1 ∗d [ n − 1 ] ;
3 //MAXIMA SCILAB TOOLBOX REQUIRED FOR THIS EXAMPLE
4 clc ;
5 clear ;
6 syms p0 p1 d0 d1 ;
7 z = %z ;
8 disp ( ’ g i v e n t h a t v [ n ] −−> V( z ) . U s i n g Time s h i f t i n g
p r o p e r t y , we g e t : ’ )
9 disp ( ’ d0 ∗Vz + d1 ∗Vz ∗ ( 1 / z ) = p0 + p1 ∗ ( 1 / z ) ’ ) ;
10 disp ( ’ R e a r r a n g i n g t h e t e r m s . . . ’ ) ;
11 Vz = ( p0 + p1 / z ) /( d0 + d1 / z ) ;
12 disp ( Vz , ’ Z−t r a n s f o r m i s Vz = ’ ) ;
1 // Example 6 . 2 6
2 //MAXIMA SCILAB TOOLBOX
3 //Z t r a n s f o r m o f ( n+1) ∗ a ˆ n∗u ( n )
4
5 clear ;
56
6 clc ;
7 syms a n z ;
8 x1 =( a ) ^ n ;
9 X1 = symsum ( x1 *( z ^( - n ) ) ,n ,0 , %inf ) ;
10 X2 = -z *( diff (X ,z ,1) ) ;
11 X = X1 + X2 ;
12 disp ( X , ’ Z transform o f ( n+1) ∗ a ˆ n∗u ( n ) i s X = ’ ) ;
1 // Example 6 . 2 7
2 // i n v e r s e Z−t r a n s f o r m o f z ˆ 3 / ( z − 0 . 5 ) ∗ ( z +1/3) ˆ 2 ;
3
4 clear ;
5 clc ;
6 z = %z ;
7 Gnum = z ^3;
8 Gden = (z -0.5) *( z +1/3) ^2;
9 G = Gnum / Gden ;
10 g1 = ldiv ( Gnum , Gden ,10) ;
11 elts = factors ( Gden ) ;
12
13 // t h e p a r t i a l f r a c t i o n g i v e s :
14 p1 = horner ((1/(1+0.3333333/ z ) ^2) ,0.5) ;
15 disp ( p1 , ’ p1 = ’ ) ;
16 p2 = horner (1/((1 -0.5/ z ) ) , -0.3333333) ;
17 disp ( p2 , ’ p2 = ’ ) ;
18 p3 = horner (0.6/((1 -0.5/ z ) ) , -0.3333333) ;
19 disp ( p3 , ’ p3 = ’ )
20 disp ( ’ p a r t i a l f r a c t i o n g i v e s : ’ ) ;
21 disp ( p1 * z / elts (1) , ’ h1 = ’ ) ;
22 disp ( p3 * z / elts (3) , ’ h2 = ’ ) ;
23 disp ( p2 * z ^2/( elts (2) * elts (2) ) , ’ h3 = ’ ) ;
24 disp ( ’ gn = 0 . 3 6 ∗ ( 0 . 5 ) ˆ n + 0 . 2 4 ∗ ( − 1 / 3 ) ˆ n + 0 . 4 ∗ ( n+1)
∗( −1/3) ˆ n ’ )
57
25 disp ( g1 , ’ t h e f i r s t 10 s a m p l e s o f g [ n ] = ’ ) ;
1 // Example 6 . 2 8
2 // E n l a r g e m e n t o f ROC by p o l e −z e r o c a n c e l l a t i o n
3 clc ;
4 clear ;
5 z = %z ;
6 Gz = (2 + 1.2*(1/ z ) ) /(1 - 0.2*(1/ z ) )
7 disp ( Gz , ’ Gz = ’ ) ;
8 disp ( ’ROC = | z | > 0 . 2 ’ ) ;
9 Hz = 3/(1 + 0.6*(1/ z ) ) ;
10 disp ( Hz , ’ Hz = ’ ) ;
11 disp ( ’ROC = | z | > 0 . 6 ’ ) ;
12 Xz = Gz * Hz ;
13 disp ( Xz , ’ Xz = ’ ) ;
14 disp ( ’ROC = | z | > 0 . 2 ’ ) ;
1 //EXAMPLE 6 . 3 0
2 //PROGRAM REQUIRES MAXIMA SCILAB TOOLBOX
3 //USE Z−TRANSFORM TO EVALUATE CONVOLUTION OF TWO
SEQUENCES :
4 clc ;
5 clear ;
6 syms n z ;
7 x = [ -2 0 1 -1 3];
8 h = [1 2 0 -1 0];
9
10 for n =0:4
11 X ( n +1) = x ( n +1) * z ^( - n ) ;
58
12 H ( n +1) = h ( n +1) * z ^( - n ) ;
13 end
14 disp (X ’ , ’X = ’ ) ;
15 disp (H ’ , ’H = ’ ) ;
16
17 for i =1:5
18 U ( i ) =0;
19 for j =1:5
20 U(i)=U(i)+X(i)*H(j);
21 end
22 end
23 Y =0;
24 for i =1:5;
25 Y = Y + U(i);
26 end
27 disp (Y , ’Y = ’ ) ;
28
29 disp ( ’ y = [ −2 −4 1 3 1 5 1 −3] ’ )
1 //EXAMPLE 6 . 3 1
2 //PROGRAM REQUIRES MAXIMA SCILAB TOOLBOX
3 //USE Z−TRANSFORM TO EVALUATE CONVOLUTION OF TWO
SEQUENCES :
4 clc ;
5 clear ;
6 syms n z ;
7 x = [3 -2 4];
8 h = [4 2 -1];
9
10 for n = -1:1
11 X ( n +2) = x ( n +2) *( z ^ - n ) ;
12 end
13 disp (X ’ , ’X = ’ ) ;
59
14
15 for n =0:2
16 H ( n +1) = h ( n +1) *( z ^ - n ) ;
17 end
18 disp (H ’ , ’H = ’ ) ;
19
20
21 for i =1:3
22 U ( i ) =0;
23 for j =1:3
24 U(i)=U(i)+X(i)*H(j);
25 end
26 end
27 Y =0;
28 for i =1:3;
29 Y = Y + U(i);
30 end
31 disp (Y , ’Y = ’ ) ;
32
33 disp ( ’ y = [ 1 2 −2 9 10 −4] ’ ) ;
1 //EXAMPLE 6 . 3 3
2 // T r a n s f e r f u n c t i o n o f moving a v e r a g e f i l t e r
3 clear ;
4 clc ;
5 syms n z M ;
6 x = z ^( - n ) ;
7 H1 = nusum (x ,n ,0 ,M -1) ;
8 H = H1 / M ;
9 disp (H , ’ T r a n s f e r f u n c t i o n , Hz = ’ ) ;
60
Scilab code Exa 6.34 Transfer function determination
1 //EXAMPLE 6 . 3 4
2 // y [ n ]= x [ n −1] − 1 . 2 ∗ x [ n −2] + x [ n −3] + 1 . 3 ∗ y [ n −1]
−1.04∗ y [ n −2] + 0 . 2 2 2 ∗ y [ n −3]
3 // T r a n s f e r f u n c t i o n d e t e r m i n a t i o n
4
5 clc ;
6 clear ;
7 z = %z ;
8 disp ( ’ Given t h e d i f f e r e n c e equation taking
z t r a n s f o r m on b o t h s i d e s : ’ )
9 Yz = z ^2 -1.2* z +1;
10 Xz = z ^3 -1.3* z ^2 + 1.04* z -0.222;
11 Hz = Yz / Xz ;
12 disp ( Hz , ’ The t r a n s f e r f u n c t i o n i s = ’ )
13 elts = factors ( Xz ) ;
14 disp ( elts , ’ f a c t o r s o f Xz a r e = ’ )
15 plzr ( Hz ) ;
61
Chapter 7
1 //EXAMPLE 7 . 1
2 //PROGRAM REQUIRES MAXIMA SCILAB TOOLBOX
3
4 clc ;
5 clear ;
6 syms K a z w ;
7
8 hzden = (1 - a *( z ^ -1) ) ; // 0 <| a | < 1 ;
9 Hz = K / hzden ;
10 disp ( ’ | H( e ˆ ( jw ) ) | ˆ 2 = Kˆ 2 / ( ( 1 + a ) ˆ2 − 2∗ c o s (w) ’ ) ;
11 // c o n s i d e r i n g a>0
12 disp ( ’ ( a t w = %pi ) ,Kˆ2/(1+ a ) ˆ2 < | H| ˆ 2 < Kˆ2/(1 − a )
ˆ 2 , ( at w = 0) ’ );
13 // c o n s i d e r i n g a<0
14 disp ( ’ ( a t w = 0 ) ,Kˆ2/(1+ a ) ˆ2 < | H| ˆ 2 < Kˆ2/(1 − a )
ˆ 2 , ( a t w = %pi ) ’ ) ;
15 disp ( ’ i f K = +/−(1−a ) , o b s e r v e . . . . ’ ) ;
16 disp ( ’ | H( e ˆ ( jw ) ) | <= 1 Hence a Bounded r e a l
f u n c t i o n . ’ );
62
17 //w=0: %pi ;
18 // [ a , b ]= f r e q ( hznum , hzden , w) ;
19 disp ( abs ( Hz ) )
1 //EXAMPLE 7 . 2
2 // | H( e ˆ ( jw ) ) | ˆ 2 = 4 ∗ ( ( 1 . 0 9 + 0 . 6 ∗ cosw ) ∗ ( 1 . 1 6 − 0 . 8 ∗
cosw ) ) / ( ( 1 . 0 4 − 0 . 2 ∗ cosw ) ∗ ( 1 . 2 5 + cosw ) )
3 //REPLACING cosw = ( z + z (ˆ −1) ) /2
4 clc ;
5 clear ;
6 z = %z ;
7 H1 =4*((1.09 + (0.3) *( z +1/ z ) ) *(1.16 - (0.4) *( z +1/ z ) ) )
;
8 H2 =((1.04 - (0.2) *( z +1/ z ) ) *(1.25 + (0.5) *( z +1/ z ) ) ) ;
9 H = H1 / H2 ;
10 disp (H , ’ The t r a n s f e r f u n c t i o n i s , H = ’ )
11 elts1 = factors ( numer ( H ) ) ;
12 disp ( elts1 , ’ The f a c t o r s o f n u m e r a t o r a r e : ’ ) ;
13 elts2 = factors ( denom ( H ) ) ;
14 disp ( elts2 , ’ The f a c t o r s o f d e n o m i n a t o r a r e : ’ ) ;
15 disp ( ’ The Four p o s i b l e s t a b l e t r a n s f e r f u n c t i o n w i t h
same s q u a r e m a g n i t u d e f u n c t i o n a r e : ’ ) ;
16 h1 =2*((1+(0.3) / z ) *(1 - (0.4) / z ) ) /((1 -(0.2) / z )
*(1+(0.5) / z ) ) ;
17 disp ( h1 , ’ s t a b l e t r a n s f e r f u n c t i o n , h1 = ’ ) ;
18 h2 =2*((1+(0.3) / z ) *((0.4) - (1) / z ) ) /((1 -(0.2) / z )
*(1+(0.5) / z ) ) ;
19 disp ( h2 , ’ s t a b l e t r a n s f e r f u n c t i o n , h 2 s = ’ ) ;
20 h3 =2*(((0.3) +1/ z ) *((1) - (0.4) / z ) ) /((1 -(0.2) / z )
*(1+(0.5) / z ) ) ;
21 disp ( h3 , ’ s t a b l e t r a n s f e r f u n c t i o n , h3 = ’ ) ;
22 h4 =2*(((0.3) +1/ z ) *((0.4) - (1) / z ) ) /((1 -(0.2) / z )
*(1+(0.5) / z ) ) ;
63
23 disp ( h4 , ’ s t a b l e t r a n s f e r f u n c t i o n , h4 = ’ ) ;
1 //EXAMPLE 7 . 6
2 // FIR T r a s n f e r f u n c t i o n s w i t h d i f f e r e n t Phase .
3 clc ;
4 clear ;
5 z = %z ;
6 W = 0:(1/400) :1;
7 z = exp ( %i *2* %pi * W ) ;
8 for i =1:401
9 H1z ( i ) = -1+ 2/ z ( i ) - 3/( z ( i ) ^2) + 6/( z ( i ) ^3)
-3/( z ( i ) ^4) +2/( z ( i ) ^5) -1/ z ( i ) ^(6) ;
10 end
11 H1z_phase = phasemag ( H1z ) ;
12
13 clf () ;
14 figure (0) ;
15 plot2d ( W /(2* %pi ) , H1z_phase ,1) ;
16 xtitle ( ’ p h a s e r e s p o n s e ’ , ’W/ ( 2 ∗ %pi ) ’ , ’ H 2 z p h a s e i n
d e g r e e s ’ );
17
18 for i =1:401
19 H2z ( i ) = +1 - 2/ z ( i ) + 3/( z ( i ) ^2) - 6/( z ( i ) ^3) +
3/( z ( i ) ^4) - 2/( z ( i ) ^5) + 1/ z ( i ) ^(6) ;
20 end
21 H2z_phase = phasemag ( H2z ) ;
22
23 plot2d ( W /(2* %pi ) , H2z_phase ,2) ;
24 xtitle ( ’ p h a s e r e s p o n s e ’ , ’W/ ( 2 ∗ %pi ) ’ , ’ H 2 z p h a s e i n
d e g r e e s ’ );
64
Chapter 8
Scilab code Exa 8.1 Analysis of Cascaded lattice digital filter structure
1 //EXAMPLE 8 . 1
2 //MAXIMA SCILAB TOOLBOX REQUIRED FOR THIS EXAMPLE
3 // D i g i t a l f i l t e r s t r u c t u r e
4 clear ;
5 clc ;
6 syms W1 W2 W3 X Y a d B y E z ;
7 // E q u a t i o n s o b t a i n e d a r e a s f o l l o w s :
8 W1 = X - a * W3 / z ;
9 W2 = W1 - d * W2 / z ;
10 W3 = W2 / z + E * W2 ;
11 Y = B * W1 + y * W3 / z ;
12 // S o l v i n g t h e a b o v e e q u a t i o n s :
13 Hz =( B + ( B * d + y * E ) / z + y /( z ^2) ) /(1 + ( d + a * E ) / z + a /( z
^2) )
14 disp ( Hz , ’ Hz = ’ ) ;
65
1 // Example 8 . 6
2 // F a c t o r i z a t i o n o f FIR T r a n s f e r F u n c t i o n
3 clear ;
4 clc ;
5 z = %z ;
6 Hz =50.4+28.02/ z +13.89/ z ^2+7.42/ z ^3+6.09/ z ^4+3/ z ^5+1/
z ^6;
7 disp ( factors ( numer ( Hz ) ) , ’ The F a c t o r s o f t h e FIR
T r a n s f e r Function a r e = ’ );
1 // Example 8 . 7
2 // F a c t o r i z a t i o n o f I I R T r a n s f e r F u n c t i o n
3 clear ;
4 clc ;
5 z = %z ;
6 // Numerator o f t h e t r a n s f e r f u n c t i o n
7 Numz =6+17.1/ z +33.05/ z ^2+24.72/ z ^3+19.908/ z ^4 -5.292/ z
^5+18.144/ z ^6;
8 // Den omin ator o f t h e t r a n s f e r f u n c t i o n
9 Denz =1+2.2/ z +2.56/ z ^2+1.372/ z ^3+0.118/ z ^4 -0.332/ z
^5 -0.168/ z ^6;
10 Fn = factors ( numer ( Numz ) ) ;
11 disp ( Fn , ’ F a c t o r s o f t h e n u m e r a t o r o f t h e T r a n s f e r
Function = ’ );
12 Fd = factors ( numer ( Denz ) ) ;
13 disp ( Fd , ’ F a c t o r s o f t h e d e n o m i n a t o r o f t h e T r a n s f e r
Function = ’ );
Scilab code Exa 8.10 Cascaded lattice realization of IIR digital Transfer Function
1 // Example 8 . 1 0
66
2 // C a s c a d e d l a t t i c e r e a l i z a t i o n o f I I R T r a n s f e r
Function
3 clear ;
4 clc ;
5 z = %z ;
6 P3z = -0.2 + 0.18/ z + 0.4/( z ^2) + 1/( z ^3) ;
7 D3z = 1 + 0.4/ z + 0.18/( z ^2) - 0.2/( z ^3) ;
8 A3z = P3z / D3z ;
9 p1 = coeff ( numer ( P3z ) ) ;
10 p = mtlb_fliplr ( p1 ) ;
11 disp ( mtlb_fliplr ( p ) , ’ The c o e f f i c i e n t s o f n u m e r a t o r
a r e = ’ );
12 d1 = coeff ( numer ( D3z -1) ) ;
13 d = mtlb_fliplr ( d1 )
14 disp (( d ) , ’ The c o e f f i c i e n t s o f n u m e r a t o r a r e = ’ ) ;
15 d1_1dash =( d (1) -d (3) * d (2) ) /(1 - d (3) * d (3) ) ;
16 disp ( d1_1dash , ” d 1 1 d a s h = ” ) ;
17 d2_1dash =( d (2) -d (3) * d (1) ) /(1 - d (3) * d (3) ) ;
18 disp ( d2_1dash , ” d 2 1 d a s h ” ) ;
19 d1_2dash =( d1_1dash ) /(1+ d2_1dash ) ;
20 disp ( d1_2dash , ” d 1 2 d a s h = ” ) ;
21 A1z =( d1_2dash + 1/ z ) /(1 + d1_2dash / z ) ;
22 disp ( A1z , ’ A1z = ’ ) ;
23 A2z =( d2_1dash + d1_1dash *1/ z + 1/ z ^2) /(1 + d1_1dash /
z - d2_1dash / z ^2) ;
24 disp ( A2z , ’ A2z = ’ ) ;
1 // Example 8 . 1 2
2 // Gray M a r k e l method o f R e a l i s a t i o n
3 clear ;
4 clc ;
5 z = %z ;
6 P3z = 0 + 0.44/ z + 0.362/( z ^2) +0.02/( z ^3) ;
67
7 D3z = 0.4/ z + 0.18/( z ^2) - .2/( z ^3) ;
8 Hz = P3z / D3z ;
9 p1 = coeff ( numer ( P3z ) ) ;
10 p = mtlb_fliplr ( p1 )
11 disp ( mtlb_fliplr ( p ) , ’ The c o e f f i c i e n t s o f n u m e r a t o r
a r e = ’ );
12 d1 = coeff ( numer ( D3z ) ) ;
13 d = mtlb_fliplr ( d1 )
14 disp ( mtlb_fliplr ( d ) , ’ The c o e f f i c i e n t s o f n u m e r a t o r
a r e = ’ );
15 d1_1dash =( d (1) -d (3) * d (2) ) /(1 - d (3) * d (3) ) ;
16 disp ( d1_1dash , ” d 1 1 d a s h = ” ) ;
17 d2_1dash =( d (2) -d (3) * d (1) ) /(1 - d (3) * d (3) ) ;
18 disp ( d2_1dash , ” d 2 1 d a s h ” ) ;
19 d1_2dash =( d1_1dash ) /(1+ d2_1dash ) ;
20 disp ( d1_2dash , ” d 1 2 d a s h = ” ) ;
21 a1 = p (3) ;
22 disp ( p (3) , ’ a1 = ’ ) ;
23 a2 = p (2) - a1 * d (1) ;
24 disp ( p (2) - a1 * d (1) , ’ a2 = ’ ) ;
25 a3 = p (1) - a1 * d (2) - a2 * d1_1dash ;
26 disp ( p (1) - a1 * d (2) - a2 * d1_1dash , ’ a3 = ’ ) ;
27 disp (0 - a1 * d (3) - a3 * d1_2dash - a2 * d2_1dash , ’ a4 = ’ ) ;
1 // Example 8 . 1 8
2 // C a s c a d e d l a t t i c e r e a l i z a t i o n o f Power−s y m m e t r i c
FIR T r a n s f e r F u n c t i o n
3 clear ;
4 clc ;
5 z = %z ;
6
7 H5z =(1 + 0.3/ z + 0.2/ z ^2 - 0.376/ z ^3 - 0.06/ z ^4 +
0.2/ z ^5) ;
68
8 disp ( H5z , ’ FIR f i l t e r = ’ ) ;
9 G5 = horner ( H5z , -1/ z ) ;
10 G5z = G5 / z ^5;
11 disp ( G5z , ’ FIR f i l t e r = ’ ) ;
12 k5 =0.2;
13 H3z =(1/(1+ k5 ^2) ) *( H5z - k5 * G5z ) ;
14 disp ( H3z , ’ S y n t h e s i s eqn , H3z = ’ ) ;
15 G3z =(1/(1+ k5 ^2) ) *( k5 * H5z + G5z ) ;
16 disp ( G3z , ’ S y n t h e s i s eqn , G3z = ’ ) ;
17 k = coeff ( numer ( G3z ) ) ;
18 disp ( k (4) , ’ k3 = ’ ) ;
19 disp ( k (2) , ’ k1 = ’ ) ;
69
Chapter 9
1 //EXAMPLE 9 . 1
2 // p a s s band & s t o p band r i p p l e
3 clc ;
4 clear ;
5 ap =0.1; // peak p a s s b a n d r i p p l e i n dB
6 as =35; // min . s t o p b a n d a t t e n u a t i o n i n dB
7
8 // c a l c u l a t i o n o f peak r i p p l e v a l u e s
9 dp =1 -10^ -( ap /20) ;
10 disp ( dp , ’ dp = ’ ) ;
11 ds =10^ -( as /20) ;
12 disp ( ds , ’ d s = ’ ) ;
Scilab code Exa 9.2 conversion of bandedged frequencies to Normalized digital freq
1 //EXAMPLE 9 . 2
2 // a n a l o g p a s s b a n d & s t o p b a n d f r e q u e n c i e s ( i n KHz) :
3 clc ;
70
4 clear ;
5 ap =7;
6 as =3;
7 // S a m p l i n g f r e q u e n c y ( i n KHz) :
8 FT =25;
9 // d i g i t a l f r e q u e n c i e s :
10 wp =2* %pi * ap / FT ;
11 disp ( wp , ’ wp = ’ ) ;
12 ws =2* %pi * as / FT ;
13 disp ( ws , ’ ws = ’ ) ;
1 // Example 9 . 3
2 // D e s i g n o f HP I I R f i l t e r
3 clc ;
4 clear ;
5 Fp =700 // Hz
6 Fs =500 // Hz
7 ap =1 //dB
8 as =32 //dB
9 FT =2000 // Hz
10 // n o r m a l i z e d a n g u l a r e d g e f r e q u e n c i e s i n r a d / s e c
11 wp =2* %pi * Fp / FT ;
12 ws =2* %pi * Fs / FT ;
13 // p r e w a r p t h e d i g i t a l e d g e f r e q u e n c i e s
14 Ap1 = tan ( wp /2) ;
15 As1 = tan ( ws /2) ;
16
17 Ap =1; // a s s u m i n g
18 As =(2* %pi * Ap1 ) * Ap /(2* %pi * As1 ) ;
19 disp ( As , ’ As = ’ )
20 // Order ’N’ o f t h e f i l t e r
21 k = Ap / As ;
22 disp (1/ k , ’ 1/ k = ’ ) ;
71
23 k1 = 1/( sqrt (((10^(0.1* as ) ) -1) /((10^(0.1* ap ) ) -1) ) ) ;
24 disp (1/ k1 , ’ 1/ k1 = ’ ) ;
25 N = ceil ( acosh (1/ k1 ) / acosh (1/ k ) ) ;
26 disp (N , ’N = ’ ) ;
27 disp (N , ’ Order o f t h e f i l t e r i s , N = ’ ) ;
28
29 e = sqrt (10^(0.1* ap ) -1) ;
30 u =1/ e + sqrt (1+(1/( e * e ) ) ) ;
31 a = Ap *( u ^(1/ N ) - u ^( -1/ N ) ) /2;
32 b = Ap *( u ^(1/ N ) + u ^( -1/ N ) ) /2;
33
34 for i =1: N
35 phi ( i ) = %pi /2 + (2* i -1) *( %pi ) /(2* N ) ;
36 p ( i ) = a * cos ( phi ( i ) ) + %i * b * sin ( phi ( i ) ) ;
37 end
38 s = %s ;
39 z = %z ;
40 H1 =1;
41 // Numerator o f H( s )
42 for i =1: N
43 H1 = H1 *( s + p ( i ) )
44 end
45 // Den omin ator o f H( s )
46 H2 = horner ( H1 ,0) ;
47 // T r a n s f e r f u n c t i o n
48 H = H2 / H1 ;
49 disp (H , ’H = ’ )
50 // B i l n e a r T r a n s f o r m a a t i o n , s =(( z −1/( z +1) ) ;
51 Hz = horner (H ,( z -1) /( z +1) ) ;
52 disp ( Hz , ’ The d i g i t a l HP f i l t e r i s Hz = ’ ) ;
Scilab code Exa 9.6 Changing passband edge frequencies to LP IIR digital frequenci
1 //EXAMPLE 9 . 6
2 //LP TO LP T r a n s f o r m a t i o n
72
3
4 clc ;
5 clear ;
6 z = %z ;
7 w =0:0.001* %pi : %pi ;
8 Glz =(0.0662272*(1+1/ z ) ^3) /((1 -0.2593284/ z )
*(1 -0.6762858/ z +0.3917468/( z ^2) ) )
9 wc =0.25* %pi ; // O r i n g i n a l p a s s b a n d e d g e
10 Wc =0.35* %pi ; // R e q u i r e d p a s s b a n d e d g e
11 l = sin (( wc - Wc ) /2) / sin (( wc + Wc ) /2)
12 disp (l , ’ lambda = ’ ) ;
13 Gdz = horner ( Glz ,((1 - l / z ) /(1/ z - l ) ) ) ;
14 disp ( Gdz , ’ The t r a n s f e r f u n c t i o n i s Gdz = ’ ) ;
Scilab code Exa 9.7 Design of HP IIR Digital Filter from LP Digital Filter
1 //EXAMPLE 9 . 7
2 //LP TO HP T r a n s f o r m a t i o n
3
4 clc ;
5 clear ;
6 z = %z ;
7 Glz =(0.0662272*(1+1/ z ) ^3) /((1 -0.2593284/ z )
*(1 -0.6762858/ z +0.3917468/( z ^2) ) ) ;
8 wc =0.25* %pi ; // O r i n g i n a l p a s s b a n d e d g e
9 Wc =0.55* %pi ; // R e q u i r e d p a s s b a n d e d g e
10 l = - cos (( wc + Wc ) /2) / cos (( wc - Wc ) /2) ;
11 disp (l , ’ lambda = ’ ) ;
12
13 w =0:0.001:1;
14 Ghz = horner ( Glz , -(( z + l ) /(1 + l * z ) ) ) ; //LP TO HP
Transformation formula
15 den = factors ( denom ( Ghz ) ) ;
16 disp ( Ghz , ’ The t r a n s f e r f u n c t i o n i s Gdz = ’ ) ;
17 disp ( den , ’ t h e f a c o r s o f t h e d e n o m i n a t o r a r e = ’ ) ;
73
Scilab code Exa 9.12 Minimum order of Type 2 Chebyshev HP IIR digital filter
1 //EXAMPLE 9 . 1 2
2 // Minimum o r d e r o f type −2 Chebyshev h i g h p a s s d i g i t a l
filter
3 clc ;
4 clear ;
5 ap = 1 //dB
6 as = 40 //dB
7 Fp = 1000 // Hz
8 Fs = 600 // Hz
9 Wp = Fp *2* %pi ;
10 Ws = Fs *2* %pi ;
11
12 F = 4000 // Hz
13 T =1/ F ;
14
15 Ap =(2/ T ) *( tan ( Wp * T /2) )
16 As =(2/ T ) *( tan ( Ws * T /2) )
17
18
19 k = Ap / As ;
20 disp (1/ k , ’ 1/ k = ’ ) ;
21 k1 = 1/( sqrt (((10^(0.1* as ) ) -1) /((10^(0.1* ap ) ) -1) ) ) ;
22 disp (1/ k1 , ’ 1/ k1 = ’ ) ;
23 N = acosh (1/ k1 ) / acosh ( k ) ; // o r d e r o f t h e f i l t e r
24 disp (N , ’N = ’ ) ;
25 disp ( ’ S i n c e o r d e r o f t h e f i l t e r i s a l w a y s an i n t e g e r
, ’ );
26 disp ( ceil ( N ) , ’ Order o f t h e f i l t e r i s , N = ’ ) ;
74
Chapter 10
1 // Example 1 0 . 0 1
2 // Order e s t i m a t i o n u s i n g K a i s e r ’ s f o r m u l a
3 clear ;
4 clc ;
5 Fp =1800; // Passband e d g e f r e q . i n Hz
6 Fs =2000; // s t o p b a n d e d g e f r e q . i n Hz
7 ap =0.1; // peak p a s s b a n d r i p p l e i n dB
8 as =35; // min . s t o p b a n d a t t e n u a t i o n i n dB
9 FT =12000; // S a m p l i n g f r e q . i n Hz
10
11 // c a l c u l a t i o n o f peak r i p p l e v a l u e s
12 dp =1 -10^ -( ap /20) ;
13 disp ( dp , ’ dp = ’ ) ;
14 ds =10^ -( as /20) ;
15 disp ( ds , ’ d s = ’ ) ;
16
17 // Order o f t h e FIR f i l t e r
18 N =( -(20* log10 ( sqrt ( ds * dp ) ) ) - 13) /((14.6) *( Fs - Fp ) / FT
);
19 disp ( ceil ( N ) , ’ Order o f t h e f i l t e r i s N = ’ )
75
Scilab code Exa 10.2 Bellenger formula
1 // Example 1 0 . 0 1
2 // Order e s t i m a t i o n u s i n g B e l l a n g e r ’ s f o r m u l a
3 clear ;
4 clc ;
5 Fp =1800; // Passband e d g e f r e q . i n Hz
6 Fs =2000; // s t o p b a n d e d g e f r e q . i n Hz
7 ap =0.1; // peak p a s s b a n d r i p p l e i n dB
8 as =35; // min . s t o p b a n d a t t e n u a t i o n i n dB
9 FT =12000; // S a m p l i n g f r e q . i n Hz
10
11 // c a l c u l a t i o n o f peak r i p p l e v a l u e s
12 dp =1 -10^ -( ap /20) ;
13 disp ( dp , ’ dp = ’ ) ;
14 ds =10^ -( as /20) ;
15 disp ( ds , ’ d s = ’ ) ;
16
17 // Order o f t h e FIR f i l t e r
18 N =(( -2* log10 (10* ds * dp ) ) /((3) *( Fs - Fp ) / FT ) ) -1 ;
19 disp ( ceil ( N ) , ’ Order o f t h e f i l t e r i s N = ’ )
1 // Example 1 0 . 0 3
2 // Order e s t i m a t i o n u s i n g Hermann ’ s f o r m u l a
3 clear ;
4 clc ;
5 Fp =1800; // Passband e d g e f r e q . i n Hz
6 Fs =2000; // s t o p b a n d e d g e f r e q . i n Hz
7 ap =0.1; // peak p a s s b a n d r i p p l e i n dB
8 as =35; // min . s t o p b a n d a t t e n u a t i o n i n dB
76
9 FT =12000; // S a m p l i n g f r e q . i n Hz
10
11 // c a l c u l a t i o n o f peak r i p p l e v a l u e s
12 dp =1 -10^ -( ap /20) ;
13 disp ( dp , ’ dp = ’ ) ;
14 ds =10^ -( as /20) ;
15 disp ( ds , ’ d s = ’ ) ;
16
17 a1 =0.005309;
18 a2 =0.07114;
19 a3 = -0.4761;
20 a4 =0.00266;
21 a5 =0.5941;
22 a6 =0.4278;
23 D_infi =(( a1 *( log10 ( dp ) ^2) + a2 * log10 ( dp ) + a3 ) * log10
( ds ) ) -( a4 *( log10 ( dp ) ) ^2 + a5 *( log10 ( dp ) ) + a6 ) ;
24 disp ( D_infi , ’ D i n f i = ’ ) ;
25 b1 =11.01217;
26 b2 =0.51244;
27 F = b1 + b2 *(( log10 ( dp ) ) -( log10 ( ds ) ) ) ;
28 disp (F , ’ F = ’ ) ;
29
30 // Order o f t h e FIR f i l t e r
31 N =( D_infi - F *(( Fs - Fp ) / FT ) ^2) /(( Fs - Fp ) / FT ) ;
32 disp ( ceil ( N ) , ’ Order o f t h e f i l t e r i s N = ’ )
1 // Example 1 0 . 0 4
2 // K a i s e r ’ s f o r m u l a f o r b a n d p a s s f i l t e r
3 clear ;
4 clc ;
5 Fp1 =300; // Passband e d g e f r e q . i n Hz
6 Fs1 =350; // s t o p b a n d e d g e f r e q . i n Hz
7 Fp2 =1000; // Passband e d g e f r e q . i n Hz
77
8 Fs2 =1100; // s t o p b a n d e d g e f r e q . i n Hz
9
10 dp =0.004; // p a s s b a n d r i p p l e i n dB
11 ds =0.01; // s t o p b a n d r i p p l e i n dB
12 FT =10000; // S a m p l i n g f r e q . i n Hz
13
14 // S i n c e ( Fp1−Fs1 ) <(Fs2−Fp2 ) , b a n d w i t h u s e d i s ( Fp1−
Fs1 )
15
16 // Order o f t h e FIR f i l t e r
17 N =( -(20* log10 ( sqrt ( ds * dp ) ) ) - 13) /((14.6) *( Fs1 - Fp1 ) /
FT ) ;
18 disp ( ceil ( N ) , ’ Order o f t h e f i l t e r is N = ’)
Scilab code Exa 10.6 Filter length estimation for window based design
1 //EXAMPLE 1 0 . 6
2 // F I l t e r l e n g t h f o r window −b a s e d d e s i g n
3 clear ;
4 clc ;
5 wp =0.3* %pi ; // r a d / s e c
6 ws =0.5* %pi ; // r a d / s e c
7 as =40; //dB
8
9 wc =( wp + ws ) /2; // c u t o f f f r e q u e n c y
10 Bw = ws - wp ;
11 disp ( Bw , ’ N o r m a l i z e d t r a n s i t i o n bandwidth i s = ’ )
12 // Hann window
13 M1 =3.11* %pi / Bw ;
14 disp ( M1 , ’ V a l u e o f M = ’ )
15 // Hamming window
16 M2 =3.32* %pi / Bw ;
17 disp ( M2 , ’ V a l u e o f M = ’ )
18 // Blackman window
19 M3 =5.56* %pi / Bw ;
78
20 disp ( M3 , ’ V a l u e o f M = ’ )
1 //EXAMPLE 1 0 . 6
2 // Order e s t i m a t i o n u s i n g Dolph−Cebyshev window
3 clear ;
4 clc ;
5 wp =0.3* %pi ; // r a d / s e c
6 ws =0.5* %pi ; // r a d / s e c
7 as =40; //dB
8
9 wc =( wp + ws ) /2; // c u t o f f f r e q u e n c y
10 Bw = ws - wp ;
11 disp ( Bw , ’ N o r m a l i z e d t r a n s i t i o n bandwidth i s = ’ )
12
13 // Order o f t h e f i l t e r
14 N = ((2.056* as ) - 16.4) /(2.285* Bw ) ;
15 disp ( ceil ( N ) , ’ Order o f t h e f i l t e r , N = ’)
1 //EXAMPLE 1 0 . 8
2 // D e s i g n o f LP FIR f i l t e r u s i n g K a i s e r window
3 clear ;
4 clc ;
5 wp =0.3* %pi ; // r a d / s e c
6 ws =0.5* %pi ; // r a d / s e c
7 as =40; //dB
8 wc =( wp + ws ) /2; // c u t o f f f r e q u e n c y
9 Bw = ws - wp ;
10 disp ( Bw , ’ N o r m a l i z e d t r a n s i t i o n bandwidth i s = ’ )
11
79
12 ds =10^( - as /20) ;
13 B = (0.5842*( as -21) ^0.4) + 0.07886*( as -21) ;
14
15 N = ceil (( as - 8) /(2.285* Bw ) ) ;
16 disp (N , ’ Order o f t h e f i l t e r , N = ’ ) ;
17 M =( N -1) *0.5;
18 disp (M , ’M = ’ ) ;
19 w = window ( ’ k r ’ ,N ,6) ; // K a i s e r window
20 i = - M :1: M ;
21
22 hn =( wc / %pi ) * sinc ( wc *i ’/( %pi ) ) ;
23 h = hn * w ;
24
25 clf () ;
26 n =0:0.001:1;
27 [H , fr ]= frmag (w ,1001) ;
28
29 plot2d (2* fr , log10 ( H ./ max ( H ) ) , style = color ( ’ b l u e ’ ) )
30 set ( gca () , ’ g r i d ’ ,[1 1]* color ( ’ g r a y ’ ) )
31 a = gca () ;
32 xlabel ( ’w/ %pi ’ ) ;
33 ylabel ( ’ Magnitude i n dB ’ ) ;
34 title ( ’ Gain R e s p o n s e o f K a i s e r Window ’ ) ;
80
Chapter 11
Scilab code Exa 11.3 Reconstruction of Transfer function from Impulse response coe
1 // e x a m p l e 1 1 . 3
2 // R e c o n s t r u c t i o n o f T r a n s f e r f u n c t i o n from I m p u l s e
response coeff .
3 clear ;
4 clc ;
5 z = %z ;
6 numz =2+6/ z +3/( z ^2) ;
7 denz =(1+1/ z +2/ z ^2) ;
8 disp ( numz / denz , ’ Hz = ’ ) ;
9 d = coeff ( numer ( denz ) ) ;
10 disp (d , ’ c o e f f i c i e n t s o f t h e d e n o m i n a t o r a r e = ’ )
11 h1 = ldiv ( numer ( numz ) , numer ( denz ) ,5) ;
12 disp ( h1 ’ , ’ The f i r s t f i v e c o e f f c i e n t s a r e o f H( z ) = ’
);
13 for i =1:3
14 for j =1:3
15 if i >= j
16 h (i , j ) = h1 (i - j +1)
17 else
18 h (i , j ) =0;
19 end
81
20 end
21 end
22 disp (h , ’ h = ’ ) ;
23 disp (( h ’* d ’) ’, ’ c o e f f i c i e n t s o f the numerator are = ’
);
1 // Example 1 1 . 1 1
2 // S i m u l a t i o n o f I I R c a s c a d e d l a t t i c e f i l t e r
structure
3 clear ;
4 clc ;
5 z = %z ;
6 P3z = 0 + 0.44/ z + 0.362/( z ^2) +0.02/( z ^3) ;
7 D3z = 1 + 0.4/ z + 0.18/( z ^2) - 0.2/( z ^3) ;
8 Hz = P3z / D3z ;
9 p1 = coeff ( numer ( P3z ) ) ;
10 p = mtlb_fliplr ( p1 )
11 disp ( mtlb_fliplr ( p ) , ’ The c o e f f i c i e n t s o f n u m e r a t o r
a r e = ’ );
12 d1 = coeff ( numer ( D3z -1) ) ;
13 d = mtlb_fliplr ( d1 )
14 disp ( mtlb_fliplr ( d ) , ’ The c o e f f i c i e n t s o f n u m e r a t o r
a r e = ’ );
15 d1_1dash =( d (1) -d (3) * d (2) ) /(1 - d (3) * d (3) ) ;
16 disp ( d1_1dash , ” d 1 1 d a s h = ” ) ;
17 d2_1dash =( d (2) -d (3) * d (1) ) /(1 - d (3) * d (3) ) ;
18 disp ( d2_1dash , ” d 2 1 d a s h ” ) ;
19 d1_2dash =( d1_1dash ) /(1+ d2_1dash ) ;
20 disp ( d1_2dash , ” d 1 2 d a s h = ” ) ;
21 a1 = p (3) ;
22 disp ( p (3) , ’ a1 = ’ ) ;
23 a2 = p (2) - a1 * d (1) ;
24 disp ( p (2) - a1 * d (1) , ’ a2 = ’ ) ;
82
25 a3 = p (1) - a1 * d (2) - a2 * d1_1dash ;
26 disp ( p (1) - a1 * d (2) - a2 * d1_1dash , ’ a3 = ’ ) ;
27 disp (0 - a1 * d (3) - a3 * d1_2dash - a2 * d2_1dash , ’ a4 = ’ ) ;
83
Chapter 12
1 // Example 1 2 . 3
2 // S i g n a l −to −q u a n t i z a t i o n N o i s e r a t i o
3 clear ;
4 clc ;
5 b =[7 9 11 13 15]; // Given v a l u e s o f b
6 K =[4 6 8]; // Given v a l u e s o f K
7 for i =1:5
8 for j =1:3
9 SNR (j , i ) =6.02* b ( i ) +16.81 -20* log10 ( K ( j ) ) ;
10 end
11 end
12 disp ( SNR , ’SNR , A/D = ’ ) ;
84
Chapter 13
1 // Example 1 3 . 1
2 // Upsampling O p e r a t i o n
3 clear ;
4 clc ;
5 clf () ;
6 a = gca () ;
7 figure (0) ;
8 n =[0:0.1:4.9];
9 a . x_location = ” o r i g i n ” ;
10 x = sin ( %pi * n ) ;
11 plot2d3 (n ,x ,2) ;
12 xtitle ( ’ The s i n e wave ’ , ’ n ’ , ’ s i n ( x ) ’ ) ;
13 plot (n ,x , ’ r . ’ ) ;
14 //Up s a m p l i n g
15 //Up s a m p l i n g v a l u e − u s e r i n p u t
16 figure (1) ;
17 L = input ( ” The up s a m p l i n g v a l u e ” ) ;
18
19 a . x_location = ” o r i g i n ” ;
85
20 x1 = sin ( %pi * n / L ) ;
21 plot2d3 (n , x1 ,5) ;
22 plot (n , x1 , ’ r . ’ ) ;
23 xtitle ( ’ The s i n e wave ’ , ’ n ’ , ’ s i n ( x /L ) ’ ) ;
1 // Example 1 3 . 2
2 // Downsampling O p e r a t i o n
3 clear ;
4 clc ;
5 clf () ;
6 a = gca () ;
7 figure (0) ;
8 n =[0:0.1:4.9];
9 a . x_location = ” o r i g i n ” ;
10 x = sin ( %pi * n ) ;
11 plot2d3 (n ,x ,2) ;
12 xtitle ( ’ The s i n e wave ’ , ’ n ’ , ’ s i n ( x ) ’ ) ;
13 plot (n ,x , ’ r . ’ ) ;
14 //Down s a m p l i n g
15 //Down s a m p l i n g − u s e r i n p u t
16 figure (1) ;
17 M = input ( ” The down s a m p l i n g f a c t o r ” ) ;
18 a . x_location = ” o r i g i n ” ;
19 x1 = sin ( %pi * n * M ) ;
20 plot2d3 (n , x1 ,1) ;
21 plot (n , x1 , ’ r . ’ ) ;
22 xtitle ( ’ The s i n e wave ’ , ’ n ’ , ’ s i n ( x ∗M) ’ ) ;
1 // Example 1 3 . 6
86
2 // D e c i m a t o r c o m p u t a t i o n a l c o m p l e x i t y
3 clear ;
4 clc ;
5 // no . o f m u l t i p l i c a t i o n s / s e c =Rm
6 FT = input ( ” S a m p l i n g F r e q u e n c y ” ) ;
7 N = input ( ” The o r d e r o f t h e FIR Hz” ) ;
8 Rm1 = N * FT ;
9 disp ( Rm1 , ’Rm, FIR = ’ ) ;
10 //M = f a c t o r o f Down s a m p l e r
11 M = input ( ” The Down S a m p l i n g f a c t o r ” ) ;
12 disp ( Rm1 /M , ’Rm, FIR−DEC = ’ ) ;
13 K = input ( ” The o r d e r o f t h e I I R Hz” ) ;
14 Rm2 = (2* K + 1) * FT ;
15 disp ( Rm2 , ’Rm, I I R = ’ ) ;
16 disp ( ( K * FT + (( K +1) * FT / M ) ) , ’Rm, IIR−DEC = ’ ) ;
87
Chapter 14
1 // Example 1 4 . 1
2 //EFFECT OF DFT LENGTH ON SPECTRAL ANALYSIS
3 clear ;
4 clc ;
5 N =16;
6 n =0: N -1;
7 f1 =0.22;
8 f2 =0.34;
9
10 R = input ( ”R p o i n t DFT(R E [ 1 6 , 1 2 8 ] ) = ” ) ; //
I n p u t f 1 = 64
11 if R >= N
12 x =0.5*( sin (2* %pi * f1 *n ’) ) + sin ( sin (2* %pi * f2 *n ’) )
;
13 x =[ x ’ , zeros (1 ,R - length ( n ) ) ];
14 disp (x , ’ t h e s e q u e n c e i s : ’ ) ;
15 for n =0: R -1
16 for k =0: R -1
17 W ( n +1 , k +1) = exp ( -( %i *2* %pi * k / R ) * n ) ;
88
18 end
19 end
20 X = W *x ’;
21 disp (X , ’DFT i s , X = ’ )
22 else
23 disp ( ’ i n v a l i d c o m p u t a t i o n ’ ) ;
24 end
25 m =0: R -1;
26 clf () ;
27 figure (0)
28 a = gca () ;
29 plot2d3 (m , abs ( X ) ,2) // p l o t t i n g DFT o f s e q u e n c e
30 plot (m , abs ( X ) , ’ r . ’ )
31 a . x_location = ’ o r i g i n ’ ;
32 a . y_location = ’ o r i g i n ’ ;
33 poly1 = a . children (1) . children (1) ;
34 poly1 . thickness = 2.5;
35 xtitle ( ’ o r i g i n a l s e q u e n c e ’ , ’ n ’ , ’ x [ n ] ’ ) ;
1 // Example 1 4 . 2
2 //EFFECT OF DFT LENGTH ON SPECTRAL ANALYSIS
3 clear ;
4 clc ;
5 N =16;
6 n =0: N -1;
7 f1 = input ( ” E n t e r f 1 v a l u e b e t w e e n 0 . 2 8 t o 0 . 3 1 = ” ) ;
// I n p u t f 1 = 64
8 f2 =0.34;
9
10 R = 128 //
11 //DFT o f t h e s e q u e n c e x [ n ]
12 x =0.5*( sin (2* %pi * f1 *n ’) ) + sin ((2* %pi * f2 *n ’) ) ;
13 x =[ x ’ , zeros (1 ,R - length ( n ) ) ];
89
14 disp (x , ’ t h e s e q u e n c e i s : ’ ) ;
15 for n =0: R -1
16 for k =0: R -1
17 W ( n +1 , k +1) = exp ( -( %i *2* %pi * k / R ) * n ) ;
18 end
19 end
20 X = W *x ’;
21 disp (X , ’DFT i s , X = ’ )
22 // p l o t t i n g DFT o f s e q u e n c e
23 m =0: R -1;
24 clf () ;
25 figure (0)
26 a = gca () ;
27 plot2d3 (m , abs ( X ) ,2)
28 plot (m , abs ( X ) , ’ r . ’ )
29 a . x_location = ’ o r i g i n ’ ;
30 a . y_location = ’ o r i g i n ’ ;
31 poly1 = a . children (1) . children (1) ;
32 poly1 . thickness = 2.5;
33 xtitle ( ’ o r i g i n a l s e q u e n c e ’ , ’ n ’ , ’ x [ n ] ’ ) ;
90