You are on page 1of 34

Scilab Manual for

Digital Signal Processing


by Mr P. Kalaiselvan
Electronics Engineering
Paavai Group of Institutions1

Solutions provided by
Mr. Prarthan Mehta
Electronics Engineering
Dharmsinh Desai University

July 30, 2019

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


http://spoken-tutorial.org/NMEICT-Intro. This Scilab Manual and Scilab codes
written in it can be downloaded from the ”Migrated Labs” section at the website
http://scilab.in
1
Contents

List of Scilab Solutions 3

1 Genaration of signals 4

2 Linear Convolution 7

3 Implementation of FIR filter 9

4 Implementation of IIR filter 11

5 Calculation of FFT 14

6 Generation of Discrete time Signals 17

7 Verification of Sampling Theorem 20

8 FFT and IFFt 22

9 Time and Frequency response of LTI systems 24

10 Linear and circular Convolution through FFT 26

11 Design of FIR filters(window design) 29

12 Design of IIR filters (Butterworth & Chebychev) 31

2
List of Experiments

Solution 1.1 Unit Sample Signal . . . . . . . . . . . . . . . . . 4


Solution 1.2 Unit Step Signal . . . . . . . . . . . . . . . . . . . 4
Solution 1.3 Ramp Signal . . . . . . . . . . . . . . . . . . . . . 5
Solution 1.4 Exponential Signal . . . . . . . . . . . . . . . . . 5
Solution 2.1 Linear Convolution . . . . . . . . . . . . . . . . . 7
Solution 3.1 FIR Low Pass Filter . . . . . . . . . . . . . . . . 9
Solution 4.1 Butterworth LPF . . . . . . . . . . . . . . . . . . 11
Solution 4.2 Chebyshev HPF . . . . . . . . . . . . . . . . . . . 12
Solution 5.1 FFT DIT . . . . . . . . . . . . . . . . . . . . . . 14
Solution 6.1 Unit Sample Signal . . . . . . . . . . . . . . . . . 17
Solution 6.2 Unit Step Signal . . . . . . . . . . . . . . . . . . . 17
Solution 6.3 Ramp Signal . . . . . . . . . . . . . . . . . . . . . 18
Solution 6.4 Exponential Signal . . . . . . . . . . . . . . . . . 18
Solution 7.1 Verification of the Sampling Theorem . . . . . . 20
Solution 8.1 FFT IFFT . . . . . . . . . . . . . . . . . . . . . . 22
Solution 9.1 Time and Frequency response of LTI system . . . 24
Solution 10.1 Linear Convolution using FFT IFFT . . . . . . . 26
Solution 10.2 Circular Convolution using FFT . . . . . . . . . . 27
Solution 11.1 FIR Low Pass Filter Rectangular Window . . . . 29
Solution 12.1 IIR Butterworth LPF . . . . . . . . . . . . . . . . 31
Solution 12.2 IIR Chebyshev Filter . . . . . . . . . . . . . . . . 32

3
Experiment: 1

Genaration of signals

Scilab code Solution 1.1 Unit Sample Signal

1 // U n i t Sample S e q u e n c e
2 clear ;
3 clc ;
4 close ;
5 l = 9; // L i m i t s
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 =5;
13 xtitle ( ’ U n i t Sample S e q u e n c e ’ , ’ n ’ , ’ x [ n ] ’ ) ;

Scilab code Solution 1.2 Unit Step Signal

1 // U n i t S t e p S e q u e n c e
2 clear ;

4
3 clc ;
4 close ;
5 l = 9; // 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 ( ’ U n i t S t e p S i g n a l ’ )
12 xlabel ( ’ n’
);
13 ylabel ( ’ x
[ n ] ’ );

Scilab code Solution 1.3 Ramp Signal

1 //Ramp S i g n a l
2 clear ;
3 clc ;
4 close ;
5 l = 9; // 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 ( ’Ramp S i g n a l ’ , ’ n ’ , ’ x [ n ] ’ ) ;

Scilab code Solution 1.4 Exponential Signal

1 // E x p o n e n t i a l S i g n a l

5
2 clear ;
3 clc ;
4 close ;
5 a =1.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 xtitle ( ’ E x p o n e n t i a l S i g n a l ’ , ’ n ’ , ’ x [ n ] ’ ) ;

6
Experiment: 2

Linear Convolution

Scilab code Solution 2.1 Linear Convolution

1 // 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 t h e s e q u e n c e x [ n ] : ’ ) ; // i n p u t −−>
Vector
6 h = input ( ’ E n t e r t h e S e q u e n c e h [ n ] : ’ ) ; // i n p u t −−>
Vector
7 m = length ( x ) ;
8 n = length ( h ) ;
9 for i = 1: n +m -1
10 conv_sum = 0;
11 for j = 1: i
12 if ((( i - j +1) <= n ) &( j <= m ) )
13 conv_sum = conv_sum + x ( j ) * h (i - j +1) ;
14 end ;
15 y ( i ) = conv_sum ;
16 end ;
17 end ;
18 disp (y ’ , ’ The L i n e a r C o n v o l u t i o n : ’ )
19 subplot (3 ,1 ,1) ;

7
20 plot2d3 ( ’ gnn ’ ,x )
21 xtitle ( ’ I n p u t S i g n a l ’ ) ;
22 subplot (3 ,1 ,2) ;
23 plot2d3 ( ’ gnn ’ ,h )
24 xtitle ( ’ System I m p u l s e R e s p o n s e ’ ) ;
25 subplot (3 ,1 ,3) ;
26 plot2d3 ( ’ gnn ’ ,y )
27 xtitle ( ’ C o n v o l v e d S i g n a l ’ ) ;
28 // x =[1 1 1 1 ] ;
29 // h =[1 − 1 ] ;
30 // y=x ∗h =[1 0 0 0 − 1 ] ;

8
Experiment: 3

Implementation of FIR filter

Scilab code Solution 3.1 FIR Low Pass Filter

1 clc ;
2 clear all ;
3
4 t =0:0.01:1; // s a m p l i n g f r e q . i s 100 Hz
5
6
7 x1 = sin (2* %pi *10* t ) ;
8 x4 = sin (2* %pi *20* t ) ;
9 x2 = sin (2* %pi *30* t ) ;
10 x3 = sin (2* %pi *50* t ) ;
11
12 x = x1 + x2 + x3 + x4 ; // i n p u t s i g n a l c o n s i s t s o f h a r m o n i c s
13
14 subplot (1 ,2 ,1)
15 plot (t , x ) ;
16 xlabel ( ’ t i m e ( s ) ’ )
17 ylabel ( ’ x ( n ) ’ )
18 title ( ’ i n p u t s i g n a l ’ )
19 // c o e f f i t i e n t s f o r t h e FIR low p a s s f i l t e r f o r Wn
=0.5 & n=10
20 b =[0.00506031712484485 -3.25061549073449 e -18

9
-0.0419428794313447 1.32104344910468 e -17
0.288484826302638 0.496795472007725
0.288484826302638 1.32104344910468 e -17
-0.0419428794313447 -3.25061549073449 e -18
0.00506031712484485];
21
22 a =[1];
23
24 y = filter (b ,a , x ) ; // F i l t e r e d signal
25
26 subplot (1 ,2 ,2)
27 plot (t , y ) ;
28 xlabel ( ’ t i m e ( s ) ’ )
29 ylabel ( ’ y ( n ) ’ )
30 title ( ’ f i l t e r e d s i g n a l ’ )

10
Experiment: 4

Implementation of IIR filter

Scilab code Solution 4.1 Butterworth LPF

1 clc ;
2 clear all ;
3
4 t =0:0.01:1; // s a m p l i n g f r e q . i s 100 Hz
5
6
7 x1 = sin (2* %pi *10* t ) ;
8 x4 = sin (2* %pi *20* t ) ;
9 x2 = sin (2* %pi *30* t ) ;
10 x3 = sin (2* %pi *50* t ) ;
11
12 x = x1 + x2 + x3 + x4 ; // i n p u t s i g n a l c o n s i s t s o f h a r m o n i c s
13
14 subplot (1 ,2 ,1)
15 plot (t , x ) ;
16 xlabel ( ’ t i m e ( s ) ’ )
17 ylabel ( ’ x ( n ) ’ )
18 title ( ’ i n p u t s i g n a l ’ )
19 // c o e f f i t i e n t s f o r t h e b u t t e r w o r t h low p a s s f i l t e r
f o r Wn=0.2 & n=10
20 b =[1.68358140723024 e -06 1.68358140723024 e -05

11
7.57611633253608 e -05 0.000202029768867629
0.000353552095518350 0.000424262514622020
0.000353552095518350 0.000202029768867629
7.57611633253608 e -05 1.68358140723024 e -05
1.68358140723024 e -06];
21
22 a =[1 -5.98758962981667 16.6721933230027
-28.2587879002005 32.1597564876946
-25.6017495970533 14.4056874262078
-5.64707434413247 1.47372793697391
-0.230919345862028 0.0164796305471308];
23
24 y = filter (b ,a , x ) ; // F i l t e r e d signal
25
26 subplot (1 ,2 ,2)
27 plot (t , y ) ;
28 xlabel ( ’ t i m e ( s ) ’ )
29 ylabel ( ’ y ( n ) ’ )
30 title ( ’ f i l t e r e d s i g n a l ’ )

Scilab code Solution 4.2 Chebyshev HPF

1 clc ;
2 clear all ;
3
4 t =0:0.01:1; // s a m p l i n g f r e q . i s 100 Hz
5
6
7 x1 = sin (2* %pi *10* t ) ;
8 x4 = sin (2* %pi *20* t ) ;
9 x2 = sin (2* %pi *30* t ) ;
10 x3 = sin (2* %pi *50* t ) ;
11
12 x = x1 + x2 + x3 + x4 ; // i n p u t s i g n a l c o n s i s t s o f h a r m o n i c s
13

12
14 subplot (1 ,2 ,1)
15 plot (t , x ) ;
16 xlabel ( ’ t i m e ( s ) ’ )
17 ylabel ( ’ x ( n ) ’ )
18 title ( ’ i n p u t s i g n a l ’ )
19 // c o e f f i t i e n t s f o r t h e Chebyshev h i g h p a s s f i l t e r
f o r Wn=0.3 & n =10 , Rp=0.5
20 b =[0.000400240193253256 -0.00400240193253256
0.0180108086963965 -0.0480288231903907
0.0840504405831837 -0.100860528699820
0.0840504405831837 -0.0480288231903907
0.0180108086963965 -0.00400240193253256
0.000400240193253256];
21
22 a =[1 2.85034451888448 6.11927077375458
9.17660129143362 10.9469114886769
10.3652047898718 7.89215238334179
4.75063718947987 2.18322035006721
0.704092699059353 0.129574325745057];
23
24 y = filter (b ,a , x ) ; // F i l t e r e d signal
25
26 subplot (1 ,2 ,2)
27 plot (t , y ) ;
28 xlabel ( ’ t i m e ( s ) ’ )
29 ylabel ( ’ y ( n ) ’ )
30 title ( ’ f i l t e r e d s i g n a l ’ )

13
Experiment: 5

Calculation of FFT

Scilab code Solution 5.1 FFT DIT

1 clc
2
3 clear all
4
5
6 x =[1 1 1 1 0 0 0 0] // I n p u t signal vector
7 N = length ( x ) ;

// c o m p u t i n g t h e a r r a y s i z e
8 S = log2 ( N ) ;

// c o m p u t i n g t h e number o f c o n v e r s i o n s t a g e s
9 Half =1;

// S e t i n g t h e i n i t i a l ” H a l f ” v a l u e
10 x =[ x (1) x (5) x (3) x (7) x (2) x (6) x (4) x (8) ] ;

// P l a c i n g t h e d a t a s a m p l e s i n b i t −r e v e r s e d o r d e r
11 for stage =1: S ;

// s t a g e s o f t r a n s f o r m a t i o n

14
12 for index =0:(2^ stage ) :( N -1) ;
//
s e r i e s o f ” b u t t e r f l i e s ” f o r each s t a g e
13 for n =0:( Half -1) ;

// c r e a t i n g ” b u t t e r f l y ” and s a v i n g t h e
results
14 pos = n + index +1;

// i n d e x o f t h e d a t a s a m p l e
15 pow =(2^( S - stage ) ) * n ;

// p a r t o f power o f t h e c o m p l e x
multiplier
16 w = exp (( -1* %i ) *(2* %pi ) * pow / N ) ;

// c o m p l e x m u l t i p l i e r
17 a = x ( pos ) + x ( pos + Half ) .* w ;

// 1− s t p a r t o f t h e ” b u t t e r f l y ”
creating operation
18 b = x ( pos ) -x ( pos + Half ) .* w ;

// 2−nd p a r t o f t h e ” b u t t e r f l y ”
creating operation
19 x ( pos ) = a ;

// s a v i n g c o m p u t a t i o n o f t h e 1− s t
part
20 x ( pos + Half ) = b ;

// s a v i n g c o m p u t a t i o n o f t h e 2−nd
part
21 end ;
22 end ;
23 Half =2* Half ;

// c o m p u t i n g t h e n e x t ” H a l f ” v a l u e

15
24 end ;
25 y = x ;
26 disp ( ” The FFT o f t h e g i v e n i n p u t s e q u e n c e i s ”);
27 disp ( y )

16
Experiment: 6

Generation of Discrete time


Signals

Scilab code Solution 6.1 Unit Sample Signal

1 // U n i t Sample S e q u e n c e
2 clear ;
3 clc ;
4 close ;
5 l = 9; // L i m i t s
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 =5;
13 xtitle ( ’ U n i t Sample S e q u e n c e ’ , ’ n ’ , ’ x [ n ] ’ ) ;

Scilab code Solution 6.2 Unit Step Signal

17
1 // U n i t S t e p S e q u e n c e
2 clear ;
3 clc ;
4 close ;
5 l = 9; // 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 ( ’ U n i t S t e p S i g n a l ’ )
12 xlabel ( ’ n’
);
13 ylabel ( ’ x
[ n ] ’ );

Scilab code Solution 6.3 Ramp Signal

1 //Ramp S i g n a l
2 clear ;
3 clc ;
4 close ;
5 l = 9; // 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 ( ’Ramp S i g n a l ’ , ’ n ’ , ’ x [ n ] ’ ) ;

Scilab code Solution 6.4 Exponential Signal

18
1 // E x p o n e n t i a l S i g n a l
2 clear ;
3 clc ;
4 close ;
5 a =1.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 xtitle ( ’ E x p o n e n t i a l S i g n a l ’ , ’ n ’ , ’ x [ n ] ’ ) ;

19
Experiment: 7

Verification of Sampling
Theorem

Scilab code Solution 7.1 Verification of the Sampling Theorem

1 // 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
( −| t | )
2 // D i s c r e t e Time Sampled S i g n a l x ( nT )= exp ( −|nT | )
3 clear ;
4 clc ;
5 close ;
6 stacksize ( ’ max ’ )
7 A =1;
8 t1 = 0.005;
9 t = -5: t1 :5;
10 xa = exp ( - A * abs ( t ) ) ;
11 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 : ’ ) ;
// I n p u t −−> S c a l a r ( e . g . : 1 , 2 , 4 , 2 0 , 1 0 0 )
12 Ts = 1/ Fs ;
13 nTs = -5: Ts :5;
14 x = exp ( - A * abs ( nTs ) ) ;
15 Xa = x * sinc ( Fs *( ones ( length ( nTs ) ,1) *t - nTs ’* ones (1 ,
length ( t ) ) ) ) ;
16

20
17 subplot (2 ,1 ,1) ;
18 a = gca () ;
19 a . x_location = ” o r i g i n ” ;
20 a . y_location = ” o r i g i n ” ;
21 plot (t , xa ) ;
22 xlabel ( ’ t i m e ( s ) ’ ) ;
23 ylabel ( ’ xa ( t ) ’ )
24 title ( ’ O r i g i n a l Analog S i g n a l ’ )
25 subplot (2 ,1 ,2) ;
26 a = gca () ;
27 a . x_location = ” o r i g i n ” ;
28 a . y_location = ” o r i g i n ” ;
29 xlabel ( ’ t i m e ( s ) . ’ ) ;
30 ylabel ( ’ xa ( t ) ’ )
31 title ( ’ R e c o n s t r u c t e d S i g n a l ’ ) ;
32 plot (t , Xa ) ;

21
Experiment: 8

FFT and IFFt

Scilab code Solution 8.1 FFT IFFT

1 //FFT and IFFT o f a D i g i t a l S i g n a l


2 clear ;
3 clc ;
4 close ;
5 x = input ( ’ E n t e r t h e D i g i t a l S i g n a l x [ n ] ’ ) ; // I n p u t
−−> V e c t o r
6 l = length ( x ) ;
7 X = fft ( x ) ;
8 disp (X , ’FFT o f x [ n ] i s X( k )= ’ )
9 x = abs ( fft (X ,1) )
10 disp (x , ’ IFFT o f X( k ) i s x [ n ]= ’ )
11 subplot (2 ,1 ,1)
12 a = gca () ;
13 a . data_bounds =[0 ,0;5 ,10];
14 plot2d3 ( ’ gnn ’ ,0: length ( x ) -1 , x )
15 b = gce () ;
16 b . children (1) . thickness =5;
17 xtitle ( ’ D i g i t a l S i g n a l ’ , ’ n ’ , ’ x [ n ] ’ ) ;
18 subplot (2 ,1 ,2)
19 a = gce () ;
20 a . data_bounds =[0 ,0;5 ,10];

22
21 plot2d3 ( ’ gnn ’ ,0: length ( X ) -1 , abs ( X ) )
22 b = gce () ;
23 b . children (1) . thickness =5;
24 xtitle ( ’ D i g i t a l Spectrum ’ , ’ k ’ , ’X( k ) ’ ) ;
25 // x [ n ] = [ 1 0 1 0 1 0 ]
26 //X [ k ] = [ 3 0 0 3 0 0 ]
27 //

23
Experiment: 9

Time and Frequency response


of LTI systems

Scilab code Solution 9.1 Time and Frequency response of LTI system

1
2 // I m p u l s e r e s p o n s e h ( t )= exp (−∗ t ) u ( t )
3 // F r e q u e n c y r e s p o n s e H( jw ) = 1 / ( jw +1)
4 clear ;
5 clc ;
6 close ;
7 t = 0:0.005:10;
8 ht = exp ( - t ) ;
9 figure (1)
10 a = gca () ;
11 // a . y l o c a t i o n = ” o r i g i n ” ;
12 plot (t , ht ) ;
13 xlabel ( ’ t i m e ( s ) ’ ) ;
14 ylabel ( ’ h ( t ) ’ )
15 title ( ’ I m p u l s e R e p s o n s e o f 1 s t Order L i n e a r
D i f f e r e n t i a l Equation ’ )
16
17 Wmax = 2* %pi *1;
18 K = 4;

24
19 k = 0:( K /1000) : K ;
20 W = k * Wmax / K ;
21 HW = ht * exp ( - sqrt ( -1) *t ’* W ) * 0.005;
22 HW_Mag = abs ( HW ) ;
23 W = [ - mtlb_fliplr ( W ) , W (2:1001) ];
24 HW_Mag = [ mtlb_fliplr ( HW_Mag ) , HW_Mag (2:1001) ];
25 [ HW_Phase , db ] = phasemag ( HW ) ;
26 HW_Phase = [ - mtlb_fliplr ( HW_Phase ) , HW_Phase (2:1001)
];
27 figure (2)
28 // P l o t t i n g Magnitude R e s p o n s e
29 subplot (2 ,1 ,1) ;
30 a = gca () ;
31 a . y_location = ” o r i g i n ” ;
32 plot (W , HW_Mag , ’ g ’ ) ;
33 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) ’ ) ;
34 ylabel ( ’H(jW) ) ’ )
35 title ( ’ Magnitude R e s p o n s e ’ )
36 // P l o t t i n g Phase Reponse
37 subplot (2 ,1 ,2) ;
38 a = gca () ;
39 a . y_location = ” o r i g i n ” ;
40 a . x_location = ” o r i g i n ” ;
41 plot (W , HW_Phase * %pi /180 , ’ r ’ ) ;
42 xlabel ( ’ Frequency in
R a d i a n s / S e c o n d s (W) ’ ) ;
43 ylabel ( ’
H(
jW) ’ )
44 title ( ’ Phase R e s p o n s e ’ )

25
Experiment: 10

Linear and circular Convolution


through FFT

Scilab code Solution 10.1 Linear Convolution using FFT IFFT

1 // L i n e a r C o n v o l u t i o n u s i n g FFT−IFFT
2 clc ;
3 clear all ;
4 close ;
5 x = input ( ’ E n t e r t h e I n p u t S i g n a l x [ n ] ’ ) ; // I n p u t −−>
Vector
6 h = input ( ’ E n t e r t h e System I m p u l s e R e s p o n s e h [ n ] ’ ) ;
// I n p u t −−> V e c t o r
7 m = length ( x ) ;
8 n = length ( h ) ;
9 N = n +m -1;
10 x = [ x zeros (1 ,N - m ) ];
11 h = [ h zeros (1 ,N - n ) ];
12 f1 = fft ( x )
13 f2 = fft ( h )
14 f3 = f1 .* f2 ; // f r e q domain m u l t i p l i c a t i o n
15 f4 = ifft ( f3 )
16 disp ( f4 , ’ C o n v o l v e d S i g n a l ’ )
17

26
18 subplot (3 ,1 ,1) ;
19 plot2d3 ( ’ gnn ’ ,x )
20 xtitle ( ’ I n p u t S i g n a l ’ ) ;
21 subplot (3 ,1 ,2) ;
22 plot2d3 ( ’ gnn ’ ,h )
23 xtitle ( ’ System I m p u l s e R e s p o n s e ’ ) ;
24 subplot (3 ,1 ,3) ;
25 plot2d3 ( ’ gnn ’ , f4 )
26 xtitle ( ’ C o n v o l v e d S i g n a l ’ ) ;
27 // x [ n ] = [ 1 1 1 1 ] ;
28 // y [ n ] = [ 1 − 1 ] ;
29 // x ∗h = [ 1 . − 5 . 5 5 1D−17 1 . 1 1 0D−16 5 . 5 5 1D−17 −
1. ]

Scilab code Solution 10.2 Circular Convolution using FFT

1 // C i r c u l a r C o n v o l u t i o n U s i n g FFT−IFFT
2 clear all ;
3 clc ;
4 close ;
5 l = input ( ’ E n t e r t h e Length o f t h e s i g n a l s : ’ ) ; // I n p u t
−−> S c a l a r
6 x1 = input ( ’ E n t e r t h e I n p u t S i g n a l x1 [ n ] ’ ) ; // I n p u t −−>
Vector
7 x2 = input ( ’ E n t e r t h e I n p u t S i g n a l x2 [ n ] ’ ) ; // I n p u t −−>
Vector
8 X1 = fft ( x1 ) ;
9 X2 = fft ( x2 ) ;
10 disp ( X1 , ’DFT o f x1 [ n ] i s X1 ( k )= ’ )
11 disp ( X2 , ’DFT o f x1 [ n ] i s X2 ( k )= ’ )
12 X3 = X1 .* X2 ;
13 disp ( X3 , ’DFT o f x3 [ n ] i s X3 ( k )= ’ )
14 x3 = abs ( fft ( X3 ,1) )
15 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 ]= ’ )
16

27
17 // l =3;
18 // x1 =[1 1 1 ]
19 // x2 =[2 2 2 ]
20 //X1 [ k ] = [ 3 0 0 ]
21 //X2 [ k ] = [ 6 0 0 ]
22 //X3 [ k ] = [ 1 8 0 0 ]
23 // x3 =[6 6 6 ]

28
Experiment: 11

Design of FIR filters(window


design)

Scilab code Solution 11.1 FIR Low Pass Filter Rectangular Window

1 // FIR Low P a s s F i l t e r
2 clc ;
3 close ;
4 M = input ( ’ E n t e r t h e Odd F i l t e r Length = ’ ) ; // I n p u t
−−>S c a l a r
5 Wc = input ( ’ 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 = ’ ) ;
// I n p u t −−>S c a l a r ( e . g .−−>0.3∗ %pi )
6 Tuo = (M -1) /2
7 for n = 1: M
8 if ( n == Tuo +1)
9 hd ( n ) = Wc / %pi ;
10 else
11 hd ( n ) = sin ( Wc *(( n -1) - Tuo ) ) /((( n -1) - Tuo ) * %pi )
;
12 end
13 end
14 // R e c t a n g u l a r Window
15 for n = 1: M
16 W ( n ) = 1;

29
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 //M=3;
37 //Wc=0.3∗ %pi ;
38 // h = 0 . 2 5 7 5 1 8 1
39 // 0.3
40 // 0.2575181

30
Experiment: 12

Design of IIR filters


(Butterworth & Chebychev)

Scilab code Solution 12.1 IIR Butterworth LPF

1 // I I R F i r s t Order B u t t e r w o r t h LPF U s i n g B i l i n e a r
Transformation
2 clear all ;
3 clc ;
4 close ;
5 s = poly (0 , ’ s ’ ) ;
6 Omegac = 0.2* %pi ; // I n p u t −>S c a l a r // C u t o f f
frequency
7 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
8 T =1; // S a m p l i n g p e r i o d T = 1 S e c o n d
9 z = poly (0 , ’ z ’ ) ;
10 Hz = horner (H ,(2/ T ) *(( z -1) /( z +1) ) ) // B i l i n e a r
Transformation
11 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
12 W = 0: %pi /511: %pi ;
13 a = gca () ;
14 a . thickness = 1;

31
15 plot ( W / %pi , HW )
16 a . foreground = 1;
17 a . font_style = 9;
18 xgrid (1)
19 xtitle ( ’ Magnitude R e s p o n s e o f S i n g l e p o l e LPF ’ , ’
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 ’ ) ;

Scilab code Solution 12.2 IIR Chebyshev Filter

1 // Chebyshev I I R F i l t e r
2 clear ;
3 clc ;
4 close ;
5 Wp = input ( ’ 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 : ’ ) ; // I n p u t −−>S c a l a r
6 Ws = input ( ’ 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 : ’ ) ; // I n p u t −−>S c a l a r
7 T = input ( ’ S a m p l i n g I n t e r v a l ’ ) // I n p u t −−>S c a l a r
8 OmegaP = (2/ T ) * tan ( Wp /2)
9 OmegaS = (2/ T ) * tan ( Ws /2)
10 Delta1 = input ( ’ E n t e r t h e P a s s Band R i p p l e : ’ ) ; //
I n p u t −−> s c a l a r
11 Delta2 = input ( ’ E n t e r t h e S t o p Band R i p p l e : ’ ) ; //
I n p u t −−> s c a l a r
12 Delta = sqrt (((1/ Delta2 ) ^2) -1)
13 Epsilon = sqrt (((1/ Delta1 ) ^2) -1)
14 N = ( acosh ( Delta / Epsilon ) ) /( acosh ( OmegaS / OmegaP ) )
15 N = ceil ( N )
16 OmegaC = OmegaP /((((1/ Delta1 ) ^2) -1) ^(1/(2* N ) ) )
17 [ pols , gn ] = zpch1 (N , Epsilon , OmegaP )
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

32
22 W = 0: %pi /511: %pi ;
23 a = gca () ;
24 a . thickness = 1;
25 plot ( W / %pi , abs ( HW ) )
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 re q u e n c y −−−> ’ , ’ Magnitude i n
dB ’ ) ;
30 //Wp=0.3∗ %pi
31 //Ws=0.5∗ %pi
32 //T=0.01
33 // D e l t a 1 =0.8
34 // D e l t a 2 =0.2

33

You might also like