Tutorial On Scilab
Tutorial On Scilab
Manolakis1
Created by Prof. R. Senthilkumar Institute of Road and Transport Technology rsenthil [email protected] Cross-Checked by Mrs. Phani Swathi Chitta Research Scholar, IITB under the guidance of Prof. Saravanan Vijayakumaran, IIT Bombay [email protected] 23 August 2010
by a grant from the National Mission on Education through ICT, http://spoken-tutorial.org/NMEICT-Intro.This text book companion and Scilab codes written in it can be downloaded from the Textbook Companion Project Section at the website http://scilab.in/
1 Funded
Book Details
Authors: J. G. Proakis and D. G. Manolakis Title: Digital Signal Processing Publisher: Prentice Hall India Edition: 3rd Year: 1997 Place: New Delhi ISBN: 81-203-1129-9
Scilab numbering policy used in this document and the relation to the above book. Prb Problem (Unsolved problem) Exa Example (Solved example) Eqn Equation (Particular equation of the above book) ARC Additionally Required Code (Scilab Code that is not part of the above book but required to solve a particular Example) AE Appendix to Example(Scilab Code that is an Appednix to a particular Example of the above book) CF Code for Figure(Scilab code that is used for plotting the respective gure of the above book ) For example, Prb 4.56 means Problem 4.56 of the above book. 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.
Contents
List of Scilab Codes 1 Introduction 1.1 Scilab Code . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Discrete Time Signals and Systems 2.1 Scilab Code . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 The z Transformation and its Applications to the Analysis of LTI Systems 3.1 Scilab Code . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Frequency Analysis of Signal and Systems 4.1 Scilab Code . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 2 2 3 3
8 8 15 15
5 Discrete Fourier Transform: its Properties and Applications 22 5.1 Scilab Code . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 6 Ecient Computation of DFT: Fast Fourier Transform, Algorithms 27 6.1 Scilab Code . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 7 Implementation of Discrete Time System 7.1 Scilab Code . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Design of Digital Filters 8.1 Scilab Code . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 30 33 33
10 Multirate Digital Signal Processing 10.1 Scilab Code . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Linear Predictions and Optimum Linear Filter 11.1 Scilab Code . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Power Spectrum Estimation 12.1 Scilab Code . . . . . . . . . . . . . . . . . . . . . . . . . . .
52 52 59 59 60 60
Exa 5.1.3 Exa 5.2.1 Exa 5.3.1 Exa 5.4.1 Exa 6.4.1 Exa 6.4.2 Prb 6.8 Prb 6.11 Exa 7.6.3 Eqn 7.7.1 Exa 7.7.1 Eqn 7.7.40 Exa 8.2.1 Exa 8.2.2 Exa 8.2.3 Exa 8.2.4 Exa 8.2.5 Exa 8.2.6 Eqn 8.2.28 Exa 8.3.2 Exa 8.3.4 Exa 8.3.5 Exa 8.3.6 Exa 8.3.7 Exa 8.4.1 Exa 8.4.2 CF 8.5 CF 8.6 CF 8.7 Exa 10.5.1 Exa 10.5.2 Exa 10.6.1 Exa 10.8.1 Exa 10.8.2 Exa 10.9.1 Exa 11.6.1 Exa 12.1.1 Exa 12.1.2
Discrete Fourier Transform . . . . . . Example 5.2.1 and 5.2.2 . . . . . . . . Linear Filtering DFT . . . . . . . . . Zero Padding . . . . . . . . . . . . . . SNR DFT . . . . . . . . . . . . . . . SNR FFT Algorithm . . . . . . . . . . FFT Exercise1 . . . . . . . . . . . . . FFT Exercise2 . . . . . . . . . . . . . Coeecient Quantization NOise . . . Dead Band . . . . . . . . . . . . . . . Round o noise variance . . . . . . . . SQNR . . . . . . . . . . . . . . . . . . Design of Filter . . . . . . . . . . . . . Design of Filter . . . . . . . . . . . . . Remez Algorithm Based . . . . . . . . Remez Algorithm Based . . . . . . . . FIR Dierentiator . . . . . . . . . . . Hilbert Transform . . . . . . . . . . . Design of Filter . . . . . . . . . . . . . Analog to Digital . . . . . . . . . . . . Analog to Digital . . . . . . . . . . . . Analog to Digital . . . . . . . . . . . . IIR Filter Design Butterworth Filter . IIR Filter Design . . . . . . . . . . . . Digital IIR Butterworth Filter . . . . IIR Filter Design Butterworth Filter . Window Functions . . . . . . . . . . . Window Functions . . . . . . . . . . . Window Functions . . . . . . . . . . . FIR Decimation . . . . . . . . . . . . FIR Interpolation . . . . . . . . . . . Sampling Rate Conversion Decimation Signal Distortion Ratio . . . . . . . . Signal Distortion Ratio . . . . . . . . Sampling Rate Conversion Decimation Wiener Filter . . . . . . . . . . . . . . Spectrum of Signal . . . . . . . . . . . Spectrum using DFT . . . . . . . . . 6
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Interpolation . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
23 23 24 25 27 27 28 28 30 30 31 32 33 34 35 36 37 38 40 41 41 42 43 45 46 47 49 49 50 52 53 54 56 56 57 59 60 61
Exa 12.5.1 Additive Noise Parameters . . . . . AE 4.2.7 Discrete Time Fourier Transform . . ARC 4A sinc . . . . . . . . . . . . . . . . . . AE 4.4.2 Frequency response . . . . . . . . . AE 8.2.28ADesign of Filter . . . . . . . . . . . . AE 8.2.28BDesign of Filter . . . . . . . . . . . . AE 8.2.28CDesign of Filter . . . . . . . . . . . . AE 8.3.5 First Order Butterworth Filter . . . AE 8.3.6 IIR Butterworth Filter . . . . . . . . AE 8.4.1 First Order Butterworth Filter . . . AE 8.4.2A IIR Filter Design Butterworth Filter AE 8.4.2B IIR Filter Design Butterworth Filter
. . . . . . . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . .
63 65 66 66 67 68 70 71 72 73 73 74
Chapter 1 Introduction
Install Symbolic Toolbox.Refer the spoken tutorial on the link (www.spokentutorial.org) for the installation of Symbolic Toolbox.
1.1
Scilab Code
Scilab code Eqn 1.2.1 Discrete time signal as implemented in the book on Page 9 // I m p l e m e n t a t i o n o f E q u a t i o n 1 . 2 . 1 i n C h a p t e r 1 // D i g i t a l S i g n a l P r o c e s s i n g by P r o a k i s , T h i r d E d i t i o n , PHI 3 // Page 9
1 2 4 5 6 7 8 9 10 11 12
clear ; clc ; close ; n = 0:10; x = (0.8) ^ n ; // p l o t 2 d 4 ( n , x ) a = gca () ; a . thickness = 2; plot2d3 ( gnn ,n , x ) 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 Time S i g n a l , n , x [ n ] );
Scilab code Eqn 2.1.6 Unit sample sequence, also known as unit impulse sequence and delta sequence // I m p l e m e n t a t i o n o f E q u a t i o n 2 . 1 . 6 i n C h a p t e r 2 // D i g i t a l S i g n a l P r o c e s s i n g by P r o a k i s , T h i r d E d i t i o n , PHI 3 // Page 45
1 2 4 5 6 7 8 9 10 11 12 13
clear ; clc ; close ; L = 4; // U p p e r l i m i t n = -L : L ; x = [ zeros (1 , L ) ,1 , zeros (1 , L ) ]; a = gca () ; a . thickness = 2; a . y_location = m i d d l e ; plot2d3 ( gnn ,n , x ) 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 ] ); Scilab code Eqn 2.1.7 Unit step sequence 3
// I m p l e m e n t a t i o n o f E q u a t i o n 2 . 1 . 7 i n C h a p t e r 2 // D i g i t a l S i g n a l P r o c e s s i n g by P r o a k i s , T h i r d E d i t i o n , PHI 3 // Page 45
1 2 4 5 6 7 8 9 10 11 12 13
clear ; clc ; close ; L = 4; // U p p e r l i m i t n = -L : L ; x = [ zeros (1 , L ) , ones (1 , L +1) ]; a = gca () ; a . thickness = 2; a . y_location = m i d d l e ; plot2d3 ( gnn ,n , x ) 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 S t e p S i g n a l , n , x [ n ] ); Scilab code Eqn 2.1.8 Unit ramp sequence
// I m p l e m e n t a t i o n o f E q u a t i o n 2 . 1 . 8 i n C h a p t e r 2 // D i g i t a l S i g n a l P r o c e s s i n g by P r o a k i s , T h i r d E d i t i o n , PHI 3 // Page 45
1 2 4 5 6 7 8 9 10 11 12 13
clear ; clc ; close ; L = 4; // U p p e r l i m i t n = -L : L ; x = [ zeros (1 , L ) ,0: L ]; a = gca () ; a . thickness = 2; a . y_location = m i d d l e ; plot2d3 ( gnn ,n , x ) 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 Ramp S i g n a l , n , x [ n ] ); Scilab code Eqn 2.1.9a Exponential sequence
// I m p l e m e n t a t i o n o f E q u a t i o n 2 . 1 . 9 i n C h a p t e r 2 4
2 3 4 5 6 7 8 9 10 11 12 13
// D i g i t a l S i g n a l P r o c e s s i n g by P r o a k i s , T h i r d E d i t i o n , PHI // Page 46 clear ; clc ; close ; a =1.5; n =1:10; x = (a)^n; a = gca () ; a . thickness = 2; plot2d3 ( gnn ,n , x ) 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 S i g n a l , n , x [ n ] ); Scilab code Eqn 2.1.9b Exponential increasing sequence
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// I m p l e m e n t a t i o n o f E q u a t i o n 2 . 1 . 9 b i n C h a p t e r 2 // D i g i t a l S i g n a l P r o c e s s i n g by P r o a k i s , T h i r d E d i t i o n , PHI // Page 46 // a < 0 clear ; clc ; close ; a = -1.5; n = 0:10; x = (a)^n; a = gca () ; a . thickness = 2; a . x_location = o r i g i n ; a . y_location = o r i g i n ; plot2d3 ( gnn ,n , x ) 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 I n c r e a s i n g D e c r e a s i n g S i g n a l , n , x [ n ] ) ; Scilab code Eqn 2.1.9c Exponential decreasing sequence 5
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// I m p l e m e n t a t i o n o f E q u a t i o n 2 . 1 . 9 c i n C h a p t e r 2 // D i g i t a l S i g n a l P r o c e s s i n g by P r o a k i s , T h i r d E d i t i o n , PHI // Page 46 // a < 1 clear ; clc ; close ; a =0.5; n = 0:10; x = (a)^n; a = gca () ; a . thickness = 2; a . x_location = m i d d l e ; plot2d3 ( gnn ,n , x ) 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 D e c r e a s i n g S i g n a l , n , x [ n ] ); Scilab code Eqn 2.1.24 Even signal
// I m p l e m e n t a t i o n o f E q u a t i o n 2 . 1 . 2 4 i n C h a p t e r 2 // D i g i t a l S i g n a l P r o c e s s i n g by P r o a k i s , T h i r d E d i t i o n , PHI 3 // Page 51
1 2 4 5 6 7 8 9 10 11 12 13
clear ; clc ; close ; n = -7:7; x1 = [0 0 0 1 2 3 4]; x = [ x1 ,5 , x1 ( length ( x1 ) : -1:1) ]; a = gca () ; a . thickness = 2; a . y_location = m i d d l e ; plot2d3 ( gnn ,n , x ) 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 Even S i g n a l , n , x [ n ] ); Scilab code Eqn 2.1.25 Odd signal 6
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// I m p l e m e n t a t i o n o f E q u a t i o n 2 . 1 . 2 5 i n C h a p t e r 2 // D i g i t a l S i g n a l P r o c e s s i n g by P r o a k i s , T h i r d E d i t i o n , PHI // Page 51 clear ; clc ; close ; n = -5:5; x1 = [0 1 2 3 4 5]; x = [ - x1 ( $ : -1:2) , x1 ]; a = gca () ; a . thickness = 2; a . y_location = m i d d l e ; a . x_location = m i d d l e plot2d3 ( gnn ,n , x ) 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 ODD S i g n a l , n , x [ n ] );
Chapter 3 The z Transformation and its Applications to the Analysis of LTI Systems
3.1 Scilab Code
// Example 3 . 1 . 1 //Z T r a n s f o r m o f F i n i t e D u r a t i o n S I g n a l s clear all ; clc ; close ; x1 = [1 ,2 ,5 ,7 ,0 ,1]; n1 = 0: length ( x1 ) -1; X1 = ztransfer_new ( x1 , n1 ) x2 = [1 ,2 ,5 ,7 ,0 ,1]; n2 = -2:3; X2 = ztransfer_new ( x2 , n2 ) x3 =[0 ,0 ,1 ,2 ,5 ,7 ,0 ,1]; n3 = 0: length ( x3 ) -1; X3 = ztransfer_new ( x3 , n3 ) x4 = [2 ,4 ,5 ,7 ,0 ,1]; n4 = -2:3;
17 18 19 20 21
X4 x5 n5 X5 x6
22 n6 23 X6 24 x7 25 n7 26 X7
= ztransfer_new ( x4 , n4 ) = [1 ,0 ,0]; // S ( n ) U n i t I m p u l s e s e q u e n c e = 0: length ( x5 ) -1; = ztransfer_new ( x5 , n5 ) = [0 ,0 ,0 ,1]; // S ( n 3) u n i t i m p u l s e s e q u e n c e shifted = 0: length ( x6 ) -1; = ztransfer_new ( x6 , n6 ) = [1 ,0 ,0 ,0]; // S ( n+3) U n i t i m p u l s e s e q u e n c e shifted = -3:0; = ztransfer_new ( x7 , n7 )
// Example 3 . 1 . 5 //Z t r a n s f o r m o f x [ n ] = a n . u [ n ]+ b n . u[ n 1] // a = 0 . 5 and b = 0 . 6 clear all ; close ; clc ; syms n z ; x1 =(0.5) ^( n ) X1 = symsum ( x1 *( z ^( - n ) ) ,n ,0 , %inf ) x2 =(0.6) ^( - n ) X2 = symsum ( x2 *( z ^( n ) ) ,n ,1 , %inf ) X = ( X1 + X2 ) disp (X , a n s= ) Scilab code Exa 3.2.1 Z transform of x(n) = 3.2n .u(n) 4.3n .u(n)
1 2 3 4 5 6 7 8 9 10 11 12
// Example 3 . 2 . 1 //Z t r a n s f o r m o f x [ n ] = 3 . 2 n . u [ n ] 4 . 3 n . u [ n ] clear all ; close ; clc ; syms n z ; x1 =(2) ^( n ) X1 = symsum (3* x1 *( z ^( - n ) ) ,n ,0 , %inf ) x2 =(3) ^( n ) X2 = symsum (4* x2 *( z ^( - n ) ) ,n ,0 , %inf ) X = ( X1 - X2 ) disp (X , a n s= ) Scilab code Exa 3.2.2 Z transform of x(n) = cos(W o.n).u(n), y (n) = sin(W o.n).u(n) 10
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// Example 3 . 2 . 2 //Z t r a n s f o r m o f x [ n ] = c o s (Wo. n ) . u [ n ] //Z t r a n s f o r m o f y [ n ] = s i n (Wo. n ) . u [ n ] clear all ; close ; clc ; syms n z ; Wo =2; x1 = exp ( sqrt ( -1) * Wo * n ) ; X1 = symsum ( x1 *( z ^( - n ) ) ,n ,0 , %inf ) ; x2 = exp ( - sqrt ( -1) * Wo * n ) ; X2 = symsum ( x2 *( z ^( - n ) ) ,n ,0 , %inf ) X =( X1 + X2 ) disp (X , a n s= ) Y =(1/(2* sqrt ( -1) ) ) *( X1 - X2 ) disp (Y , a n s= ) Scilab code Exa 3.2.3 Time shifting property of Z transform
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// Example 3 . 2 . 3 // Time S h i f t i n g P r o p e r t y o f Z t r a n s f o r m clear all ; clc ; close ; x1 = [1 ,2 ,5 ,7 ,0 ,1]; n1 = 0: length ( x1 ) -1; X1 = ztransfer_new ( x1 , n1 ) // x2 = [ 1 , 2 , 5 , 7 , 0 , 1 ] ; n2 = 0 -2: length ( x1 ) -1 -2; X2 = ztransfer_new ( x1 , n2 ) // x3 = [ 0 , 0 , 1 , 2 , 5 , 7 , 0 , 1 ] ; n3 = 0+2: length ( x1 ) -1+2; X3 = ztransfer_new ( x1 , n3 ) *Refer to the following for Scilab code of ztransfer new ARC 3A
11
// Example 3 . 2 . 7 //Z t r a n s f o r m o f x [ n ] = n . a n . u [ n ] clear all ; clc ; close ; syms n z ; x =(1) ^ n ; X = symsum ( x *( z ^( - n ) ) ,n ,0 , %inf ) disp (X , a n s= ) Y = diff (X , z ) 12
// Example 3 . 2 . 9 // C o n v o l u t i o n P r o p e r t y P r o o f clear all ; clc ; close ; x1 = [1 , -2 ,1]; n1 = 0: length ( x1 ) -1; X1 = ztransfer_new ( x1 , n1 ) x2 = [1 ,1 ,1 ,1 ,1 ,1]; n2 = 0: length ( x2 ) -1; X2 = ztransfer_new ( x2 , n2 ) X = X1 .* X2 *Refer to the following for Scilab code of ztransfer new ARC 3A
// Example 3 . 2 . 1 0 // C o r r e l a t i o n P r o p e r t y P r o o f syms n z ; x1 = (0.5) ^ n X1 = symsum ( x1 *( z ^( - n ) ) ,n ,0 , %inf ) X2 = symsum ( x1 *( z ^( n ) ) ,n ,0 , %inf ) disp ( X1 , X1 = ) disp ( X2 , X2 = ) X = X1 * X2 disp (X , X= ) // R e s u l t // Which i s e q u i v a l e n t t o Rxx ( Z ) = 1 / ( 1 0 . 5 ( z+z 1) +(0.52) ) 13 // i . e f o r a = 0 . 5 Rxx ( Z ) = 1/(1 a ( z+z 1)+(a 2 ) )
13
14
Scilab code Exa 4.1.2 Continuous time Fourier transform and Energy Density Function of Square waveform
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// Example 4 . 1 . 2 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 // and Energy D e n s i t y F u n c t i o n o f a S q u a r e Waveform // x ( t )= A, from T/2 t o T/2 clear all ; clc ; close ; // Analog S i g n a l A =1; // A m p l i t u d e Dt = 0.005; T = 4; // Time i n s e c o n d s t = -T /2: Dt : T /2; for i = 1: length ( t ) xa ( i ) = A ; end // // C o n t i n u o u s t i m e F o u r i e r T r a n s f o r m Wmax = 2* %pi *2; // Analog F r e q u e n c y = 2Hz
15
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
K = 4; k = 0:( K /800) : K ; W = k * Wmax / K ; disp ( size ( xa ) ) Xa = xa * exp ( - sqrt ( -1) *t * W ) * Dt ; Xa = real ( Xa ) ; W = [ - mtlb_fliplr ( W ) , W (2:501) ]; // Omega from Wmax t o Wmax Xa = [ mtlb_fliplr ( Xa ) , Xa (2:501) ]; ESD = Xa ^2; // Energy D e n s i t y Spectrum subplot (3 ,1 ,1) ; plot (t , xa ) ; xlabel ( t i n msec . ) ; ylabel ( xa ( t ) ) title ( Analog S i g n a l ) subplot (3 ,1 ,2) ; plot ( W /(2* %pi ) , Xa ) ; xlabel ( F r e q u e n c y i n Hz ) ; ylabel ( Xa (jW) ) title ( C o n t i n u o u s t i m e F o u r i e r T r a n s f o r m ) subplot (3 ,1 ,3) ; plot ( W /(2* %pi ) , ESD ) ; xlabel ( F r e q u e n c y i n Hz ) ; ylabel ( SXX ) title ( Energy D e n s i t y Spectrum ) Scilab code Exa 4.2.7 Sampling a Nonbandlimited Signal
1 2 3 4 5 6 7 8 9 10 11
// Example 4 . 2 . 7 S a m p l i n g a N o n b a n d l i m i t e d S i g n a l // P l o t t 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 o f // C o n t i n u o u s Time S i g n a l x ( t )= exp ( A a b s ( t ) ) clear all ; clc ; close ; // Analog S i g n a l A =1; // A m p l i t u d e Dt = 0.005; t = -2: Dt :2; xa = exp ( - A * abs ( t ) ) ; 16
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
// // C o n t i n u o u s t i m e F o u r i e r T r a n s f o r m Wmax = 2* %pi *2; // Analog F r e q u e n c y = 2Hz K = 4; k = 0:( K /500) : K ; W = k * Wmax / K ; Xa = xa * exp ( - sqrt ( -1) *t * W ) * Dt ; Xa = real ( Xa ) ; W = [ - mtlb_fliplr ( W ) , W (2:501) ]; // Omega from Wmax t o Wmax Xa = [ mtlb_fliplr ( Xa ) , Xa (2:501) ]; subplot (2 ,1 ,1) ; a = gca () ; a . x_location = o r i g i n ; a . y_location = o r i g i n ; plot (t , xa ) ; xlabel ( t i n msec . ) ; ylabel ( xa ( t ) ) title ( Analog S i g n a l ) subplot (2 ,1 ,2) ; a = gca () ; a . x_location = o r i g i n ; a . y_location = o r i g i n ; plot ( W /(2* %pi ) , Xa ) ; xlabel ( F r e q u e n c y i n Hz ) ; ylabel ( Xa (jW) 1 0 0 0 ) title ( C o n t i n u o u s t i m e F o u r i e r T r a n s f o r m ) *For further extension of the exapmle refer to AE 4.2.7
Scilab code Exa 4.3.4 Convolution Property Example x1(n) = x2(n) = [1, 1, 1]
1 2
// Example 4 . 3 . 4 // C o n v o l u t i o n P r o p e r t y Example 17
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
// x1 ( n )=x2 ( n )= [ 1 , 1 , 1 ] clear all ; clc ; close ; n = -1:1; x1 = [1 ,1 ,1]; x2 = x1 ; // D i s c r e t e t i m e F o u r i e r t r a n s f o r m K = 500; k = 0:1: K ; w = %pi * k / K ; X1 = x1 * exp ( - sqrt ( -1) *n * w ) ; X2 = x2 * exp ( - sqrt ( -1) *n * w ) ; w = [ - mtlb_fliplr ( w ) , w (2: K +1) ]; // Omega from w t o w X1 = [ mtlb_fliplr ( X1 ) , X1 (2: K +1) ]; X2 = [ mtlb_fliplr ( X2 ) , X2 (2: K +1) ]; Freq_X1 = real ( X1 ) ; Freq_X2 = real ( X2 ) ; X = X1 .* X2 ; K1 = length ( X ) k1 = 0:1: K1 ; w1 = %pi * k1 / K1 ; w1 = [ -2* mtlb_fliplr ( w ) , 2* w ]; X = [ mtlb_fliplr ( X ) , X (1: K1 ) ]; Freq_X = real ( X ) ; // Inv X = X. exp ( s q r t ( 1) n w) x = convol ( x1 , x2 ) // P l o t t i n g Magitude R e s p o n s e s figure (1) a = gca () ; a . x_location = m i d d l e a . y_location = m i d d l e a . x_label a . y_label plot2d ( w / %pi , Freq_X1 ) x_label = a . x_label y_label = a . y_label 18
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
x_label . text = Frequency in Radians y_label . text = X1 (w) // x l a b e l ( F r e q u e n c y i n Radians ) // y l a b e l ( X1 (w) ) title ( F r e q u e n c y R e s p o n s e ) figure (2) a = gca () ; a . x_location = m i d d l e a . y_location = m i d d l e a . x_label a . y_label plot2d ( w / %pi , Freq_X2 ) x_label = a . x_label y_label = a . y_label x_label . text = Frequency in Radians 55 y_label . text = X2 (w) 56 title ( F r e q u e n c y R e s p o n s e ) 57 figure (3) 58 a = gca () ; 59 a . y_location = m i d d l e 60 a . x_label 61 a . y_label 62 plot2d ( w1 /(2* %pi ) , Freq_X ) 63 x_label = a . x_label 64 y_label = a . y_label 65 x_label . text = Frequency in Radians 66 y_label . text = X(w) 67 title ( F r e q u e n c y R e s p o n s e ) Scilab code Exa 4.4.2 Frequency Response of Three point Moving Average System y (n) = (1/3)[x(n + 1) + x(n) + x(n 1)]
1
// Example 4 . 4 . 2
19
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
// F r e q u e n c y R e s p o n s e o f Three p o i n t Moving A v e r a g e System // y ( n )= ( 1 / 3 ) [ x ( n+1)+x ( n )+x ( n 1) ] // h ( n ) = [ 1 / 3 , 1 / 3 , 1 / 3 ] clear all ; clc ; close ; // C a l c u l a t i o n o f I m p u l s e R e s p o n s e n = -1:1; h = [1/3 ,1/3 ,1/3]; // D i s c r e t e t i m e F o u r i e r t r a n s f o r m K = 500; k = 0:1: K ; w = %pi * k / K ; H = h * exp ( - sqrt ( -1) *n * w ) ; // phasemag u s e d t o c a l c u l a t e p h a s e and m a g n i t u d e i n dB [ Phase_H , m ] = phasemag ( H ) ; H = abs ( H ) ; subplot (2 ,1 ,1) plot2d ( w / %pi , H ) xlabel ( F r e q u e n c y i n R a d i a n s ) ylabel ( a b s (H) ) title ( Magnitude R e s p o n s e ) subplot (2 ,1 ,2) plot2d ( w / %pi , Phase_H ) xlabel ( F r e q u e n c y i n R a d i a n s ) ylabel ( <(H) ) title ( Phase R e s p o n s e ) *For further extension of the exapmle refer to AE 4.4.2
Scilab code Exa 4.4.4 Frequency Response of First order Dierence Equation
20
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
// Example 4 . 4 . 4 // F r e q u e n c y R e s p o n s e o f F i r s t Order D i f f e r e n c e Equation // a = 0 . 9 and b = 1 a // I m p u l s e R e s p o n s e h ( n ) = b . ( a n ) . u ( n ) clear all ; clc ; close ; a = input ( E n t e r t h e c o n s t a n t v a l u e o f I s t o r d e r D i f f e r e n c e Equation ); b= 1-a; // C a l c u l a t i o n o f I m p u l s e R e s p o n s e n =0:50; h = b *( a .^ n ) ; // D i s c r e t e t i m e F o u r i e r t r a n s f o r m K = 500; k = 0:1: K ; w = %pi * k / K ; H = h * exp ( - sqrt ( -1) *n * w ) ; // phasemag u s e d t o c a l c u l a t e p h a s e and m a g n i t u d e i n dB [ Phase_H , m ] = phasemag ( H ) ; H = real ( H ) ; subplot (2 ,1 ,1) plot2d ( w / %pi , H ) xlabel ( F r e q u e n c y i n R a d i a n s ) ylabel ( a b s (H) ) title ( Magnitude R e s p o n s e ) subplot (2 ,1 ,2) plot2d ( w / %pi , Phase_H ) xlabel ( F r e q u e n c y i n R a d i a n s ) ylabel ( <(H) ) title ( Phase R e s p o n s e )
21
// Example 5 . 1 . 2 // D e t e r m i n a t i o n o f N p o i n t DFT // P l o t t i n g Magnitude and Phase s p e c t r u m clear all ; clc ; close ; L = 10; // Length o f t h e s e q u e n c e N = 10; // N p o i n t DFT for n =0: L -1 x ( n +1) = 1; end // Computing DFT and IDFT X = dft (x , -1) x_inv = abs ( dft (X ,1) ) // Computing Magnitude and Phase Spectrum // U s i n g DTFT n = 0: L -1; K = 500;
22
19 k = 0:1: K ; 20 w = 2* %pi * k / K ; 21 X_W = x * exp ( - sqrt ( -1) *n * w ) ; 22 Mag_X = abs ( X_W ) ; 23 // phasemag u s e d t o c a l c u l a t e p h a s e and m a g n i t u d e i n 24 25 26 27 28 29 30 31 32 33 34
dB Phase_X = atan ( imag ( X_W ) , real ( X_W ) ) subplot (2 ,1 ,1) plot2d (w , Mag_X ) xlabel ( F r e q u e n c y i n R a d i a n s ) ylabel ( a b s (X) ) title ( Magnitude R e s p o n s e ) subplot (2 ,1 ,2) plot2d (w , Phase_X ) xlabel ( F r e q u e n c y i n R a d i a n s ) ylabel ( <(X) ) title ( Phase R e s p o n s e ) Scilab code Exa 5.1.3 Finding DFT and IDFT
1 2 3 4 5 6 7 8 9 10 11 12
// Example 5 . 1 . 3 // F i n d i n g DFT and IDFT clear all ; clc ; close ; L = 4; // Length o f t h e s e q u e n c e N = 4; // N p o i n t DFT x = [0 ,1 ,2 ,3]; // Computing DFT X = dft (x , -1) // Computing IDFT x_inv = real ( dft (X ,1) ) Scilab code Exa 5.2.1 Performing Circular COnvolution Using DFT
1 2 3
4 5 6 7 8 9 10 11 12 13 14 15 16 17
clear all ; clc ; close ; L = 4; // Length o f t h e S e q u e n c e N = 4; // N p o i n t DFT x1 = [2 ,1 ,2 ,1]; x2 = [1 ,2 ,3 ,4]; // Computing DFT X1 = dft ( x1 , -1) X2 = dft ( x2 , -1) // M u l t i p l i c a t i o n o f 2 DFTs X3 = X1 .* X2 // 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 = abs ( dft ( X3 ,1) ) Scilab code Exa 5.3.1 Performing Linear Filtering (i.e) Linear Convolution Using DFT
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
// Example 5 . 3 . 1 // P e r f o r m i n g L i n e a r F i l t e r i n g ( i . e ) L i n e a r Convolution // U s i n g DFT clear all ; clc ; close ; h = [1 ,2 ,3]; // I m p u l s e R e s p o n s e o f LTI System x = [1 ,2 ,2 ,1]; // I n p u t R e s p o n s e o f LTI System N1 = length ( x ) N2 = length ( h ) disp ( Length o f Output R e s p o n s e y ( n ) ) N = N1 + N2 -1 // Padding z e r o s t o Make Length o f h and x // 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 h1 = [h , zeros (1 ,8 - N2 ) ] x1 = [x , zeros (1 ,8 - N1 ) ] // Computing DFT H = dft ( h1 , -1) X = dft ( x1 , -1) 24
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
// M u l t i p l i c a t i o n o f 2 DFTs Y = X .* H // L i n e a r C o n v o l u t i o n R e s u l t y = abs ( dft (Y ,1) ) for i =1:8 if ( abs ( H ( i ) ) <0.0001) H ( i ) =0; end if ( abs ( X ( i ) ) <0.0001) X ( i ) =0; end if ( abs ( y ( i ) ) <0.0001) y ( i ) =0; end end disp (X , X= ) disp (H , H= ) disp (y , Output r e s p o n s e u s i n g C o n v o l u t i o n f u n c t i o n ) y = convol (x , h ) Scilab code Exa 5.4.1 Eect of Zero padding
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// Example 5 . 4 . 1 // E f f e c t o f Z e r o Padding clear all ; clc ; close ; L = 100; // Length o f t h e s e q u e n c e N = 200; // N p o i n t DFT n = 0: L -1; x = (0.95) .^ n ; // Padding z e r o s t o f i n d N = 200 p o i n t DFT x_padd = [x , zeros (1 ,N - L ) ]; // Computing DFT X = dft (x , -1) ; X_padd = dft ( x_padd , -1) ; subplot (2 ,1 ,1) plot2d ( X ) 25
17 18 19 20 21 22 23 24
xlabel ( K ) ylabel ( X( k ) ) title ( For L =100 and N =100 ) subplot (2 ,1 ,2) plot2d ( X_padd ) xlabel ( K ) ylabel ( X( k ) z e r o padded ) title ( For L =100 and N =200 )
26
Scilab code Exa 6.4.1 Calculation of No.of bits required for given Signal to Quantization Noise Ratio in DFT
1 2 3 4 5 6 7 8 9 10 11 12 13
// Example 6 . 4 . 1 // Program t o C a l c u l a t e No . o f b i t s r e q u i r e d f o r g i v e n // S i g n a l t o Q u a n t i z a t i o n N o i s e R a t i o // i n c o m p u t i n g DFT clear all ; clc ; close ; N = 1024; SQNR = 30; //SQNR = 30 dB v = log2 ( N ) ; // number o f s t a g e s b = ( log2 (10^( SQNR /10) ) +2* v ) /2; b = ceil ( b ) disp (b , The number o f b i t s r e q u i r e d r o u n d e d t o : ) Scilab code Exa 6.4.2 Calculation of No.of bits required for given Signal to Quantization Noise Ratio in FFT algorithm 27
1 2 3 4 5 6 7 8 9 10 11 12 13
// Example 6 . 4 . 2 // Program t o C a l c u l a t e No . o f b i t s r e q u i r e d f o r g i v e n // S i g n a l t o Q u a n t i z a t i o n N o i s e R a t i o // i n FFT a l g o r i t h m clear all ; clc ; close ; N = 1024; SQNR = 30; //SQNR = 30 dB v = log2 ( N ) ; // number o f s t a g e s b = ( log2 (10^( SQNR /10) ) + v +1) /2; b = ceil ( b ) disp (b , The number o f b i t s r e q u i r e d r o u n d e d t o : ) Scilab code Prb 6.8 Program to Calculate DFT using DIF-FFT algorithm
1 // E x e r c i s e 6 . 8 2 // Program t o C a l c u l a t e DFT u s i n g DIFFFT a l g o r i t h m 3 // x [ n ]= 1 , 0<=n<=7 4 clear all ; 5 clc ; 6 close ; 7 x = [1 ,1 ,1 ,1 ,1 ,1 ,1 ,1]; 8 X = fft (x , -1) 9 // I n v e r s e FFT 10 x_inv = real ( fft (X ,1) )
Scilab code Prb 6.11 Program to Calculate DFT using DIF-FFT algorithm
1 // E x e r c i s e 6 . 1 1 2 // Program t o C a l c u l a t e DFT u s i n g DIFFFT a l g o r i t h m 3 // x [ n ]= [ 1 / 2 , 1 / 2 , 1 / 2 , 1 / 2 , 0 , 0 , 0 , 0 ] 4 clear all ; 5 clc ; 6 close ; 7 x = [1/2 ,1/2 ,1/2 ,1/2 ,0 ,0 ,0 ,0]; 8 X = fft (x , -1)
28
9 10
29
Scilab code Exa 7.6.3 Program to Calculate Quantization Noise in FIR Filter For M = 32 and No.of bits = 12
1 2 3 4 5 6 7 8 9 10
// Example 7 . 6 . 3 // Program t o C a l c u l a t e Q u a n t i z a t i o n N o i s e i n FIR Filter // For M = 32 and No . o f b i t s = 12 clear all ; clc ; close ; b = input ( E n t e r t h e number o f b i t s ) ; M = input ( E n t e r t h e FIR f i l t e r l e n g t h ) ; disp ( C o e f f i c i e n t Q u a n t i z a t i o n E r r o r i n FIR F i l t e r ) Sigma_e_square = (2^( -2*( b +1) ) ) * M /12 Scilab code Eqn 7.7.1 Program to nd Dead band of First order Recursive System y (n) = ay (n 1) + x(n); a = (1/2)anda = (3/4)
1 2
R e c u r s i v e system ); b = 4; //No . o f b i t s u s e d t o r e p r e s e n t Dead_Band = (2^ - b ) *[(1/2) *(1/(1 - a ) ) , -(1/2) *(1/(1 - a ) ) ] // R e s u l t // For a = ( 1 / 2 ) // Dead Band = [ 0 . 0 6 2 5 0 . 0 6 2 5 ] // For a = ( 3 / 4 ) // Dead Band = [ 0 . 1 2 5 0 . 1 2 5 ] Scilab code Exa 7.7.1 Determination of Variance of round-o noise at the output of cascade realization
1 // Example 7 . 7 . 1 2 // D e t e r m i n a t i o n o f V a r i a n c e o f round o f f n o i s e 3 // a t t h e o u t p u t o f c a s c a d e r e a l i z a t i o n 4 //H1 ( Z ) = 1 / ( 1 ( 1 / 2 ) z 1) 5 //H2 ( Z ) = 1 / ( 1 ( 1 / 4 ) z 1) 6 //H( Z ) = ( 2 / ( 1 ( 1 / 2 ) z 1) ) (1/(1 (1/4) z 1) ) 7 clear all ; 8 clc ; 9 close ; 10 a1 = (1/2) ; // p o l e o f f i r s t s y s t e m i n c a s c a d e 11 12 13 14 15 16
connection a2 = (1/4) ; // p l o e o f s e c o n d s y s t e m i n c a s c a d e connection sigma_e = 1; // q u a n t i z a t i o n n o i s e v a r i a n c e // N o i s e v a r i a n c e o f H1 ( Z ) sigma_2 = (1/(1 - a2 ^2) ) * sigma_e ^2 // n o i s e v a r i a n c e o f second system // N o i s e v a r i a n c e o f H2 ( Z ) sigma_1 = 1/(1 - a1 ^2) * sigma_e ^2 // n o i s e v a r i a n c e o f f i r s t system 31
17 18 19 20 21 22 23 24
// N o s i e v a r i a n c e o f H( Z ) sigma = (((2^2) /(1 - a1 ^2) ) -((2^2) /(1 - a1 * a2 ) ) +(1/(1 - a2 ^2) ) ) * sigma_e ^2 noise_variance = sigma + sigma_2 // T o t a l n o i s e variance // R e s u l t 1.0666667 // s i g m a 2 = // s i g m a 1 = 1.3333333 // s i g m a = 1.8285714 2.8952381 // n o i s e v a r i a n c e = Scilab code Eqn 7.7.40 Signal to Quantization Noise Ratio
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
// E q u a t i o n 6 . 4 . 1 7 // p a g e 4 9 2 // Program t o C a l c u l a t e Signal to Quantization Noise Ratio // i n FFT a l g o r i t h m clear all ; clc ; close ; N = input ( E n t e r t h e N p o i n t FFT v a l u e ) ; b = log2 ( N ) Quantization_Noise = (2/3) *(2^( -2* b ) ) Signal_Power = (1/(3* N ) ) SQNR = Signal_Power / Quantization_Noise //RESULT // E n t e r t h e N p o i n t FFT v a l u e 1 0 2 4 // b = 10. 0.0000006 // Q u a n t i z a t i o n N o i s e = // S i g n a l P o w e r = 0.0003255 // SQNR = 512. // >10 l o g 1 0 (SQNR) = 2 7 . 0 9 2 7
32
Scilab code Exa 8.2.1 Design of FIR Filter using Frequecny Sampling Technique
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
// Example 8 . 2 . 1 // D e s i g n o f FIR F i l t e r u s i n g F r e q u e c n y S a m p l i n g Technique //Low P a s s F i l t e r D e s i g n clear all ; clc ; close ; M =15; Hr = [1 ,1 ,1 ,1 ,0.4 ,0 ,0 ,0]; for k =1: length ( Hr ) G ( k ) =(( -1) ^( k -1) ) * Hr ( k ) ; end h = zeros (1 , M ) ; U = (M -1) /2 for n = 1: M h1 = 0; for k = 2: U +1 h1 = G ( k ) * cos ((2* %pi / M ) *( k -1) *(( n -1) +(1/2) ) ) + h1 ; end
33
19 20 21 22 23 24 25 26 27 28 29
30 31 32 33 34 35 36
37
h ( n ) = (1/ M ) * ( G (1) +2* h1 ) ; end h [ hzm , fr ]= frmag (h ,256) ; hzm_dB = 20* log10 ( hzm ) ./ max ( hzm ) ; figure plot (2* fr , hzm ) a = gca () ; 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 ) ; ylabel ( Magnitude ) ; 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 ) xgrid (2) figure plot (2* fr , hzm_dB ) a = gca () ; 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 ) ; ylabel ( Magnitude i n dB ) ; 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 ) xgrid (2) Scilab code Exa 8.2.2 Design of FIR Filter using Frequecny Sampling Technique
1 2 3 4 5 6 7 8 9 10
// Example 8 . 2 . 2 // D e s i g n o f FIR F i l t e r u s i n g F r e q u e c n y S a m p l i n g Technique //Low P a s s F i l t e r D e s i g n clear all ; clc ; close ; M =32; T1 = 0.3789795; // f o r a l p h a = 0 ( Type I ) Hr = [1 ,1 ,1 ,1 ,1 ,1 , T1 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0]; for k =1: length ( Hr ) 34
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
G ( k ) =(( -1) ^( k -1) ) * Hr ( k ) ; end h = zeros (1 , M ) ; U = (M -1) /2 for n = 1: M h1 = 0; for k = 2: U +1 h1 = G ( k ) * cos ((2* %pi / M ) *( k -1) *(( n -1) +(1/2) ) ) + h1 ; end h ( n ) = (1/ M ) * ( G (1) +2* h1 ) ; end h [ hzm , fr ]= frmag (h ,256) ; hzm_dB = 20* log10 ( hzm ) ./ max ( hzm ) ; figure plot (2* fr , hzm ) a = gca () ; 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 ) ; ylabel ( Magnitude ) ; 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 ) xgrid (2) figure plot (2* fr , hzm_dB ) a = gca () ; 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 ) ; ylabel ( Magnitude i n dB ) ; 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 ) xgrid (2) Scilab code Exa 8.2.3 Low Pass FIlter
31 32 33 34 35 36 37
38
1 2
// Example 8 . 2 . 3 //Low P a s s F I l t e r o f l e n g t h M = 61
35
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
// P a s s band Edge f r e q u e n c y f p = 0 . 1 and a S t o p e d g e frequency fs = 0.15 // 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 // i n [ 0 , 0 . 1 ) and [ 0 . 1 5 , 0 . 5 ) // 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] // W e i g h t i n g f u n c t i o n =[1 1 ] clear all ; clc ; close ; hn = eqfir (61 ,[0 .1;.15 .5] ,[1 0] ,[1 1]) ; [ hm , fr ]= frmag ( hn ,256) ; disp ( The F i l t e r C o e f f i c i e n t s a r e : ) hn figure plot ( fr , hm ) 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 f r ) ; ylabel ( Magnitude ) ; title ( F r e q u e n c y R e s p o n s e o f FIR LPF u s i n g REMEZ a l g o r i t h m M=61 ) figure plot (.5*(0:255) /256 ,20* log10 ( frmag ( hn ,256) ) ) ; 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 f r ) ; ylabel ( Magnitude i n dB ) ; title ( F r e q u e n c y R e s p o n s e o f FIR LPF u s i n g REMEZ a l g o r i t h m M=61 ) Scilab code Exa 8.2.4 Band Pass FIlter
// Example 8 . 2 . 4 // Band P a s s F I l t e r o f l e n g t h M = 32 // Lower C u t o f f f r e q u e n c y f p = 0 . 2 and Upper C u t o f f frequency fs = 0.35 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 . 1 ) and [ 0 . 2 , 0 . 3 5 ] and [ 0 . 4 2 5 , 0 . 5 ]
1 2 3
36
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
// 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 ) [0 1 0] // W e i g h t i n g f u n c t i o n =[10 1 1 0 ] clear all ; clc ; close ; hn = 0; hm = 0; hn = eqfir (32 ,[0 .1;.2 .35;.425 .5] ,[0 1 0] ,[10 1 10]) ; [ hm , fr ]= frmag ( hn ,256) ; disp ( The F i l t e r C o e f f i c i e n t s a r e : ) hn figure plot ( fr , hm ) a = gca () ; 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 f r ) ; ylabel ( Magnitude ) ; title ( F r e q u e n c y R e s p o n s e o f FIR BPF u s i n g REMEZ a l g o r i t h m M=32 ) xgrid (2) figure plot (.5*(0:255) /256 ,20* log10 ( frmag ( hn ,256) ) ) ; a = gca () ; 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 f r ) ; ylabel ( Magnitude i n dB ) ; title ( F r e q u e n c y R e s p o n s e o f FIR BPF u s i n g REMEZ a l g o r i t h m M=32 ) xgrid (2) Scilab code Exa 8.2.5 Linear Phase FIR Dierentiator of length M = 60
l e n g t h M = 60
37
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
M =60; tuo = ( M /2) -1; Wc = 0.1; h = zeros (1 , M ) ; for n = 1: M if n ~= M /2 h ( n ) = cos (( n -1 - tuo ) * Wc ) /( n -1 - tuo ) ; end end [ hm , fr ]= frmag (h ,1024) ; disp ( The F i l t e r C o e f f i c i e n t s a r e : ) h figure plot ( fr , hm / max ( hm ) ) a = gca () ; 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 f r ) ; ylabel ( Magnitude ) ; title ( F r e q u e n c y R e s p o n s e o f FIR D i f f e r e n t i a t o r f o r M=60 ) 25 xgrid (2) Scilab code Exa 8.2.6 Hilbert Transform of Length M = 31
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// Example 8 . 2 . 6 // P l o t t i n g H i b e r t T r a n s f o r m e r o f Length M = 31 // D e f a u l t Window R e c t a n g u l a r Window // Chebyshev a p p r o x d e f a u l t p a r a m e t e r = [ 0 0 ] clear all ; clc ; close ; M =31; // H i b e r t T r a n s f o r m e r Length = 31 tuo = (M -1) /2; Wc = %pi ; h = zeros (1 , M ) ; for n = 1: M if n ~= (( M -1) /2) +1 h ( n ) =(2/ %pi ) *( sin (( n -1 - tuo ) * Wc /2) ^2) /( n -1 - tuo ) ; 38
generation h_Rec = h .* Rec_Window ; // Windowing With R e c t a n g u l a r window // Hamming Window g e n e a r t i o n for n =1: M hamm_Window ( n ) = 0.54 -0.46* cos (2* %pi *( n -1) /( M -1) ) ; end h_hamm = h .* hamm_Window ; // Windowing With hamming window ; // H i l b e r t T r a n s f o r m e r u s i n g R e c t a n g u l a r window [ hm_Rec , fr ]= frmag ( h_Rec ,1024) ; hm_Rec_dB = 20* log10 ( hm_Rec ) ; figure plot ( fr , hm_Rec_dB ) a = gca () ; 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 f r ) ; ylabel ( Magnitude ) ; title ( F r e q u e n c y R e s p o n s e o f FIR H i b e r t T r a n s f o r m e r u s i n g R e c t a n g u l a r window f o r M=31 ) xgrid (2) // H i l b e r t T r a n s f o r m e r u s i n g Hamming window [ hm_hamm , fr ]= frmag ( h_hamm ,1024) ; disp ( The H i l b e r t C o e f f i c i e n t s a r e : ) hm_hamm_dB = 20* log10 ( hm_hamm ) ; figure plot ( fr , hm_hamm_dB ) a = gca () ; 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 f r ) ; ylabel ( Magnitude ) ; title ( F r e q u e n c y R e s p o n s e o f FIR H i b e r t T r a n s f o r m e r u s i n g hamming window f o r M=31 ) xgrid (2)
39
Scilab code Eqn 8.2.28 DESIGN AND OBTAIN THE FREQUENCY RESPONSE OF FIR FILTER LowPass
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
// F i g u r e 8 . 9 and 8 . 1 0 //PROGRAM TO DESIGN AND OBTAIN THE FREQUENCY RESPONSE OF FIR FILTER //LOW PASS FILTER clear all ; clc ; close ; M = 61 // F i l t e r l e n g t h = 61 Wc = %pi /5; // D i g i t a l C u t o f f f r e q u e n c y Tuo = (M -1) /2 // C e n t e r V a l u e for n = 1: M if ( n == Tuo +1) hd ( n ) = Wc / %pi ; else hd ( n ) = sin ( Wc *(( n -1) - Tuo ) ) /((( n -1) - Tuo ) * %pi ) ; end end // R e c t a n g u l a r Window for n = 1: M W ( n ) = 1; end // Windowing F i t l e r C o e f f i c i e n t s h = hd .* W ; disp ( F i l t e r C o e f f i c i e n t s a r e ) h; [ hzm , fr ]= frmag (h ,256) ; hzm_dB = 20* log10 ( hzm ) ./ max ( hzm ) ; subplot (2 ,1 ,1) plot ( fr , hzm ) 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 ) ; ylabel ( Magnitude ) ; 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 M=61 ) subplot (2 ,1 ,2) 40
33 34 35 36
plot ( fr , hzm_dB ) 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 ) ; ylabel ( Magnitude i n dB ) ; 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 M=61 ) *For further extension of the exapmle refer to AE 8.2.28A AE 8.2.28B AE 8.2.28C
// Example 8 . 3 . 2 // mapping = ( z ( z 1) ) /T //To c o n v e r t a n a l o g f i l t e r i n t o d i g i t a l clear all ; clc ; close ; s = poly (0 , s ) ; H = 1/(( s +0.1) ^2+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 z = poly (0 , z ) ; Hz = horner (H ,(1/ T ) *( z -( z ^ -1) ) ) Scilab code Exa 8.3.4 Bilinear Transformation
filter
1 // Example 8 . 3 . 4 2 // B i l i n e a r T r a n s f o r m a t i o n 3 //To c o n v e r t a n a l o g f i l t e r i n t o 4 clear all ; 5 clc ; 6 close ; 7 s = poly (0 , s ) ; 8 H = ( s +0.1) /(( s +0.1) ^2+16) ; 9 Omega_Analog = 4; 10 Omega_Digital = %pi /2; 11 // F i n d i n g S a m p l i n g P e r i o d
digital
filter
41
12 T = (2/ Omega_Analog ) *( tan ( Omega_Digital /2) ) 13 z = poly (0 , z ) ; 14 Hz = horner (H ,(2/ T ) *(( z -1) /( z +1) ) )
23 24 25 26 27 28 29 30
// Example 8 . 3 . 5 S i g l e p o l e a n a l o g f i l t e r // B i l i n e a r T r a n s f o r m a t i o n //To c o n v e r t a n a l o g f i l t e r i n t o d i g i t a l f i l t e r clear all ; clc ; close ; s = poly (0 , s ) ; Omegac = 0.2* %pi ; H = Omegac /( s + Omegac ) ; T =1; // S a m p l i n g p e r i o d T = 1 S e c o n d z = poly (0 , z ) ; Hz = horner (H ,(2/ T ) *(( z -1) /( z +1) ) ) disp ( Hz , Hz = ) HW = frmag ( Hz (2) , Hz (3) ,512) ; W = 0: %pi /511: %pi ; plot ( W / %pi , HW ) a = gca () ; a . thickness = 3; a . foreground = 1; a . font_style = 9; xgrid (1) 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 C u t o f f f r e q u e n c y = 0 . 2 p i , D i g i t a l Frequency > , Magnitude ) ; // R e s u l t // Hz = // // 0.6283185 + 0.6283185 z // // 1 . 3 7 1 6 8 1 5 + 2 . 6 2 8 3 1 8 5 z // //>Hz ( 3 )=Hz ( 3 ) / 2 . 6 2 8 3 1 8 5 42
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
// Hz = // // 0.6283185 + 0.6283185 z // // 0.5218856 + z // //>Hz ( 2 )=Hz ( 2 ) / 2 . 6 2 8 3 1 8 5 // Hz = // // 0.2390572 + 0.2390572 z // // 0.5218856 + z // // which i s e q u i v a l e n t t o // Hz = // // 0 . 2 3 9 0 5 7 2 ( 1 + z 1) // // 1 0 . 5 2 1 8 8 5 6 z 1 *For further extension of the exapmle refer to AE 8.3.5
43
12 delta2 = 10^( delta2_in_dB /20) 13 // C a l c u l a t i o n o f F i l t e r Order 14 N = log10 ((1/( delta2 ^2) ) -1) /(2* log10 ( omegas / omegap ) ) 15 N = ceil ( N ) 16 omegac = omegap ; 17 // P o l e s and Gain C a l c u l a t i o n 18 [ pols , gain ]= zpbutt (N , omegac ) ; 19 disp (N , F i l t e r o r d e r N = ) 20 disp ( pols , P o l e p o s i t i o n s a r e p o l s = ) 21 // Magnitude R e s p o n s e o f Analog I I R B u t t e r w o r t h 22 23 24 25 26 27 28 29 30 31
32 33 34 35 36 37 38 39 40
Filter h = buttmag (N , omegac ,1:1000) ; // Magnitude i n dB mag =20* log10 ( h ) ; plot2d ((1:1000) ,mag ,[0 , -180 ,1000 ,20]) ; a = gca () ; a . thickness = 3; a . foreground = 1; a . font_style = 9; xgrid (5) xtitle ( Magnitude R e s p o n s e o f B u t t e r w o r t h LPF F i l t e r C u t o f f f r e q u e n c y = 500 Hz , Analog f r e q u e n c y i n Hz> , Magnitude i n dB > ) ; // R e s u l t // F i l t e r o r d e r N = 7. // s = // column 1 t o 3 // 6 9 9 . 0 7 0 1 3 + 3 0 6 2 . 8 2 6 4 i 1958.751+2456.196 i 2830.4772+1363.086 i // column 4 t o 6 // 3 1 4 1 . 5 9 2 7 + 3 . 8 4 7D13 i 2830.4772 1363.086 i 1958.751 2456.196 i // column 7 // 6 9 9 . 0 7 0 1 3 3 0 6 2 . 8 2 6 4 i *For further extension of the exapmle refer to AE 8.3.6 44
in radians / sec omegas = 2000* %pi ; // Analog S t o p band e d g e f r e q u e n c y in radians / sec delta1_in_dB = -1; delta2_in_dB = -40; delta1 = 10^( delta1_in_dB /20) ; delta2 = 10^( delta2_in_dB /20) ; delta = sqrt (((1/ delta2 ) ^2) -1) epsilon = sqrt (((1/ delta1 ) ^2) -1) // C a l c u l a t i o n o f F i l t e r o r d e r num = (( sqrt (1 - delta2 ^2) ) +( sqrt (1 -(( delta2 ^2) *(1+ epsilon ^2) ) ) ) ) /( epsilon * delta2 ) den = ( omegas / omegap ) + sqrt (( omegas / omegap ) ^2 -1) N = log10 ( num ) / log10 ( den ) //N = ( a c o s h ( d e l t a / e p s i l o n ) ) / ( a c o s h ( omegas / omegap ) ) N = floor ( N ) // C u t o f f f r e q u e n c y omegac = omegap // C a l c u l a t i o n o f p o l e s and z e r o s [ pols , Gn ] = zpch1 (N , epsilon , omegap ) disp (N , F i l t e r o r d e r N = ) ; disp ( pols , P o l e s o f a t y p e I l o w p a s s Chebyshev f i l t e r a r e Sk = ) // Analog F i l t e r T r a n s f e r F u n c t i o n h = poly ( Gn , s , c o e f f ) / real ( poly ( pols , s ) ) // Magnitude R e s p o n s e o f Chebyshev f i l t e r [ h2 ]= cheb1mag (N , omegac , epsilon ,1:1000) 45
31 32 33 34 35 36 37 38 39
// Magnitude i n dB mag =20* log10 ( h2 ) ; plot2d ((1:1000) ,mag ,[0 , -180 ,1000 ,20]) ; a = gca () ; a . thickness = 3; a . foreground = 1; a . font_style = 9; xgrid (5) xtitle ( Magnitude R e s p o n s e o f Chebyshev Type 1 LPF F i l t e r C u t o f f f r e q u e n c y = 500 Hz , Analog f r e q u e n c y i n Hz> , Magnitude i n dB > ) ; Scilab code Exa 8.4.1 Design an Digital IIR Butterworth Filter from Analog IIR Butterworth Filter
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// C a p t i o n : C o n v e t i n g s i n g l e p o l e LPF B u t t e r w o r t h f i l t e r i n t o BPF // Exa8 . 4 . 1 // p a g e 6 9 8 clc ; Op = sym ( Op ) ; // p a s s band e d g e f r e q u e n c y o f low pass f i l t e r s = sym ( s ) ; Ol = sym ( Ol ) ; // l o w e r c u t o f f f r e q u e n c y o f band pass f i l t e r Ou = sym ( Ou ) ; // u p p e r c u t o f f f r e q u e n c y o f band pass f i l t e r s1 = Op *( s ^2+ Ol * Ou ) /( s *( Ou - Ol ) ) ; // Analog t r a n s f o r m a t i o n f o r LPF t o BPF H_Lpf = Op /( s + Op ) ; // s i n g l e p o l e a n a l o g LPF Butterworth f i l t e r H_Bpf = limit ( H_Lpf ,s , s1 ) ; // a n a l o g BPF B u t t e r w o r t h filter disp ( H_Lpf , H Lpf = ) disp ( H_Bpf , H Bpf = ) // R e s u l t // H Lpf = Op/ ( s+Op) // H Bpf = ( OuOl ) s / ( s 2+(OuOl ) s+Ol Ou) 46
// Example 8 . 4 . 2 //To D e s i g n an D i g i t a l I I R B u t t e r w o r t h F i l t e r from Analog I I R B u t t e r w o r t h F i l t e r // and t o p l o t i t s m a g n i t u d e r e s p o n s e //TRANSFORMATION OF LPF TO BPF USING DIGITAL TRANSFORMATION clear all ; clc ; close ; omegaP = 0.2* %pi ; omegaL = (2/5) * %pi ; omegaU = (3/5) * %pi ; z = poly (0 , z ) ; H_LPF = (0.245) *(1+( z ^ -1) ) /(1 -0.509*( z ^ -1) ) alpha = ( cos (( omegaU + omegaL ) /2) / cos (( omegaU - omegaL ) /2) ) ; k = ( cos (( omegaU - omegaL ) /2) / sin (( omegaU - omegaL ) /2) ) * tan ( omegaP /2) ; NUM = -(( z ^2) -((2* alpha * k /( k +1) ) * z ) +(( k -1) /( k +1) ) ) ; DEN = (1 -((2* alpha * k /( k +1) ) * z ) +((( k -1) /( k +1) ) *( z ^2) ) ); HZ_BPF = horner ( H_LPF , NUM / DEN ) disp ( HZ_BPF , D i g i t a l BPF I I R F i l t e r H( Z )= ) HW = frmag ( HZ_BPF (2) , HZ_BPF (3) ,512) ; W = 0: %pi /511: %pi ; plot ( W / %pi , HW ) a = gca () ; a . thickness = 3; a . foreground = 1; a . font_style = 9; xgrid (1) xtitle ( Magnitude R e s p o n s e o f BPF F i l t e r , D i g i t a l F r e q u e n c y > , Magnitude ) ; // R e s u l t 47
2 3 4 // 0 . 5 0 9 + 1 . 2 9 9D16 z z + 6 . 4 3 8D17 z + 5 . 5 5 1D 17 z // // which i s e q u i v a l e n t t o // H( z ) = // // 2 // 0.245 0 0.245 z + 0 + 0 // // 2 // 0 . 5 0 9 + 0 z + 0+ 0 // //H( z ) = // // 2 // 0.245 0 . 2 4 5 z // // 2 // 0.509 z // //H( z ) = // // 2 // 0.245 0 . 2 4 5 z // // 2 // 0.509+ z // 48
//
61
] ,[2 ,5 ,7 ,9]) ; 14 legend ([ R e c t a n g u l a r Window ; Hanning ; Hamming ; Balckmann ]) ; 15 title ( Window F u n c t i o n s f o r Length M = 61 ) Scilab code CF 8.6 Program to nd nd frequency response of (1) Hanning window (2)Hamming window for M = 31 and M = 61 // F i g u r e 8 . 6 and F i g u r e 8 . 7 // Program t o f r e q u e n c y r e s p o n s e o f // ( 1 ) Hanning window ( 2 ) Hamming window f o r M = 31 and M = 61 4 clear all ; 5 close ; 6 clc
1 2 3
49
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
M1 = 31; M2 = 61; for n = 1: M1 h_hann_31 ( n ) = 0.5 -0.5* cos (2* %pi *( n -1) /( M1 -1) ) ; h_hamm_31 ( n ) = 0.54 -0.46* cos (2* %pi *( n -1) /( M1 -1) ) ; end for n = 1: M2 h_hann_61 ( n ) = 0.5 -0.5* cos (2* %pi *( n -1) /( M2 -1) ) ; h_hamm_61 ( n ) = 0.54 -0.46* cos (2* %pi *( n -1) /( M2 -1) ) ; end subplot (2 ,1 ,1) [ h_hann_31_M , fr ]= frmag ( h_hann_31 ,512) ; [ h_hann_61_M , fr ]= frmag ( h_hann_61 ,512) ; h_hann_31_M = 20* log10 ( h_hann_31_M ./ max ( h_hann_31_M ) ); h_hann_61_M = = 20* log10 ( h_hann_61_M ./ max ( h_hann_61_M ) ) ; plot2d ( fr , h_hann_31_M ,2) ; plot2d ( fr , h_hann_61_M ,5) ; legend ([ Length M = 31 ; Length M = 61 ]) ; title ( F r e q u e n c y R e s p o n s e 0 f Hanning window ) subplot (2 ,1 ,2) [ h_hamm_31_M , fr ]= frmag ( h_hamm_31 ,512) ; [ h_hamm_61_M , fr ]= frmag ( h_hamm_61 ,512) ; h_hamm_31_M = 20* log10 ( h_hamm_31_M ./ max ( h_hamm_31_M ) ); h_hamm_61_M = = 20* log10 ( h_hamm_61_M ./ max ( h_hamm_61_M ) ) ; plot2d ( fr , h_hamm_31_M ,2) ; plot2d ( fr , h_hamm_61_M ,5) ; legend ([ Length M = 31 ; Length M = 61 ]) ; title ( F r e q u e n c y R e s p o n s e o f Hamming window ) Scilab code CF 8.7 Program to nd frequency response of (1) Hanning window (2)Hamming window for M = 31
1 2
// F i g u r e 8 . 6 and F i g u r e 8 . 7 // Program t o f r e q u e n c y r e s p o n s e o f 50
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
// ( 1 ) Hanning window ( 2 ) Hamming window f o r M = 31 clear all ; close ; clc M = 31; for n = 1: M h_hann_31 ( n ) = 0.5 -0.5* cos (2* %pi *( n -1) /( M -1) ) ; h_hamm_31 ( n ) = 0.54 -0.46* cos (2* %pi *( n -1) /( M -1) ) ; end subplot (2 ,1 ,1) [ h_hann_31_M , fr ]= frmag ( h_hann_31 ,512) ; h_hann_31_M = 20* log10 ( h_hann_31_M ./ max ( h_hann_31_M ) ); plot2d ( fr , h_hann_31_M ) ; 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 ) ; ylabel ( Magnitude i n dB ) ; title ( F r e q u e n c y R e s p o n s e 0 f Hanning window M = 31 ) subplot (2 ,1 ,2) [ h_hamm_31_M , fr ]= frmag ( h_hamm_31 ,512) ; h_hamm_31_M = 20* log10 ( h_hamm_31_M ./ max ( h_hamm_31_M ) ); plot2d ( fr , h_hamm_31_M ) ; 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 ) ; ylabel ( Magnitude i n dB ) ; title ( F r e q u e n c y R e s p o n s e o f Hamming window M =31 )
51
// Example 1 0 . 5 . 1 // D e c i m a t i o n by 2 , F i l t e r Length = 30 // C u t o f f F r e q u e n c y Wc = %pi /2 // 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 // 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 // i n [ 0 , 0 . 2 5 ] and [ 0 . 3 1 , 0 . 5 ] // 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] // W e i g h t i n g f u n c t i o n =[2 1 ] clear all ; clc ; close ; M = 30; // F i l t e r Length D = 2; // D e c i m a t i o n F a c t o r = 2 Wc = %pi /2; // C u t o f f F r e q u e n c y Wp = Wc /(2* %pi ) ; // Passband Edge F r e q u e n c y
52
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
Ws = 0.31; // Stopband Edge F r e q u e n c y hn = eqfir (M ,[0 Wp ; Ws .5] ,[1 0] ,[2 1]) ; [ hm , fr ]= frmag ( hn ,256) ; disp ( The LPF F i l t e r C o e f f i c i e n t s a r e : ) hn // 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 p = zeros (D , M / D ) ; for k = 1: D for n = 1:( length ( hn ) / D ) p (k , n ) = hn ( D *( n -1) + k ) ; end end disp ( 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 : ) p figure plot ( fr , hm ) 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 f r ) ; ylabel ( Magnitude ) ; title ( F r e q u e n c y R e s p o n s e o f FIR LPF u s i n g REMEZ a l g o r i t h m M=61 ) figure plot (.5*(0:255) /256 ,20* log10 ( frmag ( hn ,256) ) ) ; 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 f r ) ; ylabel ( Magnitude i n dB ) ; title ( F r e q u e n c y R e s p o n s e o f DECIMATOR (D=2) u s i n g REMEZ a l g o r i t h m M=30 ) Scilab code Exa 10.5.2 Interpolation by 5, Filter Length = 30
// Example 1 0 . 5 . 2 // I n t e r p o l a t i o n by 5 , F i l t e r Length = 30 // C u t o f f F r e q u e n c y Wc = %pi /5 // P a s s band Edge f r e q u e n c y f p = 0 . 1 and a S t o p band edge frequency f s = 0.16 5 // 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 6 // i n [ 0 , 0 . 1 ) and [ 0 . 1 6 , 0 . 5 )
1 2 3 4
53
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
// 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] // W e i g h t i n g f u n c t i o n =[3 1 ] clear all ; clc ; close ; M = 30; // F i l t e r Length I = 5; // I n t e r p o l a t i o n F a c t o r = 5 Wc = %pi /5; // C u t o f f F r e q u e n c y Wp = Wc /(2* %pi ) ; // Passband Edge F r e q u e n c y Ws = 0.16; // Stopband Edge F r e q u e n c y hn = eqfir (M ,[0 Wp ; Ws .5] ,[1 0] ,[3 1]) ; [ hm , fr ]= frmag ( hn ,256) ; disp ( The LPF F i l t e r C o e f f i c i e n t s a r e : ) hn // 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 p = zeros (I , M / I ) ; for k = 1: I for n = 1:( length ( hn ) / I ) p (k , n ) = hn ( I *( n -1) + k ) ; end end disp ( The P o l y p h a s e I n t e r p o l a t o r f o r I =5 a r e : ) p figure plot ( fr , hm ) 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 f r ) ; ylabel ( Magnitude ) ; title ( F r e q u e n c y R e s p o n s e o f FIR LPF u s i n g REMEZ a l g o r i t h m M=61 ) figure plot (.5*(0:255) /256 ,20* log10 ( frmag ( hn ,256) ) ) ; 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 f r ) ; ylabel ( Magnitude i n dB ) ; title ( F r e q u e n c y R e s p o n s e o f INTERPOLATOR( I =5) u s i n g REMEZ a l g o r i t h m M=30 )
54
// Example 1 0 . 6 . 1 // M u l t i s t a g e I m p l e m e n t a t i o n o f S a m p l i n g Rate Conversion // D e c i m a t i o n f a c t o r D = 50 //D = D1xD2 , D1 = 2 5 , D2 =2 clear all ; clc ; close ; Fs = 8000; // S a m p l i n g F r e q u e n c y = 8 0 0 0 Hz Fpc = 75; // Passband F r e q u e n c y Fsc = 80; // Stopband F r e q u e n c y Delta_F = ( Fsc - Fpc ) / Fs ; // T r a n s i t i o n Band Pass_Band = [0 , Fpc ]; Transition_Band = [ Fpc , Fsc ]; Delta1 = (10^ -2) ; // Passband R i p p l e Delta2 = (10^ -4) ; // Stopband R i p p l e D = Fs /(2* Fsc ) ; // D e c i m a t i o n F a c t o r // D e c i m a t o r I m p l e m e n t e d i n Two S t a g e s D1 = D /2; // D e c i m a t o r 1 D2 = 2; // D e c i m a t o r 2 // D e c i m a t o r S i n g l e S t a g e I m p l e m e n t a t i o n M = (( -10* log10 ( Delta1 * Delta2 ) -13) /(14.6* Delta_F ) ) +1; M = ceil ( M ) // D e c i m a t o r M u l t i s t a g e I m p l e m e n t a t i o n // F i r s t S t a g e I m p l e m e n t a t i o n F1 = Fs / D1 ; //New p a s s b a n d f o r s t a g e 1 Fsc1 = F1 - Fsc ; //New Stopband f o r s t a g e 1 Delta_F1 = ( Fsc1 - Fpc ) / Fs //New T r a n s i t i o n f o r stage1 Delta11 = Delta1 /2; //New Passband R i p p l e Delta21 = Delta2 ; // Stopband R i p p l e same M1 = (( -10* log10 ( Delta11 * Delta21 ) -13) /(14.6* Delta_F1 ) ) +1 M1 = floor ( M1 ) 55
for stage2 Delta12 = Delta1 /2; //New Passband R i p p l e Delta22 = Delta2 ; // Stopband R i p p l e same M2 = (( -10* log10 ( Delta12 * Delta22 ) -13) /(14.6* Delta_F2 ) ) +1 M2 = floor ( M2 ) disp ( The F i l t e r l e n g t h R e q u i r e d i n S i n g l e s t a g e Implementation o f Decimator i s : ) M disp ( The F i l t e r l e n g t h R e q u i r e d i n M u l t i s t a g e Implementation o f Decimator i s : ) M1 + M2 // C a l c u l a t i o n o f R e d u c t i o n F a c t o r R = M /( M1 + M2 ) ; disp ( The R e d u c t i o n i n F i l t e r Length i s : ) R
data SDR = 10^( SDR_dB /10) disp ( The Number o f s u b f i l t e r s r e q u i r e d ) I = Wx * sqrt ( SDR /12) ; I = ceil ( I )
56
Scilab code Exa 10.8.2 Signal to Distortion Ratio using Linear Interpolation
1 2 3 4 5 6 7 8 9 10 11 12
// Example 1 0 . 8 . 2 // S i g n a l t o D i s t o r t i o n R a t i o u s i n g L i n e a r Interpolation // C a l c u l a t i o n o f no . o f s u b f i l t e r s clear all ; clc ; close ; SDR_dB = 50; // S i g n a l t o d i s t o r t i o n r a t i o = 50 dB Wx = 0.8* %pi ; // D i g i t a l maximum f r e q u e n c y o f i n p u t data SDR = 10^( SDR_dB /10) disp ( The Number o f s u b f i l t e r s r e q u i r e d ) I = Wx *(( SDR /80) ^(1/4) ) ; I = ceil ( I ) Scilab code Exa 10.9.1 Multistage Implementation of Sampling Rate Conversion
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// Example 1 0 . 9 . 1 // M u l t i s t a g e I m p l e m e n t a t i o n o f S a m p l i n g Rate Conversion // D e c i m a t i o n f a c t o r D = 100 //D = D1xD2 , D1 = 5 0 , D2 =2 // I n t e r p o l a t i o n f a c t o r I = 100 // I = I 1 x I 2 , I 1 = 2 , I 2 =50 clear all ; clc ; close ; Fs = 8000; // S a m p l i n g F r e q u e n c y = 8 0 0 0 Hz Fpc = 75; // Passband F r e q u e n c y Fsc = 80; // Stopband F r e q u e n c y Delta_F = ( Fsc - Fpc ) / Fs ; // T r a n s i t i o n Band Pass_Band = [0 , Fpc ]; Transition_Band = [ Fpc , Fsc ]; Delta1 = (10^ -2) ; // Passband R i p p l e 57
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
Delta2 = (10^ -4) ; // Stopband R i p p l e D = Fs /(2* Fsc ) ; // D e c i m a t i o n F a c t o r // D e c i m a t o r I m p l e m e n t e d i n Two S t a g e s D1 = D /2; // D e c i m a t o r 1 D2 = 2; // D e c i m a t o r 2 // D e c i m a t o r S i n g l e S t a g e I m p l e m e n t a t i o n M = (( -10* log10 ( Delta1 * Delta2 /2) -13) /(14.6* Delta_F ) ) +1; M = ceil ( M ) // D e c i m a t o r M u l t i s t a g e I m p l e m e n t a t i o n // F i r s t S t a g e I m p l e m e n t a t i o n Delta_F1 = 0.020625 // O b t a i n e d from Example 1 0 . 6 . 1 M1 = (( -10* log10 ( Delta1 * Delta2 /4) -13) /(14.6* Delta_F1 ) ) +1 M1 = floor ( M1 ) // S e c o n d S t a g e I m p l e m e n t a t i o n Delta_F2 = 0.015625 // O b t a i n e d from Example 1 0 . 6 . 1 M2 = (( -10* log10 ( Delta1 * Delta2 /4) -13) /(14.6* Delta_F2 ) ) +1 M2 = floor ( M2 ) disp ( The F i l t e r l e n g t h R e q u i r e d i n S i n g l e s t a g e Implementation o f Decimator i s : ) M disp ( The F i l t e r l e n g t h R e q u i r e d i n M u l t i s t a g e Implementation o f Decimator i s : ) M1 + M2 // C a l c u l a t i o n o f R e d u c t i o n F a c t o r R = M /( M1 + M2 ) ; disp ( The R e d u c t i o n i n F i l t e r Length i s : ) R
58
8 9 10 11 12 13 14 15 16
t h e d e s i r e d i n p u t s e q u e n c e and a c t u a l i n p u t sequence C = Rdx ( M : $ ) // R i g h t s i d e d s e q u e n c e To_M = toeplitz ( C ) Rxx = [0.6 1 0.6] // Auto c o r r e l a t i o n m a t r i x Rss = Rxx ( M : $ ) // F i l t e r c o e f f i c i e n t s h = [0.451 0.165] // C a l c u l a t i o n o f Minimum Mean S q u a r e E r r o r sigma_d = 1; // A v e r a g e power o f d e s i r e d s e q u e n c e MSE = sigma_d - h * Rss
59
Scilab code Exa 12.1.1 Determination of spectrum of a signal With maximum normalized frequency f = 0.1 using Rectangular window and Blackmann window
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// Example 1 2 . 1 . 1 // D e t e r m i n a t i o n o f s p e c t r u m o f a s i g n a l // With maximum n o r m a l i z e d f r e q u e n c y f = 0 . 1 // u s i n g R e c t a n g u l a r window and Blackmann window clear all ; close ; clc ; N = 61; cfreq = [0.1 0]; [ wft , wfm , fr ]= wfir ( l p ,N , cfreq , r e ,0) ; wft ; // Time domain f i l t e r coefficients wfm ; // F r e q u e n c y domain f i l t e r values fr ; // F r e q u e n c y s a m p l e p o i n t s WFM_dB = 20* log10 ( wfm ) ; // F r e q u e n c y r e s p o n s e i n dB for n = 1: N
60
16 17 18 19 20 21 22 23
24 25 26
h_balckmann ( n ) =0.42 -0.5* cos (2* %pi * n /( N -1) ) +0.08* cos (4* %pi * n /( N -1) ) ; end wft_blmn = wft .* h_balckmann ; wfm_blmn = frmag ( wft_blmn , length ( fr ) ) ; WFM_blmn_dB =20* log10 ( wfm_blmn ) ; subplot (2 ,1 ,1) plot2d ( fr , WFM_dB ) xtitle ( F r e q u e n c y R e s p o n s e o f R e c t a n g u l a r window F i l t e r e d o u t p u t M = 61 , F r e q u e n c y i n c y c l e s p e r samples f , Energy d e n s i t y i n dB ) subplot (2 ,1 ,2) plot2d ( fr , WFM_blmn_dB ) xtitle ( F r e q u e n c y R e s p o n s e o f Blackmann window F i l t e r e d o u t p u t M = 61 , F r e q u e n c y i n c y c l e s p e r samples f , Energy d e n s i t y i n dB ) Scilab code Exa 12.1.2 Evaluating power spectrum of a discrete sequence Using N-point DFT
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// Example 1 2 . 1 . 2 // E v a l u a t i n g power s p e c t r u m o f a d i s c r e t e s e q u e n c e // U s i n g N p o i n t DFT clear all ; clc ; close ; N =16; // Number o f s a m p l e s i n g i v e n s e q u e n c e n =0: N -1; delta_f = [0.06 ,0.01]; // f r e q u e n c y s e p a r a t i o n x1 = sin (2* %pi *0.315* n ) + cos (2* %pi *(0.315+ delta_f (1) ) *n); x2 = sin (2* %pi *0.315* n ) + cos (2* %pi *(0.315+ delta_f (2) ) *n); L = [8 ,16 ,32 ,128]; k1 = 0: L (1) -1; k2 = 0: L (2) -1; k3 = 0: L (3) -1; k4 = 0: L (4) -1; 61
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
fk1 = k1 ./ L (1) ; fk2 = k2 ./ L (2) ; fk3 = k3 ./ L (3) ; fk4 = k4 ./ L (4) ; for i =1: length ( fk1 ) Pxx1_fk1 ( i ) = 0; Pxx2_fk1 ( i ) = 0; for m = 1: N Pxx1_fk1 ( i ) = Pxx1_fk1 ( i ) + x1 ( m ) * exp ( - sqrt ( -1) *2* %pi *( m -1) * fk1 ( i ) ) ; Pxx2_fk1 ( i ) = Pxx1_fk1 ( i ) + x1 ( m ) * exp ( - sqrt ( -1) *2* %pi *( m -1) * fk1 ( i ) ) ; end Pxx1_fk1 ( i ) = ( Pxx1_fk1 ( i ) ^2) / N ; Pxx2_fk1 ( i ) = ( Pxx2_fk1 ( i ) ^2) / N ; end for i =1: length ( fk2 ) Pxx1_fk2 ( i ) = 0; Pxx2_fk2 ( i ) = 0; for m = 1: N Pxx1_fk2 ( i ) = Pxx1_fk2 ( i ) + x1 ( m ) * exp ( - sqrt ( -1) *2* %pi *( m -1) * fk2 ( i ) ) ; Pxx2_fk2 ( i ) = Pxx1_fk2 ( i ) + x1 ( m ) * exp ( - sqrt ( -1) *2* %pi *( m -1) * fk2 ( i ) ) ; end Pxx1_fk2 ( i ) = ( Pxx1_fk2 ( i ) ^2) / N ; Pxx2_fk2 ( i ) = ( Pxx1_fk2 ( i ) ^2) / N ; end for i =1: length ( fk3 ) Pxx1_fk3 ( i ) = 0; Pxx2_fk3 ( i ) = 0; for m = 1: N Pxx1_fk3 ( i ) = Pxx1_fk3 ( i ) + x1 ( m ) * exp ( - sqrt ( -1) *2* %pi *( m -1) * fk3 ( i ) ) ; Pxx2_fk3 ( i ) = Pxx1_fk3 ( i ) + x1 ( m ) * exp ( - sqrt ( -1) *2* %pi *( m -1) * fk3 ( i ) ) ; end Pxx1_fk3 ( i ) = ( Pxx1_fk3 ( i ) ^2) / N ; 62
49 Pxx2_fk3 ( i ) = ( Pxx1_fk3 ( i ) ^2) / N ; 50 end 51 for i =1: length ( fk4 ) 52 Pxx1_fk4 ( i ) = 0; 53 Pxx2_fk4 ( i ) = 0; 54 for m = 1: N 55 Pxx1_fk4 ( i ) = Pxx1_fk4 ( i ) + x1 ( m ) * exp ( - sqrt ( -1) *2* 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
%pi *( m -1) * fk4 ( i ) ) ; Pxx2_fk4 ( i ) = Pxx1_fk4 ( i ) + x1 ( m ) * exp ( - sqrt ( -1) *2* %pi *( m -1) * fk4 ( i ) ) ; end Pxx1_fk4 ( i ) = ( Pxx1_fk4 ( i ) ^2) / N ; Pxx2_fk4 ( i ) = ( Pxx1_fk4 ( i ) ^2) / N ; end figure title ( f o r f r e q u e n c y s e p a r a t i o n = 0 . 0 6 ) subplot (2 ,2 ,1) plot2d3 ( gnn ,k1 , abs ( Pxx1_fk1 ) ) subplot (2 ,2 ,2) plot2d3 ( gnn ,k2 , abs ( Pxx1_fk2 ) ) subplot (2 ,2 ,3) plot2d3 ( gnn ,k3 , abs ( Pxx1_fk3 ) ) subplot (2 ,2 ,4) plot2d3 ( gnn ,k4 , abs ( Pxx1_fk4 ) ) figure title ( f o r f r e q u e n c y s e p a r a t i o n = 0 . 0 1 ) subplot (2 ,2 ,1) plot2d3 ( gnn ,k1 , abs ( Pxx2_fk1 ) ) subplot (2 ,2 ,2) plot2d3 ( gnn ,k2 , abs ( Pxx2_fk2 ) ) subplot (2 ,2 ,3) plot2d3 ( gnn ,k3 , abs ( Pxx2_fk3 ) ) subplot (2 ,2 ,4) plot2d3 ( gnn ,k4 , abs ( Pxx2_fk4 ) ) Scilab code Exa 12.5.1 Determination of power, frequency and varaince of Additive noise
63
1 // Example 1 2 . 5 . 1 2 // D e t e r m i n a t i o n o f power , f r e q u e n c y and v a r a i n c e o f 3 // A d d i t i v e n o i s e 4 clear all ; 5 clc ; 6 close ; 7 ryy = [0 ,1 ,3 ,1 ,0]; // A u t o c o r r e l a t i o n o f s i g n a l 8 cen_ter_value = ceil ( length ( ryy ) /2) ; // c e n t e r v a l u e
of autocorrelation
9 // Method1 10 //TO f i n d o u t t h e v a r i a n c e o f t h e a d d i t i v e N o i s e 11 C = ryy ( ceil ( length ( ryy ) /2) : $ ) ; 12 corr_matrix = toeplitz ( C ) ; // c o r r e l a t i o n m a t r i x 13 evals = spec ( corr_matrix ) ; // E i g e n V a l u e s c o m p u t a t i o n 14 sigma_w = min ( evals ) ; // Minimum o f e i g e n v a l u e = 15 16 17 18 19 20 21 22 23 24 25
varinace of noise // Method2 //TO f i n d o u t t h e v a r i a n c e o f t h e a d d i t i v e N o i s e P = [1 , - sqrt (2) ,1]; // P l o y n o m i a l i n d e c r e a s i n g o r d e r Z = roots ( P ) ; // r o o t s o f t h e p o l y n o m i a l P1 = ryy ( cen_ter_value +1) / real ( Z (1) ) ; // power o f t h e sinusoid A = sqrt (2* P1 ) ; // a m p l i t u d e o f t h e s i n u s o i d sigma_w1 = ryy ( cen_ter_value ) - P1 ; // v a r i a n c e o f n o i s e method2 disp ( P1 , Power o f t h e a d d i t i v e n o i s e ) f1 = acos ( real ( Z (1) ) ) /(2* %pi ) disp ( f1 , f r e q u e n c y o f t h e a d d i t i v e n o i s e ) disp ( sigma_w1 , V a r i a n c e o f t h e a d d i t i v e n o i s e )
64
Appendix to Examples
Scilab code AE 4.2.7 Sampling a Nonbandlimted signal
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
// Example 4 . 2 . 7 S a m p l i n g a N o n b a n d l i m i t e d S i g n a l // P l o t t i n g D i s c r e t e Time F o u r i e r T r a n s f o r m o f // D i s c r e t e Time S i g n a l x ( nT )= exp ( AT a b s ( n ) ) clear all ; clc ; close ; // Analog S i g n a l A =1; // A m p l i t u d e Dt = 0.005; t = -2: Dt :2; // C o n t i n u o u s Time S i g n a l xa = exp ( - A * abs ( t ) ) ; // D i s c r e t e Time S i g n a l 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 ( o r ) 20 Hz Ts = 1/ Fs ; n = -5:1:5; nTs = n * Ts ; x = exp ( - A * abs ( nTs ) ) ; // Analog S i g n a l r e c o n s t r u c t i o n Dt = 0.005; t = -2: Dt :2; Xa = x * sinc_new ( Fs *( ones ( length ( nTs ) ,1) *t - nTs * ones (1 , length ( t ) ) ) ) ; // c h e c k error = max ( abs ( Xa - xa ) ) 65
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
subplot (2 ,1 ,1) ; a = gca () ; a . x_location = o r i g i n ; a . y_location = o r i g i n ; plot (t , xa ) ; xlabel ( t i n msec . ) ; ylabel ( xa ( t ) ) title ( O r i g i n a l Analog S i g n a l ) subplot (2 ,1 ,2) ; a = gca () ; a . x_location = o r i g i n ; a . y_location = o r i g i n ; xlabel ( t i n msec . ) ; ylabel ( xa ( t ) ) title ( R e c o n s t r u c t e d S i g n a l from x ( n ) u s i n g s i n c f u n c t i o n ); 40 plot (t , Xa ) ; *Refer to the following for Scilab code of sinc ARC 4A
i s /0
warning i s o f f 4 y = sin ( %pi * x ) ./( %pi * x ) ; 5 y ( i ) = 1; 6 endfunction Scilab code AE 4.4.2 Frequency Response
1 clear all ; 2 close ; 3 clc ;
66
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
W = - %pi :(1/500) : %pi ; z = exp ( sqrt ( -1) * W ) ; H = z ./( z -0.8) ; Mag_H = abs ( H ) ; [ Phase_H , m ] = phasemag ( H ) ; // phasemag u s e d t o c a l c u l a t e p h a s e and m a g n i t u d e i n dB subplot (2 ,1 ,1) plot2d (W , Mag_H ) xlabel ( F r e q u e n c y i n R a d i a n s ) ylabel ( a b s (H) ) title ( Magnitude R e s p o n s e ) subplot (2 ,1 ,2) plot2d (W , Phase_H ) xlabel ( F r e q u e n c y i n R a d i a n s ) ylabel ( <(H) ) title ( Phase R e s p o n s e ) Scilab code AE 8.2.28A DESIGN AND OBTAIN THE FREQUENCY RESPONSE OF FIR FILTER Band Pass
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
//PROGRAM TO DESIGN AND OBTAIN THE FREQUENCY RESPONSE OF FIR FILTER // Band PASS FILTER clear all ; clc ; close ; M = 11 // F i l t e r l e n g t h = 11 Wc = [ %pi /4 ,3* %pi /4]; // D i g i t a l C u t o f f frequency Wc2 = Wc (2) Wc1 = Wc (1) Tuo = (M -1) /2 // C e n t e r V a l u e hd = zeros (1 , M ) ; W = zeros (1 , M ) ; for n = 1:11 if ( n == Tuo +1) hd ( n ) = ( Wc2 - Wc1 ) / %pi ; 67
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
else n hd ( n ) = ( sin ( Wc2 *(( n -1) - Tuo ) ) - sin ( Wc1 *(( n -1) Tuo ) ) ) /((( n -1) - Tuo ) * %pi ) ; end if ( abs ( hd ( n ) ) <(0.00001) ) hd ( n ) =0; end end hd ; // R e c t a n g u l a r Window for n = 1: M W ( n ) = 1; end // Windowing F i t l e r C o e f f i c i e n t s h = hd .* W ; disp ( F i l t e r C o e f f i c i e n t s a r e ) h; [ hzm , fr ]= frmag (h ,256) ; hzm_dB = 20* log10 ( hzm ) ./ max ( hzm ) ; subplot (2 ,1 ,1) plot (2* fr , hzm ) 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 ylabel ( Magnitude ) ; title ( F r e q u e n c y R e s p o n s e 0 f FIR BPF R e c t a n g u l a r window M=11 ) subplot (2 ,1 ,2) plot (2* fr , hzm_dB ) 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 ylabel ( Magnitude i n dB ) ; title ( F r e q u e n c y R e s p o n s e 0 f FIR BPF R e c t a n g u l a r window M=11 )
W ) ; using
W ) ; using
Scilab code AE 8.2.28B DESIGN AND OBTAIN THE FREQUENCY RESPONSE OF FIR FILTER Band Stop
1
2 // Band S t o p FILTER ( o r ) Band R e j e c t F i l t e r 3 clear all ; 4 clc ; 5 close ; 6 M = 11 // F i l t e r l e n g t h = 11 7 Wc = [ %pi /4 ,3* %pi /4]; // D i g i t a l C u t o f f 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
frequency Wc2 = Wc (2) Wc1 = Wc (1) Tuo = (M -1) /2 // C e n t e r V a l u e hd = zeros (1 , M ) ; W = zeros (1 , M ) ; for n = 1:11 if ( n == Tuo +1) hd ( n ) = 1 -(( Wc2 - Wc1 ) / %pi ) ; else hd ( n ) =( sin ( %pi *(( n -1) - Tuo ) ) - sin ( Wc2 *(( n -1) Tuo ) ) + sin ( Wc1 *(( n -1) - Tuo ) ) ) /((( n -1) - Tuo ) * %pi ) ; end if ( abs ( hd ( n ) ) <(0.00001) ) hd ( n ) =0; end end hd // R e c t a n g u l a r Window for n = 1: M W ( n ) = 1; end // Windowing F i t l e r C o e f f i c i e n t s h = hd .* W ; disp ( F i l t e r C o e f f i c i e n t s a r e ) h; [ hzm , fr ]= frmag (h ,256) ; hzm_dB = 20* log10 ( hzm ) ./ max ( hzm ) ; subplot (2 ,1 ,1) plot (2* fr , hzm ) 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 ) ; ylabel ( Magnitude ) ;
69
37 38 39 40 41 42
title ( F r e q u e n c y R e s p o n s e 0 f FIR BPF u s i n g R e c t a n g u l a r window M=11 ) subplot (2 ,1 ,2) plot (2* fr , hzm_dB ) 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 ) ; ylabel ( Magnitude i n dB ) ; title ( F r e q u e n c y R e s p o n s e 0 f FIR BPF u s i n g R e c t a n g u l a r window M=11 ) Scilab code AE 8.2.28C DESIGN AND OBTAIN THE FREQUENCY RESPONSE OF FIR FILTER High
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
// F i g u r e 8 . 9 and 8 . 1 0 //PROGRAM TO DESIGN AND OBTAIN THE FREQUENCY RESPONSE OF FIR FILTER //LOW PASS FILTER clear all ; clc ; close ; M = 61 // F i l t e r l e n g t h = 61 Wc = %pi /5; // D i g i t a l C u t o f f f r e q u e n c y Tuo = (M -1) /2 // C e n t e r V a l u e for n = 1: M if ( n == Tuo +1) hd ( n ) = Wc / %pi ; else hd ( n ) = sin ( Wc *(( n -1) - Tuo ) ) /((( n -1) - Tuo ) * %pi ) ; end end // R e c t a n g u l a r Window for n = 1: M W ( n ) = 1; end // Windowing F i l t e r C o e f f i c i e n t s h = hd .* W ; disp ( F i l t e r C o e f f i c i e n t s a r e ) h; 70
25 [ hzm , fr ]= frmag (h ,256) ; 26 hzm_dB = 20* log10 ( hzm ) ./ max ( hzm ) ; 27 subplot (2 ,1 ,1) 28 plot ( fr , hzm ) 29 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 ) ; 30 ylabel ( Magnitude ) ; 31 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 32 33 34 35 36
R e c t a n g u l a r window M=61 ) subplot (2 ,1 ,2) plot ( fr , hzm_dB ) 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 ) ; ylabel ( Magnitude i n dB ) ; 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 M=61 ) Scilab code AE 8.3.5 High Pass Filter
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
// Example 8 . 3 . 5 // F i r s t Order B u t t e r w o r t h F i l t e r //Low P a s s F i l t e r clear all ; clc ; close ; s = poly (0 , s ) ; Omegac = 0.2* %pi ; H = Omegac /( s + Omegac ) ; T =1; // S a m p l i n g p e r i o d T = 1 S e c o n d z = poly (0 , z ) ; Hz = horner (H ,(2/ T ) *(( z -1) /( z +1) ) ) HW = frmag ( Hz (2) , Hz (3) ,512) ; W = 0: %pi /511: %pi ; plot ( W / %pi , HW ) a = gca () ; a . thickness = 3; a . foreground = 1; a . font_style = 9; xgrid (1)
71
21
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 C u t o f f f r e q u e n c y = 0 . 2 p i , D i g i t a l Frequency > , Magnitude ) ; Scilab code AE 8.3.6 Analog Low Pass
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
// Example 8 . 3 . 6 // To D e s i g n an Analog Low P a s s I I R B u t t e r w o r t h Filter // For t h e g i v e n c u t o f f f r e q u e n c y Wc = 500 Hz clear all ; clc ; close ; omegap = 500; omegas = 1000; delta1_in_dB = -3; delta2_in_dB = -40; delta1 = 10^( delta1_in_dB /20) delta2 = 10^( delta2_in_dB /20) // C a l c u l a t i o n o f F i l t e r Order N = log10 ((1/( delta2 ^2) ) -1) /(2* log10 ( omegas / omegap ) ) N = ceil ( N ) omegac = omegap ; // P o l e s and Gain C a l c u l a t i o n [ pols , gain ]= zpbutt (N , omegac ) ; // Magnitude R e s p o n s e o f Analog I I R B u t t e r w o r t h Filter h = buttmag (N , omegac ,1:1000) ; // Magnitude i n dB mag =20* log10 ( h ) ; plot2d ((1:1000) ,mag ,[0 , -180 ,1000 ,20]) ; a = gca () ; a . thickness = 3; a . foreground = 1; a . font_style = 9; xgrid (5) xtitle ( Magnitude R e s p o n s e o f B u t t e r w o r t h LPF F i l t e r C u t o f f f r e q u e n c y = 500 Hz , Analog f r e q u e n c y i n 72
// Example 8 . 3 . 5 // F i r s t Order B u t t e r w o r t h F i l t e r // High P a s s F i l t e r // T a b l e 8 . 1 3 : 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 clear all ; clc ; close ; s = poly (0 , s ) ; Omegac = 0.2* %pi ; H = Omegac /( s + Omegac ) ; T =1; // S a m p l i n g p e r i o d T = 1 S e c o n d z = poly (0 , z ) ; Hz_LPF = horner (H ,(2/ T ) *(( z -1) /( z +1) ) ) ; alpha = -( cos (( Omegac + Omegac ) /2) ) /( cos (( Omegac Omegac ) /2) ) ; HZ_HPF = horner ( H_LPF , -( z + alpha ) /(1+ alpha * z ) ) HW = frmag ( HZ_HPF (2) , HZ_HPF (3) ,512) ; W = 0: %pi /511: %pi ; plot ( W / %pi , HW ) a = gca () ; a . thickness = 3; a . foreground = 1; a . font_style = 9; xgrid (1) 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 C u t o f f f r e q u e n c y = 0 . 2 p i , D i g i t a l Frequency > , Magnitude ) ; Scilab code AE 8.4.2A Analog Filter Transformation
73
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
//TRANSFORMATION OF LPF TO BSF USING DIGITAL TRANSFORMATION clear all ; clc ; close ; omegaP = 0.2* %pi ; omegaL = (2/5) * %pi ; omegaU = (3/5) * %pi ; z = poly (0 , z ) ; H_LPF = (0.245) *(1+( z ^ -1) ) /(1 -0.509*( z ^ -1) ) alpha = ( cos (( omegaU + omegaL ) /2) / cos (( omegaU - omegaL ) /2) ) ; k = tan (( omegaU - omegaL ) /2) * tan ( omegaP /2) ; NUM =(( z ^2) -((2* alpha /(1+ k ) ) * z ) +((1 - k ) /(1+ k ) ) ) ; DEN = (1 -((2* alpha /(1+ k ) ) * z ) +(((1 - k ) /(1+ k ) ) *( z ^2) ) ) ; HZ_BPF = horner ( H_LPF , NUM / DEN ) HW = frmag ( HZ_BPF (2) , HZ_BPF (3) ,512) ; W = 0: %pi /511: %pi ; plot ( W / %pi , HW ) a = gca () ; a . thickness = 3; a . foreground = 1; a . font_style = 9; xgrid (1) xtitle ( Magnitude R e s p o n s e o f BSF F i l t e r , D i g i t a l F r e q u e n c y > , Magnitude ) ; Scilab code AE 8.4.2B Digital Filter Transformation
1 2 3 4 5 6
74
7 Ol = sym ( Ol ) ;
// l o w e r c u t o f f f r e q u e n c y o f band f r e q u e n c y o f band
pass f i l t e r
8 Ou = sym ( Ou ) ; // u p p e r c u t o f f
pass f i l t e r
9 s1 = Op *( s ^2+ Ol * Ou ) /( s *( Ou - Ol ) ) ; // Analog 10 11 12 13 14 15 16
t r a n s f o r m a t i o n f o r LPF t o BPF H_Lpf = Op /( s + Op ) ; // s i n g l e p o l e a n a l o g LPF Butterworth f i l t e r H_Bpf = limit ( H_Lpf ,s , s1 ) ; // a n a l o g BPF B u t t e r w o r t h filter disp ( H_Lpf , H Lpf = ) disp ( H_Bpf , H Bpf = ) // R e s u l t // H Lpf = Op/ ( s+Op) // H Bpf = ( OuOl ) s / ( s 2+(OuOl ) s+Ol Ou)
75