1 Funded
Book Description
Title: Digital Signal Processing
Author: R. Babu
Publisher: Scitech Publications
Edition: 4
Year: 2010
ISBN: 81-8371-081-7
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
11
2 THE Z TRANSFORM
34
59
79
90
102
141
143
146
1.1
1.2
1.3.a
1.3.b
1.4.a
1.4.d
1.5.a
1.5.c
1.5.d
1.11
1.12
1.13
1.18
1.19
1.32.a
1.37
1.38
1.45
1.57.a
1.61
1.62
1.64.a
1.64.c
2.1
2.2
2.3
2.4
2.5
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
11
13
14
14
15
15
16
18
19
21
21
22
22
23
24
24
26
27
29
29
30
31
31
34
34
35
36
36
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
Exa
2.6
2.7
2.8.a
2.9
2.10
2.11
2.13.a
2.13.b
2.13.c
2.13.d
2.16
2.17
2.19
2.20.a
2.22
2.23
2.34
2.35.a
2.35.b
2.38
2.40
2.41.a
2.41.b
2.41.c
2.45
2.53.a
2.53.b
2.53.c
2.53.d
2.54
2.58
3.1
3.2
3.3
3.4
3.7
3.9
3.11
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
37
37
37
38
38
39
39
40
40
41
41
42
44
44
45
45
46
47
49
50
52
53
53
54
55
55
55
56
56
56
58
59
60
62
62
63
64
65
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
Exa
3.13
3.14
3.15
3.16
3.17
3.18
3.20
3.21
3.23.a
3.23.b
3.23.c
3.23.d
3.23.e
3.23.f
3.24
3.25
3.26
3.27.a
3.27.b
3.30
3.32
3.36
4.3
4.4
4.6
4.8
4.9
4.10
4.11
4.12
4.13
4.14
4.15
4.16.a
4.16.b
4.17
4.18
4.19
Circular Convolution . . . . . . . . . . . . .
Circular Convolution . . . . . . . . . . . . .
Determine Sequence x3 . . . . . . . . . . .
Circular Convolution . . . . . . . . . . . . .
Circular Convolution . . . . . . . . . . . . .
Output Response . . . . . . . . . . . . . . .
Output Response . . . . . . . . . . . . . . .
Linear Convolution . . . . . . . . . . . . . .
N Point DFT Computation . . . . . . . . .
N Point DFT Computation . . . . . . . . .
N Point DFT Computation . . . . . . . . .
N Point DFT Computation . . . . . . . . .
N Point DFT Computation . . . . . . . . .
N Point DFT Computation . . . . . . . . .
DFT of the Sequence . . . . . . . . . . . .
8 Point Circular Convolution . . . . . . . .
Linear Convolution using DFT . . . . . . .
Circular Convolution Computation . . . . .
Circular Convolution Computation . . . . .
Calculate value of N . . . . . . . . . . . . .
Sketch Sequence . . . . . . . . . . . . . . .
Determine IDFT . . . . . . . . . . . . . . .
Shortest Sequence N Computation . . . . .
Twiddle Factor Exponents Calculation . . .
DFT using DIT Algorithm . . . . . . . . .
DFT using DIF Algorithm . . . . . . . . .
8 Point DFT of the Sequence . . . . . . . .
4 Point DFT of the Sequence . . . . . . . .
IDFT of the Sequence using DIT Algorithm
8 Point DFT of the Sequence . . . . . . . .
8 Point DFT of the Sequence . . . . . . . .
DFT using DIT Algorithm . . . . . . . . .
DFT using DIF Algorithm . . . . . . . . .
8 Point DFT using DIT FFT . . . . . . . .
8 Point DFT using DIT FFT . . . . . . . .
IDFT using DIF Algorithm . . . . . . . . .
IDFT using DIT Algorithm . . . . . . . . .
FFT Computation of the Sequence . . . . .
6
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
65
66
67
67
68
69
70
70
71
71
72
72
72
73
73
73
74
75
75
76
76
78
79
80
80
81
81
82
82
82
83
83
84
84
85
85
86
86
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
Exa
4.20
4.21
4.22
4.23
4.24
5.1
5.2
5.4
5.5
5.6
5.7
5.8
5.9
5.10
5.11
5.12
5.13
5.15
5.16
5.17
5.18
5.19
5.29
6.1
6.5
6.6
6.7
6.8
6.9.a
6.9.b
6.10
6.11
6.12
6.13.a
6.13.b
6.14.a
6.14.b
6.15
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
86
87
87
88
88
90
90
91
92
92
93
93
94
94
95
96
96
97
97
98
99
99
100
102
104
104
106
108
110
112
114
116
118
122
124
124
126
127
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
6.16
6.17
6.18.a
6.18.b
6.19
6.20
6.21
6.28
6.29
7.2
7.14
8.9
8.10
9.7.a
9.7.b
9.8.a
9.8.b
11.3
11.5
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
128
128
129
130
131
133
135
137
138
139
139
141
142
143
144
144
145
146
146
List of Figures
1.1
1.2
1.3
1.4
1.5
1.6
1.7
1.8
1.9
1.10
1.11
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
12
13
17
18
20
23
25
26
28
30
32
2.1
2.2
2.3
2.4
2.5
2.6
2.7
2.8
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
42
43
46
48
49
51
54
57
3.1
3.2
3.3
60
63
77
6.1
6.2
6.3
6.4
.
.
.
.
.
.
.
.
9
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
103
105
107
109
6.5
6.6
6.7
6.8
6.9
6.10
6.11
6.12
6.13
6.14
6.15
6.16
6.17
6.18
10
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
111
113
115
117
119
121
123
125
126
129
131
132
134
136
Chapter 1
DISCRETE TIME SIGNALS
AND LINEAR SYSTEMS
Scilab code Exa 1.1 Continuous Time Plot and Discrete Time Plot
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// Example 1 . 1
// S k e t c h t h e c o n t i n u o u s t i m e s i g n a l x ( t ) =2 exp (2 t )
and a l s o i t s d i s c r e t e t i m e e q u i v a l e n t s i g n a l w i t h
a sampling period T = 0.2 sec
clear all ;
clc ;
close ;
t =0:0.01:2;
x1 =2* exp ( -2* t ) ;
subplot (1 ,2 ,1) ;
plot (t , x1 ) ;
xlabel ( t ) ;
ylabel ( x ( t ) ) ;
title ( CONTINUOUS TIME PLOT ) ;
n =0:0.2:2;
x2 =2* exp ( -2* n ) ;
subplot (1 ,2 ,2) ;
11
12
plot2d3 (n , x2 ) ;
xlabel ( n ) ;
ylabel ( x ( n ) ) ;
title ( DISCRETE TIME PLOT ) ;
Scilab code Exa 1.2 Continuous Time Plot and Discrete Time Plot
1
2
// Example 1 . 2
// S k e t c h t h e c o n t i n u o u s t i m e s i g n a l x=s i n ( 7 t )+s i n
( 1 0 t ) and a l s o i t s d i s c r e t e t i m e e q u i v a l e n t
s i g n a l with a sampling p e r i o d T = 0 . 2 s e c
13
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
clear all ;
clc ;
close ;
t =0:0.01:2;
x1 = sin (7* t ) + sin (10* t ) ;
subplot (1 ,2 ,1) ;
plot (t , x1 ) ;
xlabel ( t ) ;
ylabel ( x ( t ) ) ;
title ( CONTINUOUS TIME PLOT ) ;
n =0:0.2:2;
x2 = sin (7* n ) + sin (10* n ) ;
subplot (1 ,2 ,2) ;
plot2d3 (n , x2 ) ;
xlabel ( n ) ;
ylabel ( x ( n ) ) ;
title ( DISCRETE TIME PLOT ) ;
1 // Example 1 . 3 ( b )
2 //MAXIMA SCILAB TOOLBOX REQUIRED FOR THIS PROGRAM
3 // C a l c u l a t e F o l l o w i n g Summations
4 clear all ;
5 clc ;
6 close ;
7 syms n ;
8 X = symsum ( %e ^(2* n ) ,n ,0 , 0) ;
9 // D i s p l a y t h e r e s u l t i n command window
10 disp (X , The V a l u e o f summation comes o u t t o be : ) ;
// Example 1 . 4 ( a )
//MAXIMA SCILAB TOOLBOX REQUIRED FOR THIS PROGRAM
// Find Energy and Power o f Given S i g n a l s
clear all ;
clc ;
close ;
syms n N ;
x =(1/3) ^ n ;
E = symsum ( x ^2 , n ,0 , %inf ) ;
// D i s p l a y t h e r e s u l t i n command window
disp (E , Energy : ) ;
p =(1/(2* N +1) ) * symsum ( x ^2 , n ,0 , N ) ;
P = limit (p ,N , %inf ) ;
disp (P , Power : ) ;
// The Energy i s F i n i t e and Power i s 0 . T h e r e f o r e t h e
g i v e n s i g n a l i s an Energy S i g n a l
// Example 1 . 4 ( d )
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// Example 1 . 5 ( a )
//To D e t e r m i n e Whether Given S i g n a l i s P e r i o d i c o r
not
clear all ;
clc ;
close ;
t =0:0.01:2;
x1 = exp ( %i *6* %pi * t ) ;
subplot (1 ,2 ,1) ;
plot (t , x1 ) ;
xlabel ( t ) ;
ylabel ( x ( t ) ) ;
title ( CONTINUOUS TIME PLOT ) ;
n =0:0.2:2;
16
17
// Example 1 . 5 ( c )
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// Example 1 . 5 ( d )
//To D e t e r m i n e Whether Given S i g n a l i s P e r i o d i c o r
not
clear all ;
clc ;
close ;
t =0:0.01:50;
x1 = cos ( %pi * t /3) + cos (3* %pi * t /4) ;
subplot (1 ,2 ,1) ;
plot (t , x1 ) ;
19
20
10 xlabel ( t ) ;
11 ylabel ( x ( t ) ) ;
12 title ( CONTINUOUS TIME PLOT ) ;
13 n =0:0.2:50;
14 x2 = cos ( %pi * n /3) + cos (3* %pi * n /4) ;
15 subplot (1 ,2 ,2) ;
16 plot2d3 (n , x2 ) ;
17 xlabel ( n ) ;
18 ylabel ( x ( n ) ) ;
19 title ( DISCRETE TIME PLOT ) ;
20 // Hence Given S i g n a l i s P e r i o d i c w i t h N=24
S t a b l e );
// Example 1 . 1 2
// Program t o Compute c o n v o l u t i o n o f g i v e n s e q u e n c e s
// x ( n ) =[3 2 1 2 ] , h ( n ) =[1 2 1 2 ] ;
21
4
5
6
7
8
9
10
clear all ;
clc ;
close ;
x =[3 2 1 2];
h =[1 2 1 2];
y = convol (x , h ) ;
disp ( y ) ;
// Example 1 . 1 3
// Program t o Compute c o n v o l u t i o n o f g i v e n s e q u e n c e s
// x ( n ) =[1 2 1 1 ] , h ( n ) =[1 1 1 1 ] ;
clear all ;
clc ;
close ;
x =[1 2 1 1];
h =[1 -1 1 -1];
y = convol (x , h ) ;
disp ( round ( y ) ) ;
// Example 1 . 1 8
// Program t o Compute C r o s s c o r r e l a t i o n
sequences
// x ( n ) =[1 2 1 1 ] , h ( n ) =[1 1 2 1 ] ;
clear all ;
clc ;
close ;
x =[1 2 1 1];
h =[1 1 2 1];
h1 =[1 2 1 1];
22
of given
^(1) ) +1;
9 b = z ^6+2* z ^(5) +1* z ^(4) ;
10 x = ldiv (a ,b ,5) ;
11 disp (x , x ( n )= ) ;
23
// Example 1 . 3 2
// Program t o P l o t Magnitude and Phase R e s p o n c e
clear all ;
clc ;
close ;
w = - %pi :0.01: %pi ;
H =1+2* cos ( w ) +2* cos (2* w ) ;
// c a l u c u l a t i o n o f Phase and Magnitude o f H
[ phase_H , m ]= phasemag ( H ) ;
Hm = abs ( H ) ;
a = gca () ;
subplot (2 ,1 ,1) ;
a . y_location = o r i g i n ;
plot2d ( w / %pi , Hm ) ;
xlabel ( F r e q u e n c y i n R a d i a n s )
ylabel ( a b s (Hm) ) ;
title ( MAGNITUDE RESPONSE ) ;
subplot (2 ,1 ,2) ;
a = gca () ;
a . x_location = o r i g i n ;
a . y_location = o r i g i n ;
plot2d ( w /(2* %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 RESPONSE ) ;
24
// Example 1 . 3 7
// Program t o P l o t Magnitude and Phase R e s p o n c e
// y ( n ) =1/2[ x ( n )+x ( n2) ]
clear all ;
clc ;
close ;
w =0:0.01: %pi ;
H =(1+ cos (2* w ) - %i * sin (2* w ) ) /2;
// c a l u c u l a t i o n o f Phase and Magnitude o f H
[ phase_H , m ]= phasemag ( H ) ;
Hm = abs ( H ) ;
a = gca () ;
subplot (2 ,1 ,1) ;
a . y_location = o r i g i n ;
plot2d ( w / %pi , Hm ) ;
xlabel ( F r e q u e n c y i n R a d i a n s )
ylabel ( a b s (Hm) ) ;
title ( MAGNITUDE RESPONSE ) ;
subplot (2 ,1 ,2) ;
a = gca () ;
a . x_location = o r i g i n ;
a . y_location = o r i g i n ;
plot2d ( w /(2* %pi ) , phase_H ) ;
25
xlabel ( F r e q u e n c y i n R a d i a n s ) ;
ylabel ( <(H) ) ;
title ( PHASE RESPONSE ) ;
// Example 1 . 3 8
// Program t o P l o t Magnitude and Phase R e s p o n c e
// 0 . 5 d e l t a ( n )+d e l t a ( n1) +0.5 d e l t a ( n2)
clear all ;
clc ;
close ;
w = - %pi :0.01: %pi ;
H =0.5+ exp ( - %i * w ) +0.5* exp ( - %i * w ) ;
// c a l u c u l a t i o n o f Phase and Magnitude o f H
[ phase_H , m ]= phasemag ( H ) ;
Hm = abs ( H ) ;
a = gca () ;
subplot (2 ,1 ,1) ;
26
14
15
16
17
18
19
20
21
22
23
24
25
26
a . y_location = o r i g i n ;
plot2d ( w / %pi , Hm ) ;
xlabel ( F r e q u e n c y i n R a d i a n s )
ylabel ( a b s (Hm) ) ;
title ( MAGNITUDE RESPONSE ) ;
subplot (2 ,1 ,2) ;
a = gca () ;
a . x_location = o r i g i n ;
a . y_location = o r i g i n ;
plot2d ( w /(2* %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 RESPONSE ) ;
// Example 1 . 4 5
//
clear all ;
clc ;
close ;
t =0:0.01:10;
x =2* cos (5* t ) + cos (300* t ) ;
x1 =2* cos (5* t ) ;
b =[0.05 0.05];
a =[1 -0.9];
y = filter (b ,a , x ) ;
subplot (2 ,1 ,1) ;
plot (t , x ) ;
xlabel ( Time i n S e c ) ;
ylabel ( A m p l i t u d e ) ;
subplot (2 ,1 ,2) ;
plot (t , y ) ;
27
28
18
19
20
subplot (2 ,1 ,2) ;
plot (t , x1 , : ) ;
title ( x : SIGNAL WITHOUT NOISE
;
21 xlabel ( Time i n S e c ) ;
22 ylabel ( A m p l i t u d e ) ;
// Example 1 . 5 7 ( a )
// Program t o Compute d i s c r e t e c o n v o l u t i o n o f g i v e n
sequences
// x ( n ) =[1 2 1 1 ] , h ( n ) =[1 0 1 1 ] ;
clear all ;
clc ;
close ;
x =[1 2 -1 1];
h =[1 0 1 1];
y = convol (x , h ) ;
disp ( round ( y ) ) ;
29
disp (X , The F o u r i e r T r a n s f o r m d o e s n o t e x i t a s x ( n )
i s n o t a b s o l u t e l y summable and a p p r o a c h e s
i n f i n i t y i . e . );
((0.8) ^ n * %e ^( - %i * w * n ) ,n ,0 , %inf )
// D i s p l a y t h e r e s u l t i n command window
disp (X , The F o u r i e r T r a n s f o r m comes o u t t o be : ) ;
30
// Example 1 . 6 4 ( a )
// Program t o C a l c u l a t e P l o t Magnitude and Phase
Responce
clear all ;
clc ;
close ;
w =0:0.01: %pi ;
H =1/(1 -0.5* %e ^( - %i * w ) ) ;
// c a l u c u l a t i o n o f Phase and Magnitude o f H
[ phase_H , m ]= phasemag ( H ) ;
Hm = abs ( H ) ;
a = gca () ;
subplot (2 ,1 ,1) ;
a . y_location = o r i g i n ;
plot2d ( w / %pi , Hm ) ;
xlabel ( F r e q u e n c y i n R a d i a n s )
ylabel ( a b s (Hm) ) ;
title ( MAGNITUDE RESPONSE ) ;
subplot (2 ,1 ,2) ;
a = gca () ;
a . x_location = o r i g i n ;
a . y_location = o r i g i n ;
plot2d ( w /(2* %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 RESPONSE ) ;
32
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 1 . 6 4 ( c )
// Program t o C a l c u l a t e P l o t Magnitude and Phase
Responce
clear all ;
clc ;
close ;
w =0:0.01: %pi ;
H =1/(1 -0.9* %i * %e ^( - %i * w ) ) ;
// c a l u c u l a t i o n o f Phase and Magnitude o f H
[ phase_H , m ]= phasemag ( H ) ;
Hm = abs ( H ) ;
a = gca () ;
subplot (2 ,1 ,1) ;
a . y_location = o r i g i n ;
plot2d ( w / %pi , Hm ) ;
xlabel ( F r e q u e n c y i n R a d i a n s )
ylabel ( a b s (Hm) ) ;
title ( MAGNITUDE RESPONSE ) ;
subplot (2 ,1 ,2) ;
a = gca () ;
a . x_location = o r i g i n ;
a . y_location = o r i g i n ;
plot2d ( w /(2* %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 RESPONSE ) ;
33
Chapter 2
THE Z TRANSFORM
// Example 2 . 1
//Z t r a n s f o r m o f [ 1 0 3 1 2 ]
clear all ;
clc ;
close ;
function [ za ]= ztransfer ( sequence , n )
z = poly (0 , z , r )
za = sequence *(1/ z ) ^n
endfunction
x1 =[1 0 3 -1 2];
n =0: length ( x1 ) -1;
zz = ztransfer ( x1 , n ) ;
// D i s p l a y t h e r e s u l t i n command window
disp ( zz , Zt r a n s f o r m o f s e q u e n c e i s : ) ;
disp ( ROC i s t h e e n t i r e p l a n e e x c e p t z = 0 ) ;
34
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// Example 2 . 2
//Z t r a n s f o r m o f [ 3 2 1 0 1 ]
clear all ;
clc ;
close ;
function [ za ]= ztransfer ( sequence , n )
z = poly (0 , z , r )
za = sequence *(1/ z ) ^n
endfunction
x1 =[ -3 -2 -1 0 1];
n = -( length ( x1 ) -1) :0;
zz = ztransfer ( x1 , n ) ;
// D i s p l a y t h e r e s u l t i n command window
disp ( zz , Zt r a n s f o r m o f s e q u e n c e i s : ) ;
disp ( ROC i s t h e e n t i r e p l a n e e x c e p t z = % i n f ) ;
// Example 2 . 3
//Z t r a n s f o r m o f [ 2 1 3 2 1 0 2 3 1]
clear all ;
clc ;
close ;
function [ za ]= ztransfer ( sequence , n )
z = poly (0 , z , r )
za = sequence *(1/ z ) ^n
endfunction
x1 =[2 -1 3 2 1 0 2 3 -1];
n = -4:4;
zz = ztransfer ( x1 , n ) ;
// D i s p l a y t h e r e s u l t i n command window
disp ( zz , Zt r a n s f o r m o f s e q u e n c e i s : ) ;
disp ( ROC i s t h e e n t i r e p l a n e e x c e p t z = 0 and z =
%inf );
35
36
// Example 2 . 7
//MAXIMA SCILAB TOOLBOX REQUIRED FOR THIS PROGRAM
//Z t r a n s f o r m o f [ 3 ( 3 n ) 4(2) n ] u ( n )
clear all ;
clc ;
close ;
syms n z ;
x1 =(3) ^( n ) ;
X1 = symsum (3* x1 *( z ^( - n ) ) ,n ,0 , %inf ) ;
x2 =(4) ^( n ) ;
X2 = symsum (4* x2 *( z ^( - n ) ) ,n ,0 , %inf ) ;
X = ( X1 - X2 ) ;
// D i s p l a y t h e r e s u l t i n command window
disp (X , Zt r a n s f o r m o f [ 3 ( 3 n ) 4(2) n ] u ( n )
i s : );
1
2
3
4
5
6
7
8
9
10
11
// Example 2 . 8 ( a )
//MAXIMA SCILAB TOOLBOX REQUIRED FOR THIS PROGRAM
//Z t r a n s f o r m o f c o s (Won )
clc ;
syms Wo n z ;
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 ) /2;
disp (X , X( z )= ) ;
38
6
7
8
9
10
11
// Example 2 . 1 1
//MAXIMA SCILAB TOOLBOX
//Z t r a n s f o r m o f n . a n
clear all ;
clc ;
close ;
syms a n z ;
x =( a ) ^ n ;
X = symsum ( x *( z ^( - n ) ) ,n
Y = diff (X , z ) ;
// D i s p l a y t h e r e s u l t i n
disp (Y , Zt r a n s f o r m o f
,0 , %inf )
command window
n . a n u ( n )
i s : );
39
10 x2 =(1/2) ^( - n ) ;
11 X2 = symsum (5* x2 *( z ^( - n ) ) ,n ,0 , %inf ) ;
12 X = ( X1 - X2 ) ;
13 // D i s p l a y t h e r e s u l t i n command window
14 disp (X , Zt r a n s f o r m o f [ 3 ( 3 n ) 4(2) n ] u ( n )
15 disp ( ROC i s t h e R e g i o n 1/5 < mod ( z ) < 2 ) ;
i s : );
// Example 2 . 1 3 ( b )
//MAXIMA SCILAB TOOLBOX REQUIRED FOR THIS PROGRAM
//Z t r a n s f o r m
clc ;
syms n z k ;
x1 =1;
X1 = symsum ( x1 * z ^( - n ) ,n ,0 ,0) ;
x2 =1;
X2 = symsum ( x2 * z ^( - n ) ,n ,1 ,1) ;
x3 =1;
X3 = symsum ( x3 * z ^( - n ) ,n ,2 ,2) ;
X =0.5* X1 + X2 -1/3* X3 ;
disp (X , X( z )= ) ;
40
// Example 2 . 1 3 ( d )
//MAXIMA SCILAB TOOLBOX REQUIRED FOR THIS PROGRAM
//Z t r a n s f o r m o f ( n + 0 . 5 ) ( ( 1 / 3 ) n ) u ( n )
clear all ;
clc ;
close ;
syms n z ;
x1 =(1/3) ^ n ;
X11 = symsum ( x1 *( z ^( - n ) ) ,n ,0 , %inf )
X1 = diff ( X11 , z ) ;
x2 =(1/3) ^( n ) ;
X2 = symsum (0.5* x2 *( z ^( - n ) ) ,n ,0 , %inf ) ;
X = ( X1 + X2 ) ;
// D i s p l a y t h e r e s u l t i n command window
disp (X , Zt r a n s f o r m o f ( n + 0 . 5 ) ( ( 1 / 3 ) n ) u ( n )
i s : );
// Example 2 . 1 6
//To f i n d i n p u t h ( n )
// a =[1 2 4 1 ] , b = [ 1 ]
clear all ;
clc ;
close ;
z = %z ;
a = z ^3+2*( z ^(2) ) -4*( z ) +1;
b = z ^3;
41
Scilab code Exa 2.17 Pole Zero Plot of the Difference Equation
1 // Example 2 . 1 7
2 //To draw t h e p o l e z e r o p l o t
3 clear all ;
4 clc ;
5 close ;
6 z = %z
7 H1Z =(( z ) *( z -1) ) /(( z -0.25) *( z -0.5) ) ;
8 xset ( window ,1) ;
9 plzr ( H1Z ) ;
42
43
// Example 2 . 1 9
// Program t o P l o t Magnitude and Phase R e s p o n c e
clear all ;
clc ;
close ;
w = - %pi :0.01: %pi ;
H =1/(1 -0.5*( cos ( w ) - %i * sin ( w ) ) ) ;
// c a l u c u l a t i o n o f Phase and Magnitude o f H
[ phase_H , m ]= phasemag ( H ) ;
Hm = abs ( H ) ;
a = gca () ;
subplot (2 ,1 ,1) ;
a . y_location = o r i g i n ;
plot2d ( w / %pi , Hm ) ;
xlabel ( F r e q u e n c y i n R a d i a n s ) ;
ylabel ( a b s (Hm) ) ;
title ( MAGNITUDE RESPONSE ) ;
subplot (2 ,1 ,2) ;
a = gca () ;
a . x_location = o r i g i n ;
a . y_location = o r i g i n ;
plot2d ( w /(2* %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 RESPONSE ) ;
44
6
7
8
9
10
11
close ;
z = %z ;
a =( z +0.5) *( z -1) ;
b = z +0.2;
h = ldiv (b ,a ,4) ;
disp (h , h ( n )= ) ;
// Example 2 . 2 2
//To f i n d i n p u t x ( n )
//X( z ) =1/(2 z ( 2) +2 z ( 1) +1) ;
clear all ;
clc ;
close ;
z = %z ;
a =(2+2* z + z ^2) ;
b = z ^2;
h = ldiv (b ,a ,6) ;
disp (h , F i r s t s i x v a l u e s o f h ( n )= ) ;
// Example 2 . 2 3
//To f i n d i n p u t x ( n )
//X( z ) =1/(12 z ( 1) ) (1 z ( 1) ) 2 ;
clear all ;
clc ;
close ;
z = %z ;
a =( z -2) *( z -1) ^2;
b = z ^3;
h = ldiv (b ,a ,6) ;
45
disp (h , F i r s t s i x v a l u e s o f h ( n )= ) ;
// Example 2 . 3 4
//To p l o t t h e i m p u l s e r e s p o n c e o f t h e s y s t e m
a n a l y i c a l l y and u s i n g s c i l a b
clear all ;
clc ;
close ;
n =0:1:50;
46
hence i t
plot
47
48
plzr ( H1Z ) ;
// Example 2 . 3 5 ( b )
//To p l o t t h e r e s p o n c e o f t h e s y s t e m a n a l y i c a l l y and
using s c i l a b
clear all ;
clc ;
close ;
n =0:1:20;
49
7 x = ones (1 , length ( n ) ) ;
8 b =[0 1];
9 a =[1 -1 -1];
10 yanaly =0.447*(1.618) .^ n -0.447*( -0.618) .^ n ; //
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
Analytical Solution
[ ymat , zf ]= filter (b , a , x ) ;
subplot (3 ,1 ,1) ;
plot2d3 (n , x ) ;
xlabel ( n ) ;
ylabel ( x ( n ) ) ;
title ( INPUT SEQUENCE (STEP FUNCTION) ) ;
subplot (3 ,1 ,2) ;
plot2d3 (n , yanaly ) ;
xlabel ( n ) ;
ylabel ( y ( n ) ) ;
title ( OUTPUT SEQUENCE y a n a l y ) ;
subplot (3 ,1 ,3) ;
plot2d3 (n , ymat , zf ) ;
xlabel ( n ) ;
ylabel ( y ( n ) ) ;
title ( OUTPUT SEQUENCE ymat ) ;
// As t h e A n a l t i c a l P l o t m a t c h e s t h e S c i l a b P l o t
hence i t i s the Responce o f the system
// Example 2 . 3 8
//To p l o t t h e r e s p o n c e o f t h e s y s t e m a n a l y i c a l l y and
using s c i l a b
clear all ;
clc ;
close ;
n =0:1:20;
50
51
7 x=n;
8 b =[0 1 1];
9 a =[1 -0.7 0.12];
10 yanaly =38.89*(0.4) .^ n -26.53*(0.3) .^ n -12.36+4.76* n ; //
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
Analytical Solution
ymat = filter (b ,a , x ) ;
subplot (3 ,1 ,1) ;
plot2d3 (n , x ) ;
xlabel ( n ) ;
ylabel ( x ( n ) ) ;
title ( INPUT SEQUENCE (RAMP FUNCTION) ) ;
subplot (3 ,1 ,2) ;
plot2d3 (n , yanaly ) ;
xlabel ( n ) ;
ylabel ( y ( n ) ) ;
title ( OUTPUT SEQUENCE y a n a l y ) ;
subplot (3 ,1 ,3) ;
plot2d3 (n , ymat ) ;
xlabel ( n ) ;
ylabel ( y ( n ) ) ;
title ( OUTPUT SEQUENCE ymat ) ;
// As t h e A n a l t i c a l P l o t m a t c h e s t h e S c i l a b P l o t
hence i t i s the Responce o f the system
^(1) ) +2;
52
// Example 2 . 4 1 ( a )
//MAXIMA SCILAB TOOLBOX REQUIRED FOR THIS PROGRAM
//Z t r a n s f o r m o f n . ( 1 ) n u ( n )
clear all ;
clc ;
close ;
syms a n z ;
x =( -1) ^ n ;
X = symsum ( x *( z ^( - n ) ) ,n ,0 , %inf )
Y = diff (X , z ) ;
// D i s p l a y t h e r e s u l t i n command window
disp (Y , Zt r a n s f o r m o f n . ( 1 ) n u ( n )
i s : );
// Example 2 . 4 1 ( b )
//MAXIMA SCILAB TOOLBOX REQUIRED FOR THIS PROGRAM
//Z t r a n s f o r m o f n 2 u ( n )
clear all ;
clc ;
close ;
syms n z ;
x =1;
X = symsum ( x *( z ^( - n ) ) ,n ,0 , %inf )
Y = diff ( diff (X , z ) ,z ) ;
// D i s p l a y t h e r e s u l t i n command window
disp (Y , Zt r a n s f o r m o f n 2 u ( n )
i s : );
53
// Example 2 . 4 1 ( c )
//MAXIMA SCILAB TOOLBOX REQUIRED FOR THIS PROGRAM
//Z t r a n s f o r m o f ( 1) n . c o s ( %pi /3 n )
clc ;
syms n z ;
Wo = %pi /3;
x1 = exp ( sqrt ( -1) * Wo * n ) ;
X1 =( -1) ^ n * symsum ( x1 *( z ^ - n ) ,n ,0 , %inf ) ;
x2 = exp ( - sqrt ( -1) * Wo * n ) ;
X2 =( -1) ^ n * symsum ( x2 *( z ^ - n ) ,n ,0 , %inf ) ;
X =( X1 + X2 ) /2;
disp (X , X( z )= ) ;
54
// Example 2 . 5 3 ( a )
//Z t r a n s f o r m o f [ 3 1 2 5 7 0 1 ]
clear all ;
clc ;
close ;
function [ za ]= ztransfer ( sequence , n )
z = poly (0 , z , r )
za = sequence *(1/ z ) ^n
endfunction
x1 =[3 1 2 5 7 0 1];
n = -3:3;
zz = ztransfer ( x1 , n ) ;
// D i s p l a y t h e r e s u l t i n command window
disp ( zz , Zt r a n s f o r m o f s e q u e n c e i s : ) ;
// Example 2 . 5 3 ( b )
//MAXIMA SCILAB TOOLBOX REQUIRED FOR THIS PROGRAM
55
3 //Z t r a n s f o r m o f d e l t a ( n )
4 clc ;
5 syms n z ;
6 x =1;
7 X = symsum ( x * z ^( - n ) ,n ,0 ,0) ;
8 disp (X , X( z )= ) ;
// Example 2 . 5 4
//MAXIMA SCILAB TOOLBOX REQUIRED FOR THIS PROGRAM
//Z t r a n s f o r m o f c o s (Won )
clc ;
syms Wo n z ;
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 ) /2;
disp (X , X( z )= ) ;
57
// Example 2 . 5 8
//To p l o t t h e r e s p o n c e o f t h e s y s t e m a n a l y i c a l l y and
using s c i l a b
clear all ;
clc ;
close ;
n =0:1:20;
x =[1 zeros (1 ,20) ];
b =[1 -0.5];
a =[1 -1 3/16];
yanaly =0.5*(0.75) .^ n +0.5*(0.25) .^ n ; // A n a l y t i c a l
Solution
ymat = filter (b ,a , x ) ;
subplot (3 ,1 ,1) ;
plot2d3 (n , x ) ;
xlabel ( n ) ;
ylabel ( x ( n ) ) ;
title ( INPUT SEQUENCE (IMPULSE FUNCTION) ) ;
subplot (3 ,1 ,2) ;
plot2d3 (n , yanaly ) ;
xlabel ( n ) ;
ylabel ( y ( n ) ) ;
title ( OUTPUT SEQUENCE y a n a l y ) ;
subplot (3 ,1 ,3) ;
plot2d3 (n , ymat ) ;
xlabel ( n ) ;
ylabel ( y ( n ) ) ;
title ( OUTPUT SEQUENCE ymat ) ;
// As t h e A n a l t i c a l P l o t m a t c h e s t h e S c i l a b P l o t
hence i t i s the Responce o f the system
58
Chapter 3
THE DISCRETE FOURIER
TRANSFORM
// Example 3 . 1
// Program t o Compute t h e DFT o f a S e q u e n c e x [ n
]=[1 ,1 ,0 ,0]
// and IDFT o f a S e q u e n c e Y [ k ] = [ 1 , 0 , 1 , 0 ]
clear all ;
clc ;
close ;
x = [1 ,1 ,0 ,0];
//DFT Computation
X = fft ( x , -1) ;
Y = [1 ,0 ,1 ,0];
//IDFT Computation
y = fft ( Y , 1) ;
// D i s p l a y s e q u e n c e X [ k ] and y [ n ] i n command window
disp (X , X [ k ]= ) ;
disp (y , y [ n ]= ) ;
59
// Example 3 . 2
// Program t o Compute t h e DFT o f a S e q u e n c e x [ n ] = 1 ,
0<=n<=2; and 0 o t h e r w i s e
// f o r N=4 and N=8. P l o t Magnitude and p h a s e p l o t s o f
each .
clear all ;
clc ;
close ;
//N=4
x1 = [1 ,1 ,1 ,0];
//DFT Computation
X1 = fft ( x1 , -1) ;
//N=8
x2 = [1 ,1 ,1 ,0 ,0 ,0 ,0 ,0];
//DFT Computation
X2 = fft ( x2 , -1) ;
// D i s p l a y s e q u e n c e X1 [ k ] and X2 [ k ] i n command window
disp ( X1 , X1 [ k ]= ) ;
60
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
49
50
51
52
53
disp ( X2 , X2 [ k ]= ) ;
// P l o t s f o r N=4
n1 =0:1:3;
subplot (2 ,2 ,1) ;
a = gca () ;
a . y_location = o r i g i n ;
a . x_location = o r i g i n ;
plot2d3 ( n1 , abs ( X1 ) ,2) ;
poly1 = a . children (1) . children (1) ;
poly1 . thickness =2;
xtitle ( N=4 , k , | X1 ( k ) | ) ;
subplot (2 ,2 ,2) ;
a = gca () ;
a . y_location = o r i g i n ;
a . x_location = o r i g i n ;
plot2d3 ( n1 , atan ( imag ( X1 ) , real ( X1 ) ) ,5) ;
poly1 = a . children (1) . children (1) ;
poly1 . thickness =2;
xtitle ( N=4 , k , <X1 ( k ) ) ;
// P l o t s f o r N=8
n2 =0:1:7;
subplot (2 ,2 ,3) ;
a = gca () ;
a . y_location = o r i g i n ;
a . x_location = o r i g i n ;
plot2d3 ( n2 , abs ( X2 ) ,2) ;
poly1 = a . children (1) . children (1) ;
poly1 . thickness =2;
xtitle ( N=8 , k , | X2 ( k ) | ) ;
subplot (2 ,2 ,4) ;
a = gca () ;
a . y_location = o r i g i n ;
a . x_location = o r i g i n ;
plot2d3 ( n2 , atan ( imag ( X2 ) , real ( X2 ) ) ,5) ;
poly1 = a . children (1) . children (1) ;
poly1 . thickness =2;
xtitle ( N=8 , k , <X2 ( k ) ) ;
61
// Example 3 . 3
// Program t o Compute t h e 8 p o i n t DFT o f t h e S e q u e n c e
x [ n]=[1 ,1 ,1 ,1 ,1 ,1 ,0 ,0]
clear all ;
clc ;
close ;
x = [1 ,1 ,1 ,1 ,1 ,1 ,0 ,0];
//DFT Computation
X = fft ( x , -1) ;
// D i s p l a y s e q u e n c e X [ k ] i n command window
disp (X , X [ k ]= ) ;
// Example 3 . 4
// Program t o Compute t h e IDFT o f t h e S e q u e n c e X [ k
]=[5 ,0 ,1 j ,0 ,1 ,0 ,1+ j , 0 ]
clear all ;
clc ;
close ;
j = sqrt ( -1) ;
X = [5 ,0 ,1 -j ,0 ,1 ,0 ,1+ j ,0]
//IDFT Computation
x = fft ( X , 1) ;
// D i s p l a y s e q u e n c e s x [ n ] i n command window
disp (x , x [ n ]= ) ;
62
// Example 3 . 7
// Program t o Compute c i r c u l a r c o n v o l u t i o n o f
following sequences
// x [ n ] = [ 1 , 2 , 2 , 1 , 0 ]
//Y [ k ]= exp ( j 4 p i k / 5 ) . X [ k ]
clear all ;
clc ;
close ;
x =[1 ,2 ,2 ,1 ,0];
X = fft (x , -1) ;
k =0:1:4;
j = sqrt ( -1) ;
pi =22/7;
63
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
10
11
12
// Example 3 . 9
// Program t o r e m a i n i n g s a m p l e s o f t h e s e q u e n c e
//X( 0 ) =12 ,X( 1 )=1+j 3 , X( 2 ) =3+j 4 , X( 3 ) =1j 5 , X( 4 )=2+j 2 ,
X( 5 ) =6+j 3 , X( 6 ) =2j 3 , X( 7 ) =10
clear all ;
clc ;
close ;
j = sqrt ( -1) ;
z =1;
X (0+ z ) =12 , X (1+ z ) = -1+ j *3 , X (2+ z ) =3+ j *4 , X (3+ z ) =1 - j *5 , X
(4+ z ) = -2+ j *2 , X (5+ z ) =6+ j *3 , X (6+ z ) = -2 - j *3 , X (7+ z )
=10;
for a =9:1:14 do X ( a ) = conj ( X (16 - a ) ) , end ;
// D i s p l a y t h e c o m p l e t e s e q u e n c e X [ k ] i n command
window
disp (X , X [ k ]= ) ;
64
// Example 3 . 1 1
// Program t o Compute t h e 8 p o i n t DFT o f t h e
following sequences
// x1 [ n ] = [ 1 , 0 , 0 , 0 , 0 , 1 , 1 , 1 ]
// x2 [ n ] = [ 0 , 0 , 1 , 1 , 1 , 1 , 0 , 0 ]
clear all ;
clc ;
close ;
x1 =[1 ,0 ,0 ,0 ,0 ,1 ,1 ,1];
x2 =[0 ,0 ,1 ,1 ,1 ,1 ,0 ,0];
//DFT Computation
X1 = fft ( x1 , -1) ;
X2 = fft ( x2 , -1) ;
// D i s p l a y s e q u e n c e s X1 [ k ] and X2 [ k ] i n command
window
disp ( X1 , X1 [ k ]= ) ;
disp ( X2 , X2 [ k ]= ) ;
// Example 3 . 1 3
// Program t o Compute c i r c u l a r c o n v o l u t i o n o f
following sequences
// x1 [ n ] = [ 1 , 1 , 2 , 3 , 1 ]
// x2 [ n ] = [ 1 , 2 , 3 ]
clear all ;
clc ;
close ;
x1 =[1 , -1 , -2 ,3 , -1];
x2 =[1 ,2 ,3];
65
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// Loop f o r z e r o p a d d i n g t h e s m a l l e r s e q u e n c e o u t o f
t h e two
n1 = length ( x1 ) ;
n2 = length ( x2 ) ;
n3 = n2 - n1 ;
if ( n3 >=0) then
x1 =[ x1 , zeros (1 , n3 ) ];
else
x2 =[ x2 , zeros (1 , - n3 ) ];
end
//DFT Computation
X1 = fft ( x1 , -1) ;
X2 = fft ( x2 , -1) ;
Y = X1 .* X2 ;
//IDFT Computation
y = fft (Y ,1) ;
// D i s p l a y s e q u e n c e y [ n ] i n command window
disp (y , y [ n ]= ) ;
// Example 3 . 1 4
// Program t o Compute c i r c u l a r c o n v o l u t i o n o f
following sequences
// x1 [ n ] = [ 1 , 2 , 2 , 1 ]
// x2 [ n ] = [ 1 , 2 , 3 , 1 ]
clear all ;
clc ;
close ;
x1 =[1 ,2 ,2 ,1];
x2 =[1 ,2 ,3 ,1];
//DFT Computation
X1 = fft ( x1 , -1) ;
X2 = fft ( x2 , -1) ;
Y = X1 .* X2 ;
66
14 //IDFT Computation
15 y = fft (Y ,1) ;
16 // D i s p l a y s e q u e n c e y [ n ]
17 disp (y , y [ n ]= ) ;
i n command window
// Example 3 . 1 5
// Program t o Compute x3 [ n ] where X3 [ k ]=X1 [ k ] . X2 [ k ]
// x1 [ n ] = [ 1 , 2 , 3 , 4 ]
// x2 [ n ] = [ 1 , 1 , 2 , 2 ]
clear all ;
clc ;
close ;
x1 =[1 ,2 ,3 ,4];
x2 =[1 ,1 ,2 ,2];
//DFT Computation
X1 = fft ( x1 , -1) ;
X2 = fft ( x2 , -1) ;
X3 = X1 .* X2 ;
//IDFT Computation
x3 = fft ( X3 ,1) ;
// D i s p l a y s e q u e n c e x3 [ n ] i n command window
disp ( x3 , x3 [ n ]= ) ;
67
6
7
8
9
10
11
12
13
14
15
16
17
clc ;
close ;
x1 =[1 ,1 ,2 ,1];
x2 =[1 ,2 ,3 ,4];
//DFT Computation
X1 = fft ( x1 , -1) ;
X2 = fft ( x2 , -1) ;
X3 = X1 .* X2 ;
//IDFT Computation
x3 = fft ( X3 ,1) ;
// D i s p l a y s e q u e n c e x3 [ n ] i n command window
disp ( x3 , x3 [ n ]= ) ;
// Example 3 . 1 7
// Program t o Compute y [ n ] where Y [ k ]=X1 [ k ] . X2 [ k ]
// x1 [ n ] = [ 0 , 1 , 2 , 3 , 4 ]
// x2 [ n ] = [ 0 , 1 , 0 , 0 , 0 ]
clear all ;
clc ;
close ;
x1 =[0 ,1 ,2 ,3 ,4];
x2 =[0 ,1 ,0 ,0 ,0];
//DFT Computation
X1 = fft ( x1 , -1) ;
X2 = fft ( x2 , -1) ;
Y = X1 .* X2 ;
//IDFT Computation
y = round ( fft (Y ,1) ) ;
// D i s p l a y s e q u e n c e y [ n ] i n command window
disp (y , y [ n ]= ) ;
68
// Example 3 . 1 8
// Program t o Compute o u t p u t r e s p o n c e o f f o l l o w i n g
sequences
// x [ n ] = [ 1 , 2 , 3 , 1 ]
// h [ n ] = [ 1 , 1 , 1 ]
// ( 1 ) L i n e a r C o n v o l u t i o n
// ( 2 ) C i r c u l a r C o n v o l u t i o n
// ( 3 ) C i r c u l a r C o n v o l u t i o n w i t h z e r o p a d d i n g
clear all ;
clc ;
close ;
x =[1 ,2 ,3 ,1];
h =[1 ,1 ,1];
// ( 1 ) L i n e a r C o n v o l u t i o n Computation
ylinear = convol (x , h ) ;
// D i s p l a y L i n e a r C o n v o l u t e d S e q u e n c e y [ n ] i n command
window
disp ( ylinear , y l i n e a r [ n ]= ) ;
// ( 2 ) C i r c u l a r C o n v o l u t i o n Computation
//Now z e r o p a d d i n g i n h [ n ] s e q u e n c e t o make l e n g t h
o f x [ n ] and h [ n ] e q u a l
h1 =[ h , zeros (1 ,1) ];
//Now P e r f o r m i n g C i r c u l a r C o n v o l u t i o n by DFT method
X = fft (x , -1) ;
H = fft ( h1 , -1) ;
Y = X .* H ;
ycircular = fft (Y ,1) ;
// D i s p l a y C i r c u l a r C o n v o l u t e d S e q u e n c e y [ n ] i n
command window
disp ( ycircular , y c i r c u l a r [ n ]= ) ;
// ( 3 ) C i r c u l a r C o n v o l u t i o n Computation w i t h z e r o
Padding
x2 =[ x , zeros (1 ,2) ];
h2 =[ h , zeros (1 ,3) ];
//Now P e r f o r m i n g C i r c u l a r C o n v o l u t i o n by DFT method
X2 = fft ( x2 , -1) ;
69
32 H2 = fft ( h2 , -1) ;
33 Y2 = X2 .* H2 ;
34 ycircularp = fft ( Y2 ,1) ;
35 // D i s p l a y C i r c u l a r C o n v o l u t e d S e q u e n c e w i t h z e r o
// Example 3 . 2 0
// Program t o Compute L i n e a r C o n v o l u t i o n o f f o l l o w i n g
sequences
// x [ n ] = [ 3 , 1 , 0 , 1 , 3 , 2 , 0 , 1 , 2 , 1 ]
// h [ n ] = [ 1 , 1 , 1 ]
clear all ;
clc ;
close ;
x =[3 , -1 ,0 ,1 ,3 ,2 ,0 ,1 ,2 ,1];
h =[1 ,1 ,1];
// L i n e a r C o n v o l u t i o n Computation
y = convol (x , h ) ;
// D i s p l a y S e q u e n c e y [ n ] i n command window
disp (y , y [ n ]= ) ;
// Example 3 . 2 1
// Program t o Compute L i n e a r C o n v o l u t i o n o f f o l l o w i n g
sequences
// x [ n ] = [ 1 , 2 , 1 , 2 , 3 , 2 , 3 , 1 , 1 , 1 , 2 , 1 ]
// h [ n ] = [ 1 , 2 ]
clear all ;
clc ;
70
7 close ;
8 x =[1 ,2 , -1 ,2 ,3 , -2 , -3 , -1 ,1 ,1 ,2 , -1];
9 h =[1 ,2];
10 // L i n e a r C o n v o l u t i o n Computation
11 y = convol (x , h ) ;
12 // D i s p l a y S e q u e n c e y [ n ] i n command window
13 disp (y , y [ n ]= ) ;
71
72
// Example 3 . 2 4
// Program t o Compute t h e DFT o f t h e S e q u e n c e x [ n
]=( 1) n , f o r N=4
clear all ;
clc ;
close ;
N =4;
n =0:1: N -1;
x =( -1) ^ n ;
//DFT Computation
X = fft (x , -1) ;
// D i s p l a y S e q u e n c e X [ k ] i n command window
disp (X , X [ k ]= ) ;
73
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// Example 3 . 2 5
// Program t o Compute t h e 8 p o i n t C i r c u l a r
Convolution of the Sequences
// x1 [ n ] = [ 1 , 1 , 1 , 1 , 0 , 0 , 0 , 0 ]
// x2 [ n ]= s i n ( 3 p i n / 8 )
clear all ;
clc ;
close ;
x1 =[1 ,1 ,1 ,1 ,0 ,0 ,0 ,0];
n =0:1:7;
pi =22/7;
x2 = sin (3* pi * n /8) ;
//DFT Computation
X1 = fft ( x1 , -1) ;
X2 = fft ( x2 , -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
Y = X1 .* X2 ;
//IDFT Computation
y = fft (Y ,1) ;
// D i s p l a y s e q u e n c e y [ n ] i n command window
disp (y , y [ n ]= ) ;
// Example 3 . 2 6
// Program t o Compute t h e L i n e a r C o n v o l u t i o n o f t h e
f o l l o w i n g Sequences
// x [ n ] = [ 1 , 1 , 1 ]
// h [ n ] = [ 2 , 2 , 1 ]
clear all ;
clc ;
close ;
x =[1 , -1 ,1];
h =[2 ,2 ,1];
// C o n v o l u t i o n Computation
74
11 y = convol (x , h ) ;
12 // D i s p l a y s e q u e n c e y [ n ]
13 disp (y , y [ n ]= ) ;
i n command window
// Example 3 . 2 7 ( a )
// Program t o Compute t h e C o n v o l u t i o n o f t h e
f o l l o w i n g Sequences
// x1 [ n ] = [ 1 , 1 , 1 ]
// x2 [ n ] = [ 2 , 1 , 2 ]
clear all ;
clc ;
close ;
x1 =[1 ,1 ,1];
x2 =[2 , -1 ,2];
// C o n v o l u t i o n Computation
X1 = fft ( x1 , -1) ;
X2 = fft ( x2 , -1) ;
Y = X1 .* X2 ;
y = fft (Y ,1) ;
// D i s p l a y S e q u e n c e y [ n ] i n command window
disp (y , y [ n ]= ) ;
// Example 3 . 2 7 ( b )
// Program t o Compute t h e C o n v o l u t i o n o f t h e
f o l l o w i n g Sequences
// x1 [ n ] = [ 1 , 1 , 1 , 1 , 0 ]
// x2 [ n ] = [ 1 , 0 , 1 , 0 , 1 ]
clear all ;
clc ;
75
7
8
9
10
11
12
13
14
15
16
close ;
x1 =[1 ,1 , -1 , -1 ,0];
x2 =[1 ,0 , -1 ,0 ,1];
// C o n v o l u t i o n Computation
X1 = fft ( x1 , -1) ;
X2 = fft ( x2 , -1) ;
Y = X1 .* X2 ;
y = fft (Y ,1) ;
// D i s p l a y S e q u e n c e y [ n ] i n command window
disp (y , y [ n ]= ) ;
// Example 3 . 3 0
// Program t o C a l c u l a t e N from g i v e n d a t a
// fm =5000Hz
// d f =50Hz
// t =0.5 s e c
clear all ;
clc ;
close ;
fm =5000 // Hz
df =50
// Hz
t =0.5
// s e c
N1 =2* fm / df ;
N =2;
while N <= N1 , N = N *2 , end
// D i s p l a y i n g t h e v a l u e o f N i n command window
disp (N , N= ) ;
// Example 3 . 3 2
// Program t o p l o t t h e r e s u l t o f t h e g i v e n s e q u e n c e
//X [ k ] = [ 1 , 2 , 2 , 1 , 0 , 2 , 1 , 2 ]
// y [ n ]= x [ n / 2 ] f o r n=even , 0 f o r n=odd
clear all ;
clc ;
close ;
X =[1 ,2 ,2 ,1 ,0 ,2 ,1 ,2];
x = fft ( X , 1) ;
y =[ x (1) ,0 , x (2) ,0 , x (3) ,0 , x (4) ,0 , x (5) ,0 , x (6) ,0 , x (7) ,0 ,
x (8) ,0];
Y = fft ( y , -1) ;
// D i s p l a y s e q u e n c e Y [ k ] and i n command window
disp (Y , Y [ k ]= ) ;
// P l o t t i n g t h e s e q u e n c e Y [ k ]
k =0:1:15;
a = gca () ;
a . y_location = o r i g i n ;
a . x_location = o r i g i n ;
plot2d3 (k ,Y ,2) ;
poly1 = a . children (1) . children (1) ;
poly1 . thickness =2;
xtitle ( P l o t o f Y( k ) , k , Y( k ) ) ;
77
// Example 3 . 3 6
// Program t o Compute t h e IDFT o f t h e f o l l o w i n g
Sequence
//X [ k ] = [ 1 2 , 1 . 5 + j 2 . 5 9 8 , 1 . 5 + j 0 . 8 6 6 , 0 , 1 . 5 j 0
.866 , 1.5 j 2 . 5 9 8 ]
clear all ;
clc ;
close ;
j = sqrt ( -1) ;
X =[12 , -1.5+ j *2.598 , -1.5+ j *0.866 ,0 , -1.5 - j *0.866 , -1.5 j *2.598];
//IDFT Computation
x = fft ( X , 1) ;
// D i s p l a y S e q u e n c e x [ n ] i n command window
disp ( round ( x ) , x [ n ]= ) ;
78
Chapter 4
THE FAST FOURIER
TRANSFORM
// Example 4 . 3
// Program t o c a l c u l a t e s h o r t e s t s e q u e n c e N s u c h t h a t
a l g o r i t h m B r u n s // f a s t e r t h a n A
clear all ;
clc ;
close ;
i =0;
N =32; // Given
// C a l c u l a t i o n o f T w i d d l e f a c t o r e x p o n e n t s f o r e a c h
stage
while 1==1
i = i +1;
N =2^ i ;
A = N ^2;
B =5* N * log2 ( N ) ;
if A > B then break ;
end ;
end
disp (N , SHORTEST SEQUENCE N = ) ;
79
// Example 4 . 4
// Program t o c a l c u l a t e T w i d d l e f a c t o r e x p o n e n t s f o r
each s t a g e
clear all ;
clc ;
close ;
N =32; // Given
// C a l c u l a t i o n o f T w i d d l e f a c t o r e x p o n e n t s f o r e a c h
stage
for m =1:5
disp (m , S t a g e : m = ) ;
disp ( k = ) ;
for t =0:(2^( m -1) -1)
k = N * t /2^ m ;
disp ( k ) ;
end
end
// Example 4 . 6
// Program t o f i n d t h e DFT o f a S e q u e n c e x [ n
]=[1 ,2 ,3 ,4 ,4 ,3 ,2 ,1]
// u s i n g DIT A l g o r i t h m .
clear all ;
clc ;
close ;
x = [1 ,2 ,3 ,4 ,4 ,3 ,2 ,1];
//FFT Computation
80
9 X = fft ( x , -1) ;
10 disp (X , X( z ) = ) ;
// Example 4 . 8
// Program t o f i n d t h e DFT o f a S e q u e n c e x [ n
]=[1 ,2 ,3 ,4 ,4 ,3 ,2 ,1]
// u s i n g DIF A l g o r i t h m .
clear all ;
clc ;
close ;
x = [1 ,2 ,3 ,4 ,4 ,3 ,2 ,1];
//FFT Computation
X = fft ( x , -1) ;
disp (X , X( z ) = ) ;
// Example 4 . 9
// Program t o f i n d t h e 8 p o i n t DFT o f a S e q u e n c e x [ n
] = 1 , 0<=n<=7
// u s i n g DIT , DIF A l g o r i t h m .
clear all ;
clc ;
close ;
x = [1 ,1 ,1 ,1 ,1 ,1 ,1 ,1];
//FFT Computation
X = fft ( x , -1) ;
disp (X , X( z ) = ) ;
81
// Example 4 . 1 0
// Program t o Compute t h e 4 p o i n t DFT o f a S e q u e n c e x
[n]=[0 ,1 ,2 ,3]
// u s i n g DITDIF A l g o r i t h m .
clear all ;
clc ;
close ;
x = [0 ,1 ,2 ,3];
//FFT Computation
X = fft ( x , -1) ;
disp (X , X( z ) = ) ;
Scilab code Exa 4.11 IDFT of the Sequence using DIT Algorithm
1
2
3
4
5
6
7
8
9
10
11
// Example 4 . 1 1
// Program t o Compute t h e IDFT o f a S e q u e n c e u s i n g
DIT A l g o r i t h m .
//X [ k ] = [7 , 0.707 j 0 . 7 0 7 , j , 0 . 7 0 7 j 0 . 7 0 7 , 1 , 0 . 7 0 7 + j 0
. 7 0 7 , j , 0.707+ j 0 . 7 0 7 ]
clear all ;
clc ;
close ;
j = sqrt ( -1) ;
X = [7 , -0.707 - j *0.707 , - j ,0.707 - j *0.707 ,1 ,0.707+ j
*0.707 , j , -0.707+ j *0.707];
// I n v e r s e FFT Computation
x = fft ( X , 1) ;
disp (x , x ( n ) = ) ;
1
2
3
4
5
6
7
8
9
10
// Example 4 . 1 2
// Program t o Compute t h e 8 p o i n t DFT o f a S e q u e n c e
// x [ n ] = [ 0 . 5 , 0 . 5 , 0 . 5 , 0 . 5 , 0 , 0 , 0 , 0 ] u s i n g r a d i x 2 DIT
Algorithm .
clear all ;
clc ;
close ;
x =[0.5 ,0.5 ,0.5 ,0.5 ,0 ,0 ,0 ,0];
//FFT Computation
X = fft ( x , -1) ;
disp (X , X( z ) = ) ;
// Example 4 . 1 3
// Program t o Compute t h e 8 p o i n t DFT o f a S e q u e n c e
// x [ n ] = [ 0 . 5 , 0 . 5 , 0 . 5 , 0 . 5 , 0 , 0 , 0 , 0 ] u s i n g r a d i x 2 DIF
Algorithm .
clear all ;
clc ;
close ;
x =[0.5 ,0.5 ,0.5 ,0.5 ,0 ,0 ,0 ,0];
//FFT Computation
X = fft ( x , -1) ;
disp (X , X( z ) = ) ;
1
2
83
5 clc ;
6 close ;
7 x =[1 , -1 ,1 , -1];
8 //FFT Computation
9 X = fft ( x , -1) ;
10 disp (X , X( z ) = ) ;
// Example 4 . 1 5
// Program t o Compute t h e 4 p o i n t DFT o f a S e q u e n c e x
[n]=[1 ,0 ,0 ,1]
// u s i n g DIF A l g o r i t h m .
clear all ;
clc ;
close ;
x =[1 ,0 ,0 ,1];
//FFT Computation
X = fft ( x , -1) ;
disp (X , X( z ) = ) ;
// Example 4 . 1 6 ( a )
// Program t o E v a l u a t e and Compare t h e 8 p o i n t DFT o f
the given Sequence
// x1 [ n ] = 1 , 3<=n<=3 u s i n g DITFFT A l g o r i t h m .
clear all ;
clc ;
close ;
x1 =[1 ,1 ,1 ,1 ,0 ,1 ,1 ,1];
//FFT Computation
X1 = fft ( x1 , -1) ;
84
10
disp ( X1 , X1 ( k ) = ) ;
// Example 4 . 1 6 ( b )
// Program t o E v a l u a t e and Compare t h e 8 p o i n t DFT o f
the given Sequence
// x2 [ n ] = 1 , 0<=n<=6 u s i n g DITFFT A l g o r i t h m .
clear all ;
clc ;
close ;
x2 =[1 ,1 ,1 ,1 ,1 ,1 ,1 ,0];
//FFT Computation
X2 = fft ( x2 , -1) ;
disp ( X2 , X2 ( k ) = ) ;
// Example 4 . 1 7
// Program t o f i n d t h e IDFT o f t h e S e q u e n c e u s i n g DIF
Algorithm .
//X [ k ]= [4 ,1 j 2 . 4 1 4 , 0 , 1 j 0 . 4 1 4 , 0 , 1 + j 0 . 4 1 4 , 0 , 1 + j 2
.414]
clear all ;
clc ;
close ;
j = sqrt ( -1) ;
X = [4 ,1 - j *2.414 ,0 ,1 - j *0.414 ,0 ,1+ j *0.414 ,0 ,1+ j
*2.414];
// I n v e r s e FFT Computation
x = fft ( X , 1) ;
disp (x , x ( n ) = ) ;
85
// Example 4 . 1 8
// Program t o f i n d t h e IDFT o f t h e S e q u e n c e X [ k ]=
[10 , 2+ j 2 ,2,2 j 2 ]
// u s i n g DIT A l g o r i t h m .
clear all ;
clc ;
close ;
j = sqrt ( -1) ;
X = [10 , -2+ j *2 , -2 , -2 - j *2];
// I n v e r s e FFT Computation
x = fft ( X , 1) ;
disp (x , x ( n ) = ) ;
// Example 4 . 1 9
// Program t o Compute t h e FFT o f g i v e n S e q u e n c e x [ n
]=[1 ,0 ,0 ,0 ,0 ,0 ,0 ,0].
clear all ;
clc ;
close ;
x = [1 ,0 ,0 ,0 ,0 ,0 ,0 ,0];
//FFT Computation
X = fft ( x , -1) ;
disp (X , X( z ) = ) ;
1
2
3
4
5
6
7
8
9
10
// Example 4 . 2 0
// Program t o Compute t h e 8 p o i n t DFT o f g i v e n
Sequence
// x [ n ] = [ 2 , 2 , 2 , 2 , 1 , 1 , 1 , 1 ] u s i n g DIT , r a d i x 2 ,FFT
Algorithm .
clear all ;
clc ;
close ;
x = [2 ,2 ,2 ,2 ,1 ,1 ,1 ,1];
//FFT Computation
X = fft ( x , -1) ;
disp (X , X( z ) = ) ;
// Example 4 . 2 1
// Program t o Compute t h e DFT o f g i v e n S e q u e n c e
// x [ n ] = [ 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 ] u s i n g DITFFT A l g o r i t h m
.
clear all ;
clc ;
close ;
x = [1 , -1 , -1 , -1 ,1 ,1 ,1 , -1];
//FFT Computation
X = fft ( x , -1) ;
disp (X , X( z ) = ) ;
// Example 4 . 2 2
// Program t o Compute t h e DFT o f g i v e n S e q u e n c e
// x [ n ]=2 n and N=8 u s i n g DITFFT A l g o r i t h m .
clear all ;
87
5
6
7
8
9
10
11
12
clc ;
close ;
N =8;
n =0:1: N -1;
x =2^ n ;
//FFT Computation
X = fft ( x , -1) ;
disp (X , X( z ) = ) ;
// Example 4 . 2 3
// Program t o Compute t h e DFT o f g i v e n S e q u e n c e
// x [ n ]= c o s ( n p i / 2 ) , and N=4 u s i n g DIFFFT A l g o r i t h m .
clear all ;
clc ;
close ;
N =4;
pi =22/7;
n =0:1: N -1;
x = cos ( n * pi /2) ;
//FFT Computation
X = fft ( x , -1) ;
disp (X , X( z ) = ) ;
1
2
88
5 clc ;
6 close ;
7 x = [0 ,1 ,2 ,3 ,4 ,5 ,6 ,7];
8 //FFT Computation
9 X = fft ( x , -1) ;
10 disp (X , X( z ) = ) ;
89
Chapter 5
INFINITE IMPULSE
RESPONSE FILTERS
1
2
3
4
5
6
7
8
9
10
// Example 5 . 2
90
2
3
4
5
6
7
8
9
10
11
12
13
// Example 5 . 4
//To D e s i g n an Analog B u t t e r w o r t h F i l t e r
clear all ;
clc ;
close ;
ap =2; // db
as =10; // db
op =20; // r a d / s e c
os =30; // r a d / s e c
N = log ( sqrt ((10^(0.1* as ) -1) /(10^(0.1* ap ) -1) ) ) / log ( os /
op ) ;
disp ( ceil ( N ) , Order o f t h e f i l t e r , N = ) ;
s = %s ;
HS =1/(( s ^2+0.76537* s +1) *( s ^2+1.8477* s +1) ) ; // T r a n s f e r
F u n c t i o n f o r N=4
oc = op /(10^(0.1* ap ) -1) ^(1/(2* ceil ( N ) ) ) ;
HS1 = horner ( HS , s / oc ) ;
disp ( HS1 , N o r m a l i z e d T r a n s f e r F u n c t i o n , H( s ) = ) ;
91
// Example 5 . 6
//To Find o u t t h e o r d e r o f t h e F i l t e r u s i n g
Chebyshev A p p r o x i m a t i o n
clear all ;
clc ;
close ;
ap =3; // db
92
as =16; // db
fp =1000; // Hz
fs =2000; // Hz
op =2* %pi * fp ;
os =2* %pi * fs ;
N = acosh ( sqrt ((10^(0.1* as ) -1) /(10^(0.1* ap ) -1) ) ) / acosh
( os / op ) ;
13 disp ( ceil ( N ) , Order o f t h e f i l t e r , N = ) ;
7
8
9
10
11
12
// Example 5 . 7
//To D e s i g n an a n a l o g Chebyshev F i l t e r w i t h Given
Specifications
clear all ;
clc ;
close ;
os =2;
op =1;
ap =3; // db
as =16; // db
e1 =1/ sqrt (2) ;
l1 =0.1;
epsilon = sqrt (1/( e1 ^2) -1) ;
lambda = sqrt (1/( l1 ^2) -1) ;
N = acosh ( lambda / epsilon ) / acosh ( os / op ) ;
disp ( ceil ( N ) , Order o f t h e f i l t e r , N = ) ;
Scilab code Exa 5.8 Order of Type 1 Low Pass Chebyshev Filter
1
2
// Example 5 . 8
//To Find o u t t h e o r d e r o f t h e p o l e s o f t h e Type 1
Lowpass Chebyshev F i l t e r
93
3
4
5
6
7
8
9
10
clear all ;
clc ;
close ;
ap =1; //dB
as =40; //dB
op =1000* %pi ;
os =2000* %pi ;
N = acosh ( sqrt ((10^(0.1* as ) -1) /(10^(0.1* ap ) -1) ) ) / acosh
( os / op ) ;
11 disp ( ceil ( N ) , Order o f t h e f i l t e r , N = ) ;
// Example 5 . 9
//To D e s i g n a Chebyshev F i l t e r w i t h Given
Specifications
clear all ;
clc ;
close ;
ap =2.5; // db
as =30; // db
op =20; // r a d / s e c
os =50; // r a d / s e c
N = acosh ( sqrt ((10^(0.1* as ) -1) /(10^(0.1* ap ) -1) ) ) / acosh
( os / op ) ;
disp ( ceil ( N ) , Order o f t h e f i l t e r , N = ) ;
Scilab code Exa 5.10 HPF Filter Design with given Specifications
1 // Example 5 . 1 0
2 //To D e s i g n a H . P . F . w i t h g i v e n
3 clear all ;
4 clc ;
94
specifications
5
6
7
8
9
10
11
12
13
14
15
16
close ;
ap =3; // db
as =15; // db
op =500; // r a d / s e c
os =1000; // r a d / s e c
N = log ( sqrt ((10^(0.1* as ) -1) /(10^(0.1* ap ) -1) ) ) / log ( os /
op ) ;
disp ( ceil ( N ) , Order o f t h e f i l t e r , N = ) ;
s = %s ;
HS =1/(( s +1) *( s ^2+ s +1) ) ; // T r a n s f e r F u n c t i o n f o r N=3
oc =1000 // r a d / s e c
HS1 = horner ( HS , oc / s ) ;
disp ( HS1 , N o r m a l i z e d T r a n s f e r F u n c t i o n , H( s ) = ) ;
// Example 5 . 1 1
//To D e s i g n t h e F i l t e r u s i n g I m p u l s e I n v a r i e n t
Method
clear all ;
clc ;
close ;
s = %s ;
T =1;
HS =(2) /( s ^2+3* s +2) ;
elts = pfss ( HS ) ;
disp ( elts , F a c t o r i z e d HS = ) ;
// The p o l e s comes o u t t o be a t 2 and 1
p1 = -2;
p2 = -1;
z = %z ;
HZ =(2/(1 - %e ^( p2 * T ) * z ^( -1) ) ) -(2/(1 - %e ^( p1 * T ) * z ^( -1) ) )
disp ( HZ , HZ = ) ;
95
// Example 5 . 1 2
//MAXIMA SCILAB TOOLBOX REQUIRED FOR THIS PROGRAM
//To D e s i g n t h e F i l t e r u s i n g I m p u l s e I n v a r i e n t
Method
clear all ;
clc ;
close ;
s = %s ;
HS =1/( s ^2+ sqrt (2) * s +1) ;
pp = ilaplace ( HS ) ;
syms n z ;
t =1;
X = symsum ( pp *( z ^( - n ) ) ,n ,0 , %inf ) ;
disp (X , F a c t o r i z e d HS = ) ;
// Example 5 . 1 3
//MAXIMA SCILAB TOOLBOX REQUIRED FOR THIS PROGRAM
//To D e s i g n t h e 3 r d Order B u t t e r w o r t h F i l t e r u s i n g
I m p u l s e I n v a r i e n t Method
clear all ;
clc ;
close ;
s = %s ;
HS =1/(( s +1) *( s ^2+ s +1) ) ;
pp = ilaplace ( HS ) ; // I n v e r s e L a p l a c e
syms n z ;
t =1;
X = symsum ( pp *( z ^( - n ) ) ,n ,0 , %inf ) ; //Z T r a n s f o r m
96
13
disp (X , H( z )= ) ;
// Example 5 . 1 5
//To D e s i g n t h e F i l t e r u s i n g I m p u l s e I n v a r i e n t
Method
clear all ;
clc ;
close ;
s = %s ;
T =0.2;
HS =10/( s ^2+7* s +10) ;
elts = pfss ( HS ) ;
disp ( elts , F a c t o r i z e d HS = ) ;
// The p o l e s comes o u t t o be a t 5 and 2
p1 = -5;
p2 = -2;
z = %z ;
HZ = T *(( -3.33/(1 - %e ^( p1 * T ) * z ^( -1) ) ) +(3.33/(1 - %e ^( p2 * T
) * z ^( -1) ) ) )
disp ( HZ , HZ = ) ;
// Example 5 . 1 6
//To Find o u t B i l i n e a r T r a n s f o r m a t i o n o f HS=2/(( s +1)
( s +2) )
clear all ;
clc ;
close ;
s = %s ;
z = %z ;
97
// Example 5 . 1 7
//To D e s i g n an H . P . F . m o n o t o n i c i n p a s s b a n d u s i n g
B i l i n e a r Transform
clear all ;
clc ;
close ;
ap =3; // db
as =10; // db
fp =1000; // Hz
fs =350; // Hz
f =5000;
T =1/ f ;
wp =2* %pi * fp ;
ws =2* %pi * fs ;
op =2/ T * tan ( wp * T /2) ;
os =2/ T * tan ( ws * T /2) ;
N = log ( sqrt ((10^(0.1* as ) -1) /(10^(0.1* ap ) -1) ) ) / log ( op /
os ) ;
disp ( ceil ( N ) , Order o f t h e f i l t e r , N = ) ;
s = %s ;
HS =1/( s +1) // T r a n s f e r F u n c t i o n f o r N=1
oc = op // r a d / s e c
HS1 = horner ( HS , oc / s ) ;
disp ( HS1 , N o r m a l i z e d T r a n s f e r F u n c t i o n , H( s ) = ) ;
z = %z ;
HZ = horner ( HS ,(2/ T ) *( z -1) /( z +1) ) ;
disp ( HZ , H( z ) = ) ;
98
// Example 5 . 1 8
//To Find o u t B i l i n e a r T r a n s f o r m a t i o n o f H( s ) =( s
2+4.525) /( s 2+0.692 s +0.504)
clear all ;
clc ;
close ;
s = %s ;
z = %z ;
HS =( s ^2+4.525) /( s ^2+0.692* s +0.504) ;
T =1;
HZ = horner ( HS ,(2/ T ) *( z -1) /( z +1) ) ;
disp ( HZ , H( z ) = ) ;
Scilab code Exa 5.19 Single Pole LPF into BPF Conversion
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// Example 5 . 1 9
//To C o n v e r t a s i n g l e P o l e LPF i n t o BPF
clear all ;
clc ;
close ;
s = %s ;
z = %z ;
HZ =(0.5*(1+ z ^( -1) ) ) /(1 -0.302* z ^( -2) ) ;
T =1;
wu =3* %pi /4;
wl = %pi /4;
wp = %pi /6;
k = tan ( wp /2) / tan (( wu - wl ) /2) ;
a = cos (( wu + wl ) /2) / cos (( wu - wl ) /2) ;
99
15
Scilab code Exa 5.29 Pole Zero IIR Filter into Lattice Ladder 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 5 . 2 9
// Program t o c o n v e r t g i v e n I I R p o l e z e r o F i l t e r i n t o
L a t t i c e Ladder S t r u c t u r e .
clear all ;
clc ;
close ;
U =1;
// Z e r o A d j u s t
a (3+ U ,0+ U ) =1;
a (3+ U ,1+ U ) =13/24;
a (3+ U ,2+ U ) =5/8;
a (3+ U ,3+ U ) =1/3;
a (2+ U ,0+ U ) =1; // a (m, 0 ) =1
a (2+ U ,3+ U ) =1/3;
m =3 , k =1;
a (m -1+ U , k + U ) =( a ( m +U , k + U ) -a ( m +U , m + U ) * a ( m +U ,m - k + U ) )
/(1 - a ( m +U , m + U ) * a ( m +U , m + U ) ) ;
m =3 , k =2;
a (m -1+ U , k + U ) =( a ( m +U , k + U ) -a ( m +U , m + U ) * a ( m +U ,m - k + U ) )
/(1 - a ( m +U , m + U ) * a ( m +U , m + U ) ) ;
m =2 , k =1;
a (m -1+ U , k + U ) =( a ( m +U , k + U ) -a ( m +U , m + U ) * a ( m +U ,m - k + U ) )
/(1 - a ( m +U , m + U ) * a ( m +U , m + U ) ) ;
disp ( LATTICE COEFFICIENTS ) ;
disp ( a (1+ U ,1+ U ) , k1 ) ;
disp ( a (2+ U ,2+ U ) , k2 ) ;
disp ( a (3+ U ,3+ U ) , k3 ) ;
b0 =1;
100
24
25
26
27
28
29
30
31
32
33
34
35
b1 =2;
b2 =2;
b3 =1;
c3 = b3 ;
c2 = b2 - c3 * a (3+ U ,1+ U ) ;
c1 = b1 -( c2 * a (2+ U ,1+ U ) + c3 * a (3+ U ,2+ U ) ) ;
c0 = b0 -( c1 * a (1+ U ,1+ U ) + c2 * a (2+ U ,2+ U ) + c3 * a (3+ U ,3+ U ) ) ;
disp ( LADDER COEFFICIENTS ) ;
disp ( c0 , c 0 = ) ;
disp ( c1 , c 1 = ) ;
disp ( c2 , c 2 = ) ;
disp ( c3 , c 3 = ) ;
101
Chapter 6
FINITE IMPULSE
RESPONSE FILTERS
102
103
// Example 6 . 5
// Program t o P l o t Magnitude R e s p o n c e o f i d e a l L . P . F .
w i t h wc =0.5 p i
//N=11
clear all ;
clc ;
close ;
N =11;
U =6;
for n = -5+ U :1:5+ U
if n ==6
hd ( n ) =0.5;
else
hd ( n ) =( sin ( %pi *( n - U ) /2) ) /( %pi *( n - U ) ) ;
end
end
[ hzm , fr ]= frmag ( hd ,256) ;
hzm_dB = 20* log10 ( hzm ) ./ max ( hzm ) ;
figure ;
plot (2* fr , hzm_dB ) ;
a = gca () ;
xlabel ( F r e q u e n c y w p i ) ;
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 w i t h N=11 ) ;
xgrid (2) ;
// Example 6 . 6
// Program t o P l o t Magnitude R e s p o n c e o f i d e a l H . P . F .
w i t h wc = 0.2 5 p i
104
105
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//N=11
clear all ;
clc ;
close ;
N =11;
U =6;
for n = -5+ U :1:5+ U
if n ==6
hd ( n ) =0.5;
else
hd ( n ) =( sin ( %pi *( n - U ) ) - sin ( %pi *( n - U ) /4) ) /( %pi *( n - U ) ) ;
end
end
[ hzm , fr ]= frmag ( hd ,256) ;
hzm_dB = 20* log10 ( hzm ) ./ max ( hzm ) ;
figure
plot (2* fr , hzm_dB )
a = gca () ;
xlabel ( F r e q u e n c y w p i ) ;
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 HPF w i t h N=11 ) ;
xgrid (2) ;
// Example 6 . 7
// Program t o P l o t Magnitude R e s p o n c e o f i d e a l B . P . F .
with
// wc1 = 0. 25 p i and wc2 = 0.7 5 p i
//N=11
clear all ;
clc ;
close ;
106
107
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
N =11;
U =6;
for n = -5+ U :1:5+ U
if n ==6
hd ( n ) =0.5;
else
hd ( n ) =( sin ( %pi *3*( n - U ) /4) - sin ( %pi *( n - U ) /4) ) /( %pi *( n U));
end
end
[ hzm , fr ]= frmag ( hd ,256) ;
hzm_dB = 20* log10 ( hzm ) ./ max ( hzm ) ;
figure
plot (2* fr , hzm_dB )
a = gca () ;
xlabel ( F r e q u e n c y w p i ) ;
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 w i t h N=11 ) ;
xgrid (2) ;
// Example 6 . 8
// Program t o P l o t Magnitude R e s p o n c e o f i d e a l B . R . F .
with
// wc1 = 0. 33 p i and wc2 = 0.6 7 p i
//N=11
clear all ;
clc ;
close ;
N =11;
U =6;
for n = -5+ U :1:5+ U
108
109
11 if n ==6
12 hd ( n ) =0.5;
13 else
14 hd ( n ) =( sin ( %pi *( n - U ) ) + sin ( %pi *( n - U ) /3) - sin ( %pi *2*( n 15
16
17
18
19
20
21
22
23
24
25
U ) /3) ) /( %pi *( n - U ) ) ;
end
end
[ hzm , fr ]= frmag ( hd ,256) ;
hzm_dB = 20* log10 ( hzm ) ./ max ( hzm ) ;
figure
plot (2* fr , hzm_dB )
a = gca () ;
xlabel ( F r e q u e n c y w p i ) ;
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 BRF w i t h N=11 ) ;
xgrid (2) ;
Scilab code Exa 6.9.a HPF Magnitude Response using Hanning Window
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// Example 6 . 9 a
// Program t o P l o t Magnitude R e s p o n c e o f i d e a l H . P . F .
// u s i n g Hanning Window
// wc1 = 0. 25 p i
//N=11
clear all ;
clc ;
close ;
N =11;
U =6;
h_hann = window ( hn ,N ) ;
for n = -5+ U :1:5+ U
if n ==6
hd ( n ) =0.75;
110
111
15
16
17
18
19
20
21
22
23
24
25
26
27
else
hd ( n ) =( sin ( %pi *( n - U ) ) - sin ( %pi *( n - U ) /4) ) /( %pi *( n - U ) ) ;
end
h ( n ) = h_hann ( n ) * hd ( n ) ;
end
[ hzm , fr ]= frmag ( h ,256) ;
hzm_dB = 20* log10 ( hzm ) ./ max ( hzm ) ;
figure
plot (2* fr , hzm_dB )
a = gca () ;
xlabel ( F r e q u e n c y w p i ) ;
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 HRF w i t h N=11
u s i n g Hanning Window ) ;
28 xgrid (2) ;
Scilab code Exa 6.9.b HPF Magnitude Response using Hamming Window
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// Example 6 . 9 b
// Program t o P l o t Magnitude R e s p o n c e o f i d e a l H . P . F .
// u s i n g Hamming Window
// wc1 = 0. 25 p i
//N=11
clear all ;
clc ;
close ;
N =11;
U =6;
h_hamm = window ( hm ,N ) ;
for n = -5+ U :1:5+ U
if n ==6
hd ( n ) =0.75;
112
113
15
16
17
18
19
20
21
22
23
24
25
26
27
else
hd ( n ) =( sin ( %pi *( n - U ) ) - sin ( %pi *( n - U ) /4) ) /( %pi *( n - U ) ) ;
end
h ( n ) = h_hamm ( n ) * hd ( n ) ;
end
[ hzm , fr ]= frmag ( h ,256) ;
hzm_dB = 20* log10 ( hzm ) ./ max ( hzm ) ;
figure
plot (2* fr , hzm_dB )
a = gca () ;
xlabel ( F r e q u e n c y w p i ) ;
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 HRF w i t h N=11
u s i n g Hamming Window ) ;
28 xgrid (2) ;
// Example 6 . 1 0
// Program t o P l o t Magnitude R e s p o n c e o f g i v e n L . P . F .
with s p e c i f i c a t i o n s :
//N=7 ,w=p i /4
// U s i n g Hanning Window
clear all ;
clc ;
close ;
N =7;
alpha =3;
U =1;
h_hann = window ( hn ,N ) ;
for n =0+ U :1:6+ U
if n ==4
hd ( n ) =0.25;
114
115
15
16
17
18
19
20
21
22
23
24
25
26
27
28
else
hd ( n ) =( sin ( %pi *( n -U - alpha ) /4) ) /( %pi *( n -U - alpha ) ) ;
end
h ( n ) = hd ( n ) * h_hann ( n ) ;
end
[ hzm , fr ]= frmag ( h ,256) ;
hzm_dB = 20* log10 ( hzm ) ./ max ( hzm ) ;
figure
plot (2* fr , hzm_dB )
a = gca () ;
xlabel ( F r e q u e n c y w p i ) ;
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 g i v e n LPF w i t h N=7 ) ;
xgrid (2) ;
Scilab code Exa 6.11 LPF Filter Design using Kaiser Window
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// Example 6 . 1 1
// Program t o P l o t Magnitude R e s p o n c e o f g i v e n L . P . F .
with s p e c i f i c a t i o n s :
//wp=20 r a d / s e c , ws=30 r a d / s e c , w s f =100 r a d / s e c
// a s =44.0dB , ap =0.1dB
// U s i n g K a i s e r Window
clear all ;
clc ;
close ;
wsf =100 // r a d / s e c
ws =30; // r a d / s e c
wp =20; // r a d / s e c
as =44.0 //dB
ap =0.1 //dB
B = ws - wp ;
wc =0.5*( ws + wp ) ;
116
117
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
Scilab code Exa 6.12 BPF Filter Design using Kaiser Window
118
119
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 6 . 1 2
// Program t o P l o t Magnitude R e s p o n c e o f g i v e n B . P . F .
with s p e c i f i c a t i o n s :
// wp1=40 p i r a d / s e c , wp2=60 p i r a d / s e c
// ws1=20 p i r a d / s e c , ws2=80 p i r a d / s e c
// a s =30dB , ap =0.5dB
//F=100 Hz
// U s i n g K a i s e r Window
clear all ;
clc ;
close ;
wsf =200* %pi ; // r a d / s e c
ws1 =20* %pi ; // r a d / s e c
ws2 =80* %pi ; // r a d / s e c
wp1 =40* %pi ; // r a d / s e c
wp2 =60* %pi ; // r a d / s e c
as =30 //dB
ap =0.5 //dB
B = min ( wp1 - ws1 , ws2 - wp2 ) ;
wc1 = wp1 - B /2;
wc2 = wp2 + B /2;
wc1 = wc1 *2* %pi / wsf ;
wc2 = wc2 *2* %pi / wsf ;
delta1 =10^( -0.05* as ) ;
delta2 =(10^(0.05* as ) -1) /(10^(0.05* as ) +1) ;
delta = min ( delta1 , delta2 ) ;
alphas = -20* log10 ( delta ) ;
alpha =0.5842*( alphas -21) ^0.4+0.07886*( alphas -21)
D =( alphas -7.95) /14.36;
N1 = wsf * D / B +1;
N = ceil ( N1 ) ;
U = ceil ( N /2) ;
win_l = window ( k r ,N , alpha ) ;
for n = - floor ( N /2) + U :1: floor ( N /2) + U
if n == ceil ( N /2) ;
hd ( n ) =0.4;
else
hd ( n ) =( sin (0.7* %pi *( n - U ) ) - sin (0.3* %pi *( n - U ) ) ) /( %pi *(
120
38
39
40
41
42
43
44
45
46
47
48
121
// Example 6 . 1 3 a
// Program t o P l o t Magnitude R e s p o n c e o f i d e a l
d i f f e r e n t i a t o r with s p e c i f i c a t i o n s :
//N=8 ,w=p i
// u s i n g R e c t a n g u l a r window
clear all ;
clc ;
close ;
N =8;
alpha =7/2;
U =1;
h_Rect = window ( r e ,N ) ;
for n =0+ U :1:7+ U
hd ( n ) = -( sin ( %pi *( n -U - alpha ) ) ) /( %pi *( n -U - alpha ) *( n -U alpha ) ) ;
h ( n ) = hd ( n ) * h_Rect ( n ) ;
end
[ hzm , fr ]= frmag ( h ,256) ;
hzm_dB = 20* log10 ( hzm ) ./ max ( hzm ) ;
figure
plot (2* fr , hzm_dB )
a = gca () ;
xlabel ( F r e q u e n c y w p i ) ;
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 g i v e n i d e a l
d i f f e r e n t i a t o r u s i n g R e c t a n g u l a r Window , N=8 ) ;
xgrid (2)
122
123
// Example 6 . 1 3 b
// Program t o P l o t Magnitude R e s p o n c e o f i d e a l
d i f f e r e n t i a t o r with s p e c i f i c a t i o n s :
//N=8 ,w=p i
// u s i n g Hamming window
clear all ;
clc ;
close ;
N =8;
alpha =7/2;
U =1;
// Z e r o A d j u s t
h_hamm = window ( hm ,N ) ;
for n =0+ U :1:7+ U
hd ( n ) = -( sin ( %pi *( n -U - alpha ) ) ) /( %pi *( n -U - alpha ) *( n -U alpha ) ) ;
h ( n ) = hd ( n ) * h_hamm ( n ) ;
end
[ hzm , fr ]= frmag ( h ,256) ;
hzm_dB = 20* log10 ( hzm ) ./ max ( hzm ) ;
figure
plot (2* fr , hzm_dB )
a = gca () ;
xlabel ( F r e q u e n c y w p i ) ;
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 g i v e n i d e a l
d i f f e r e n t i a t o r u s i n g Hamming Window , N=8 ) ;
xgrid (2)
// Example 6 . 1 4 a
124
// Program t o P l o t Magnitude R e s p o n c e o f i d e a l
H i l b e r t Transformer
// u s i n g R e c t a n g u l a r Window
//N=11
clear all ;
clc ;
close ;
N =11;
U =6;
h_Rect = window ( r e ,N ) ;
for n = -5+ U :1:5+ U
if n ==6
hd ( n ) =0;
else
hd ( n ) =(1 - cos ( %pi *( n - U ) ) ) /( %pi *( n - U ) ) ;
end
h ( n ) = hd ( n ) * h_Rect ( n ) ;
end
[ hzm , fr ]= frmag (h ,256) ;
125
H i l b e r t Transformer
w i t h N=11 u s i n g R e c t a n g u l a r Window ) ;
26 xgrid (2) ;
// Example 6 . 1 4 b
// Program t o P l o t Magnitude R e s p o n c e o f i d e a l
H i l b e r t Transformer
126
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// u s i n g Blackman Window
//N=11
clear all ;
clc ;
close ;
N =11;
U =6;
for n = -5+ U :1:5+ U
h_balckmann ( n ) = 0.42+0.5* cos (2* %pi *( n - U ) /( N -1) )
+0.08* cos (4* %pi *( n - U ) /( N -1) ) ;
if n ==6
hd ( n ) =0;
else
hd ( n ) =(1 - cos ( %pi *( n - U ) ) ) /( %pi *( n - U ) ) ;
end
h ( n ) = hd ( n ) * h_balckmann ( n ) ;
end
[ hzm , fr ]= frmag (h ,256) ;
figure
plot (2* fr ,- hzm ) ;
a = gca () ;
xlabel ( F r e q u e n c y w p i ) ;
ylabel ( H( exp ( j w) ) ) ;
title ( F r e q u e n c y R e s p o n s e o f H i l b e r t T r a n s f o r m e r
w i t h N=11 u s i n g Blackman Window ) ;
xgrid (2) ;
127
c o e f f i c i e n t s obtained
6
7
8
9
10
11
12
close ;
N =7;
U =1;
// Z e r o A d j u s t
for n =0+ U :1: N -1+ U
h ( n ) =(1+2* cos (2* %pi *( n -U -3) /7) ) / N
end
disp (h , F i l t e r C o e f f i c i e n t s , h ( n )= )
// Example 6 . 1 6
// Program t o d e t e r m i n e f i l t e r c o e f f i c i e n t s o b t a i n e d
by s a m p l i n g :
//N=15
clear all ;
clc ;
close ;
N =15;
U =1;
// Z e r o A d j u s t
for n =0:1: N -1
h ( n + U ) =(1+2* cos (2* %pi *(7 - n ) / N ) +2* cos (4* %pi *(7 - n ) / N )
+2* cos (6* %pi *(7 - n ) / N ) ) / N ;
end
disp (h , F i l t e r C o e f f i c i e n t s , h ( n )= ) ;
Scilab code Exa 6.17 BPF Filter Design using Sampling Method
// Example 6 . 1 7
// Program t o d e s i g n b a n d p a s s f i l t e r w i t h f o l l o w i n g
specifications :
3 //N=7 , f c 1 =1000Hz , f c 2 =3000Hz , F=8000Hz
4 clear all ;
5 clc ;
1
2
128
close ;
N =7;
U =1;
// Z e r o A d j u s t
for n =0:1: N -1
h ( n + U ) =2*( cos (2* %pi *(3 - n ) / N ) + cos (4* %pi *(3 - n ) / N ) ) / N ;
end
disp (h , F i l t e r C o e f f i c i e n t s , h ( n )= ) ;
Scilab code Exa 6.18.a Frequency Sampling Method FIR LPF Filter
1
// Example 6 . 1 8 a
129
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// Program t o d e s i g n L . P . F . f i l t e r w i t h f o l l o w i n g
specifications :
//N=15 , wc=p i /4
clear all ;
clc ;
close ;
N =15;
U =1;
for n =0+ U :1: N -1+ U
h ( n ) =(1+ cos (2* %pi *(7 - n ) / N ) ) / N ;
end
[ hzm , fr ]= frmag ( h ,256) ;
hzm_dB = 20* log10 ( hzm ) ./ max ( hzm ) ;
figure ;
plot (2* fr , hzm_dB ) ;
a = gca () ;
xlabel ( F r e q u e n c y w p i ) ;
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 w i t h N=15 ) ;
xgrid (2)
Scilab code Exa 6.18.b Frequency Sampling Method FIR LPF Filter
1
2
3
4
5
6
7
8
9
// Example 6 . 1 8 b
// Program t o d e s i g n L . P . F .
specifications :
//N=15 , wc=p i /4
clear all ;
clc ;
close ;
N =15;
U =1;
for n =0+ U :1: N -1+ U
f i l t e r with f o l l o w i n g
130
132
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 6 . 1 9
// Program t o P l o t Magnitude R e s p o n c e o f g i v e n L . P . F .
with s p e c i f i c a t i o n s :
//N=13 ,w=p i /6
clear all ;
clc ;
close ;
alpha =6;
U =1;
for n =0+ U :1:12+ U
if n ==7
hd ( n ) =0.167;
else
hd ( n ) =( sin ( %pi *( n -U - alpha ) /6) ) /( %pi *( n -U - alpha ) ) ;
end
end
[ hzm , fr ]= frmag ( hd ,256) ;
hzm_dB = 20* log10 ( hzm ) ./ max ( hzm ) ;
figure
plot (2* fr , hzm_dB )
a = gca () ;
xlabel ( F r e q u e n c y w p i ) ;
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 g i v e n LPF w i t h N=13 ) ;
xgrid (2)
disp ( hd , F i l t e r C o e f f i c i e n t s , h ( n )= ) ;
133
134
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
// U s i n g Hamming Window
clear all ;
clc ;
close ;
N =13;
alpha =6;
U =1;
h_hamm = window ( hm ,N ) ;
for n =0+ U :1:12+ U
if n ==7
hd ( n ) =0.167;
else
hd ( n ) =( sin ( %pi *( n -U - alpha ) /6) ) /( %pi *( n -U - alpha ) ) ;
end
h ( n ) = hd ( n ) * h_hamm ( n ) ;
end
[ hzm , fr ]= frmag ( h ,256) ;
hzm_dB = 20* log10 ( hzm ) ./ max ( hzm ) ;
figure
plot (2* fr , hzm_dB )
a = gca () ;
xlabel ( F r e q u e n c y w p i ) ;
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 g i v e n LPF w i t h N=13 ) ;
xgrid (2)
disp (h , F i l t e r C o e f f i c i e n t s , h ( n )= ) ;
disp (h , F i l t e r C o e f f i c i e n t s , h ( n )= ) ;
// Example 6 . 2 1
// Program t o P l o t Magnitude R e s p o n c e o f g i v e n L . P . F .
with s p e c i f i c a t i o n s :
135
22 a = gca () ;
23 xlabel ( F r e q u e n c y w p i ) ;
24 ylabel ( Magnitude i n dB ) ;
25 title ( F r e q u e n c y R e s p o n s e o f FIR LPF w i t h N=7 ) ;
26 xgrid (2)
27 disp (h , F i l t e r C o e f f i c i e n t s , h ( n )= ) ;
Scilab code Exa 6.28 Filter Coefficients for Direct Form Structure
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 8
// Program t o c a l c u l a t e FIR F i l t e r c o e f f i c i e n t s f o r
t h e d i r e c t form s t r u c t u r e
// k1 =1/2 , k2 =1/3 , k3 =1/4
clear all ;
clc ;
close ;
U =1;
k1 =1/2;
k2 =1/3;
k3 =1/4;
a (3+ U ,0+ U ) =1;
a (1+ U ,1+ U ) = k1 ;
a (2+ U ,2+ U ) = k2 ;
a (3+ U ,3+ U ) = k3 ;
m =2 , k =1;
a ( m +U , k + U ) = a (m -1+ U , k + U ) + a ( m +U , m + U ) * a (m -1+ U ,m - k + U ) ;
m =3 , k =1;
a ( m +U , k + U ) = a (m -1+ U , k + U ) + a ( m +U , m + U ) * a (m -1+ U ,m - k + U ) ;
m =3 , k =2;
a ( m +U , k + U ) = a (m -1+ U , k + U ) + a ( m +U , m + U ) * a (m -1+ U ,m - k + U ) ;
disp ( a (3+ U ,0+ U ) , a ( 3 , 0 ) ) ;
disp ( a (3+ U ,1+ U ) , a ( 3 , 1 ) ) ;
disp ( a (3+ U ,2+ U ) , a ( 3 , 2 ) ) ;
disp ( a (3+ U ,3+ U ) , a ( 3 , 3 ) ) ;
137
// Example 6 . 2 9
// Program t o c a l c u l a t e g i v e n FIR F i l t e r s L a t t i c e
form c o e f f i c i e n t s .
clear all ;
clc ;
close ;
U =1;
// Z e r o A d j u s t
a (3+ U ,0+ U ) =1;
a (3+ U ,1+ U ) =2/5;
a (3+ U ,2+ U ) =3/4;
a (3+ U ,3+ U ) =1/3;
a (2+ U ,0+ U ) =1; // a (m, 0 ) =1
a (2+ U ,3+ U ) =1/3;
m =3 , k =1;
a (m -1+ U , k + U ) =( a ( m +U , k + U ) -a ( m +U , m + U ) * a ( m +U ,m - k + U ) )
/(1 - a ( m +U , m + U ) * a ( m +U , m + U ) ) ;
m =3 , k =2;
a (m -1+ U , k + U ) =( a ( m +U , k + U ) -a ( m +U , m + U ) * a ( m +U ,m - k + U ) )
/(1 - a ( m +U , m + U ) * a ( m +U , m + U ) ) ;
m =2 , k =1;
a (m -1+ U , k + U ) =( a ( m +U , k + U ) -a ( m +U , m + U ) * a ( m +U ,m - k + U ) )
/(1 - a ( m +U , m + U ) * a ( m +U , m + U ) ) ;
disp ( a (1+ U ,1+ U ) , k1 ) ;
disp ( a (2+ U ,2+ U ) , k2 ) ;
disp ( a (3+ U ,3+ U ) , k3 ) ;
138
Chapter 7
FINITE WORD LENGTH
EFFECTS IN DIGITAL
FILTERS
// Example 7 . 2
//To Compute S u b t r a c t i o n
// ( a ) 0 . 2 5 from 0 . 5
clear all ;
clc ;
close ;
a =0.5;
b =0.25;
c =a - b ;
disp (c , = ,b , ,a , PART 1 ) ;
// ( a ) 0 . 5 from 0 . 2 5
d =b - a ;
disp (d , = ,a , ,b , PART 2 ) ;
139
// Example 7 . 1 4
//To Compare t h e V a r i e n c e o f Output due t o A/D
Conversion process
// y ( n ) =0.8 y ( n 1)+x ( n )
clear all ;
clc ;
close ;
n =8; // B i t s
r =100; // Range
Q =2* r /(2^ n ) ; // Q u a n t i z a t i o n S t e p S i z e
Ve =( Q ^2) /12;
Vo = Ve *(1/(1 -0.8^2) ) ;
disp (Q , QUANTIZATION STEP SIZE = ) ;
disp ( Ve , VARIANCE OF ERROR SIGNAL = ) ;
disp ( Vo , VARIANCE OF OUTPUT = ) ;
140
Chapter 8
MULTIRATE SIGNAL
PROCESSING
// Example 8 . 9
//MAXIMA SCILAB TOOLBOX REQUIRED FOR THIS PROGRAM
// D e v e l o p a two component d e c o m p o s i t i o n f o r t h e
transfer function
// and d e t e r m i n e P0 ( z ) and P1 ( z )
clear all ;
clc ;
close ;
syms z a n ;
HZ =( z ) /( z - a ) ;
hn = a ^ n ; // I n v e r s e Z T r a n s f o r m o f HZ
h2n = a ^(2* n ) ;
P0 = symsum ( h2n * z ^( - n ) ,n ,0 , %inf ) ;
h2n1 = a ^(2* n +1) ;
P1 = symsum ( h2n1 * z ^( - n ) ,n ,0 , %inf ) ;
disp ( P0 , P0 ( Z ) = ) ;
disp ( P1 , P1 ( Z ) = ) ;
141
// Example 8 . 1 0
// D e v e l o p a two band p o l y p h a s e d e c o m p o s i t i o n f o r t h e
transfer function
//H( z )=z 2+ z +2/ z 2 + 0 . 8 z +0.6
clear all ;
clc ;
close ;
z = %z ;
HZ =( z ^2+ z +2) /( z ^2+0.8* z +0.6) ;
HZa = horner ( HZ , - z ) ;
P0 =0.5*( HZ + HZa ) ;
P1 =0.5*( HZ - HZa ) ;
disp ( P1 /z , + ,P0 , H( z ) = )
142
Chapter 9
STATISTICAL DIGITAL
SIGNAL PROCESSING
// Example 9 . 7 ( a )
// Program To D e t e r m i n e F r e q u e n c y R e s o l u t i o n o f
Bartlett ,
// Welch ( 5 0% O v e r l a p ) and BlackmannTukey Methods
clear all ;
clc ;
close ;
// Data
Q =10; // Q u a l i t y F a c t o r
N =1000; // S a m p l e s
//FREQUENCY RESOLUTION CALCULATION
K=Q;
rb =0.89*(2* %pi * K / N ) ;
rw =1.28*(2* %pi *9* Q ) /(16* N ) ;
rbt =0.64*(2* %pi *2* Q ) /(3* N ) ;
// D i s p l a y t h e r e s u l t i n command window
disp ( rb , R e s o l u t i o n o f B a r t l e t t Method ) ;
disp ( rw , R e s o l u t i o n o f Welch ( 5 0% o v e r l a p ) Method ) ;
disp ( rbt , R e s o l u t i o n o f BlackmannTukey Method ) ;
143
// Example 9 . 8 ( a )
// Program To D e t e r m i n e S m a l l e s t Record Length o f
B a r t l e t t Method
clear all ;
clc ;
close ;
// Data
fr =0.01; // F r e q u e n c y R e s o l u t i o n
144
8 N =2400; // S a m p l e s
9 //RECORD LENGTH CALCULATION
10 lb =0.89/ fr ;
11 // D i s p l a y t h e r e s u l t i n command window
12 disp ( lb , Record Length o f B a r t l e t t Method ) ;
// Example 9 . 8 ( b )
// Program To D e t e r m i n e Q u a l i t y F a c t o r o f B a r t l e t t
Method
clear all ;
clc ;
close ;
// Data
fr =0.01; // F r e q u e n c y R e s o l u t i o n
N =2400; // S a m p l e s
lb =0.89/ fr ;
//QUALITY FACTOR CALCULATION
Q = N / lb ;
// D i s p l a y t h e r e s u l t i n command window
disp (Q , Q u a l i t y F a c t o r o f B a r t l e t t Method ) ;
145
Chapter 11
DIGITAL SIGNAL
PROCESSORS
// Program 1 1 . 3
// Program To C a l c u l a t e t h e v a l u e o f t h e f u n c t i o n
//Y=AB
clear all ;
clc ;
close ;
// I n p u t Data
A = input ( E n t e r I n t e g e r Number A = ) ;
B = input ( E n t e r I n t e g e r Number B = ) ;
// M u l t i p l i c a t i o n Computation
Y=A*B;
// D i s p l a y t h e r e s u l t i n command window
disp (Y , Y = AB = ) ;
146
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// Program 1 1 . 5
// Program To C a l c u l a t e t h e v a l u e o f t h e f u n c t i o n
//Y=AX1+BX2+CX3
clear all ;
clc ;
close ;
// Data
A =1;
B =2;
C =3;
X1 =4;
X2 =5;
X3 =6;
// Compute t h e f u n c t i o n
Y = A * X1 + B * X2 + C * X3 ;
// D i s p l a y t h e r e s u l t i n command window
disp (Y , Y = AX1+BX2+CX3 = ) ;
147