You are on page 1of 90

Scilab Textbook Companion for

Digital Signal Processing: A Computer Based


Approach
by S. K. Mitra1
Created by
Sanjeev Irny
B.Tech
Instrumentation Engineering
Shri Gurugobind Singhji Institute of Engg. & Tech.
College Teacher
Dr. B. M. Patre
Cross-Checked by

November 3, 2014

1 Funded

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


http://spoken-tutorial.org/NMEICT-Intro. This Textbook Companion and Scilab
codes written in it can be downloaded from the Textbook Companion Project
section at the website http://scilab.in

Book Description
Title: Digital Signal Processing: A Computer Based Approach
Author: S. K. Mitra
Publisher: Tata McGraw - Hill Education
Edition: 3
Year: 2008
ISBN: 978-0-07-066756-3

Scilab numbering policy used in this document and the relation to the
above book.
Exa Example (Solved example)
Eqn Equation (Particular equation of the above book)
AP Appendix to Example(Scilab Code that is an Appednix to a particular
Example of the above book)
For example, Exa 3.51 means solved example 3.51 of this book. Sec 2.3 means
a scilab code whose theory is explained in Section 2.3 of the book.

Contents
List of Scilab Codes

2 Discreet Time Signals and Systems

3 Discreet TIme Fourier Transform

22

4 Digital Processing of Continous TIme Systems

29

5 Finite Length Discreet Transform

33

6 z Transform

45

7 LTI Discreet Time systems in the Transform Domain

61

8 Digital Filter Structures

64

9 IIR digital filter design

69

10 FIR digital filter design

74

11 DSP Algorithm implementation

80

12 Analysis of Finite Wordlength Effects

83

13 Multirate DIgital Signal Processing Findamentals

84

14 Applications of Digital Signal Processing

87

List of Scilab Codes


Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa

2.1
2.2
2.3
2.5
2.6
2.7
2.9
2.16
2.20
2.22
2.26
2.27
2.28
2.29
2.30
2.31
2.32
2.33
2.46
3.5
3.6
3.7
3.10
3.12
3.13
3.14
3.15
4.5

Ensemble Averaging . . . . . . . . . . . . .
Basic operations . . . . . . . . . . . . . . .
Unequal length sequence . . . . . . . . . . .
Generating symmetric parts . . . . . . . . .
Energy Signal . . . . . . . . . . . . . . . . .
Power Signal . . . . . . . . . . . . . . . . .
Square wave generation . . . . . . . . . . .
Linearity of accumulator . . . . . . . . . . .
Passive system . . . . . . . . . . . . . . . .
Impulse response of Accumulator . . . . . .
Convolution . . . . . . . . . . . . . . . . . .
Convolution . . . . . . . . . . . . . . . . . .
Convolution . . . . . . . . . . . . . . . . . .
Convolution . . . . . . . . . . . . . . . . . .
Convolution . . . . . . . . . . . . . . . . . .
Stabbility of causal system . . . . . . . . .
Stability of Anti causal system . . . . . . .
Stability of a system . . . . . . . . . . . . .
Cross coreation computation . . . . . . . .
DTFT computation . . . . . . . . . . . . .
DTFT computation . . . . . . . . . . . . .
Plotting real and imaginary part . . . . . .
DTFT of finite length exponential sequence
Plotting DTFT of exponential sequence . .
DTFT computation . . . . . . . . . . . . .
Energy of signal . . . . . . . . . . . . . . .
Energy of exponential sequence . . . . . . .
Passband and Stopband ripple computation
4

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

8
9
9
10
11
12
12
13
13
14
15
15
16
17
18
19
19
20
21
22
23
24
25
25
26
27
28
29

Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa

4.6
4.7
4.8
4.16
5.1
5.2
5.3
5.4
5.5
5.7
5.8
5.10
5.11
5.12
5.14
5.15
6.1
6.2
6.3
6.4
6.5
6.9
6.10
6.11
6.12
6.13
6.14
6.15
6.16
6.17
6.18
6.19
6.20
6.22
6.23
6.24
6.25

Order of Analog filter . . . . . . . . . . . . . . . . . .


Order of Analog Chebyshev Filter . . . . . . . . . . .
Order of Analog Lowpass Elliptic Filter . . . . . . . .
Design of Analof Butterworth HP Filter . . . . . . . .
DFT computation . . . . . . . . . . . . . . . . . . . .
DFT of sinusoidal sequence . . . . . . . . . . . . . . .
DFT computation . . . . . . . . . . . . . . . . . . . .
IDFT Computation . . . . . . . . . . . . . . . . . . .
DFT computation . . . . . . . . . . . . . . . . . . . .
Cicular convolution computation . . . . . . . . . . . .
Cicular convolution computation . . . . . . . . . . . .
Generating symmetric parts . . . . . . . . . . . . . . .
Cicular convolution computation . . . . . . . . . . . .
Linear Convolution using DFT . . . . . . . . . . . . .
DFT computationusing single DFT . . . . . . . . . . .
DFT computationusing single DFT of shorter length .
z Transform of causal exponential sequence . . . . . .
z transform of anticausal sequence . . . . . . . . . . .
z Transform . . . . . . . . . . . . . . . . . . . . . . . .
z Transform . . . . . . . . . . . . . . . . . . . . . . . .
Z transform of causal sequence . . . . . . . . . . . . .
z Transform . . . . . . . . . . . . . . . . . . . . . . . .
Rational form of z Transform from its zero and pole
locations . . . . . . . . . . . . . . . . . . . . . . . . .
Inverse z Transform . . . . . . . . . . . . . . . . . . .
Inverse z Transform . . . . . . . . . . . . . . . . . . .
Proper fraction of Rational z Transform . . . . . . . .
Inverse z Transform by partial fraction expansion . . .
residue computation using coefficient matching approach
Inverse z Transform by power series expansion . . . . .
Coefficients of rational form . . . . . . . . . . . . . . .
Inverse z Transform using long division . . . . . . . .
Inverse z Transform using long division . . . . . . . .
Inverse z Transform . . . . . . . . . . . . . . . . . . .
z Transform . . . . . . . . . . . . . . . . . . . . . . . .
z Transform . . . . . . . . . . . . . . . . . . . . . . . .
sum of sequences of non overlapping ROC . . . . . . .
z Transform . . . . . . . . . . . . . . . . . . . . . . . .
5

29
30
31
31
33
33
34
35
37
37
38
39
40
41
42
42
45
45
46
46
46
47
47
48
48
49
50
50
51
52
52
53
53
53
54
54
55

Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa

6.26
6.27
6.28
6.30
6.31
6.33
6.34
7.1
7.2
7.6
8.1
8.6
8.7
8.10

z Transform . . . . . . . . . . . . . . . . . . . . . . . .
Inverse z Transform . . . . . . . . . . . . . . . . . . .
Enlargement of ROC by pole zero cancellation . . . .
Convolution . . . . . . . . . . . . . . . . . . . . . . . .
Convolution . . . . . . . . . . . . . . . . . . . . . . . .
Transfer Function of Moving Average Filter . . . . . .
Transfer function determination . . . . . . . . . . . . .
Bounded real function . . . . . . . . . . . . . . . . . .
Transfer function determination . . . . . . . . . . . . .
FIR Transfer function . . . . . . . . . . . . . . . . . .
Analysis of Cascaded lattice digital filter structure . .
Factorization of FIR Transfer Function . . . . . . . . .
Factorization of IIR Transfer Function . . . . . . . . .
Cascaded lattice realization of IIR digital Transfer Function . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Exa 8.12 Gray Markel method of realization . . . . . . . . . . .
Exa 8.18 Cascaded lattice realization . . . . . . . . . . . . . . .
Exa 9.1
Computating ripple values . . . . . . . . . . . . . . . .
Exa 9.2
conversion of bandedged frequencies to Normalized digital frequencies . . . . . . . . . . . . . . . . . . . . . .
Exa 9.3
Design of HP Digital Filter . . . . . . . . . . . . . . .
Exa 9.6
Changing passband edge frequencies to LP IIR digital
frequencies . . . . . . . . . . . . . . . . . . . . . . . .
Exa 9.7
Design of HP IIR Digital Filter from LP Digital Filter
Exa 9.12 Minimum order of Type 2 Chebyshev HP IIR digital
filter . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Exa 10.1 Kaiser formula . . . . . . . . . . . . . . . . . . . . . .
Exa 10.2 Bellenger formula . . . . . . . . . . . . . . . . . . . .
Exa 10.3 Hermann formula . . . . . . . . . . . . . . . . . . . .
Exa 10.4 Order Estimation . . . . . . . . . . . . . . . . . . . . .
Exa 10.6 Filter length estimation for window based design . . .
Exa 10.7 Order Estimation . . . . . . . . . . . . . . . . . . . . .
Exa 10.8 Kaiser window . . . . . . . . . . . . . . . . . . . . . .
Exa 11.3 Reconstruction of Transfer function from Impulse response coeeficients . . . . . . . . . . . . . . . . . . . .
Exa 11.11 Cascaded lattice Filter structure . . . . . . . . . . . .
Exa 12.3 Signal to Quantisation Noise Ratio . . . . . . . . . . .
Exa 13.1 Up sampling operation . . . . . . . . . . . . . . . . . .
6

55
56
57
57
58
59
60
61
62
63
64
64
65
65
66
67
69
69
70
71
72
73
74
75
75
76
77
78
78
80
81
83
84

Exa
Exa
Exa
Exa

13.2
13.6
14.1
14.2

Down sampling operation . . . . . .


Decimator Computation complexity
Effect of DFT length . . . . . . . . .
Effect of DFT length . . . . . . . . .

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

85
85
87
88

Chapter 2
Discreet Time Signals and
Systems

Scilab code Exa 2.1 Ensemble Averaging


//EXAMPLE 2 . 1
// Ensemble a v e r a g i n g
clear ;
clc ;
n = 1:50;
clf () ;
figure (0)
a = gca () ;
a . x_location = o r i g i n ;
a . y_location = o r i g i n ;
for i =1: length ( n )
s ( i ) =2* n ( i ) *((0.9) ^ n ( i ) ) ;
d ( i ) =( -0.1) ^ n ( i ) ;
// a r b i t r a r y n o i s e
signal .
14
end

1
2
3
4
5
6
7
8
9
10
11
12
13

15
16 M = length ( n ) ;
17
18
for i =1: M

19
d ( i ) =( -0.1) ^ i ;
20
S = sum ( d ) ;
21
end
22 Eav =( s + S / M ) ;
// Ensemble a v e r a g e .
23 disp ( Eav , The o u t p u t o f Ensemble a v e r a g i n g
24 plot2d3 (n , s )
25 plot (n ,s , r . )
26 xtitle ( Ensemble a v e r a g i n g , n , Eavs ) ;
27 a . children . children . thickness =2;
28 a . children . children . foreground =2;

is )

Scilab code Exa 2.2 Basic operations


1
2
3
4
5
6
7
8
9
10
11
12
13

//EXAMPLE 2 . 2 , BASIC OPERATIONS .


clear ;
clc ;
c =[3.2 41 36 -9.5 0];
disp (c , c = ) ;
d =[1.7 -0.5 0 0.8 1];
disp (d , d = ) ;
w1 = c .* d ;
// M u l t i p l i c a t i o n
disp ( w1 , The p r o d u c t o f two i n p u t v e c t o r s i s = ) ;
w2 = c + d ;
// a d d i t i o n
disp ( w2 , The a d d i t i o n o f two i n p u t v e c t o r s i s = ) ;
w3 =3.5* c ;
disp ( w3 , The s c a l i n g o f f i r s t i n p u t v e c t o r i s = ) ;

Scilab code Exa 2.3 Unequal length sequence


1 //EXAMPLE 2 . 3 , B a s i c o p s on u n e q u a l l e n g t h s e q u e n c e
2 clear ;
3 clc ;
4 c =[3.2 41 36 -9.5 0];

5
6
7
8
9
10
11
12
13
14
15
16
17
18

disp (c , c = ) ;
g =[ -21 1.5 3];
disp (g , g = ) ;
a = length ( g ) ;
b = length ( c ) ;
i =0;
while (i <b - a )
g (b - i ) =0;
i = i +1;
end
w4 = g .* c ;
disp ( w4 , The p r o d u c t o f two s e q u e n c e s i s = ) ;
w5 = c + g ;
disp ( w5 , The a d d i t i o n
o f two s e q u e n c e s i s = ) ;

Scilab code Exa 2.5 Generating symmetric parts


1

//EXAMPLE 2 . 5 , C o n j u g a t e A n t i s y m m e t r i c & C o n j u g a t e
symmetric p a r t s o f Sequence
clc ;
clear ;
g =[0 , 1+ %i *4 , -2+ %i *3 , 4 - %i *2 , -5 - %i *6 , - %i *2 , 3];
disp (g , g = )
g1 = conj ( g ) ;
// C o n j u g a t e o f g ;
disp ( g1 , conj ( g ) ) ;
a = length ( g ) ;
for i =1: a
g2 (1 , i ) = g1 (a - i +1) ;
end

2
3
4
5
6
7
8
9
10
11
12
13 gcs =( g + g2 ) /2
// C o n j u g a t e Symmetric p a r t
14 disp ( gcs , The C o n j u g a t e s y m m e t r i c p a r t i s = ) ;
15 gcas =( g - g2 ) /2;
// C o n j u g a t e A n t i s y m m e t r i c p a r t
16 disp ( gcas , The C o n j u g a t e a n t i s y m m e t r i c p a r t i s = ) ;

10

Scilab code Exa 2.6 Energy 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
25
26
27
28
29
30
31
32

//EXAMPLE 2 . 6 , Energy S i g n a l
clear ;
clc ;
n = -5:5;
for i =1: length ( n )
if ( n ( i ) >=1)
h ( i ) =1/ n ( i ) ;
else
h ( i ) =0;
end
end
Sum =0;
N =1:10000;
for i =1: length ( N )
h ( i ) =(1/ N ( i ) ) ^2;
end
Energy = sum ( h ) ;
if ( Energy < %inf ) then
disp ( Energy S i g n a l ) ;
disp ( Energy , Energy o f s i g n a l = ) ;
else
if ( Energy / length ( N ) < %inf ) then
disp ( Power S i g n a l ) ;
else
disp ( N i e t h e r Energy n o r Power S i g n a l ) ;
end
end
11

Scilab code Exa 2.7 Power Signal


1 //EXAMPLE 2 . 7 , Example o f Power s i g n a l
2 clear ;
3 clc ;
4
5 Sum =0;
6 N =1:10000;
7
for i =1: length ( N )
8
h1 = 3*(( -1) ^ i ) ;
9
h = h1 ^2;
10
end
11
12 Energy = sum ( h ) ;
13
if ( Energy /(2*( length ( N ) ) +1) < %inf ) then
14
disp ( Power S i g n a l ) ;
15
disp ( Energy /2 , Power S i g n a l = ) ;
16
else
17
disp ( Not a Power S i g n a l ) ;
18
end

Scilab code Exa 2.9 Square wave generation


1
2
3
4
5
6
7
8
9

//EXAMPLE 2 . 9 , G e n e r a t i o n o f a S q u a r e wave s e q u e n c e :
clc ;
clear ;
clf () ;
a = gca () ;
figure (0) ;
a . x_location = o r i g i n ;
x =[0:1:80];
y1 = sin ( x *.05* %pi ) ;
12

10
11
12
13
14
15
16

y2 = sin ( x *.15* %pi ) ;


y3 = sin ( x *.25* %pi ) ;
y4 = y1 + y2 /3+ y3 /5;
plot2d3 (x , y4 ,2)
plot (x , y4 , r . )
xtitle ( Approximate S q u a r e wave , x , y4 ) ;
a . children . children . thickness =3;

Scilab code Exa 2.16 Linearity of accumulator


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

//EXAMPLE 2 . 1 6 ,
clear ;
clc ;
// Given i n p u t s e q u e n c e = [ 3 4 5 ]
x =[0 3 4 5 0];
disp ([3 4 5] , I n p u t s e q u e n c e = )
// d e t e r m i n i n g median f i l t e r
// f i r s t s e q u e n c e
for k =2:4
if x ( k ) >x (k -1) & x ( k +1) >x (k -1) & x ( k +1) >x ( k )
y (k -1) = x ( k ) ;
else
x (k -1) >x ( k +1) & x ( k ) >x ( k +1) & x ( k ) >x (k -1)
y (k -1) = x (k -1) ;
end
end
disp (y , The Median F i l t e r o f t h e g i v e n i n p u t i s = )
;

Scilab code Exa 2.20 Passive system


1
2

//EXAMPLE 2 . 2 0 , P a s s i v e o r l o s s l e s s s y s t e m .
clear ;
13

3 clc ;
4 a = input ( any v a l u e o f a l e s s t h a n o r e q u a l t o one )
5 n = -10:1:10;
6
x=n;
7
y=a*n;
8
S =0;
9
for i =1: length ( n )
10
S = S + y ^2;
11
end
12
13
if a <1 then
14
disp ( t h e s y s t e m i s p a s s i v e )
15
else
16
17
a ==1
18
disp ( t h e s y s t e m i s l o s s l e s s )
19
20
end

Scilab code Exa 2.22 Impulse response of Accumulator


1
2
3
4
5
6
7
8
9
10
11
12
13
14

//EXAMPLE 2 . 2 2 , i m p u l s e r e s p o n s e o f a c c u m u l a t o r
clear ;
clc ;
d =[1];
t = -1:.01:1;
h =0;
clf () ;
figure (0) ;
a = gca () ;
a . x_location = o r i g i n ;
for i =1: length ( t )
if t ( i ) <0
14

15
16
17
18
19
20

h =0;
else
h=d;
plot2d3 (i -101 , h )
plot (i -101 , h , . r )
xtitle ( I m p u l s e R e s p o n s e o f a c c u m u l a t o r , t
, Y ) ;
a . children . children . thickness =1;
a . children . children . foreground =2;
end

21
22
23
24 end
25 disp (h , The i m p u l s e r e s p o n s e

o f Accumulator i s = )

Scilab code Exa 2.26 Convolution


1
2
3
4
5
6
7
8

//EXAMPLE 2 . 2 6 , c o n v o l u t i o n o f x & h
x =[ -2 0 1 -1 3];
disp (x , x = ) ;
h =[1 2 0 -1];
disp (h , h = ) ;
n =0:7;
y = convol (x , h ) ;
disp (y , The c o n v o l u t i o n o f t h e two i n p u t s i s : )

Scilab code Exa 2.27 Convolution


1
2
3
4
5
6

//EXAMPLE 2 . 2 7 , c o n v o l u t i o n o f an e x p o n e n t i a l
sequence
clear ;
clc ;
n =0:.5:5
c =0.5;
b =0.4;
15

7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

clf () ;
figure (0) ;
a = gca () ;
a . x_location = o r i g i n ;
x = c^n;
subplot (2 ,2 ,1) ;
plot2d3 (n ,x ,2) ;
plot (n ,x , . r ) ;
xtitle ( , n , x ) ;
h = b^n ;
subplot (2 ,2 ,2) ;
plot2d3 (n ,h ,2)
plot (n ,h , . r )
xtitle ( , n , h ) ;
N =0:.5:10;
y = convol ( x , h ) ;
subplot (2 ,2 ,3) ;
plot2d3 (N ,y ,2)
plot (N ,y , . r )
xtitle ( c o n v o l ( x , h ) , n , y ) ;
disp (y , C o n v o l u t i o n o f t h e two e x p o n e n t i a l s e q u e n c e s
is =)

Scilab code Exa 2.28 Convolution


1
2
3
4
5
6
7
8
9

//EXAMPLE 2 . 2 8 , g r a p h i c a l r e p r e s e n t a t i o n o f
convolution of x & h .
clear ;
clc ;
x =[ -2 0 1 -1 3];
disp (x , x ) ;
h =[1 2 0 -1];
disp (h , h ) ;
n =0:7;
y = convol (x , h ) ;
16

10
11
12
13
14
15
16
17
18
19
20

disp (y , c o n v o l u t i o n = ) ;
clf () ;
figure (0) ;
a = gca () ;
a . x_location = o r i g i n ;
a . y_location = o r i g i n ;
plot2d3 (n , y )
plot (n ,y , r . )
xtitle ( c o n v o l u t i o n , n , Y ) ;
a . children . children . thickness =2;
a . children . children . foreground =2;

Scilab code Exa 2.29 Convolution


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

// Example 2 . 2 9 , C o n v o l u t i o n u s i n g T a b u l a r method .
clear ;
clc ;
x =[ -2 0 1 -1 3];
h =[1 2 0 -1];
q = length ( x ) ;
w = length ( h ) ;
z = q +w -1;
y0 =0;
for i =1: z ;
y ( i ) =0;
for k =1: i ;
if k > q
x ( k ) =0;
else
if (i - k +1) >w
h (i - k +1) =0;
else
y ( i ) = y ( i ) + x ( k ) * h (i - k +1) ;
end
end
17

22
end
23 end
24 disp (y , The C o n v o l u t i o n o f t h e two s e q u e n c e s

is =)

Scilab code Exa 2.30 Convolution


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 2 . 3 0
// C o n v o l u t i o n o f two s i d e d s e q u e n c e s
clear ;
clc ;
g =[3 -2 4]; // o r i g i n a t i n g a t n=1
h =[4 2 -1]; // o r i g i n a t i n g a t n=0
q = length ( g ) ;
w = length ( h ) ;
z = q +w -1;
y0 =0;
for i =1: z ;
y ( i ) =0;
for k =1: i ;
if k > q
g ( k ) =0;
else
if (i - k +1) >w
h (i - k +1) =0;
else
y ( i ) = y ( i ) + g ( k ) * h (i - k +1) ;
end
end
end
end
n = -1: z -2;
disp (y , The C o n v o l u t i o n o f t h e two s e q u e n c e s i s = )
clf () ;
a = gca () ;
figure (0) ;
18

30 a . x_location = o r i g i n ;
31 plot2d3 (n ,y ,2) ;
32 plot (n ,y , r . ) ;
33 xtitle ( c o n v o l u t i o n , n , y ) ;

Scilab code Exa 2.31 Stabbility of causal system


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

//EXAMPLE 2 . 3 1 , S t a b i l i t y f o r c a u s a l s y s t e m .
// h [ i ]= i m p u l s e r e s p o n s e o f LTI s y s t e m .
clear ;
clc ;
n = -5:0.001:5;
a =0.6;
for i =1: length ( n )
if ( n ( i ) <0)
h ( i ) =0;
else
h ( i ) = abs ( a ^ n ( i ) ) ;
end
end
S = sum ( h ) ;
if (S < %inf )
disp ( BIBO s t a b l e s y s t e m ) ;
else
disp ( BIBO u n s t a b l e s y s t e m ) ;
end

Scilab code Exa 2.32 Stability of Anti causal system


1

//EXAMPLE2. 3 2 S t a b i l i t y f o r a n t i C a u s a l s y s t e m .
19

2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

// h [ i ]= i m p u l s e r e s p o n s e o f LTI s y s t e m .
clear ;
clc ;
n = -5:1/1000:5;
a =5;
for i =1: length ( n )
if ( n ( i ) > -1)
h ( i ) =0;
else
h(i)=a^n(i);
S = sum ( h ) ;
end
end
if (S < %inf )
disp ( BIBO s t a b l e s y s t e m ) ;
else
disp ( BIBO u n s t a b l e s y s t e m ) ;
end

Scilab code Exa 2.33 Stability of a system


1
2
3
4
5
6
7
8
9
10
11

//EXAMPLE 2 . 3 3 , s t a b i l i t y o f f i n i t e i m p u l s e r e s p o n s e
.
// h [ i ]= i m p u l s e r e s p o n s e o f LTI s y s t e m .
clear ;
clc ;
n = -5:1/100:5;
a = input ( v a l u e o f a ) ;
N1 = input ( l o w e r l i m i t ) ;
N2 = input ( u p p e r l i m i t ) ;
for i =1: length ( a )
if ( n ( i ) < N1 & n ( i ) > N2 )
h ( i ) =0;
20

12
else
13
h(i)=a^n(i);
14
S = sum ( h ) ;
15
end
16 end
17
18 if (S < %inf )
19
disp ( BIBO s t a b l e s y s t e m ) ;

// a s l o n g a s

N1 , N2!= % i n f
20 else
21
disp ( BIBO u n s t a b l e s y s t e m ) ;
22
23 end

Scilab code Exa 2.46 Cross coreation computation


1
2
3
4
5
6
7
8
9
10
11
12

//EXAMPLE 2 . 4 6 , C r o s s c o r e l a t i o n Computation .
// Given two f i n i t e l e n g t h s e q u e n c e . x [ n ] , y [ n ] :
clear ;
clc ;
x =[1 3 -2 1 2 -1 4 4 2];
disp (x , x ) ;
y =[2 -1 4 1 -2 3];
disp (y , y ) ;
// C r o s s c o r e l a t i o n r x y [ n ] :
rxy = convol (x , mtlb_fliplr ( y ) ) ;
disp ( rxy , The C r o s s C o r e l a t i o n O p e r a t i o n o f t h e
Inputs i s =)

21

Chapter 3
Discreet TIme Fourier
Transform

Scilab code Exa 3.5 DTFT computation


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

//EXAMPLE 3 . 5
//DTFT o f u n i t s a m p l e s e q u e n c e
clc ;
clear ;
// a = 0 . 5 ;
n =0:9;
x = [1 , zeros (1 ,9) ];
disp (x , x [ n ] = )
K = 4;
k = 0:4/1000:4;
W = k *2* %pi / K ;
X = ( x ) * exp ( %i *n * W ) ;
disp (X , DTFT, x [ n ] > )
X_mag
= abs ( X ) ;
X_phase = phasemag ( X ) ; // no p h a s e e x i s t s
figure (0) ;
plot2d3 ( mtlb_fliplr ( W ) , X_mag ) ;
22

20
21
22
23

xtitle ( Magnitude p l o t , W > , X mag > ) ;


figure (1) ;
plot2d3 ( mtlb_fliplr ( W ) , X_phase ) ;
xtitle ( z e r o p h a s e p l o t , W > , X p h a s e > ) ;

Scilab code Exa 3.6 DTFT computation


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

//EXAMPLE 3 . 6
// D e t e r m i n e DTFT o f s e q u e n c e
//PROGRAM REQUIRES MAXIMA SCILAB TOOLBOX
clc ;
clear ;
// S y m b o l i c c a l c u l a t i o n
Syms n w a ;
x1 =( a ^ n ) * exp ( - %i * n * w ) ;
X1 = nusum ( x1 ,n ,0 , %inf ) ;
disp ( X1 , DFT, X = ) ;
// Given :
a =0.5;
n =0:9;
// x [ n ]= a nu [ n ]
for i = 0:9
x ( i +1) = a ^ i ;
end
// The DTFT o f t h e s e q u e n c e
K = 4;
k = 0:4/1000:4;
W = k *6* %pi / K ;
X = (x ) * exp ( %i *n * W ) ;
X_mag
= abs ( X ) ;
[ X_phase , db ] = phasemag ( X ) ;
clf () ;
23

29 a = gca () ;
30 figure (0) ;
31 // Note %pi 3 . 1 4
32 plot2d3 ( mtlb_fliplr ( W ) , X_mag ) ;
33 xtitle ( Magnitude r e s p o n s e , W > , A m p l i t u d e >

);
34 figure (1) ;
35 plot2d3 ( mtlb_fliplr ( W ) , X_phase ) ;
36 xtitle ( Phase r e s p o n s e , W > , Phase i n d e g r e e s
> ) ;

Scilab code Exa 3.7 Plotting real and imaginary part


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

//EXAMPLE 3 . 1 2
// x [ n ]=(( 1) n ) ( a n ) u [ n ] . . . . . g i v e n a = 0 . 5 ;
clc ;
clear ;
a =0.5;
n =0:9;
for i = 0:9
x ( i +1) = ( a * exp ( - %i * %pi ) ) ^ i ;
end
// The DTFT o f t h e s e q u e n c e
K = 4;
k = 0:4/1000:4;
W = k *6* %pi / K ;
X = (x ) * exp ( %i *n * W ) ;
X_mag
= abs ( X ) ;
X_phase = phasemag ( X ) ;
//PLOTTING GRAPHS FOR THE INTERVAL OF 0 TO 6 %pi
clf () ;
24

23 a = gca () ;
24 figure (0) ;
25 plot2d3 ( mtlb_fliplr ( W ) , X_mag ) ;
26 xtitle ( Magnitude r e s p o n s e , W , A m p l i t u d e ) ;
27 figure (1) ;
28 plot2d3 ( mtlb_fliplr ( W ) , X_phase ) ;
29 xtitle ( Phase r e s p o n s e , W , X phase , d e g r e e s ) ;

Scilab code Exa 3.10 DTFT of finite length exponential sequence


1 //EXAMPLE 3 . 1 0
2 // DTFT o f a s e q u e n c e
3 clc ;
4 clear ;
5 syms a n M w ;
6 x=a^n;
7 X = nusum ( x *( exp ( - %i * w * n ) ) ,n ,0 ,M -1)
8 disp ( limit ( X ) , The DTFT o f t h e g i v e n s e q u e n c e , X =

Scilab code Exa 3.12 Plotting DTFT of exponential sequence


1
2
3
4
5
6
7
8
9
10
11

//EXAMPLE 3 . 1 2
// x [ n ]=(( 1) n ) ( a n ) u [ n ] . . . . . g i v e n a = 0 . 5 ;
clc ;
clear ;
a =0.5;
n =0:9;
for i = 0:9
x ( i +1) = ( a * exp ( - %i * %pi ) ) ^ i ;
end
25

12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

// The DTFT o f t h e s e q u e n c e
K = 4;
k = 0:4/1000:4;
W = k *6* %pi / K ;
X = (x ) * exp ( %i *n * W ) ;
X_mag
= abs ( X ) ;
X_phase = phasemag ( X ) ;
//PLOTTING GRAPHS FOR THE INTERVAL OF 0 TO 6 %pi
clf () ;
a = gca () ;
figure (0) ;
plot2d3 ( mtlb_fliplr ( W ) , X_mag ) ;
xtitle ( Magnitude r e s p o n s e , W , A m p l i t u d e ) ;
figure (1) ;
plot2d3 ( mtlb_fliplr ( W ) , X_phase ) ;
xtitle ( Phase r e s p o n s e , W , X phase , d e g r e e s ) ;

Scilab code Exa 3.13 DTFT computation


1
2
3
4
5
6
7
8
9
10
11
12
13
14

//EXAMPLE 3 . 1 3
clc ;
clear ;
a =0.5;
n =0:9;
for i = 0:9
x ( i +1) = a ^ i ;
end
// The DTFT o f t h e s e q u e n c e
K = 4;
k = 0:4/1000:4;
W = k *6* %pi / K ;
X1 = (x ) * exp ( %i *n * W ) ;
26

15
16
17
18
19
20
21
22
23
24
25
26
27
28

X
X

= %i * diff ( X1 ) ;
= [X ,0] + X1 ;

X_mag
= abs ( X ) ;
[ X_phase , db ] = phasemag ( X ) ;
clf () ;
a = gca () ;
figure (0) ;
plot2d3 ( mtlb_fliplr ( W ) , X_mag ) ;
xtitle ( Magnitude r e s p o n s e , W , X mag ) ;
figure (1) ;
plot2d3 ( mtlb_fliplr ( W ) , X_phase ) ;
xtitle ( Phase r e s p o n s e , W , X p h a s e ) ;

Scilab code Exa 3.14 Energy of signal


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

//EXAMPLE 3 . 1 4
//ENERGY OF LP DISCREET TIME SIGNAL
//PROGRAM REQUIRES MAXIMA SCILAB TOOLBOX
clc ;
clear ;
syms n wc w ;
wc = input ( t h e v a l u e o f wc ( l e s s t h a n %pi )= ) ;
n = -5:0.05:5;
for i =0: length ( n )
hlp ( i +1) = ( wc / %pi ) * sinc (( wc * i ) / %pi ) ;
E ( i +1) =( abs ( hlp ( i +1) ) ) ^2;
end
Energy = sum ( E ) ;
if ( Energy < %inf ) then
disp ( The f i l t e r i s Energy S i g n a l ) ;
else
27

19
20
21
22
23
24
25

if ( Energy / length ( N ) < %inf ) then


disp ( Power S i g n a l ) ;
else
disp ( N i e t h e r Energy n o r Power S i g n a l ) ;
end
end
disp ( Energy , t h e e n e r g y i s = ) ;

Scilab code Exa 3.15 Energy of exponential sequence


1
2
3
4
5
6
7
8
9
10
11
12
13

//EXAMPLE 3 . 1 5
//ENERGY OF A SIGNAL x [ n ]= a nu [ n ]
clc ;
clear ;
a =0.5;
n =0:0.1:9.9;
// x [ n ]= a nu [ n ]
for i = 0: length ( n )
x ( i +1) = a ^ i ;
E =( abs ( x ) ) ^2;
end
Energy = sum ( E ) ;
disp ( Energy , Energy o f t h e s i g n a l = ) ;

28

Chapter 4
Digital Processing of Continous
TIme Systems

Scilab code Exa 4.5 Passband and Stopband ripple computation


1
2
3
4
5
6
7
8
9
10

//EXAMPLE 4 . 5
// d e t e r m i n e r i p p l e v a l u e s i n db ;
clc
clear ;
ap = 0.01 // Peak p a s s b a n d r i p p l e i n dB
as = 70 // min . s t o p b a n d a t t e u a t i o n i n dB
dp = 1 -10^ -( ap /20) ;
ds = 10^ -( as /20) ;
disp ( dp , dp = ) ;
disp ( ds , d s = ) ;

Scilab code Exa 4.6 Order of Analog filter


1 //EXAMPLE 4 . 6
2 // Order o f LP f i l t e r
3 clc ;

29

4
5
6
7
8
9
10
11
12
13

clear ;
ap = 1 // Peak p a s s b a n d r i p p l e i n dB
as = 40 // min . s t o p b a n d a t t e u a t i o n i n dB
wp = 1000 // Hz
ws = 5000 // Hz
k = wp / ws ;
disp (1/ k , 1/ k = ) ;
k1 = 1/( sqrt ((10^(0.1* as ) -1) /(10^(0.1* ap ) -1) ) ) ;
disp (1/ k1 , 1/ k1 = ) ;
N = ceil ( log10 ( sqrt ((10^(0.1* as ) -1) /(10^(0.1* ap ) -1) ) )
/ log10 (1/ k ) ) ;
14 disp (N , o r d e r o f t h e f i l t e r i s : ) ;

Scilab code Exa 4.7 Order of Analog Chebyshev Filter


//EXAMPLE 4 . 7
// D e t e r m i n e t h e o r d e r o f Analog Chebyshev LP f i l t e r .
clc ;
clear ;
ap = 1 //dB
as = 40 //dB
wp = 1000 // Hz
ws = 5000 // Hz
k = wp / ws ;
disp (1/ k , 1/ k = ) ;
k1 = 1/( sqrt (((10^(0.1* as ) ) -1) /((10^(0.1* ap ) ) -1) ) ) ;
disp (1/ k1 , 1/ k1 = ) ;
N = acosh (1/ k1 ) / acosh (1/ k ) ;
disp (N , N = ) ;
disp ( S i n c e o r d e r o f t h e f i l t e r i s a l w a y s an i n t e g e r
, );
16 disp ( ceil ( N ) , Order o f t h e f i l t e r i s , N = ) ;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

30

Scilab code Exa 4.8 Order of Analog Lowpass Elliptic Filter


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 . 8
// D e t e r m i n e t h e o r d e r o f Analog E l l i p t i c LP f i l t e r .
clc ;
clear ;
ap = 1 //dB
as = 40 //dB
Fp = 1000 // Hz
Fs = 5000 // Hz
wp = Fp *2* %pi ;
ws = Fs *2* %pi ;

k1 = 1/( sqrt ((10^(0.1* as ) -1) /(10^(0.1* ap ) -1) ) ) ;


disp (1/ k1 , 1/ k1 = ) ;
k = wp / ws ;
k2 = sqrt (1 - ( k * k ) ) ;
disp ( k2 , k2 = ) ;
po = (1 - sqrt ( k2 ) ) /(2*(1 + sqrt ( k2 ) ) ) ;
disp ( po , po = ) ;
p = po +2* po ^5 + 15* po ^9 + 150* po ^13;
disp (p , p = ) ;
N = (2* log10 (4/ k1 ) ) / log10 (1/ p ) ;
disp (N , N = ) ;
disp ( S i n c e o r d e r o f t h e f i l t e r i s a l w a y s an i n t e g e r
, );
25 disp ( ceil ( N ) , Order o f t h e f i l t e r i s , N = ) ;

Scilab code Exa 4.16 Design of Analof Butterworth HP Filter


1 //EXAMPLE 4 . 1 6
2 // D e s i g n a n a l o g b u t t e r w o r t h High p a s s
3 clc ;
4 clear ;

31

filter

5
6
7
8
9
10
11
12
13

wp =4000;
ws =1000;
ap =0.1;
as =40;
Ap =1; // a s s u m p t i o n
As =(2* %pi * wp ) * Ap /(2* %pi * ws ) ;

N = ceil ( log10 ( sqrt ((10^(0.1* as ) -1) /(10^(0.1* ap ) -1) ) )


/ log10 ( As / Ap ) ) ;
14 disp (N , o r d e r o f t h e f i l t e r i s : ) ;
15
16 Ac = As /((10^(0.1* as ) -1) ^(1/( N *2) ) ) ;
17 disp ( Ac , c u t o f f f r e q u e n c y = )
18
19 // [ hs , p o l e , z e r o , g a i n ]= a n a l p f (N, b u t t , Ac ) ;
20
21 s = %s ;
22 hs =1/(( s + 1) *( s ^2 + 0.61803* s + 1) *( s ^2 + 1.61803* s

+ 1) ) ;
23 Hs = horner ( hs , s / Ac ) ;
24 H1 = numer ( Hs ) /0.0976514;
25 H2 = denom ( Hs ) /0.0976514;
26 disp ( H1 / H2 , t h e low p a s s t r a n s f e r

f u n c t i o n i s , HLP( s )

= );
27 Hs = horner ( hs , Ac / s ) ;
28 H1 = numer ( Hs ) ;
29 H2 = denom ( Hs ) ;
30 disp ( H1 / H2 , t h e High p a s s

t r a n s f e r f u n c t i o n i s , HHP( s

) = );

32

Chapter 5
Finite Length Discreet
Transform

Scilab code Exa 5.1 DFT computation


//EXAMPLE 5 . 1
//DETERMINE DFT OF GIVEN SEQUENCE
clc ;
clear ;
N = input ( l e n g t h o f s e q u e n c e = ) ;
x = [1 , zeros (1 ,N -1) ];
disp (x , The s e q u e n c e i s , x = ) ;
X = dft (x ,0) ;
disp (X , DFT o f t h e s e q u e n c e i s X = ) ;
m = input ( v a l u e o f some i n t e m e d i a t e ( mth ) p o i n t
);
11 y = [ zeros (1 ,m -1) ,1 , zeros (1 ,N - m ) ];
12 Y = dft (y ,0) ;
13 disp (Y , DFT o f t h e s e q u e n c e i s Y = ) ;
1
2
3
4
5
6
7
8
9
10

Scilab code Exa 5.2 DFT of sinusoidal sequence


33

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

//EXAMPLE 5 . 2
//DFT o f s i n u s o i d a l s e q u e n c e
clc ;
clear ;
N = input (
input value of N
);
r = input (
input r value
) ;
n = 0: N -1;
x = cos (2* %pi * r * n / N )
X = dft (x , -1)
//X e x i s i t s o n l y a t n={r , Nr } where X = N/2
clf () ;
a = gca () ;
a . x_location = o r i g i n ;
a . y_location = o r i g i n ;
plot2d3 (n ,X ,2) ;
a . thickness =1;
plot (n ,X , r . ) ;
xtitle ( DFT , K > , X [ K ] > ) ;
X = disp (X , DFT o f x> ) ;

Scilab code Exa 5.3 DFT computation


1
2
3
4
5
6
7
8
9
10
11

//EXAMPLE 5 . 3
//DETERMINE DFT OF GIVEN SEQUENCE
clc ;
clear ;
N = input ( l e n g t h o f s e q u e n c e , N =
M = input ( M p o i n t DFT = ) ;
if M > N
x = [ ones (1 , N ) , zeros (1 ,M - N ) ];
disp (x , t h e s e q u e n c e i s : ) ;
for n =0: M -1
34

);

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
40
41
42
43

for k =0: M -1
W ( n +1 , k +1) = exp ( -( %i *2* %pi * k / M ) * n ) ;
end
end
X = W *x ;
disp (X , DFT i s , X = ) ;
else
disp ( i n v a l i d c o m p u t a t i o n ) ;
end

n =0: M -1;
clf () ;
figure (0)
a = gca () ;
plot2d3 (n ,x ,2) // p l o t t i n g t h e s e q u e n c e
plot (n ,x , r . ) ;
a . x_location = o r i g i n ;
a . y_location = o r i g i n ;
poly1 = a . children (1) . children (1) ;
poly1 . thickness = 2.5;
xtitle ( o r i g i n a l s e q u e n c e , n , x [ n ] ) ;
figure (1)
a = gca () ;
plot2d3 (n , abs ( X ) ,2) // p l o t t i n g a b s o l u t e v a l u e o f
DFT o f s e q u e n c e
plot (n , abs ( X ) , r . ) ;
a . x_location = o r i g i n ;
a . y_location = o r i g i n ;
poly1 = a . children (1) . children (1) ;
poly1 . thickness = 2.5;
xtitle ( m a g n i t u d e p l o t , M , A b s o l u t e v a l u e ) ;

Scilab code Exa 5.4 IDFT Computation


35

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
33
34
35
36
37

//EXAMPLE 5 . 4
//DETERMINE IDFT OF GIVEN SEQUENCE
clc ;
clear ;
K = input (
value of K
);
disp ( i n p u t M > K ) ;
M = input (
value of M
);
k1 = 0: K -1;
V1 = k1 ./ K ; //DFT
k =0: M -1;
N = length ( V1 ) ;
V = [ V1 , zeros (1 ,M - N ) ];
v = dft (V ,1) ; //IDFT
clf () ;
subplot (1 ,2 ,1)
a = gca () ;
plot2d3 (k , real ( v ) ,2) ;
plot (k , real ( v ) , r . ) ;
a . x_location = o r i g i n ;
a . y_location = o r i g i n ;
poly1 = a . children (1) . children (1) ;
poly1 . thickness = 2;
xtitle ( r e a l p a r t , N , v ) ;
subplot (1 ,2 ,2)
a = gca () ;
plot2d3 (k , imag ( v ) ,2)
plot (k , imag ( v ) , r . ) ;
a . x_location = o r i g i n ;
a . y_location = o r i g i n ;
poly1 = a . children (1) . children (1) ;
poly1 . thickness = 2;
xtitle ( i m a g i n a r y p a r t , N , v ) ;
v = disp ( v ) ;

36

Scilab code Exa 5.5 DFT computation


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

//EXAMPLE 5 . 5
//DFT c o m p u t a t i o n
clc ;
clear ;
N = 16 ;
r = 3 ;
n = 0: N -1;
x = cos (2* %pi * r * n / N )
X = fft (x , -1) //DFT o f t h e s e q u e n c e
clf () ;
a = gca () ;
plot2d3 (n ,X ,2) ;
plot (n ,X , r . )
a . x_location = o r i g i n ;
a . y_location = o r i g i n ;
poly1 = a . children (1) . children (1) ;
poly1 . thickness = 3;
xtitle ( DFT , k , X ) ;
X = disp ( real ( X ) , X = ) ;

Scilab code Exa 5.7 Cicular convolution computation


1 //EXAMPLE 5 . 7
2 // C i r c u l a r c o n v o l u t i o n
3 clear ;
4 clc ;
5 g = [1 2 0 1];
6 disp (g , g [ n ] = ) ;
7 h = [2 2 1 1];

37

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

disp (h , h [ n ] = ) ;
G = fft (g , -1) ;
H = fft (h , -1) ;
Y = G .* H ;
yc = fft (Y ,1) ;
n1 = 0: length ( yc ) -1;
yl = convol (g , h ) ;
n2 = 0: length ( yl ) -1;
clf () ;
subplot (2 ,1 ,1)
a = gca () ;
plot2d3 ( n1 , yc ,2) ;
plot ( n1 , yc , r . ) ;
a . x_location = o r i g i n ;
a . y_location = o r i g i n ;
poly1 = a . children (1) . children (1) ;
poly1 . thickness = 3;
xtitle ( c i r c u l a r c o n v o l u t i o n , n , yc ) ;
subplot (2 ,1 ,2)
a = gca () ;
plot2d3 ( n2 , yl ,2) ;
plot ( n2 , yl , r . ) ;
a . x_location = o r i g i n ;
a . y_location = o r i g i n ;
poly1 = a . children (1) . children (1) ;
poly1 . thickness = 3;
xtitle ( l i n e a r c o n v o l u t i o n , n , y l ) ;
disp ( real ( yc ) , c i r c u l a r c o n v o l u t i o n , yc = ) ;
disp ( yl , l i n e a r c o n v o l u t i o n , y l = ) ;

Scilab code Exa 5.8 Cicular convolution computation

38

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 5 . 8
// C i c u l a r c o n v o l u t i o n
clc ;
clear ;
g = [1 2 0 1];
disp (g , g [ n ] = ) ;
h = [2 2 1 1];
disp (h , h [ n ] = ) ;
G = fft (g , -1) ;
H = fft (h , -1) ;
Y = G .* H ;
yc = fft (Y ,1) ; //IDFT o f Y
disp ( yc , c i r c u l a r c o n v o l u t i o n , yc = )
n =0:3;
clf () ;
figure (0) ;
a = gca () ;
plot2d3 (n , yc ,2) ;
plot (n , yc , r . ) ;
a . x_location = o r i g i n ;
a . y_location = o r i g i n ;
poly1 = a . children (1) . children (1) ;
poly1 . thickness = 3;
xtitle ( C i r c u l a r c o n v o l u t i o n , n , yc ) ;

Scilab code Exa 5.10 Generating symmetric parts


1
2
3
4
5
6
7

//EXAMPLE 5 1 0
// c o n j u g a t e s y m m e t r i c & a n t i s y m m e t r i c p a r t s o f
complex s e q u e n c e
clear ;
clc ;
un =[1+ %i *4 , -2+ %i *3 ,4 - %i *2 , -5 - %i *6];
disp ( un , u [ n ] = ) ;
u1 = conj ( un ) ;
39

8 disp ( u1 , u [ n ] = ) ;
9 // modulo 4 c i r c u l a r l y t i m e r e v e r s e d v e r s i o n :
10 disp ( pmodulo (0 ,4) , u[<0>4] = ) ;
11 disp ( pmodulo ( -1 ,4) , u[<1>4] = ) ;
12 disp ( pmodulo ( -2 ,4) , u[<2>4] = ) ;
13 disp ( pmodulo ( -3 ,4) , u[<3>4] = ) ;
14 un1 =[ u1 ( pmodulo ( -0 ,4) +1) , u1 ( pmodulo ( -1 ,4) +1) , u1 (

pmodulo ( -2 ,4) +1) , u1 ( pmodulo ( -3 ,4) +1) ];


15 disp ( un1 , u[<n >4] = ) ;
16 disp (0.5*( un + un1 ) , u c s [ n ] = ) ;
17 disp (0.5*( un - un1 ) , uca [ n ] = ) ;

Scilab code Exa 5.11 Cicular convolution computation


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

//EXAMPLE 5 . 1 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 DFT
clc ;
clear ;
g = [1 2 0 1];
disp (g , g [ n ] = ) ;
h = [2 2 1 1];
disp (h , h [ n ] = ) ;
M =4;
for n =0: M -1
for k =0: M -1
W ( n +1 , k +1) = exp ( -( %i *2* %pi * k / M ) * n ) ;
end
end
G = W *g ;
H = W *h ;
disp (G , DFT i s , G = ) ;
disp (H , DFT i s , H = ) ;
Y = G .* H ;
y =(1/4) * conj ( W ) *( Y ) ;
40

22

disp ( real ( y ) , C i r c u l a r c o n v o l u t i o n = ) ;

Scilab code Exa 5.12 Linear Convolution using DFT


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

//EXAMPLE 5 . 1 2
// L i n e a r c o n v o l u t i o n u s i n g C i r c u l a r c o n v o l u t i o n
clc ;
clear ;
g = [1 2 0 1];
disp (g , g [ n ] = ) ;
h = [2 2 1 1];
disp (h , h [ n ] = ) ;
// l i n e a c o n v o l u t i o n l e n g t h = 4+41 = 7
// a p p e n d i n g t h e two s i g n a l s w i t h z e r o s
g = [g , zeros (1 ,3) ]
h = [h , zeros (1 ,3) ]
G = fft (g , -1) ;
H = fft (h , -1) ;
Y = G .* H ;
// e l e m e n t w i s e m u l t i p l i c a t i o n
y = fft (Y ,1) ; //IDFT
// P l o t t i n g l i n e a r c o n v o l u t i o n
n =0:6;
figure (0) ;
clf () ;
a = gca () ;
a . x_location = o r i g i n ;
a . y_location = o r i g i n ;
plot2d3 (n ,y ,2) ;
plot (n ,y , r . ) ;
poly1 = a . children (1) . children (1) ;
poly1 . thickness = 2;
xtitle ( L i n e a r c o n v o l u t i o n , n , y ) ;
disp (y , l i n e a r c o n v o l u t i o n , y = ) ;
41

Scilab code Exa 5.14 DFT computationusing single DFT


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 5 . 1 4
//DFT o f two r e a l s e q u e n c e s u s i n g one DFT
clear ;
clc ;
g = [1 2 0 1];
disp (g , g [ n ] = ) ;
h = [2 2 1 1];
disp (h , h [ n ] = ) ;
x = g + %i .* h ;
disp (x , x [ n ] = ) ;
X = fft (x , -1) ;
disp (X , The DFT, X [ k ] = ) ;
X1 = conj ( X ) ;
disp ( X1 , X [ k ] = ) ;
for i =0:3;
a ( i +1) = pmodulo ( -i ,4) ;
X2 ( i +1) = X1 ( a ( i +1) +1) ;
end
X3 = conj ( X2 ) ;
disp ( X3 , X[<4k >4] = ) ;
disp (0.5*( X + X3 ) , G[ k ] = ) ;
disp (( X - X3 ) /(2* %i ) , H [ k ] = ) ;

Scilab code Exa 5.15 DFT computationusing single DFT of shorter length
1
2

//EXAMPLE 5 . 1 5
//DFT c o m p u t a t i o n u s i n g DFT o f s h o r t e r l e n g t h
sequences
42

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
40

clc ;
clear ;
v = [1 2 2 2 0 1 1 1];
disp (v , Length 8 r e a l s e q u e n c e v [ n ] = )
for i =1:4
g ( i ) = v (2* i -1) ;
h ( i ) = v (2* i ) ;
end
G = fft (g , -1) ;
H = fft (h , -1) ;
M = length ( v ) ;
// f o r n =0:M1
for k =0: M -1
W (1 , k +1) = exp ( -( %i *2* %pi * k / M ) *1) ;
end
// end
G =[ G (1) G (2) G (3) G (4) G (1) G (2) G (3) G (4) ] ;
H =[ H (1) H (2) H (3) H (4) H (1) H (2) H (3) H (4) ] ;
V = G + W .* H ;
disp (V , DFt , V [ k ] = ) ;

// f o r k = 0 : 3
43

41
42
43
44
45
46
47

//

V1 ( k +1) = G( k +1) + ( exp ( 2 %pi %i k / 8 ) ) H( k +1)

;
// end
// f o r k = 4 : 7
//
V2 ( k ) = G( k 3) + ( exp ( 2 %pi %i k / 8 ) ) H( k 3) ;
// end
// d i s p ( [ V1 , z e r o s ( 1 , 3 ) ]+V2 )

44

Chapter 6
z Transform

Scilab code Exa 6.1 z Transform of causal exponential sequence


1 //EXAMPLE 6 . 1
2 //ZT r a n s f o r m o f c a u s a l s e q u e n c e
3 clc ;
4 clear ;
5 syms n a z ;
6 x = a^n;
7 X = nusum ( x *(1/ z ) ^n ,n ,0 , %inf ) ;
8 limit ( X ) ;
9 disp ( X = ,X ) ;
10 disp ( ROC = | z | > | a | )

Scilab code Exa 6.2 z transform of anticausal sequence


1
2 clc ;
3 clear ;
4 syms n a z ;
5 x = a^n;

45

6 X = nusum ( - x *(1/ z ) ^n ,n , - %inf , -1) ;


7 limit ( X ) ;
8 disp ( X = ,X ) ;
9 disp ( ROC = | z | < | a | )

Scilab code Exa 6.3 z Transform


1 //EXAMPLE 6 . 3
2 //ZT r a n s f o r m
3 clc ;
4 clear ;
5 syms n a z M N ;
6 x = a^n;
7 X = nusum ( x *(1/ z ) ^n ,n ,M ,N -1) ;
8 limit ( X ) ;
9 disp (X , X = ) ;

Scilab code Exa 6.4 z Transform


1 //EXAMPLE 6 . 4
2 //ZT r a n s f o r m
3 clc ;
4 clear ;
5 syms n z ;
6 x = ( -0.6) ^ n ;
7 X = nusum ( x *((1/ z ) ^ n ) ,n ,0 , %inf ) ;
8 limit ( X ) ;
9 disp (X , X = ) ;

Scilab code Exa 6.5 Z transform of causal sequence


46

1 //EXAMPLE 6 . 9
2 //ZT r a n s f o r m o f c a u s a l s e q u e n c e
3 clc ;
4 clear ;
5 // z=%z ;
6 syms n a z M N ;
7 x = a^n;
8 X = nusum ( x *(1/ z ) ^n ,n , -M , N ) ;
9 limit ( X ) ;
10 disp (X , X = ) ;

Scilab code Exa 6.9 z Transform


1
2
3
4
5
6
7
8
9
10
11
12
13
14

//EXAMPLE 6 . 9
// D e t e r m i n a t i o n o f ROC
clc ;
clear ;
z = %z ;
a =2* z ^4+16* z ^3+44* z ^2+56* z +32;
b =3* z ^4+3* z ^3 -15* z ^2+18* z -12;
[ h1 , g1 ]= factors ( a ) ;
[ h2 , g2 ]= factors ( b ) ;
disp ( h1 , h1 = ) ;
disp ( h2 , h2 = ) ;
c=a/b;
disp (c , f u n c t i o n i s = ) ;
plzr ( c ) ;

Scilab code Exa 6.10 Rational form of z Transform from its zero and pole
locations
1
2

//EXAMPLE 6 . 1 0
//Zt r a n s f o r m from p o l e z e r o l o c a t i o n s
47

3 clc ;
4 clear ;
5 z = %z ;
6 // u s i n g t h e p o l e & z e r o l o c a t i o n s p r o v i d e d
7 num =( z -0.21) *( z -3.14) *( z -( -0.3+ %i *0.5) ) *( z -( -0.3 - %i
8
9
10
11

*0.5) ) ;
den =( z +0.45) *( z -0.67) *( z -(0.81+ %i *0.72) ) *( z -(0.81 - %i
*0.72) ) ;
k =2.2;
Gz =( num / den ) ;
disp ( k * Gz , Gz = ) ;

Scilab code Exa 6.11 Inverse z Transform


1
2
3
4
5
6
7
8
9
10
11
12
13
14

//EXAMPLE 6 . 1 1
// I n v e r s e Zt r a n s f o r m
clc ;
clear ;
syms n z1 ;
z = %z ;
num = z ;
// g i v e n | z | > 1 ;
den = (z -1) ^2;

// Power s e r i e s e x p a n s i o n
x = ldiv ( num , den ,20) ;
disp (x , x = ) ;
disp ( x = nu [ n ] ) ;

Scilab code Exa 6.12 Inverse z Transform


1
2

//EXAMPLE 6 . 1 2
// I n v e r s e Zt r a n s f o r m
48

3
4
5
6
7
8
9
10
11
12
13
14

clear ;
clc ;
z = %z ;
num = 0.5* z ;
den = z ^2 -z + 0.25;
func = num / den ;
v = factors ( den ) ;
disp (v , f a c t o r s a r e = ) ;
h1 = ldiv ( num , den ,10) ;
disp ( h1 , h = ) ;
// u s i n g t h e p r o p e r t y o f zt r a s n f o r m
disp ( o b s e r v i n g v ( 1 ) & v ( 2 ) we c o n c l u d e
( 0 . 5 ) n )

Scilab code Exa 6.13 Proper fraction of Rational z Transform


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

//EXAMPLE 6 . 1 3
// D e t e r m i n i n g p r o p e r f r a c t i o n
clc ;
clear ;
z = %z ;
num = 2* z ^3 + 0.8* z ^2 + 0.5* z +0.3;
den = ( z ^3 + 0.8* z ^2 + 0.2* z ) ;
func = num / den ;
disp ( func , t h e p o l y n o m i a l f u n c t i o n i s H = )
if degree ( num ) >= degree ( den )
disp ( An i m p r o p e r f r a c t i o n ) ;
else disp ( A p r o p e r f r a c t i o n ) ;
end
disp ( d e c o m p o s i n g t h e f r a c t i o n we g e t
H1 = func -( -3.5* z + 1.5) / z ;
disp ( H1 , H1 = ) ;
49

. . . . . );

h=n

20

disp ( H1 i s a P r o p e r f r a c t i o n )

Scilab code Exa 6.14 Inverse z Transform by partial fraction expansion


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

//EXAMPLE 6 . 1 4
// I n v e r s e Zt r a n s f o r m
clear ;
clc ;
z = %z ;
num = z *( z +2) ;
den =( z -0.2) *( z +0.6) ;
H = num / den ;
elts = factors ( den ) ;
disp ( elts ) ;
// s o l v i n g P a r t i a l F r a c t i o n s , we g e t :
Hz = 2.75/(1 -(0.2) / z ) - 1.75/(1+(0.6) / z ) ;
disp ( Hz ) ;
// d i s p ( h = 2 . 7 5 ( 0 . 2 n ) 1 . 7 5 ( 0 . 6 n ) u ( n ) ) ;
h1 = ldiv (2.75* z ,( z -(0.2) ) ,10)
disp ( h1 /2.75 , h1 = ) ;
h1 = ldiv (1.75* z ,( z +(0.6) ) ,10)
disp ( h1 /1.75 , h2 = ) ;
disp ( t h e i n v e r s e zt r a n s f o r m i s : )
disp ( h = 2 . 7 5 ( 0 . 2 n ) u ( n ) 1 . 7 5 ( 0 . 6 n ) u ( n ) )

Scilab code Exa 6.15 residue computation using coefficient matching approach
1 //EXAMPLE 6 . 1 5
2 // s o l v i n g f o r c o e f f i c i e n t s ;
3 clear ;
4 clc ;
5 z = %z ;

50

6
7
8
9
10
11
12
13
14
15
16
17

num = z *( z +2) ;
den =( z -0.2) *( z +0.6) ;
H = num / den ;
disp ( t h e f a c t o r s a r e : ) ;
elts = factors ( den ) ;
disp ( elts ) ;
// c o e f f a r e :
disp ( The c o e f f i c i e n t s a r e p1 , p2 : ) ;
p1 = horner (( z +2) /( z +0.6) ,0.2) ;
disp ( p1 , p1 = ) ;
p2 = horner (( z +2) /( z -0.2) , -0.6) ;
disp ( p2 , p2 = ) ;

Scilab code Exa 6.16 Inverse z Transform by power series expansion


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

//EXAMPLE 6 . 1 6
// P a r t i a l f r a c t i o n e x p a n s i o n
clc ;
clear ;
z = %z ;
num = z ^3;
den = 18* z ^3 + 3* z ^2 - 4* z - 1;
elts = factors ( den ) ;
disp ( elts , t h e f a c t o r s a r e : ) ;
func = num / den ;
// t h e p a r t i a l f r a c t i o n g i v e s :
p1 = horner ((1/(1+0.3333333/ z ) ^2) ,0.5) ;
disp ( p1 , p1 = ) ;
p2 = horner (1/((1 -0.5/ z ) ) , -0.3333333) ;
disp ( p2 , p2 = ) ;
p3 = horner (0.6/((1 -0.5/ z ) ) , -0.3333333) ;
disp ( p3 , p3 = ) ;
disp ( p a r t i a l f r a c t i o n g i v e s : ) ;
disp ( p1 * z / elts (1) , h1 = ) ;
51

21
22

disp ( p3 * z / elts (3) , h2 = ) ;


disp ( p2 * z ^2/( elts (2) * elts (2) ) , h3 = ) ;

Scilab code Exa 6.17 Coefficients of rational form


1
2
3
4
5
6
7
8
9

//EXAMPLE 6 . 1 6
// C o e f f i c i e n t s o f R a t i o n a l form

clc ;
clear ;
z = %z ;
num = 18* z ^3;
den = 18* z ^3 + 3* z ^2 - 4* z - 1;
disp ( coeff ( num ) /18 , t h e Numerator p o l y n o m i a l
c o e f f i c i e n t s a r e : );
10 disp ( coeff ( den ) /18 , t h e d e n o m i n a t o r p o l y n o m i a l
c o e f f i c i e n t s a r e : );

Scilab code Exa 6.18 Inverse z Transform using long division


1 //EXAMPLE 6 . 1 8
2 // I n v e r s e Zt r a n s f o r m u s i n g power s e r i e s
3 clc ;
4 clear ;
5 z = %z ;
6 Xnum = z ;
7 Xden =( z -1) ^2;
8 xn = ldiv ( Xnum , Xden ,15) ;
9 disp ( xn , The f u n c t i o n i s = ) ;
10 disp ( Thus , xn = nu ( n ) ) ;

52

expansion

Scilab code Exa 6.19 Inverse z Transform using long division


1 //EXAMPLE 6 . 1 9
2 // I n v e r s e Zt r a n s f o r m u s i n g Long d i v i s i o n method
3 clc ;
4 clear ;
5 z = %z ;
6 Hnum = z ^2 + 2* z ;
7 Hden = z ^2 + 0.4* z -0.12;
8 hn = ldiv ( Hnum , Hden ,20) ;
9 disp ( hn , The f u n c t i o n i s , hn = ) ;

Scilab code Exa 6.20 Inverse z Transform


1 //EXAMPLE 6 . 2 0
2 // I n v e r s e Zt r a n s f o r m u s i n g power s e r i e s e x p a n s i o n
3 clc ;
4 clear ;
5 z = %z ;
6 Hnum = z ^2 + 2* z ;
7 Hden = z ^2 + 0.4* z -0.12;
8 hn = ldiv ( Hnum , Hden ,20) ;
9 disp ( hn , The i m p u l s e r e s p o n s e i s , hn = ) ;

Scilab code Exa 6.22 z Transform


1
// Example 6 . 2 2
2 //MAXIMA SCILAB TOOLBOX REQUIRED FOR THIS PROGRAM
3 //Z t r a n s f o r m o f r n . c o s ( w o n )
4 clc ;
5 clear ;
6 syms r wo n z ;
7 x1 =( r ^ n ) * exp ( %i * wo * n ) ;

53

8
9
10
11
12
13

X1 = nusum ( x1 *( z ^ - n ) ,n ,0 , %inf ) ;
x2 =( r ^ n ) * exp ( - %i * wo * n ) ;
X2 = nusum ( x2 *( z ^ - n ) ,n ,0 , %inf ) ;
X =( X1 + X2 ) /2 ;
disp (X , X( z )= ) ;
disp ( ROC : | z |> r ) ;

Scilab code Exa 6.23 z Transform


1
2
3
4
5
6
7
8
9
10
11
12
13

// Example 6 . 2 3
//MAXIMA SCILAB TOOLBOX REQUIRED FOR THIS PROGRAM
//Z t r a n s f o r m o f w( n ) =( ( 0 .5 ) ( n2) + ( 0 . 2 ) ( n 1) )
u(n)
syms n z ;
w1 = 4*( -0.5) ^ n
W1 = nusum ( w1 ,n ,0 , %inf ) ;
disp ( W1 , ROC = | z |> 0 . 5 ,W1 = ) ;
w2 = 5*(0.2) ^ n
W2 = nusum ( w2 ,n ,0 , %inf ) ;
disp ( W2 , ROC = | z |> 0 . 2 , W2 = ) ;
disp ( W1 + W2 , The ZT r a n s f o r m i s = ) ;
disp ( ROC = | z | > 0 . 5 ) ;

Scilab code Exa 6.24 sum of sequences of non overlapping ROC


// Example 6 . 2 4
//MAXIMA SCILAB TOOLBOX REQUIRED FOR THIS PROGRAM
//Z t r a n s f o r m o f v ( n ) =(a ) ( n ) u ( n ) ( b ) ( n ) u(n
1)
4 clc ;
5 clear ;
6 disp ( a s s u m i n g
| b | > a );
1
2
3

54

7
8
9
10
11
12
13
14

syms a n ;
x1 = a ^ n ;
X1 = nusum ( x1 ,n ,0 , %inf ) ;
x1 = b ^ n ;
X1 = nusum ( x2 ,n , - %inf , -1) ;
Vz = X1 + X2 ;
disp ( Vz , The Zt r a n s f o r m i s = ) ;
disp ( ROC = | a | < | z | < | b | ) ;

Scilab code Exa 6.25 z Transform


1
2
3
4
5
6
7
8
9
10
11
12

// Example 6 . 2 5
//Z t r a n s f o r m o f Vz , d0 v [ n ] + d1 v [ n 1] = p0 d [ n ]
+ p1 d [ n 1 ] ;
//MAXIMA SCILAB TOOLBOX REQUIRED FOR THIS EXAMPLE
clc ;
clear ;
syms p0 p1 d0 d1 ;
z = %z ;
disp ( g i v e n t h a t v [ n ] > V( z ) . U s i n g Time s h i f t i n g
p r o p e r t y , we g e t : )
disp ( d0 Vz + d1 Vz ( 1 / z ) = p0 + p1 ( 1 / z ) ) ;
disp ( R e a r r a n g i n g t h e t e r m s . . . ) ;
Vz = ( p0 + p1 / z ) /( d0 + d1 / z ) ;
disp ( Vz , Zt r a n s f o r m i s Vz = ) ;

Scilab code Exa 6.26 z Transform


1
2
3
4
5

// Example 6 . 2 6
//MAXIMA SCILAB TOOLBOX
//Z t r a n s f o r m o f ( n+1) a nu ( n )
clear ;
55

6
7
8
9
10
11
12

clc ;
syms a n z ;
x1 =( a ) ^ n ;
X1 = symsum ( x1 *( z ^( - n ) ) ,n ,0 , %inf ) ;
X2 = -z *( diff (X ,z ,1) ) ;
X = X1 + X2 ;
disp ( X , Z t r a n s f o r m o f ( n+1) a nu ( n ) i s X = ) ;

Scilab code Exa 6.27 Inverse z Transform


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 6 . 2 7
// i n v e r s e Zt r a n s f o r m o f z 3 / ( z 0 . 5 ) ( z +1/3) 2 ;
clear ;
clc ;
z = %z ;
Gnum = z ^3;
Gden = (z -0.5) *( z +1/3) ^2;
G = Gnum / Gden ;
g1 = ldiv ( Gnum , Gden ,10) ;
elts = factors ( Gden ) ;
// t h e p a r t i a l f r a c t i o n g i v e s :
p1 = horner ((1/(1+0.3333333/ z ) ^2) ,0.5) ;
disp ( p1 , p1 = ) ;
p2 = horner (1/((1 -0.5/ z ) ) , -0.3333333) ;
disp ( p2 , p2 = ) ;
p3 = horner (0.6/((1 -0.5/ z ) ) , -0.3333333) ;
disp ( p3 , p3 = )
disp ( p a r t i a l f r a c t i o n g i v e s : ) ;
disp ( p1 * z / elts (1) , h1 = ) ;
disp ( p3 * z / elts (3) , h2 = ) ;
disp ( p2 * z ^2/( elts (2) * elts (2) ) , h3 = ) ;
disp ( gn = 0 . 3 6 ( 0 . 5 ) n + 0 . 2 4 ( 1 / 3 ) n + 0 . 4 ( n+1)
( 1/3) n )
56

25

disp ( g1 , t h e f i r s t 10 s a m p l e s o f g [ n ] = ) ;

Scilab code Exa 6.28 Enlargement of ROC by pole zero cancellation


1
2
3
4
5
6
7
8
9
10
11
12
13
14

// Example 6 . 2 8
// E n l a r g e m e n t o f ROC by p o l e z e r o c a n c e l l a t i o n
clc ;
clear ;
z = %z ;
Gz = (2 + 1.2*(1/ z ) ) /(1 - 0.2*(1/ z ) )
disp ( Gz , Gz = ) ;
disp ( ROC = | z | > 0 . 2 ) ;
Hz = 3/(1 + 0.6*(1/ z ) ) ;
disp ( Hz , Hz = ) ;
disp ( ROC = | z | > 0 . 6 ) ;
Xz = Gz * Hz ;
disp ( Xz , Xz = ) ;
disp ( ROC = | z | > 0 . 2 ) ;

Scilab code Exa 6.30 Convolution


1
2
3

//EXAMPLE 6 . 3 0
//PROGRAM REQUIRES MAXIMA SCILAB TOOLBOX
//USE ZTRANSFORM TO EVALUATE CONVOLUTION OF TWO
SEQUENCES :
clc ;
clear ;
syms n z ;
x = [ -2 0 1 -1 3];
h = [1 2 0 -1 0];

4
5
6
7
8
9
10 for n =0:4
11
X ( n +1) = x ( n +1) * z ^( - n ) ;

57

12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

H ( n +1) = h ( n +1) * z ^( - n ) ;
end
disp (X , X = ) ;
disp (H , H = ) ;
for i =1:5
U ( i ) =0;
for j =1:5
U(i)=U(i)+X(i)*H(j);
end
end
Y =0;
for i =1:5;
Y = Y + U(i);
end
disp (Y , Y = ) ;
disp ( y = [ 2 4 1 3 1 5 1 3] )

Scilab code Exa 6.31 Convolution


1
2
3

//EXAMPLE 6 . 3 1
//PROGRAM REQUIRES MAXIMA SCILAB TOOLBOX
//USE ZTRANSFORM TO EVALUATE CONVOLUTION OF TWO
SEQUENCES :
clc ;
clear ;
syms n z ;
x = [3 -2 4];
h = [4 2 -1];

4
5
6
7
8
9
10 for n = -1:1
11
X ( n +2) = x ( n +2) *( z ^ - n ) ;
12 end
13 disp (X , X = ) ;

58

14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

for n =0:2
H ( n +1) = h ( n +1) *( z ^ - n ) ;
end
disp (H , H = ) ;

for i =1:3
U ( i ) =0;
for j =1:3
U(i)=U(i)+X(i)*H(j);
end
end
Y =0;
for i =1:3;
Y = Y + U(i);
end
disp (Y , Y = ) ;
disp ( y = [ 1 2 2 9 10 4] ) ;

Scilab code Exa 6.33 Transfer Function of Moving Average Filter


1
2
3
4
5
6
7
8
9

//EXAMPLE 6 . 3 3
// T r a n s f e r f u n c t i o n o f moving a v e r a g e f i l t e r
clear ;
clc ;
syms n z M ;
x = z ^( - n ) ;
H1 = nusum (x ,n ,0 ,M -1) ;
H = H1 / M ;
disp (H , T r a n s f e r f u n c t i o n , Hz = ) ;

59

Scilab code Exa 6.34 Transfer function determination


//EXAMPLE 6 . 3 4
// y [ n ]= x [ n 1] 1 . 2 x [ n 2] + x [ n 3] + 1 . 3 y [ n 1]
1.04 y [ n 2] + 0 . 2 2 2 y [ n 3]
3 // T r a n s f e r f u n c t i o n d e t e r m i n a t i o n

1
2

4
5 clc ;
6 clear ;
7 z = %z ;
8 disp ( Given t h e
9
10
11
12
13
14
15

d i f f e r e n c e equation taking
z t r a n s f o r m on b o t h s i d e s : )
Yz = z ^2 -1.2* z +1;
Xz = z ^3 -1.3* z ^2 + 1.04* z -0.222;
Hz = Yz / Xz ;
disp ( Hz , The t r a n s f e r f u n c t i o n i s = )
elts = factors ( Xz ) ;
disp ( elts , f a c t o r s o f Xz a r e = )
plzr ( Hz ) ;

60

Chapter 7
LTI Discreet Time systems in
the Transform Domain

Scilab code Exa 7.1 Bounded real function


1 //EXAMPLE 7 . 1
2 //PROGRAM REQUIRES MAXIMA SCILAB TOOLBOX
3
4 clc ;
5 clear ;
6 syms K a z w ;
7
8 hzden = (1 - a *( z ^ -1) ) ; // 0 <| a | < 1 ;
9 Hz = K / hzden ;
10 disp ( | H( e ( jw ) ) | 2 = K 2 / ( ( 1 + a ) 2 2 c o s (w) ) ;
11 // c o n s i d e r i n g a>0
12 disp ( ( a t w = %pi ) ,K2/(1+ a ) 2 < | H| 2 < K2/(1 a )
13
14
15
16

2 , ( at w = 0) );
// c o n s i d e r i n g a<0
disp ( ( a t w = 0 ) ,K2/(1+ a ) 2 < | H| 2 < K2/(1 a )
2 , ( a t w = %pi ) ) ;
disp ( i f K = +/(1a ) , o b s e r v e . . . . ) ;
disp ( | H( e ( jw ) ) | <= 1
Hence a Bounded r e a l
f u n c t i o n . );
61

17
18
19

//w=0: %pi ;
// [ a , b ]= f r e q ( hznum , hzden , w) ;
disp ( abs ( Hz ) )

Scilab code Exa 7.2 Transfer function determination


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

//EXAMPLE 7 . 2
// | H( e ( jw ) ) | 2 = 4 ( ( 1 . 0 9 + 0 . 6 cosw ) ( 1 . 1 6 0 . 8
cosw ) ) / ( ( 1 . 0 4 0 . 2 cosw ) ( 1 . 2 5 + cosw ) )
//REPLACING cosw = ( z + z ( 1) ) /2
clc ;
clear ;
z = %z ;
H1 =4*((1.09 + (0.3) *( z +1/ z ) ) *(1.16 - (0.4) *( z +1/ z ) ) )
;
H2 =((1.04 - (0.2) *( z +1/ z ) ) *(1.25 + (0.5) *( z +1/ z ) ) ) ;
H = H1 / H2 ;
disp (H , The t r a n s f e r f u n c t i o n i s , H = )
elts1 = factors ( numer ( H ) ) ;
disp ( elts1 , The f a c t o r s o f n u m e r a t o r a r e : ) ;
elts2 = factors ( denom ( H ) ) ;
disp ( elts2 , The f a c t o r s o f d e n o m i n a t o r a r e : ) ;
disp ( The Four p o s i b l e s t a b l e t r a n s f e r f u n c t i o n w i t h
same s q u a r e m a g n i t u d e f u n c t i o n a r e : ) ;
h1 =2*((1+(0.3) / z ) *(1 - (0.4) / z ) ) /((1 -(0.2) / z )
*(1+(0.5) / z ) ) ;
disp ( h1 , s t a b l e t r a n s f e r f u n c t i o n , h1 = ) ;
h2 =2*((1+(0.3) / z ) *((0.4) - (1) / z ) ) /((1 -(0.2) / z )
*(1+(0.5) / z ) ) ;
disp ( h2 , s t a b l e t r a n s f e r f u n c t i o n , h 2 s = ) ;
h3 =2*(((0.3) +1/ z ) *((1) - (0.4) / z ) ) /((1 -(0.2) / z )
*(1+(0.5) / z ) ) ;
disp ( h3 , s t a b l e t r a n s f e r f u n c t i o n , h3 = ) ;
h4 =2*(((0.3) +1/ z ) *((0.4) - (1) / z ) ) /((1 -(0.2) / z )
*(1+(0.5) / z ) ) ;
62

23

disp ( h4 , s t a b l e t r a n s f e r f u n c t i o n , h4 = ) ;

Scilab code Exa 7.6 FIR Transfer function


//EXAMPLE 7 . 6
// FIR T r a s n f e r f u n c t i o n s w i t h d i f f e r e n t Phase .
clc ;
clear ;
z = %z ;
W = 0:(1/400) :1;
z = exp ( %i *2* %pi * W ) ;
for i =1:401
H1z ( i ) = -1+ 2/ z ( i ) - 3/( z ( i ) ^2) + 6/( z ( i ) ^3)
-3/( z ( i ) ^4) +2/( z ( i ) ^5) -1/ z ( i ) ^(6) ;
10 end
11 H1z_phase = phasemag ( H1z ) ;
1
2
3
4
5
6
7
8
9

12
13 clf () ;
14 figure (0) ;
15 plot2d ( W /(2* %pi ) , H1z_phase ,1) ;
16 xtitle ( p h a s e r e s p o n s e , W/ ( 2 %pi ) , H 2 z p h a s e i n

d e g r e e s );
17
18 for i =1:401
19
H2z ( i ) = +1 - 2/ z ( i ) + 3/( z ( i ) ^2) - 6/( z ( i ) ^3) +

3/( z ( i ) ^4) - 2/( z ( i ) ^5) + 1/ z ( i ) ^(6) ;


20 end
21 H2z_phase = phasemag ( H2z ) ;
22
23 plot2d ( W /(2* %pi ) , H2z_phase ,2) ;
24 xtitle ( p h a s e r e s p o n s e , W/ ( 2 %pi ) , H 2 z p h a s e i n

d e g r e e s );

63

Chapter 8
Digital Filter Structures

Scilab code Exa 8.1 Analysis of Cascaded lattice digital filter structure
//EXAMPLE 8 . 1
//MAXIMA SCILAB TOOLBOX REQUIRED FOR THIS EXAMPLE
// D i g i t a l f i l t e r s t r u c t u r e
clear ;
clc ;
syms W1 W2 W3 X Y a d B y E z ;
// E q u a t i o n s o b t a i n e d a r e a s f o l l o w s :
W1 = X - a * W3 / z ;
W2 = W1 - d * W2 / z ;
W3 = W2 / z + E * W2 ;
Y = B * W1 + y * W3 / z ;
// S o l v i n g t h e a b o v e e q u a t i o n s :
Hz =( B + ( B * d + y * E ) / z + y /( z ^2) ) /(1 + ( d + a * E ) / z + a /( z
^2) )
14 disp ( Hz , Hz = ) ;

1
2
3
4
5
6
7
8
9
10
11
12
13

Scilab code Exa 8.6 Factorization of FIR Transfer Function

64

1 // Example 8 . 6
2 // F a c t o r i z a t i o n o f FIR T r a n s f e r F u n c t i o n
3 clear ;
4 clc ;
5 z = %z ;
6 Hz =50.4+28.02/ z +13.89/ z ^2+7.42/ z ^3+6.09/ z ^4+3/ z ^5+1/
7

z ^6;
disp ( factors ( numer ( Hz ) ) , The F a c t o r s o f t h e FIR
T r a n s f e r Function a r e = );

Scilab code Exa 8.7 Factorization of IIR Transfer Function


1 // Example 8 . 7
2 // F a c t o r i z a t i o n o f I I R T r a n s f e r F u n c t i o n
3 clear ;
4 clc ;
5 z = %z ;
6 // Numerator o f t h e t r a n s f e r f u n c t i o n
7 Numz =6+17.1/ z +33.05/ z ^2+24.72/ z ^3+19.908/ z ^4 -5.292/ z
8
9
10
11
12
13

^5+18.144/ z ^6;
// Den omin ator o f t h e t r a n s f e r f u n c t i o n
Denz =1+2.2/ z +2.56/ z ^2+1.372/ z ^3+0.118/ z ^4 -0.332/ z
^5 -0.168/ z ^6;
Fn = factors ( numer ( Numz ) ) ;
disp ( Fn , F a c t o r s o f t h e n u m e r a t o r o f t h e T r a n s f e r
Function = );
Fd = factors ( numer ( Denz ) ) ;
disp ( Fd , F a c t o r s o f t h e d e n o m i n a t o r o f t h e T r a n s f e r
Function = );

Scilab code Exa 8.10 Cascaded lattice realization of IIR digital Transfer
Function
65

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 8 . 1 0
// C a s c a d e d l a t t i c e r e a l i z a t i o n o f I I R T r a n s f e r
Function
clear ;
clc ;
z = %z ;
P3z = -0.2 + 0.18/ z + 0.4/( z ^2) + 1/( z ^3) ;
D3z = 1 + 0.4/ z + 0.18/( z ^2) - 0.2/( z ^3) ;
A3z = P3z / D3z ;
p1 = coeff ( numer ( P3z ) ) ;
p = mtlb_fliplr ( p1 ) ;
disp ( mtlb_fliplr ( p ) , The c o e f f i c i e n t s o f n u m e r a t o r
a r e = );
d1 = coeff ( numer ( D3z -1) ) ;
d = mtlb_fliplr ( d1 )
disp (( d ) , The c o e f f i c i e n t s o f n u m e r a t o r a r e = ) ;
d1_1dash =( d (1) -d (3) * d (2) ) /(1 - d (3) * d (3) ) ;
disp ( d1_1dash , d 1 1 d a s h = ) ;
d2_1dash =( d (2) -d (3) * d (1) ) /(1 - d (3) * d (3) ) ;
disp ( d2_1dash , d 2 1 d a s h ) ;
d1_2dash =( d1_1dash ) /(1+ d2_1dash ) ;
disp ( d1_2dash , d 1 2 d a s h = ) ;
A1z =( d1_2dash + 1/ z ) /(1 + d1_2dash / z ) ;
disp ( A1z , A1z = ) ;
A2z =( d2_1dash + d1_1dash *1/ z + 1/ z ^2) /(1 + d1_1dash /
z - d2_1dash / z ^2) ;
disp ( A2z , A2z = ) ;

Scilab code Exa 8.12 Gray Markel method of realization


1 // Example 8 . 1 2
2 // Gray M a r k e l method o f
3 clear ;
4 clc ;
5 z = %z ;

Realisation

66

6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

P3z = 0 + 0.44/ z + 0.362/( z ^2) +0.02/( z ^3) ;


D3z = 0.4/ z + 0.18/( z ^2) - .2/( z ^3) ;
Hz = P3z / D3z ;
p1 = coeff ( numer ( P3z ) ) ;
p = mtlb_fliplr ( p1 )
disp ( mtlb_fliplr ( p ) , The c o e f f i c i e n t s o f n u m e r a t o r
a r e = );
d1 = coeff ( numer ( D3z ) ) ;
d = mtlb_fliplr ( d1 )
disp ( mtlb_fliplr ( d ) , The c o e f f i c i e n t s o f n u m e r a t o r
a r e = );
d1_1dash =( d (1) -d (3) * d (2) ) /(1 - d (3) * d (3) ) ;
disp ( d1_1dash , d 1 1 d a s h = ) ;
d2_1dash =( d (2) -d (3) * d (1) ) /(1 - d (3) * d (3) ) ;
disp ( d2_1dash , d 2 1 d a s h ) ;
d1_2dash =( d1_1dash ) /(1+ d2_1dash ) ;
disp ( d1_2dash , d 1 2 d a s h = ) ;
a1 = p (3) ;
disp ( p (3) , a1 = ) ;
a2 = p (2) - a1 * d (1) ;
disp ( p (2) - a1 * d (1) , a2 = ) ;
a3 = p (1) - a1 * d (2) - a2 * d1_1dash ;
disp ( p (1) - a1 * d (2) - a2 * d1_1dash , a3 = ) ;
disp (0 - a1 * d (3) - a3 * d1_2dash - a2 * d2_1dash , a4 = ) ;

Scilab code Exa 8.18 Cascaded lattice realization


// Example 8 . 1 8
// C a s c a d e d l a t t i c e r e a l i z a t i o n o f Powers y m m e t r i c
FIR T r a n s f e r F u n c t i o n
3 clear ;
4 clc ;
5 z = %z ;
1
2

6
7 H5z =(1 + 0.3/ z + 0.2/ z ^2 - 0.376/ z ^3 - 0.06/ z ^4 +

67

8
9
10
11
12
13
14
15
16
17
18
19

0.2/ z ^5) ;
disp ( H5z , FIR f i l t e r = ) ;
G5 = horner ( H5z , -1/ z ) ;
G5z = G5 / z ^5;
disp ( G5z , FIR f i l t e r = ) ;
k5 =0.2;
H3z =(1/(1+ k5 ^2) ) *( H5z - k5 * G5z ) ;
disp ( H3z , S y n t h e s i s eqn , H3z = ) ;
G3z =(1/(1+ k5 ^2) ) *( k5 * H5z + G5z ) ;
disp ( G3z , S y n t h e s i s eqn , G3z = ) ;
k = coeff ( numer ( G3z ) ) ;
disp ( k (4) , k3 = ) ;
disp ( k (2) , k1 = ) ;

68

Chapter 9
IIR digital filter design

Scilab code Exa 9.1 Computating ripple values


1
2
3
4
5
6
7
8
9
10
11
12

//EXAMPLE 9 . 1
// p a s s band & s t o p band r i p p l e
clc ;
clear ;
ap =0.1; // peak p a s s b a n d r i p p l e i n dB
as =35; // min . s t o p b a n d a t t e n u a t i o n i n dB
// c a l c u l a t i o n o f peak r i p p l e v a l u e s
dp =1 -10^ -( ap /20) ;
disp ( dp , dp = ) ;
ds =10^ -( as /20) ;
disp ( ds , d s = ) ;

Scilab code Exa 9.2 conversion of bandedged frequencies to Normalized


digital frequencies
1
2

//EXAMPLE 9 . 2
// a n a l o g p a s s b a n d & s t o p b a n d f r e q u e n c i e s ( i n KHz) :
69

3
4
5
6
7
8
9
10
11
12
13

clc ;
clear ;
ap =7;
as =3;
// S a m p l i n g f r e q u e n c y ( i n KHz) :
FT =25;
// d i g i t a l f r e q u e n c i e s :
wp =2* %pi * ap / FT ;
disp ( wp , wp = ) ;
ws =2* %pi * as / FT ;
disp ( ws , ws = ) ;

Scilab code Exa 9.3 Design of HP Digital Filter


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

// Example 9 . 3
// D e s i g n o f HP I I R f i l t e r
clc ;
clear ;
Fp =700 // Hz
Fs =500 // Hz
ap =1 //dB
as =32 //dB
FT =2000 // Hz
// n o r m a l i z e d a n g u l a r e d g e f r e q u e n c i e s i n r a d / s e c
wp =2* %pi * Fp / FT ;
ws =2* %pi * Fs / FT ;
// p r e w a r p t h e d i g i t a l e d g e f r e q u e n c i e s
Ap1 = tan ( wp /2) ;
As1 = tan ( ws /2) ;
Ap =1; // a s s u m i n g
As =(2* %pi * Ap1 ) * Ap /(2* %pi * As1 ) ;
disp ( As , As = )
// Order N o f t h e f i l t e r
k = Ap / As ;
70

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
49
50
51
52

disp (1/ k , 1/ k = ) ;
k1 = 1/( sqrt (((10^(0.1* as ) ) -1) /((10^(0.1* ap ) ) -1) ) ) ;
disp (1/ k1 , 1/ k1 = ) ;
N = ceil ( acosh (1/ k1 ) / acosh (1/ k ) ) ;
disp (N , N = ) ;
disp (N , Order o f t h e f i l t e r i s , N = ) ;
e = sqrt (10^(0.1* ap ) -1) ;
u =1/ e + sqrt (1+(1/( e * e ) ) ) ;
a = Ap *( u ^(1/ N ) - u ^( -1/ N ) ) /2;
b = Ap *( u ^(1/ N ) + u ^( -1/ N ) ) /2;
for i =1: N
phi ( i ) = %pi /2 + (2* i -1) *( %pi ) /(2* N ) ;
p ( i ) = a * cos ( phi ( i ) ) + %i * b * sin ( phi ( i ) ) ;
end
s = %s ;
z = %z ;
H1 =1;
// Numerator o f H( s )
for i =1: N
H1 = H1 *( s + p ( i ) )
end
// Den omin ator o f H( s )
H2 = horner ( H1 ,0) ;
// T r a n s f e r f u n c t i o n
H = H2 / H1 ;
disp (H , H = )
// B i l n e a r T r a n s f o r m a a t i o n , s =(( z 1/( z +1) ) ;
Hz = horner (H ,( z -1) /( z +1) ) ;
disp ( Hz , The d i g i t a l HP f i l t e r i s Hz = ) ;

Scilab code Exa 9.6 Changing passband edge frequencies to LP IIR digital frequencies

71

1
2
3
4
5
6
7
8
9
10
11
12
13
14

//EXAMPLE 9 . 6
//LP TO LP T r a n s f o r m a t i o n
clc ;
clear ;
z = %z ;
w =0:0.001* %pi : %pi ;
Glz =(0.0662272*(1+1/ z ) ^3) /((1 -0.2593284/ z )
*(1 -0.6762858/ z +0.3917468/( z ^2) ) )
wc =0.25* %pi ; // O r i n g i n a l p a s s b a n d e d g e
Wc =0.35* %pi ; // R e q u i r e d p a s s b a n d e d g e
l = sin (( wc - Wc ) /2) / sin (( wc + Wc ) /2)
disp (l , lambda = ) ;
Gdz = horner ( Glz ,((1 - l / z ) /(1/ z - l ) ) ) ;
disp ( Gdz , The t r a n s f e r f u n c t i o n i s Gdz = ) ;

Scilab code Exa 9.7 Design of HP IIR Digital Filter from LP Digital Filter
1 //EXAMPLE 9 . 7
2 //LP TO HP T r a n s f o r m a t i o n
3
4 clc ;
5 clear ;
6 z = %z ;
7 Glz =(0.0662272*(1+1/ z ) ^3) /((1 -0.2593284/ z )
8
9
10
11
12
13
14

*(1 -0.6762858/ z +0.3917468/( z ^2) ) ) ;


wc =0.25* %pi ; // O r i n g i n a l p a s s b a n d e d g e
Wc =0.55* %pi ; // R e q u i r e d p a s s b a n d e d g e
l = - cos (( wc + Wc ) /2) / cos (( wc - Wc ) /2) ;
disp (l , lambda = ) ;

w =0:0.001:1;
Ghz = horner ( Glz , -(( z + l ) /(1 + l * z ) ) ) ; //LP TO HP
Transformation formula
15 den = factors ( denom ( Ghz ) ) ;
72

16
17

disp ( Ghz , The t r a n s f e r f u n c t i o n i s Gdz = ) ;


disp ( den , t h e f a c o r s o f t h e d e n o m i n a t o r a r e = ) ;

Scilab code Exa 9.12 Minimum order of Type 2 Chebyshev HP IIR digital
filter
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

//EXAMPLE 9 . 1 2
// Minimum o r d e r o f type 2 Chebyshev h i g h p a s s d i g i t a l
filter
clc ;
clear ;
ap = 1 //dB
as = 40 //dB
Fp = 1000 // Hz
Fs = 600 // Hz
Wp = Fp *2* %pi ;
Ws = Fs *2* %pi ;
F = 4000 // Hz
T =1/ F ;
Ap =(2/ T ) *( tan ( Wp * T /2) )
As =(2/ T ) *( tan ( Ws * T /2) )

k = Ap / As ;
disp (1/ k , 1/ k = ) ;
k1 = 1/( sqrt (((10^(0.1* as ) ) -1) /((10^(0.1* ap ) ) -1) ) ) ;
disp (1/ k1 , 1/ k1 = ) ;
N = acosh (1/ k1 ) / acosh ( k ) ; // o r d e r o f t h e f i l t e r
disp (N , N = ) ;
disp ( S i n c e o r d e r o f t h e f i l t e r i s a l w a y s an i n t e g e r
, );
26 disp ( ceil ( N ) , Order o f t h e f i l t e r i s , N = ) ;

73

Chapter 10
FIR digital filter design

Scilab code Exa 10.1 Kaiser formula


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

// Example 1 0 . 0 1
// Order e s t i m a t i o n u s i n g K a i s e r s f o r m u l a
clear ;
clc ;
Fp =1800; // Passband e d g e f r e q . i n Hz
Fs =2000; // s t o p b a n d e d g e f r e q . i n Hz
ap =0.1; // peak p a s s b a n d r i p p l e i n dB
as =35; // min . s t o p b a n d a t t e n u a t i o n i n dB
FT =12000; // S a m p l i n g f r e q . i n Hz
// c a l c u l a t i o n o f peak r i p p l e v a l u e s
dp =1 -10^ -( ap /20) ;
disp ( dp , dp = ) ;
ds =10^ -( as /20) ;
disp ( ds , d s = ) ;

// Order o f t h e FIR f i l t e r
N =( -(20* log10 ( sqrt ( ds * dp ) ) ) - 13) /((14.6) *( Fs - Fp ) / FT
);
19 disp ( ceil ( N ) , Order o f t h e f i l t e r i s N = )

74

Scilab code Exa 10.2 Bellenger formula


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

// Example 1 0 . 0 1
// Order e s t i m a t i o n u s i n g B e l l a n g e r s f o r m u l a
clear ;
clc ;
Fp =1800; // Passband e d g e f r e q . i n Hz
Fs =2000; // s t o p b a n d e d g e f r e q . i n Hz
ap =0.1; // peak p a s s b a n d r i p p l e i n dB
as =35; // min . s t o p b a n d a t t e n u a t i o n i n dB
FT =12000; // S a m p l i n g f r e q . i n Hz
// c a l c u l a t i o n o f peak r i p p l e v a l u e s
dp =1 -10^ -( ap /20) ;
disp ( dp , dp = ) ;
ds =10^ -( as /20) ;
disp ( ds , d s = ) ;
// Order o f t h e FIR f i l t e r
N =(( -2* log10 (10* ds * dp ) ) /((3) *( Fs - Fp ) / FT ) ) -1 ;
disp ( ceil ( N ) , Order o f t h e f i l t e r i s N = )

Scilab code Exa 10.3 Hermann formula


1
2
3
4
5
6
7
8

// Example 1 0 . 0 3
// Order e s t i m a t i o n u s i n g Hermann s f o r m u l a
clear ;
clc ;
Fp =1800; // Passband e d g e f r e q . i n Hz
Fs =2000; // s t o p b a n d e d g e f r e q . i n Hz
ap =0.1; // peak p a s s b a n d r i p p l e i n dB
as =35; // min . s t o p b a n d a t t e n u a t i o n i n dB
75

9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

FT =12000; // S a m p l i n g f r e q . i n Hz
// c a l c u l a t i o n o f peak r i p p l e v a l u e s
dp =1 -10^ -( ap /20) ;
disp ( dp , dp = ) ;
ds =10^ -( as /20) ;
disp ( ds , d s = ) ;
a1 =0.005309;
a2 =0.07114;
a3 = -0.4761;
a4 =0.00266;
a5 =0.5941;
a6 =0.4278;
D_infi =(( a1 *( log10 ( dp ) ^2) + a2 * log10 ( dp ) + a3 ) * log10
( ds ) ) -( a4 *( log10 ( dp ) ) ^2 + a5 *( log10 ( dp ) ) + a6 ) ;
disp ( D_infi , D i n f i = ) ;
b1 =11.01217;
b2 =0.51244;
F = b1 + b2 *(( log10 ( dp ) ) -( log10 ( ds ) ) ) ;
disp (F , F = ) ;

24
25
26
27
28
29
30 // Order o f t h e FIR f i l t e r
31 N =( D_infi - F *(( Fs - Fp ) / FT ) ^2) /(( Fs - Fp ) / FT ) ;
32 disp ( ceil ( N ) , Order o f t h e f i l t e r i s N = )

Scilab code Exa 10.4 Order Estimation


1
2
3
4
5
6
7

// Example 1 0 . 0 4
// K a i s e r s f o r m u l a f o r b a n d p a s s f i l t e r
clear ;
clc ;
Fp1 =300; // Passband e d g e f r e q . i n Hz
Fs1 =350; // s t o p b a n d e d g e f r e q . i n Hz
Fp2 =1000; // Passband e d g e f r e q . i n Hz
76

8
9
10
11
12
13
14

Fs2 =1100; // s t o p b a n d e d g e f r e q . i n Hz
dp =0.004; // p a s s b a n d r i p p l e i n dB
ds =0.01; // s t o p b a n d r i p p l e i n dB
FT =10000; // S a m p l i n g f r e q . i n Hz
// S i n c e ( Fp1Fs1 ) <(Fs2Fp2 ) , b a n d w i t h u s e d i s ( Fp1
Fs1 )

15
16 // Order o f t h e FIR f i l t e r
17 N =( -(20* log10 ( sqrt ( ds * dp ) ) ) - 13) /((14.6) *( Fs1 - Fp1 ) /
18

FT ) ;
disp ( ceil ( N ) , Order o f t h e f i l t e r

is N = )

Scilab code Exa 10.6 Filter length estimation for window based design
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

//EXAMPLE 1 0 . 6
// F I l t e r l e n g t h
f o r window b a s e d d e s i g n
clear ;
clc ;
wp =0.3* %pi ; // r a d / s e c
ws =0.5* %pi ; // r a d / s e c
as =40; //dB
wc =( wp + ws ) /2; // c u t o f f f r e q u e n c y
Bw = ws - wp ;
disp ( Bw , N o r m a l i z e d t r a n s i t i o n bandwidth i s = )
// Hann window
M1 =3.11* %pi / Bw ;
disp ( M1 , V a l u e o f M = )
// Hamming window
M2 =3.32* %pi / Bw ;
disp ( M2 , V a l u e o f M = )
// Blackman window
M3 =5.56* %pi / Bw ;
77

20

disp ( M3 , V a l u e o f M = )

Scilab code Exa 10.7 Order Estimation


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

//EXAMPLE 1 0 . 6
// Order e s t i m a t i o n u s i n g DolphCebyshev window
clear ;
clc ;
wp =0.3* %pi ; // r a d / s e c
ws =0.5* %pi ; // r a d / s e c
as =40; //dB
wc =( wp + ws ) /2; // c u t o f f f r e q u e n c y
Bw = ws - wp ;
disp ( Bw , N o r m a l i z e d t r a n s i t i o n bandwidth i s = )
// Order o f t h e f i l t e r
N = ((2.056* as ) - 16.4) /(2.285* Bw ) ;
disp ( ceil ( N ) , Order o f t h e f i l t e r , N =

Scilab code Exa 10.8 Kaiser window


1
2
3
4
5
6
7
8
9
10
11

//EXAMPLE 1 0 . 8
// D e s i g n o f LP FIR f i l t e r u s i n g K a i s e r window
clear ;
clc ;
wp =0.3* %pi ; // r a d / s e c
ws =0.5* %pi ; // r a d / s e c
as =40; //dB
wc =( wp + ws ) /2; // c u t o f f f r e q u e n c y
Bw = ws - wp ;
disp ( Bw , N o r m a l i z e d t r a n s i t i o n bandwidth i s = )

78

12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34

ds =10^( - as /20) ;
B = (0.5842*( as -21) ^0.4) + 0.07886*( as -21) ;
N = ceil (( as - 8) /(2.285* Bw ) ) ;
disp (N , Order o f t h e f i l t e r , N = ) ;
M =( N -1) *0.5;
disp (M , M = ) ;
w = window ( k r ,N ,6) ; // K a i s e r window
i = - M :1: M ;
hn =( wc / %pi ) * sinc ( wc *i /( %pi ) ) ;
h = hn * w ;
clf () ;
n =0:0.001:1;
[H , fr ]= frmag (w ,1001) ;
plot2d (2* fr , log10 ( H ./ max ( H ) ) , style = color ( b l u e ) )
set ( gca () , g r i d ,[1 1]* color ( g r a y ) )
a = gca () ;
xlabel ( w/ %pi ) ;
ylabel ( Magnitude i n dB ) ;
title ( Gain R e s p o n s e o f K a i s e r Window ) ;

79

Chapter 11
DSP Algorithm implementation

Scilab code Exa 11.3 Reconstruction of Transfer function from Impulse


response coeeficients
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

// e x a m p l e 1 1 . 3
// R e c o n s t r u c t i o n o f T r a n s f e r f u n c t i o n from I m p u l s e
response coeff .
clear ;
clc ;
z = %z ;
numz =2+6/ z +3/( z ^2) ;
denz =(1+1/ z +2/ z ^2) ;
disp ( numz / denz , Hz = ) ;
d = coeff ( numer ( denz ) ) ;
disp (d , c o e f f i c i e n t s o f t h e d e n o m i n a t o r a r e = )
h1 = ldiv ( numer ( numz ) , numer ( denz ) ,5) ;
disp ( h1 , The f i r s t f i v e c o e f f c i e n t s a r e o f H( z ) =
);
for i =1:3
for j =1:3
if i >= j
h (i , j ) = h1 (i - j +1)
else
h (i , j ) =0;
80

19
end
20
end
21 end
22 disp (h , h = ) ;
23 disp (( h * d ) , c o e f f i c i e n t s

o f the numerator are =

);

Scilab code Exa 11.11 Cascaded lattice Filter structure


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

// Example 1 1 . 1 1
// S i m u l a t i o n o f I I R c a s c a d e d l a t t i c e f i l t e r
structure
clear ;
clc ;
z = %z ;
P3z = 0 + 0.44/ z + 0.362/( z ^2) +0.02/( z ^3) ;
D3z = 1 + 0.4/ z + 0.18/( z ^2) - 0.2/( z ^3) ;
Hz = P3z / D3z ;
p1 = coeff ( numer ( P3z ) ) ;
p = mtlb_fliplr ( p1 )
disp ( mtlb_fliplr ( p ) , The c o e f f i c i e n t s o f n u m e r a t o r
a r e = );
d1 = coeff ( numer ( D3z -1) ) ;
d = mtlb_fliplr ( d1 )
disp ( mtlb_fliplr ( d ) , The c o e f f i c i e n t s o f n u m e r a t o r
a r e = );
d1_1dash =( d (1) -d (3) * d (2) ) /(1 - d (3) * d (3) ) ;
disp ( d1_1dash , d 1 1 d a s h = ) ;
d2_1dash =( d (2) -d (3) * d (1) ) /(1 - d (3) * d (3) ) ;
disp ( d2_1dash , d 2 1 d a s h ) ;
d1_2dash =( d1_1dash ) /(1+ d2_1dash ) ;
disp ( d1_2dash , d 1 2 d a s h = ) ;
a1 = p (3) ;
disp ( p (3) , a1 = ) ;
a2 = p (2) - a1 * d (1) ;
81

24 disp ( p (2) - a1 * d (1) , a2 = ) ;


25 a3 = p (1) - a1 * d (2) - a2 * d1_1dash ;
26 disp ( p (1) - a1 * d (2) - a2 * d1_1dash , a3 = ) ;
27 disp (0 - a1 * d (3) - a3 * d1_2dash - a2 * d2_1dash , a4 = ) ;

82

Chapter 12
Analysis of Finite Wordlength
Effects

Scilab code Exa 12.3 Signal to Quantisation Noise Ratio


1
2
3
4
5
6
7
8
9
10
11
12

// Example 1 2 . 3
// S i g n a l to q u a n t i z a t i o n N o i s e r a t i o
clear ;
clc ;
b =[7 9 11 13 15]; // Given v a l u e s o f b
K =[4 6 8]; // Given v a l u e s o f K
for i =1:5
for j =1:3
SNR (j , i ) =6.02* b ( i ) +16.81 -20* log10 ( K ( j ) ) ;
end
end
disp ( SNR , SNR , A/D = ) ;

83

Chapter 13
Multirate DIgital Signal
Processing Findamentals

Scilab code Exa 13.1 Up sampling operation


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

// Example 1 3 . 1
// Upsampling O p e r a t i o n
clear ;
clc ;
clf () ;
a = gca () ;
figure (0) ;
n =[0:0.1:4.9];
a . x_location = o r i g i n ;
x = sin ( %pi * n ) ;
plot2d3 (n ,x ,2) ;
xtitle ( The s i n e wave , n , s i n ( x ) ) ;
plot (n ,x , r . ) ;
//Up s a m p l i n g
//Up s a m p l i n g v a l u e u s e r i n p u t
figure (1) ;
L = input ( The up s a m p l i n g v a l u e ) ;
a . x_location = o r i g i n ;
84

20 x1 = sin ( %pi * n / L ) ;
21 plot2d3 (n , x1 ,5) ;
22 plot (n , x1 , r . ) ;
23 xtitle ( The s i n e wave , n , s i n ( x /L ) ) ;

Scilab code Exa 13.2 Down sampling operation


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

// Example 1 3 . 2
// Downsampling O p e r a t i o n
clear ;
clc ;
clf () ;
a = gca () ;
figure (0) ;
n =[0:0.1:4.9];
a . x_location = o r i g i n ;
x = sin ( %pi * n ) ;
plot2d3 (n ,x ,2) ;
xtitle ( The s i n e wave , n , s i n ( x ) ) ;
plot (n ,x , r . ) ;
//Down s a m p l i n g
//Down s a m p l i n g u s e r i n p u t
figure (1) ;
M = input ( The down s a m p l i n g f a c t o r ) ;
a . x_location = o r i g i n ;
x1 = sin ( %pi * n * M ) ;
plot2d3 (n , x1 ,1) ;
plot (n , x1 , r . ) ;
xtitle ( The s i n e wave , n , s i n ( x M) ) ;

Scilab code Exa 13.6 Decimator Computation complexity


1

// Example 1 3 . 6
85

2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

// D e c i m a t o r c o m p u t a t i o n a l c o m p l e x i t y
clear ;
clc ;
// no . o f m u l t i p l i c a t i o n s / s e c =Rm
FT = input ( S a m p l i n g F r e q u e n c y ) ;
N = input ( The o r d e r o f t h e FIR Hz ) ;
Rm1 = N * FT ;
disp ( Rm1 , Rm, FIR = ) ;
//M = f a c t o r o f Down s a m p l e r
M = input ( The Down S a m p l i n g f a c t o r ) ;
disp ( Rm1 /M , Rm, FIRDEC = ) ;
K = input ( The o r d e r o f t h e I I R Hz ) ;
Rm2 = (2* K + 1) * FT ;
disp ( Rm2 , Rm, I I R = ) ;
disp ( ( K * FT + (( K +1) * FT / M ) ) , Rm, IIRDEC = ) ;

86

Chapter 14
Applications of Digital Signal
Processing

Scilab code Exa 14.1 Effect of DFT length


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

// Example 1 4 . 1
//EFFECT OF DFT LENGTH ON SPECTRAL ANALYSIS
clear ;
clc ;
N =16;
n =0: N -1;
f1 =0.22;
f2 =0.34;
R = input ( R p o i n t DFT(R E [ 1 6 , 1 2 8 ] ) = ) ;
//
I n p u t f 1 = 64
if R >= N
x =0.5*( sin (2* %pi * f1 *n ) ) + sin ( sin (2* %pi * f2 *n ) )
;
x =[ x , zeros (1 ,R - length ( n ) ) ];
disp (x , t h e s e q u e n c e i s : ) ;
for n =0: R -1
for k =0: R -1
W ( n +1 , k +1) = exp ( -( %i *2* %pi * k / R ) * n ) ;
87

18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35

end
end
X = W *x ;
disp (X , DFT i s , X = )
else
disp ( i n v a l i d c o m p u t a t i o n ) ;
end
m =0: R -1;
clf () ;
figure (0)
a = gca () ;
plot2d3 (m , abs ( X ) ,2) // p l o t t i n g DFT o f s e q u e n c e
plot (m , abs ( X ) , r . )
a . x_location = o r i g i n ;
a . y_location = o r i g i n ;
poly1 = a . children (1) . children (1) ;
poly1 . thickness = 2.5;
xtitle ( o r i g i n a l s e q u e n c e , n , x [ n ] ) ;

Scilab code Exa 14.2 Effect of DFT length


// Example 1 4 . 2
//EFFECT OF DFT LENGTH ON SPECTRAL ANALYSIS
clear ;
clc ;
N =16;
n =0: N -1;
f1 = input ( E n t e r f 1 v a l u e b e t w e e n 0 . 2 8 t o 0 . 3 1 = ) ;
// I n p u t f 1 = 64
8 f2 =0.34;

1
2
3
4
5
6
7

9
10 R = 128 //
11 //DFT o f t h e s e q u e n c e x [ n ]
12
x =0.5*( sin (2* %pi * f1 *n ) ) + sin ((2* %pi * f2 *n ) ) ;
13
x =[ x , zeros (1 ,R - length ( n ) ) ];

88

14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

disp (x , t h e s e q u e n c e i s : ) ;
for n =0: R -1
for k =0: R -1
W ( n +1 , k +1) = exp ( -( %i *2* %pi * k / R ) * n ) ;
end
end
X = W *x ;
disp (X , DFT i s , X = )
// p l o t t i n g DFT o f s e q u e n c e
m =0: R -1;
clf () ;
figure (0)
a = gca () ;
plot2d3 (m , abs ( X ) ,2)
plot (m , abs ( X ) , r . )
a . x_location = o r i g i n ;
a . y_location = o r i g i n ;
poly1 = a . children (1) . children (1) ;
poly1 . thickness = 2.5;
xtitle ( o r i g i n a l s e q u e n c e , n , x [ n ] ) ;

89

You might also like