Scilab Programs
Scilab Programs
Solutions provided by
Mr. R.Senthilkumar- Assistant Professor
Electronics Engineering
Institute of Road and Transport Technology
2
List of Experiments
3
Experiment: 1
1 // C a p t i o n : U n i t Sample S e q u e n c e
2 clear ;
3 clc ;
4 close ;
5 L = 4; // U p p e r l i m i t
6 n = -L : L ;
7 x = [ zeros (1 , L ) ,1 , zeros (1 , L ) ];
8 b = gca () ;
9 b . y_location = ” m i d d l e ” ;
10 plot2d3 ( ’ gnn ’ ,n , x )
11 a = gce () ;
12 a . children (1) . thickness =4;
13 xtitle ( ’ 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 U n i t Sample
Sequence ’ , ’ n ’ , ’ x [ n ] ’ );
1 // C a p t i o n : U n i t S t e p S e q u e n c e
4
2 clear ;
3 clc ;
4 close ;
5 L = 4; // U p p e r l i m i t
6 n = -L : L ;
7 x = [ zeros (1 , L ) , ones (1 , L +1) ];
8 a = gca () ;
9 a . y_location = ” m i d d l e ” ;
10 plot2d3 ( ’ gnn ’ ,n , x )
11 title ( ’ 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 U n i t S t e p S i g n a l ’
)
12 xlabel ( ’ n’
);
13 ylabel ( ’ x
[ n ] ’ );
1 // C a p t i o n : D i s c r e t e Ramp S e q u e n c e
2 clear ;
3 clc ;
4 close ;
5 L = 4; // U p p e r l i m i t
6 n = -L : L ;
7 x = [ zeros (1 , L ) ,0: L ];
8 b = gca () ;
9 b . y_location = ’ m i d d l e ’ ;
10 plot2d3 ( ’ gnn ’ ,n , x )
11 a = gce () ;
12 a . children (1) . thickness =2;
13 xtitle ( ’ 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 D i s c r e t e U n i t
Ramp S e q u e n c e ’ , ’ n ’ , ’ x [ n ] ’ ) ;
5
Scilab code Solution 1.4 Exponentially Decreasing Signal
1 // C a p t i o n : E x p o n e n t i a l l y D e c r e a s i n g S i g n a l
2 clear ;
3 clc ;
4 close ;
5 a =0.5;
6 n = 0:10;
7 x = (a)^n;
8 a = gca () ;
9 a . x_location = ” o r i g i n ” ;
10 a . y_location = ” o r i g i n ” ;
11 plot2d3 ( ’ gnn ’ ,n , x )
12 a . thickness = 2;
13 xtitle ( ’ 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 E x p o n e n t i a l l y
D e c r e a s i n g S i g n a l ’ , ’ n ’ , ’ x [ n ] ’ );
1 // C a p t i o n : E x p o n e n t i a l l y I n c r e a s i n g S i g n a l
2 clear ;
3 clc ;
4 close ;
5 a =1.5;
6 n =1:10;
7 x = (a)^n;
8 a = gca () ;
9 a . thickness = 2;
10 plot2d3 ( ’ gnn ’ ,n , x )
11 xtitle ( ’ 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 E x p o n e n t i a l l y
I n c r e a s i n g S i g n a l ’ , ’ n ’ , ’ x [ n ] ’ );
6
Experiment: 2
1 // C a p t i o n : Program f o r L i n e a r C o n v o l u t i o n
2 clc ;
3 clear all ;
4 close ;
5 x = input ( ’ e n t e r x s e q ’ ) ;
6 h = input ( ’ e n t e r h s e q ’ ) ;
7 m = length ( x ) ;
8 n = length ( h ) ;
9 // Method 1 U s i n g D i r e c t C o n v o l u t i o n Sum Formula
10 for i = 1: n +m -1
11 conv_sum = 0;
12 for j = 1: i
13 if ((( i - j +1) <= n ) &( j <= m ) )
14 conv_sum = conv_sum + x ( j ) * h (i - j +1) ;
15 end ;
16 y ( i ) = conv_sum ;
17 end ;
18 end ;
19 disp (y ’ , ’ C o n v o l u t i o n Sum u s i n g D i r e c t Formula Method
7
=’)
20 // Method 2 U s i n g I n b u i l t F u n c t i o n
21 f = convol (x , h )
22 disp (f , ’ C o n v o l u t i o n Sum R e s u l t u s i n g I n b u i l t F u n t i o n
=’)
23 // Method 3 U s i n g f r e q u e n c y Domain m u l t i p l i c a t i o n
24 N = n +m -1;
25 x = [ x zeros (1 ,N - m ) ];
26 h = [ h zeros (1 ,N - n ) ];
27 f1 = fft ( x )
28 f2 = fft ( h )
29 f3 = f1 .* f2 ; // f r e q domain m u l t i p l i c a t i o n
30 f4 = ifft ( f3 )
31 disp ( f4 , ’ C o n v o l u t i o n Sum R e s u l t DFT − IDFT method = ’
)
32 // f 4 = r e a l ( f 4 )
33 subplot (3 ,1 ,1) ;
34 plot2d3 ( ’ gnn ’ ,x )
35 xtitle ( ’ 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 I n p u t s i g n a l x ’ )
;
36 subplot (3 ,1 ,2) ;
37 plot2d3 ( ’ gnn ’ ,h )
38 xtitle ( ’ 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 I m p u l s e s i g n a l h
’ );
39 subplot (3 ,1 ,3) ;
40 plot2d3 ( ’ gnn ’ ,y )
41 xtitle ( ’ 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 Output s i g n a l y ’
);
42 // R e s u l t
43 // e n t e r x s e q [ 1 1 1 1 ]
44 // e n t e r h s e q [ 1 2 3 ]
45 // C o n v o l u t i o n Sum u s i n g D i r e c t Formula Method =
46 // 1. 3. 6. 6. 5. 3.
47 // C o n v o l u t i o n Sum R e s u l t u s i n g I n b u i l t F u n t i o n =
48 // 1. 3. 6. 6. 5. 3.
49 // C o n v o l u t i o n Sum R e s u l t DFT − IDFT method =
50 // 1. 3. 6. 6. 5. 3.
8
Scilab code Solution 2.2 Program to find the Cicrcular Convolution
1 // C a p t i o n : Program t o f i n d t h e C i c r c u l a r C o n v o l u t i o n
of given
2 // d i s c r e t e s e q u e n c e s u s i n g M a t r i x method
3
4 clear ;
5 clc ;
6 x1 = [2 ,1 ,2 ,1]; // F i r s t s e q u e n c e
7 x2 = [1 ,2 ,3 ,4]; // S e c o n d s e q u e n c e
8 m = length ( x1 ) ; // l e n g t h o f f i r s t s e q u e n c e
9 n = length ( x2 ) ; // l e n g t h o f s e c o n d s e q u e n c e
10 //To make l e n g t h o f x1 and x2 a r e Equal
11 if ( m >n )
12 for i = n +1: m
13 x2 ( i ) = 0;
14 end
15 elseif (n > m )
16 for i = m +1: n
17 x1 ( i ) = 0;
18 end
19 end
20 N = length ( x1 ) ;
21 x3 = zeros (1 , N ) ; // x3 = C i r c u l a r c o n v o l u t i o n r e s u l t
22 a (1) = x2 (1) ;
23 for j = 2: N
24 a ( j ) = x2 (N - j +2) ;
25 end
26 for i =1: N
27 x3 (1) = x3 (1) + x1 ( i ) * a ( i ) ;
28 end
29 X (1 ,:) = a ;
30 // C a l c u l a t i o n o f c i r c u l a r c o n v o l u t i o n
31 for k = 2: N
9
32 for j =2: N
33 x2 ( j ) = a (j -1) ;
34 end
35 x2 (1) = a ( N ) ;
36 X (k ,:) = x2 ;
37 for i = 1: N
38 a ( i ) = x2 ( i ) ;
39 x3 ( k ) = x3 ( k ) + x1 ( i ) * a ( i ) ;
40 end
41 end
42 disp (X , ’ C i r c u l a r C o n v o l u t i o n M a t r i x x2 [ n ]= ’ )
43 disp ( x3 , ’ C i r c u l a r C o n v o l u t i o n R e s u l t x3 [ n ] = ’ )
44 // R e s u l t
45 // C i r c u l a r C o n v o l u t i o n M a t r i x x2 [ n ]=
46 //
47 // 1. 4. 3. 2.
48 // 2. 1. 4. 3.
49 // 3. 2. 1. 4.
50 // 4. 3. 2. 1.
51 //
52 // C i r c u l a r C o n v o l u t i o n R e s u l t x3 [ n ] =
53 //
54 // 14. 16. 14. 16.
10
Experiment: 3
1 // C a p t i o n : P e r f o r m i n g 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−
IDFT method
2 clear all ;
3 clc ;
4 close ;
5 L = 4; // Length o f t h e S e q u e n c e
6 N = 4; // N −p o i n t DFT
7 x1 = [2 ,1 ,2 ,1];
8 x2 = [1 ,2 ,3 ,4];
9 // Computing DFT
10 X1 = fft ( x1 , -1) ;
11 X2 = fft ( x2 , -1) ;
12 disp ( X1 , ’DFT o f x1 [ n ] i s X1 ( k )= ’ )
13 disp ( X2 , ’DFT o f x1 [ n ] i s X2 ( k )= ’ )
14 // M u l t i p l i c a t i o n o f 2 DFTs
15 X3 = X1 .* X2 ;
16 disp ( X3 , ’DFT o f x3 [ n ] i s X3 ( k )= ’ )
17 // C i r c u l a r C o n v o l u t i o n R e s u l t
18 x3 = abs ( fft ( X3 ,1) )
19 disp ( x3 , ’ C i r c u l a r C o n v o l u t i o n R e s u l t x3 [ n ]= ’ )
11
20 // R e s u l t
21 // DFT o f x1 [ n ] i s X1 ( k )=
22 //
23 // 6. 0 2. 0
24 //
25 // DFT o f x1 [ n ] i s X2 ( k )=
26 //
27 // 10. − 2. + 2. i − 2. − 2. − 2. i
28 //
29 // DFT o f x3 [ n ] i s X3 ( k )=
30 //
31 // 60. 0 − 4. 0
32 //
33 // C i r c u l a r C o n v o l u t i o n R e s u l t x3 [ n ]=
34 //
35 // 14. 16. 14. 16.
12
Experiment: 4
1 // C a p t i o n : P e r f o r m i n g L i n e a r C o n v o l u t i o n u s i n g
Circular Convolution
2
3 clear ;
4 clc ;
5 close ;
6 h = [1 ,2 ,3]; // I m p u l s e R e s p o n s e o f LTI System
7 x = [1 ,2 ,2 ,1]; // I n p u t R e s p o n s e o f LTI System
8 N1 = length ( x ) ;
9 N2 = length ( h ) ;
10 N = N1 + N2 -1
11 disp (N , ’ Length o f Output R e s p o n s e y ( n ) ’ )
12 // Padding z e r o s t o Make Length o f ’ h ’ and ’ x ’
13 // Equal t o l e n g t h o f o u t p u t r e s p o n s e ’ y ’
14 h1 = [h , zeros (1 ,N - N2 ) ];
15 x1 = [x , zeros (1 ,N - N1 ) ];
16 // Computing FFT
17 H = fft ( h1 , -1) ;
13
18 X = fft ( x1 , -1) ;
19 // M u l t i p l i c a t i o n o f 2 DFTs
20 Y = X .* H
21 // L i n e a r C o n v o l u t i o n R e s u l t
22 y = abs ( fft (Y ,1) )
23 disp (X , ’DFT o f i / p X( k )= ’ )
24 disp (H , ’DFT o f i m p u l s e s e q u e n c e H( k )= ’ )
25 disp (Y , ’DFT o f L i n e a r F i l t e r o /p Y( k )= ’ )
26 disp (y , ’ L i n e a r C o n v o l u t i o n r e s u l t y [ n ]= ’ )
27 // R e s u l t
28 // Length o f Output R e s p o n s e y ( n )
29 //
30 // 6.
31 //
32 // DFT o f i / p X( k )=
33 //
34 // 6. − 3.4641016 i 0 0 0 3.4641016 i
35 //
36 // DFT o f i m p u l s e s e q u e n c e H( k )=
37 //
38 // 6. 0.5 − 4.330127 i − 1.5 + 0.8660254 i
2. − 1.5 − 0.8660254 i 0.5 + 4.330127 i
39 //
40 // DFT o f L i n e a r F i l t e r o / p Y( k )=
41 //
42 // 36. − 15. − 1.7320508 i 0 0 0 − 15.
+ 1.7320508 i
43 //
44 // L i n e a r C o n v o l u t i o n r e s u l t y [ n ]=
45 //
46 // 1. 4. 9. 11. 8. 3.
14
Experiment: 5
Scilab code Solution 5.5 Performing FFT and IFFT of a discrete sequence
15
19 b . children (1) . thickness =3;
20 xtitle ( ’ G r a p h i c a l R e p r e s e n t a t i o n of Discrete
Sequence ’ , ’ n ’ , ’ x [ n ] ’ );
21 subplot (2 ,1 ,2)
22 a = gce () ;
23 a . data_bounds =[0 ,0;5 ,10];
24 plot2d3 ( ’ gnn ’ ,0: length ( X ) -1 , abs ( X ) )
25 b = gce () ;
26 b . children (1) . thickness =3;
27 xtitle ( ’ 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 D i s c r e t e
Spectrum ’ , ’ k ’ , ’X( k ) ’ ) ;
28 // R e s u l t
29 //FFT o f x [ n ] i s X( k )=
30 //
31 // 10. − 2. + 2. i − 2. − 2. − 2. i
32 //
33 // IFFT o f X( k ) i s x [ n ]=
34 //
35 // 1. 2. 3. 4.
16
Experiment: 6
1 // C a p t i o n : Program t o g e n e r a t e and p l o t t h e i m p u l s e
r e s p o n s e and f r e q u e n c y
2 // r e s p o n s e o f a L i n e a r c o n s t a n t c o e f f i c i e n t f i r s t
order D i f f e r e n t i a l Equation
3 // [ 1 ] . I m p u l s e r e s p o n s e h ( t )= exp (−a ∗ t ) u ( t ) , A>0
4 // [ 2 ] . F r e q u e n c y r e s p o n s e H( jw ) = 1 / ( jw+a )
5 clear ;
6 clc ;
7 close ;
8 // [ 1 ] . To g e n e r a t e and p l o t t h e i m p u l s e r e s p o n s e
9 a =1; // C o n s t a n t c o e f f i c i e n t a =1
10 Dt = 0.005;
11 t = 0: Dt :10;
12 ht = exp ( - a * t ) ;
13 figure (1)
14 a = gca () ;
15 a . y_location = ” o r i g i n ” ;
16 plot (t , ht ) ;
17 xlabel ( ’ t i m e t −−−−−−> ’ ) ;
17
18 ylabel ( ’ h ( t ) ’ )
19 title ( ’ I m p u l s e R e p s o n s e o f I s t Order L i n e a r C o n s t a n t
C o e f f . D i f f e r e n t i a l Equ . ’ )
20 //
21 // [ 2 ] . F i n d i n g F r e q u e n c y r e s p o n s e u s i n g C o n t i n u o u s
Time F o u r i e r T r a n s f o r m
22 Wmax = 2* %pi *1; // Analog F r e q u e n c y = 1Hz
23 K = 4;
24 k = 0:( K /1000) : K ;
25 W = k * Wmax / K ;
26 HW = ht * exp ( - sqrt ( -1) *t ’* W ) * Dt ;
27 HW_Mag = abs ( HW ) ;
28 W = [ - mtlb_fliplr ( W ) , W (2:1001) ]; // Omega from −
Wmax t o Wmax
29 HW_Mag = [ mtlb_fliplr ( HW_Mag ) , HW_Mag (2:1001) ];
30 [ HW_Phase , db ] = phasemag ( HW ) ;
31 HW_Phase = [ - mtlb_fliplr ( HW_Phase ) , HW_Phase (2:1001)
];
32 figure (2)
33 // P l o t t i n g Magnitude R e s p o n s e
34 subplot (2 ,1 ,1) ;
35 a = gca () ;
36 a . y_location = ” o r i g i n ” ;
37 plot (W , HW_Mag ) ;
38 xlabel ( ’ F r e q u e n c y i n R a d i a n s / S e c o n d s −−−> W’ ) ;
39 ylabel ( ’ a b s (H(jW) ) ’ )
40 title ( ’ Magnitude R e s p o n s e ’ )
41 // P l o t t i n g Phase Reponse
42 subplot (2 ,1 ,2) ;
43 a = gca () ;
44 a . y_location = ” o r i g i n ” ;
45 a . x_location = ” o r i g i n ” ;
46 plot (W , HW_Phase * %pi /180) ;
47 xlabel ( ’ Frequency in
R a d i a n s / S e c o n d s −−−> W’ ) ;
48 ylabel ( ’
<H
(jW) ’ )
18
49 title ( ’ Phase R e s p o n s e i n R a d i a n s ’ )
19
Experiment: 7
Sampling, Verification of
Sampling and Effect of aliasing
sincnew.sce
1 // C a p t i o n : S a m p l i n g and R e c o n s t r u c t i o n o f a S i g n a l x
( t ) = exp (−A∗ | t | )
2 // D i s c r e t e Time Sampled S i g n a l x ( nT )= exp (−A∗ | nT | )
3 // F o l l o w i n g S a m p l i n g F r e q u e n c i e s a r e u s e d :
4 // [ 1 ] . Fs = 1 Hz [ 2 ] . Fs = 2 Hz [ 3 ] . Fs = 4Hz [ 4 ] . Fs
=20 Hz [ 5 ] . Fs =100Hz
5 // A l i a s i n g E f f e c t : As t h e S a m p l i n g f r e q u e n c y
increases aliasing effect decreases
6 clear ;
7 clc ;
8 close ;
9 // Analog S i g n a l
10 A =1; // A m p l i t u d e
11 Dt = 0.005;
12 t = -2: Dt :2;
20
13 // C o n t i n u o u s Time S i g n a l
14 xa = exp ( - A * abs ( t ) ) ;
15 // D i s c r e t e Time S i g n a l
16 Fs = input ( ’ E n t e r t h e S a m p l i n g F r e q u e n c y i n H e r t z ’ ) ;
// Fs = 1Hz , 2 Hz , 4 Hz , 2 0 Hz , 1 0 0 Hz
17 Ts = 1/ Fs ;
18 nTs = -2: Ts :2;
19 x = exp ( - A * abs ( nTs ) ) ;
20 // Analog S i g n a l r e c o n s t r u c t i o n
21 Dt = 0.005;
22 t = -2: Dt :2;
23 Xa = x * sincnew ( Fs *( ones ( length ( nTs ) ,1) *t - nTs ’* ones
(1 , length ( t ) ) ) ) ;
24 // P l o t t i n g t h e o r i g i n a l s i g n a l and r e c o n s t r u c t e d
signal
25 subplot (2 ,1 ,1) ;
26 a = gca () ;
27 a . x_location = ” o r i g i n ” ;
28 a . y_location = ” o r i g i n ” ;
29 plot (t , xa ) ;
30 xlabel ( ’ t i n s e c . ’ ) ;
31 ylabel ( ’ xa ( t ) ’ )
32 title ( ’ O r i g i n a l Analog S i g n a l ’ )
33 subplot (2 ,1 ,2) ;
34 a = gca () ;
35 a . x_location = ” o r i g i n ” ;
36 a . y_location = ” o r i g i n ” ;
37 xlabel ( ’ t i n s e c . ’ ) ;
38 ylabel ( ’ xa ( t ) ’ )
39 title ( ’ R e c o n s t r u c t e d S i g n a l u s i n g s i n c f u n c t i o n , Fs
= 100 Hz ’ ) ;
40 plot (t , Xa ) ;
21
Experiment: 8
Scilab code Solution 8.1 Program to Design FIR Low Pass Filter
22
17 end
18 // Windowing F i t l e r C o e f f i c i e n t s
19 h = hd .* W ;
20 disp (h , ’ F i l t e r C o e f f i c i e n t s a r e ’ )
21
22 [ hzm , fr ]= frmag (h ,256) ;
23 hzm_dB = 20* log10 ( hzm ) ./ max ( hzm ) ;
24 subplot (2 ,1 ,1)
25 plot (2* fr , hzm )
26 xlabel ( ’ N o r m a l i z e d D i g i t a l F r e q u e n c y W’ ) ;
27 ylabel ( ’ Magnitude ’ ) ;
28 title ( ’ F r e q u e n c y R e s p o n s e 0 f FIR LPF u s i n g
R e c t a n g u l a r window ’ )
29 xgrid (1)
30 subplot (2 ,1 ,2)
31 plot (2* fr , hzm_dB )
32 xlabel ( ’ N o r m a l i z e d D i g i t a l F r e q u e n c y W’ ) ;
33 ylabel ( ’ Magnitude i n dB ’ ) ;
34 title ( ’ F r e q u e n c y R e s p o n s e 0 f FIR LPF u s i n g
R e c t a n g u l a r window ’ )
35 xgrid (1)
36 // R e s u l t
37 // E n t e r t h e Odd F i l t e r Length = 7
38 // E n t e r t h e D i g i t a l C u t o f f f r e q u e n c y = %pi /2
39 //
40 // F i l t e r C o e f f i c i e n t s a r e
41 //
42 // − 0 . 1 0 6 1 0 3 3
43 // 1 . 9 4 9D−17 = 0 . 0
44 // 0.3183099
45 // 0.5
46 // 0.3183099
47 // 1 . 9 4 9D−17 = 0 . 0
48 // − 0 . 1 0 6 1 0 3 3
23
Scilab code Solution 8.2 rogram to Design FIR High Pass Filter
24
33 ylabel ( ’ Magnitude i n dB ’ ) ;
34 title ( ’ F r e q u e n c y R e s p o n s e 0 f FIR HPF u s i n g
R e c t a n g u l a r window ’ )
35 xgrid (1)
36 // R e s u l t
37 // E n t e r t h e Odd F i l t e r Length = 5
38 // E n t e r t h e D i g i t a l C u t o f f f r e q u e n c y = %pi /4
39 // F i l t e r C o e f f i c i e n t s a r e
40 //
41 // − 0 . 1 5 9 1 5 4 9
42 // − 0 . 2 2 5 0 7 9 1
43 // 0.75
44 // − 0 . 2 2 5 0 7 9 1
45 // − 0 . 1 5 9 1 5 4 9
Scilab code Solution 8.3 Program to Design FIR Band Pass Filter
25
17 n
18 hd ( n ) = ( sin ( Wc2 *(( n -1) - Tuo ) ) - sin ( Wc1 *(( n -1) -
Tuo ) ) ) /((( n -1) - Tuo ) * %pi ) ;
19 end
20 if ( abs ( hd ( n ) ) <(0.00001) )
21 hd ( n ) =0;
22 end
23 end
24 hd ;
25 // R e c t a n g u l a r Window
26 for n = 1: M
27 W ( n ) = 1;
28 end
29 // Windowing F i t l e r C o e f f i c i e n t s
30 h = hd .* W ;
31 disp (h , ’ F i l t e r C o e f f i c i e n t s a r e ’ )
32 [ hzm , fr ]= frmag (h ,256) ;
33 hzm_dB = 20* log10 ( hzm ) ./ max ( hzm ) ;
34 subplot (2 ,1 ,1)
35 plot (2* fr , hzm )
36 xlabel ( ’ N o r m a l i z e d D i g i t a l F r e q u e n c y W’ ) ;
37 ylabel ( ’ Magnitude ’ ) ;
38 title ( ’ F r e q u e n c y R e s p o n s e 0 f FIR BPF using
R e c t a n g u l a r window ’ )
39 xgrid (1)
40 subplot (2 ,1 ,2)
41 plot (2* fr , hzm_dB )
42 xlabel ( ’ N o r m a l i z e d D i g i t a l F r e q u e n c y W’ ) ;
43 ylabel ( ’ Magnitude i n dB ’ ) ;
44 title ( ’ F r e q u e n c y R e s p o n s e 0 f FIR BPF using
R e c t a n g u l a r window ’ )
45 xgrid (1)
46 // R e s u l t
47 // E n t e r t h e Odd F i l t e r Length = 11
48 // E n t e r t h e D i g i t a l C u t o f f f r e q u e n c y = [ %pi / 4 , 3 ∗ %pi
/4]
49 // F i l t e r C o e f f i c i e n t s a r e
50 // 0 . 0. 0. − 0.3183099 0. 0.5 0. −
26
0.3183099 0. 0. 0.
Scilab code Solution 8.4 Program to Design FIR Band Reject Filter
27
29 h = hd .* W ;
30 disp (h , ’ F i l t e r C o e f f i c i e n t s a r e ’ )
31 [ hzm , fr ]= frmag (h ,256) ;
32 hzm_dB = 20* log10 ( hzm ) ./ max ( hzm ) ;
33 subplot (2 ,1 ,1)
34 plot (2* fr , hzm )
35 xlabel ( ’ N o r m a l i z e d D i g i t a l F r e q u e n c y W’ ) ;
36 ylabel ( ’ Magnitude ’ ) ;
37 title ( ’ F r e q u e n c y R e s p o n s e 0 f FIR BSF u s i n g
R e c t a n g u l a r window ’ )
38 xgrid (1)
39 subplot (2 ,1 ,2)
40 plot (2* fr , hzm_dB )
41 xlabel ( ’ N o r m a l i z e d D i g i t a l F r e q u e n c y W’ ) ;
42 ylabel ( ’ Magnitude i n dB ’ ) ;
43 title ( ’ F r e q u e n c y R e s p o n s e 0 f FIR BSF u s i n g
R e c t a n g u l a r window ’ )
44 xgrid (1)
45 // R e s u l t
46 // E n t e r t h e Odd F i l t e r Length = 11
47 // E n t e r t h e D i g i t a l C u t o f f f r e q u e n c y =[ %pi / 3 , 2 ∗ %pi
/3]
48 // F i l t e r C o e f f i c i e n t s a r e
49 // column 1 t o 9
50 // 0. − 0.1378322 0. 0.2756644 0.
0.6666667 0. 0.2756644 0.
51 // column 10 t o 11
52 // − 0 . 1 3 7 8 3 2 2 0.
28
Experiment: 9
Scilab code Solution 9.1 Design of FIR LPF Filter using Frequecny Sam-
pling Technique
1 // C p a t i o n : D e s i g n o f FIR LPF F i l t e r u s i n g F r e q u e c n y
Sampling Technique
2
3 clear ;
4 clc ;
5 close ;
6 M =15;
7 Hr = [1 ,1 ,1 ,1 ,0.4 ,0 ,0 ,0];
8 for k =1: length ( Hr )
9 G ( k ) =(( -1) ^( k -1) ) * Hr ( k ) ;
10 end
11 h = zeros (1 , M ) ;
12 U = (M -1) /2
13 for n = 1: M
14 h1 = 0;
15 for k = 2: U +1
16 h1 = G ( k ) * cos ((2* %pi / M ) *( k -1) *(( n -1) +(1/2) ) ) + h1 ;
17 end
29
18 h ( n ) = (1/ M ) * ( G (1) +2* h1 ) ;
19 end
20 disp (h , ’ F i l t e r C o e f f i c i e n t s a r e h ( n )= ’ )
21 [ hzm , fr ]= frmag (h ,256) ;
22 hzm_dB = 20* log10 ( hzm ) ./ max ( hzm ) ;
23 subplot (2 ,1 ,1)
24 plot (2* fr , hzm )
25 a = gca () ;
26 xlabel ( ’ N o r m a l i z e d D i g i t a l F r e q u e n c y W’ ) ;
27 ylabel ( ’ Magnitude ’ ) ;
28 title ( ’ F r e q u e n c y R e s p o n s e 0 f FIR LPF u s i n g F r e q u e n c y
S a m p l i n g T e c h n i q u e w i t h M = 15 w i t h C u t o f f
Frequency = 0.466 ’ )
29 xgrid (2)
30 subplot (2 ,1 ,2)
31 plot (2* fr , hzm_dB )
32 a = gca () ;
33 xlabel ( ’ N o r m a l i z e d D i g i t a l F r e q u e n c y W’ ) ;
34 ylabel ( ’ Magnitude i n dB ’ ) ;
35 title ( ’ F r e q u e n c y R e s p o n s e 0 f FIR LPF u s i n g F r e q u e n c y
S a m p l i n g T e c h n i q u e w i t h M = 15 w i t h C u t o f f
Frequency = 0.466 ’ )
36 xgrid (2)
37 // R e s u l t
38 // F i l t e r C o e f f i c i e n t s a r e h ( n )=
39 // column 1 t o 7
40 //
41 // − 0 .0 1 41 2 8 9 − 0 .0 0 1 94 5 3 0 . 0 4 0.0122345
− 0 .0 9 13 8 8 0 − 0 .0 1 8 08 9 9 0 . 3 1 3 3 1 7 6
42 //
43 // column 8 t o 14
44 //
45 // 0 . 5 2 0.3133176 − 0.0180899 − 0.0913880
0.0122345 0.04 − 0.0019453
46 //
47 // column 15
48 //
49 // − 0 . 0 1 4 1 2 8 9
30
31
Experiment: 10
Scilab code Solution 10.1 Digital IIR First Order Butterworth LPF Fil-
ter
1 // C a p t i o n : To d e s i g n a d i g i t a l I I R F i r s t Order
B u t t e r w o r t h LPF F i l t e r
2 // U s i n g B i l i n e a r T r a n s f o r m a t i o n
3 clear all ;
4 clc ;
5 close ;
6 s = poly (0 , ’ s ’ ) ;
7 Omegac = 0.2* %pi ; // C u t o f f f r e q u e n c y
8 H = Omegac /( s + Omegac ) ; // Analog f i r s t o r d e r
Butterworth f i l t e r t r a n f e r f u n c t i o n
9 T =1; // S a m p l i n g p e r i o d T = 1 S e c o n d
10 z = poly (0 , ’ z ’ ) ;
11 Hz = horner (H ,(2/ T ) *(( z -1) /( z +1) ) ) // B i l i n e a r
Transformation
12 HW = frmag ( Hz (2) , Hz (3) ,512) ; // F r e q u e n c y r e s p o n s e
f o r 512 p o i n t s
13 W = 0: %pi /511: %pi ;
14 a = gca () ;
32
15 a . thickness = 1;
16 plot ( W / %pi , HW , ’ r ’ )
17 a . foreground = 1;
18 a . font_style = 9;
19 xgrid (1)
20 xtitle ( ’ Magnitude R e s p o n s e o f S i n g l e p o l e LPF F i l t e r
Cutoff frequency = 0.2∗ pi ’ , ’ Normalized D i g i t a l
F r e q u e n c y −−−> ’ , ’ Magnitude ’ ) ;
1 // C a p t i o n : To d e s i g n F i r s t Order B u t t e r w o r t h Low
P a s s F i l t e r and c o v e r t i t i n t o
2 // HPF U s i n g D i g i t a l F i l t e r T r a n s f o r m a t i o n
3 clear all ;
4 clc ;
5 close ;
6 s = poly (0 , ’ s ’ ) ;
7 Omegac = 0.2* %pi ; // F i l t e r c u t o f f f r e q u e n c y
8 H = Omegac /( s + Omegac ) ; // F i r s t o r d e r B u t t e r w o r t h I I R
filter
9 T =1; // S a m p l i n g p e r i o d T = 1 S e c o n d
10 z = poly (0 , ’ z ’ ) ;
11 Hz_LPF = horner (H ,(2/ T ) *(( z -1) /( z +1) ) ) ; // B i l i n e a r
Transformation
12 alpha = -( cos (( Omegac + Omegac ) /2) ) /( cos (( Omegac -
Omegac ) /2) ) ;
13 HZ_HPF = horner ( Hz_LPF , -( z + alpha ) /(1+ alpha * z ) ) //LPF t o
HPF d i g i t a l t r a n s f o r m a t i o n
14 HW = frmag ( HZ_HPF (2) , HZ_HPF (3) ,512) ; // F r e q u e n c y
r e s p o n s e f o r 512 p o i n t s
15 W = 0: %pi /511: %pi ;
16 a = gca () ;
17 a . thickness = 1;
18 plot ( W / %pi , HW , ’ r ’ )
33
19 a . foreground = 1;
20 a . font_style = 9;
21 xgrid (1)
22 xtitle ( ’ Magnitude R e s p o n s e o f S i n g l e p o l e HPF F i l t e r
Cutoff frequency = 0.2∗ pi ’ , ’ Normalized D i g i t a l
F r e q u e n c y W/ p i −−−> ’ , ’ Magnitude ’ ) ;
1 // / C a p t i o n : To D e s i g n a D i g i t a l I I R B u t t e r w o r t h LPF
F i l t e r from Analog I I R
2 // B u t t e r w o r t h F i l t e r and LPF t o BPF u s i n g D i g i t a l
Transformation
3 clear all ;
4 clc ;
5 close ;
6 omegaP = 0.2* %pi ; // F i l t e r c u t o f f f r e q u e n c y
7 omegaL = (1/5) * %pi ; // Lower C u t o f f f r e q u e n c y f o r
BSF
8 omegaU = (3/5) * %pi ; // Upper C u t o f f f r e q u e n c y f o r
BSF
9 z = poly (0 , ’ z ’ ) ;
10 H_LPF = (0.245) *(1+( z ^ -1) ) /(1 -0.509*( z ^ -1) ) ; //
Bilinear transformation
11 alpha = ( cos (( omegaU + omegaL ) /2) / cos (( omegaU - omegaL )
/2) ) ; // p a r a m e t e r ’ a l p h a ’
12 // p a r a m e t e r ’ k ’
13 k = ( cos (( omegaU - omegaL ) /2) / sin (( omegaU - omegaL )
/2) ) * tan ( omegaP /2) ;
14 NUM = -(( z ^2) -((2* alpha * k /( k +1) ) * z ) +(( k -1) /( k +1) ) ) ;
15 DEN = (1 -((2* alpha * k /( k +1) ) * z ) +((( k -1) /( k +1) ) *( z ^2) )
);
16 HZ_BPF = horner ( H_LPF , NUM / DEN ) ; //LPF t o BPF c o n v e r s i o n
using d i g i t a l transformation
17 disp ( HZ_BPF , ’ D i g i t a l BPF I I R F i l t e r H( Z )= ’ ) ;
34
18 HW = frmag ( HZ_BPF (2) , HZ_BPF (3) ,512) ; // f r e q u e n c y
response
19 W = 0: %pi /511: %pi ;
20 a = gca () ;
21 a . thickness = 1;
22 plot ( W / %pi , HW , ’ r ’ )
23 a . foreground = 1;
24 a . font_style = 9;
25 xgrid (1)
26 xtitle ( ’ Magnitude R e s p o n s e o f BPF F i l t e r c u t o f f
frequency [ 0 . 2 , 0 . 6 ] ’ , ’ Normalized D i g i t a l
F r e q u e n c y −−−> ’ , ’ Magnitude ’ ) ;
1 // C a p t i o n : To D e s i g n a D i g i t a l I I R B u t t e r w o r t h LPF
F i l t e r from Analog I I R
2 // B u t t e r w o r t h F i l t e r and LPF t o BSF u s i n g D i g i t a l
Transformation
3 clear all ;
4 clc ;
5 close ;
6 omegaP = 0.2* %pi ; // F i l t e r c u t o f f f r e q u e n c y
7 omegaL = (1/5) * %pi ; // Lower C u t o f f f r e q u e n c y f o r
BSF
8 omegaU = (3/5) * %pi ; // Upper C u t o f f f r e q u e n c y f o r
BSF
9 z = poly (0 , ’ z ’ ) ;
10 H_LPF = (0.245) *(1+( z ^ -1) ) /(1 -0.509*( z ^ -1) ) //
Bilinear transformation
11 alpha = ( cos (( omegaU + omegaL ) /2) / cos (( omegaU - omegaL )
/2) ) ; // p a r a m e t e r ’ a l p h a ’
12 k = tan (( omegaU - omegaL ) /2) * tan ( omegaP /2) ; //
parameter ’k ’
13 NUM =(( z ^2) -((2* alpha /(1+ k ) ) * z ) +((1 - k ) /(1+ k ) ) ) ; //
35
Numerator
14 DEN = (1 -((2* alpha /(1+ k ) ) * z ) +(((1 - k ) /(1+ k ) ) *( z ^2) ) ) ;
// Den omin ator
15 HZ_BSF = horner ( H_LPF , NUM / DEN ) ; //LPF t o BSF
conversion using d i g i t a l transformation
16 HW = frmag ( HZ_BSF (2) , HZ_BSF (3) ,512) ; // f r e q u e n c y
r e s p o n s e f o r 512 p o i n t s
17 W = 0: %pi /511: %pi ;
18 a = gca () ;
19 a . thickness = 1;
20 plot ( W / %pi , HW , ’ r ’ )
21 a . foreground = 1;
22 a . font_style = 9;
23 xgrid (1)
24 xtitle ( ’ Magnitude R e s p o n s e o f BSF F i l t e r c u t o f f f r e q
[ 0 . 2 , 0 . 6 ] ’ , ’ N o r m a l i z e d D i g i t a l F re q u e n c y −−−> ’ , ’
Magnitude ’ ) ;
36
Experiment: 11
Scilab code Solution 11.1 To Design the Digtial Chebyshev IIR Filter
37
18 Hs = poly ( gn , ’ s ’ , ’ c o e f f ’ ) / real ( poly ( pols , ’ s ’ ) )
19 z = poly (0 , ’ z ’ ) ;
20 Hz = horner ( Hs ,((2/ T ) *(( z -1) /( z +1) ) ) )
21 HW = frmag ( Hz (2) , Hz (3) ,512) ; // F r e q u e n c y r e s p o n s e
f o r 512 p o i n t s
22 W = 0: %pi /511: %pi ;
23 a = gca () ;
24 a . thickness = 1;
25 plot ( W / %pi , abs ( HW ) , ’ r ’ )
26 a . foreground = 1;
27 a . font_style = 9;
28 xgrid (1)
29 xtitle ( ’ Magnitude R e s p o n s e o f Chebyshev LPF F i l t e r ’ ,
’ N o r m a l i z e d D i g i t a l F r e q u e n c y −−−> ’ , ’ Magnitude i n
dB ’ ) ;
30 //RESULT
31 // E n t e r t h e D i g i t a l P a s s Band Edge F r e q u e n c y 0 . 2 ∗ %pi
32 // E n t e r t h e D i g i t a l S t o p Band Edge F r e q u e n c y 0 . 6 ∗ %pi
33 // S a m p l i n g I n t e r v a l 1
34 // T =
35 //
36 // 1.
37 // OmegaP =
38 //
39 // 0.6498394
40 // OmegaS =
41 //
42 // 2.7527638
43 // E n t e r t h e P a s s Band R i p p l e 0 . 8
44 // E n t e r t h e S t o p Band R i p p l e 0 . 2
45 // D e l t a =
46 //
47 // 4.8989795
48 // E p s i l o n =
49 //
50 // 0.75
51 // N =
52 //
38
53 // 1.2079548
54 // N =
55 //
56 // 2.
57 // OmegaC =
58 //
59 // 0.7503699
60 // gn =
61 //
62 // 0.2815275
63 // pols =
64 //
65 // − 0.2652958 + 0.5305916 i − 0.2652958 −
0.5305916 i
66 // Hs =
67 //
68 // 0.2815275
69 // −−−−−−−−−−−−−−−−−−−−−−−−−
70 // 2
71 // 0.3519094 + 0.5305916 s + s
72 // Hz =
73 //
74 // 2
75 // 0.2815275 + 0.5630550 z + 0.2815275 z
76 // −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
77 // 2
78 // 3.2907261 − 7.2961813 z + 5.4130926 z
79 // − − >0.5∗0.5629
80 // a n s =
81 //
82 // 0.28145
83 //
84 //−−>Hz ( 2 )= Hz ( 2 ) / 5 . 4 1 3 0 9 2 6
85 // Hz =
86 //
87 // 2
88 // 0.0520086 + 0.1040172 z + 0.0520086 z
89 // −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
39
90 // 2
91 // 3.2907261 − 7.2961813 z + 5.4130926 z
92 //
93 //−−>Hz ( 3 ) = Hz ( 3 ) / 5 . 4 1 3 0 9 2 6
94 // Hz =
95 //
96 // 2
97 // 0.0520086 + 0.1040172 z + 0.0520086 z
98 // −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
99 // 2
100 // 0.6079198 − 1.3478767 z + z
101 //
40
Experiment: 12
Decimation by polyphase
decomposition
1 // C a p t i o n : D e c i m a t i o n by 2 , F i l t e r Length = 30
2 // C u t o f f F r e q u e n c y Wc = %pi /2
3 // P a s s band Edge f r e q u e n c y f p = 0 . 2 5 and a S t o p band
edge frequency f s = 0.31
4 // Choose t h e number o f c o s i n e f u n c t i o n s and c r e a t e
a dense grid
5 // i n [ 0 , 0 . 2 5 ] and [ 0 . 3 1 , 0 . 5 ]
6 // m a g n i t u d e f o r p a s s band = 1 & s t o p band = 0 ( i . e )
[1 0]
7 // W e i g h t i n g f u n c t i o n =[2 1 ]
8 clear ;
9 clc ;
10 close ;
11 M = 30; // F i l t e r Length
12 D = 2; // D e c i m a t i o n F a c t o r = 2
13 Wc = %pi /2; // C u t o f f F r e q u e n c y
14 Wp = Wc /(2* %pi ) ; // Passband Edge F r e q u e n c y
15 Ws = 0.31; // Stopband Edge F r e q u e n c y
16 hn = eqfir (M ,[0 Wp ; Ws .5] ,[1 0] ,[2 1]) ;
41
17 disp ( hn , ’ The LPF F i l t e r C o e f f i c i e n t s a r e : ’)
18 // O b t a i n i n g P o l y p h a s e F i l t e r C o e f f i c i e n t s from hn
19 p = zeros (D , M / D ) ;
20 for k = 1: D
21 for n = 1:( length ( hn ) / D )
22 p (k , n ) = hn ( D *( n -1) + k ) ;
23 end
24 end
25 disp (p , ’ The P o l y p h a s e D e c i m a t o r f o r D =2 are : ’)
26 // R e s u l t
27 // The LPF F i l t e r C o e f f i c i e n t s a r e :
28 // column 1 t o 7
29 // 0 . 0 0 6 0 2 0 3 − 0 . 0 1 2 8 0 3 7 − 0 . 0 0 2 8 5 3 4 0.0136687
− 0.0046761 − 0.0197002 0.0159915
30
31 // column 8 t o 14
32 // 0 . 0 2 1 3 8 1 1 − 0 . 0 3 4 9 8 0 8 − 0.0156251 0.0640230
− 0.0073600 − 0.1187325 0.0980522
33 // column 15 t o 21
34 // 0 . 4 9 2 2 4 7 6 0.4922476 0.0980522 − 0.1187325
− 0.0073600 0.0640230 − 0.0156251
35 // column 22 t o 28
36 //− 0 . 0 3 4 9 8 0 8 0.0213811 0.0159915 − 0.0197002
− 0.0046761 0.0136687 − 0.0028534
37
38 // column 29 t o 30
39 //− 0 . 0 1 2 8 0 3 7 0.0060203
40
41 // The P o l y p h a s e D e c i m a t o r f o r D =2 a r e :
42 // column 1 t o 7
43 // 0 . 0 0 6 0 2 0 3 − 0 . 0 0 2 8 5 3 4 − 0 . 0 0 4 6 7 6 1 0.0159915
− 0.0349808 0.0640230 − 0.1187325
44 //− 0 . 0 1 2 8 0 3 7 0.0136687 − 0.0197002 0.0213811
− 0.0156251 − 0.0073600 0.0980522
45
46 // column 8 t o 14
47 // 0 . 4 9 2 2 4 7 6 0.0980522 − 0.0073600 − 0.0156251
0.0213811 − 0.0197002 0.0136687
42
48 // 0 . 4 9 2 2 4 7 6 − 0 . 1 1 8 7 3 2 5 0.0640230 − 0.0349808
0.0159915 − 0.0046761 − 0.0028534
49 // column 15
50 //− 0 . 0 1 2 8 0 3 7
51 // 0.0060203
43
Experiment: 13
1 // C a p t i o n : P e r i o d o g r a m E s t i m a t e o f Given D i s c r e t e
Sequence
2 // x ( n ) = { 1 , 0 , 2 , 0 , 3 , 1 , 0 , 2 }
3 // u s i n g DFT
4 clear ;
5 clc ;
6 close ;
7 N =8; //8− p o i n t DFT
8 x = [1 ,0 ,2 ,0 ,3 ,1 ,0 ,2]; // g i v e n d i s c r e t e s e q u e n c e
9 X = dft (x , -1) ; //8− p o i n t DFT o f g i v e n d i s c r e t e
sequence
10 Pxx = (1/ N ) *( abs ( X ) .^2) ; // P e r i d o g r a m E s t i m a t e
11 disp (X , ’DFT o f x ( n ) i s X( k )= ’ )
12 disp ( Pxx , ’ P e r i d o g r a m o f x ( n ) i s Pxx ( k /N)= ’ )
13 figure (1)
14 a = gca () ;
15 a . data_bounds =[0 ,0;8 ,11];
16 plot2d3 ( ’ gnn ’ ,[1: N ] , Pxx )
44
17 a . foreground = 5;
18 a . font_color = 5;
19 a . font_style = 5;
20 title ( ’ P e r i d o g r a m E s t i m a t e ’ )
21 xlabel ( ’ D i s c r e t e F r e q u e n c y V a r i a b l e K −−−−−> ’ )
22 ylabel ( ’ P e r i o d o g r a m Pxx ( k /N) −−−−> ’ )
23 // R e s u l t
24 //DFT o f x ( n ) i s X( k )=
25 //
26 // 9.
27 // − 1 . 2 9 2 8 9 3 2 + 0 . 1 2 1 3 2 0 3 i
28 // 2. + i
29 // − 2 . 7 0 7 1 0 6 8 + 4 . 1 2 1 3 2 0 3 i
30 // 3 . − 3 . 6 7 4D−16 i
31 // − 2 . 7 0 7 1 0 6 8 − 4 . 1 2 1 3 2 0 3 i
32 // 2. − i
33 // − 1 . 2 9 2 8 9 3 2 − 0 . 1 2 1 3 2 0 3 i
34 //
35 // P e r i d o g r a m o f x ( n ) i s Pxx ( k /N)=
36 //
37 // 10.125
38 // 0.2107864
39 // 0.625
40 // 3.0392136
41 // 1.125
42 // 3.0392136
43 // 0.625
44 // 0.2107864
45
Appendix
46