WoodardRebecca1987

CALIFORNIA STATE UNIVERSITY, NORTHRIDGE
DESIGNING NON-RECURSIVE DIGITAL FILTERS
USING THE IBM PERSONAL COMPUTER
A graduate project submitted in partial satisfaction of
the requirements for the degree of Master of Science in
Electrical Engineering
by
Rebecca Elizabeth Woodard
August 1987
The Graduate Project of Rebecca Elizabeth Woodard is
approved:
Dr. T. Lee
Dr. R1& H. Pettit
Dr. ~ohn
w.
Adams, Chair
Cal lfornla State University, Northridge
ii
TABLE OF CONTENTS
Abstract . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
lv
Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
v
Chapter
1•
Page
Introduction
2. Digital Filters . . . . . . . . . . . . . . . . . . . . . . . . .
2
3. F I Iter DesIgn A Igor I thm . . . . . . . . . . . . . . . . .
3
3. 1 Descr I pt I on . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3
3.2 Program Implementation
4.
Input Parameter
Estimation Algorithm
11
24
5. Plotting the Frequency Response .........
37
5.1 Evaluating the Frequency Response ......
37
5.2 Program Implementation .................
39
Appendix
A. Program Mechanics .......................
41
B. Data Fl les and Plots ....................
44
C . Program L I s t I n g s . . . . ......... : . . . . . . . . . .
60
References ..................................... 120
iii
ABSTRACT
DESIGNING NON-RECURSIVE DIGITAL FILTERS
USING THE IBM PERSONAL COMPUTER
by
Rebecca Elizabeth Woodard
Master of Science in Electrical Engineering
This paper presents a FORTRAN-77 program developed
for the IBM personal computer (PC) which can be used to
design non-recursive I lnear phase digital filters.
The
program can design such filters as multiple
passband/stopband filters, differentiation filters and
Hi I bert transformers.
In addition, the program is capable
of estimating Input parameters for designing lowpass
filters.
A plotting package written in BASIC and FORTRAN-
77 Is included to permit the user to observe the
magnitude,
log-magnitude and phase response, as wei I as
the group delay and the Impulse response.
menu-oriented to faci I ltate usage.
iv
The program Is
PREFACE
This fi Iter design program has served as a very
practical graduate project.
It wi I I be very useful to the
digital signal processing engineer with access to the
widely used IBM (or IBM-compatible) personal computer in
designing digital filters.
Chapter One Is an introduction which describes the
general background to this project.
Chapter Two Introduces the general concepts of
digital filters pertinent to the discussion within this
paper.
Chapter Three describes the background Information
and theory applied to Implement the program for designing
the f I Iter s.
Chapter Four describes the background Information
pertinent to the design of the input parameters
estimation program.
Chapter Five discusses the methods taken to obtain
the frequency response of the fi Iter and to plot several
aspects of the fl Iter.
v
Chapter Six i I lustrates the actual
implementation of
the fi Iter design program, the Input estimation program
and the frequency response plotting program.
Appendix A contains a qulde for explaining the program
mechanics.
Appendix B i I lustrates the results of several filters
that were designed using this program.
Appendix C consists of alI the program I istings.
I would I Ike to thank Dr. John Adams for his time and
patience which lead to the completion of this project.
would especially I Ike to thank him for suggesting this
project which I found both enjoyable and challenging.
I would also I ike to thank committee members, Dr. Ray
H. Pettit and
Dr.
T. Lee, for their time In reading and
reviewing my project.
Special thanks to my faml ly, Kevin and Jonathan
Slattery, for their support although they were neglected,
my supervisor, Scott Johnston, with his Idle threats and
especially my father, Professor Kenneth Woodard, for
always bel levlng In me.
vi
1.
INTRODUCTION
This text presents a computer program which can be
used to design a large class of finite impulse response
(FIR) filters.
The main program Is based on a fi Iter design program
written
by
J. McClellan,
T. Parks and L. Rabiner [2].
Their program is based on the Remez Exchange Algorithm to
des i g n F I R f i I t e r s w i t h a m i n i murn we i g h ted e r r or [ 1 J .
The fi Iter design program was expanded by developing
a program to allow the designer to estimate an input
parameter In the case of a
low pass fi Iter.
This program
is based on a paper presented by Lawrence R. Rabiner [3].
The final portion of the project enables the designer
to plot various aspects of the frequency response such
as magnItude,
I og-magn I tude, phase and group de I ay.
It
also Includes the option of plotting the Impulse response.
1
2. DIGITAL FILTERS
There are many reasons to operate on a signal:
remove noise, extract signal components within a certain
frequency range or
to smooth a signal.
These are forms
of f i Iter I ng.
The simplest kind of digital fl Iter is the nonrecursive f I 1 ter.
The non recursive f i Iter generates
outputs based on I y on the inputs to the f i Iter as shown.
I
I
x(n) ----->1
I
I
h(n)
1-----> y(n)
N-1
Y ( n)
•
~ X(k) h(n-k) • X
k•O
* H
( 2 • 1)
To obtain the output, the Input Is convolved with the
filter coefficients.
2
3. FILTER DESIGN ALGORITHM
3.1 Description
It is desirable to design filters with I inear phase to
ensure a constant group delay for alI frequencies.
group delay is the slope of the phase.
The
Finite length
(FIR) filters can be designed with exactly I inear phase.
A general form of the frequency response of a
I inear
phase fi Iter can be written as a function of the real
valued magnitude, G(f), and a
H (f)
=
I inear phase term:
f'-':
...,
G(f) * exp[j(LII/2- (N-1)/2*211 f)]
(3 . 1)
For the FIR fi lt€r to be constrained to have I I near phase,
the impulse response must have positive or negative
symmetry, L
=
0 or 1 (Equation 3.1), respectively.
The
length of the finite Impulse response can be even or odd.
This results In four different types of FIR fl lters with
I inear phase.
3
4
The real-valued magnitude, usually refer to as the
zero-phase response,
is defined uniquely for the four
types of FIR fl lters:
1. Positive symmetry [h(k)=h(N-1-k)] and odd length
[n=(N-1)/2]:
n
G (f)
•
~ a ( k) cos ( 2
k•O
,...,
11
k f)
(3.2)
a(O) • h(n)
a(k) • 2h(n-k), k•1,2, ... ,n
2. Positive symmetry and even
n
G( f)
•
~
length [n=N/2]:
....,.
b(k)cos[2
11
(k-1/2)f]
(3.3)
k·1
b(k) .. 2h(n-k), k•1,2, ... ,n
3. Negative symmetry [h(k)=-h(N-1-k)] and odd
G( f)
,..,
=
c(k)sln(211 kf)
length:
(3.4)
h(n) • 0
c(k) • 2h(n-k), k•1,2, ... ,n
4. Negative symmetry and even
length:
n
G( f)
•
L
d(k)sln[20 (k-1/2)f]
k•1
d(k) • 2h(n-k), k-1,2, ... ,n
(3.5)
5
The estimation of G(f) has only been established for
the function with positive symmetry and odd length (type
1). To adapt the procedure to the other three types, each
is rewritten to resemble the first type consisting of a
multiplying coefficient, Q(f), and the cosine function,
p ( f) .
The function with positive symmetry and even length
is rewritten as follows:
G ( f ) • cos (
,.._.
II
n-1
""'
~ b ' ( k) cos ( 2 \1 k f )
f)
(3.6)
k•O
b(1)
b(k)
•
•
b'(O)
(1/2)
•
[
(1/2) b'(1)
b'(k-1) + b'(k)
b(n)
•
(1/2)
b(n-1)
],
k•2,3, ... ,n-1
The function with negative symmetry and odd length is
rewritten as follows:
,..,
G(f)
•
sln(211 f)
n-1
~
,._
c' ( k) cos ( 2 \\ k f)
(3.7)
k•O
c(1)
c(k)
•
•
c'(O)- (1/2) c'(2)
(1/2) [ c'(k-1)- c'(k+1)
c(n-1)
•
c(n)
(1/2)
•
(1/2) c'(n-2)
c'(n-1)
],
k•2,3, ... ,n-2
6
The function with negative symmetry and even length
is rewritten as follows:
,._
n-1
,.....
G(f) • sln(tl f)
L:
d
I
(
k) cos ( 2 II k f )
(3.8)
k-0
d(1) • d'(O) - (1/2) d'(1)
d(k) • (1/2) [ d'(k-1)- d'(k) ], k•2,3, ... ,n-1
d(n) •
(1/2) d'(n-1)
Now that alI
four types are written in a common form,
one method can be used to approximate the best magnitude
and weight function.
With the magnitude response, D(f), and the weight
function, W(f), along with the set of zero-phase response
values, G(f), the maximum absolute weighted error
Is
defined as
E(f) • max W(f) [D(F)-G(f)J
O.sf_s0.5
(3.9)
This Is the weighted difference between the ideal response
and the actua I f i Iter response.
The goa I Is to mIn lm i ze
this difference over the values of G(f) within a
normalized frequency range between 0 and 0.5
radians/second.
7
This error function must be rewritten to be
compatible withal I four fi Iter types.
E(f) • max W'(f) [D'(f)-P(f)]
O..s.f..s.0.5
(3.10)
where W'(f) • W(f)Q(f)
D' ( f ) • D( f ) /Q ( f)
P(f) • Q(f), omitting those freq. endpts
where Q(f)•O
The error function must be made to satisfy the set
of necessary and sufficient conditions for optimality.
P (f)
Is defined as the set of I i near cosine functions,
Q(f), excluding the endpoints where Q(f)
r-1
p (f)
-
L
k•O
=
0.
,._
(3.11)
g ( k) COS ( 2 II k f)
In order for P(f) to be the unique best approximatipn on
the set of frequencies to D(f), E(f) must exhibit at
(r+1) alternations on the set of
freq~encles.
least
In other
words, there must be (r+1) extremal frequencies [1].
These extremal points, F( i), are where the absolute valued
error function goes through local maxima,
:E[F( I)]:.
The
error function at each extremal point should alternate In
sign.
8
"Error
Curve"
E(F( I)J • -E(F(I+1)J,
(3.12)
1•1,2, ... ,r
From the extremal frequencies, F(k), k=0,1, ... n+1, we
use the Remez Exchange Algorithm to find the unique best
approximation.
A set of (n+2) frequencies requires a
solution to n+2 error equations.
W'{F(k)} * ( P{F(k)}- D'{F(k)} J • -(-1)k(DEV) (3.13)
where
P{F(k)} •
,...,
g(k)cos(2 \1 k*F(k) J, k•0,1, ... ,n+1
DEV is the error function magnitude and the error
function
Itself alternates In sign.
given In Figure 3.1.
The matrix form is
9
cos2'iiF(O)
cos4'i1F(O) ... cos2ilnF(0)
cos2'ii F( 1)
1/W'[F(O)]
g(O)
D'[F(O)]
-1/W'[F(1)]
g(1)
D'[F(1)]
1/W'[F(2)]
I
I
•
I
I
I
1 cos2'iiF(n+1)
Figure 3.1
(-1)n+1/W[F(n+1)]
J
DEV
D'[F(n+1)]
Matrix Form of the Error Function
10
The solution of DEV Is obtained analytically since it is
less time consuming:
(3.14)
a(O)*D[F(0)]+a(1)*D[F(1)]+ ... +a(n+1)*D[F(n+1)J
DEV • ----------------------------------------------------n+1
a(0)/W[F(O)]-a(1)/W[F(1)]+ ... +(-1)
a(n+1)/W[F(n+1)]
k
a(k) - C-1)
n
1
Il
1•1,1<>1<
,...
....,
cos [ 2 l\ F ( I ) J - cos [ 2 \1 F (I<) J
The LaGrange interpolation formula Is used to interpolate
the D(f) functions on the (n+1) points, F(O),F(1), ... ,F(n).
IV
D( f) •
IV
{a(I<)/[COS(2ll F)-COS(211 F(I<)J) * C(l<)
,.,
,..,
{a(k)/[COS(2 II F)-cos(211 F(k) J)
(3.15)
((
where c(l<) • D[F(k)J- (-1)
*{DEV/W[F(k)]), l<•0,1, ... n
From D(f), the error function
error function magnitude,
Is evaluated at f.
If the
:E(f):, obtained Is less than
the previously found error function magnitude, DEV, then
the best approximation has been found.
If :ECf):
Is
greater than DEV, points are searched and tried within
the search range between adjacent points unti I the set of
extremal frequencies which satisfies the criteria is
obtained.
points.
This method is better known as exchanging
Q
'
3.2 Program Implementation
Figure 3.2
I lustrates the flow of the fi Iter design
program.
The r e q u i red I n puts ( 1 ) are as f o I I ow s : ( a ) The f i I t e r
length corresponds to the length of the fi Iter impulse
response.
(b) The f i Iter type IndIcates whether the
fi Iter has positive or negative symmetry.
Three fi Iter
types are supported: multiple passband/stopband (positive
symmetry), dlfferentiator and Hilbert transform (both
negative symmetry).
(c) The number of f I Iter bands
Indicates the actual number of passbands and stopbands In
the fl Iter, where appl lcable.
(d) The grid density
variable along With the fl Iter length determines the
number of finite points along the horizontal axis at which
to evaluate functions.
(e) The filter band Information is
given In the form of lower and upper cutoff frequencies
for each band with a frequency spectrum normal lzed
between 0 and 0.5 cycles/sample.
(f) The desired function
for each band Is required In a normal lzed form.
desired function
The
In the passband Is "1" and In the
stopband Is "0". For the dlfferentlator, the desired
function Is the slope of the differentiating function.
(g) The weight function Is a relative tolerance error.
11
12
It is equal to the 1/k, where "k"
is the ratio of the
stopband deviation to the passband deviation.
dlfferentiator, the weight function
For the
is inversely
proportional to the slope of the differentiating function.
To set up the dense grid (2), the spacing Is obtained
between points of
Fmax - Fmln
(3.16)
LGRID * r
where
Fmax • 0.5, Fmln • 0.0
LGRID Is the grid density Input and r
is the number of
cosine functions required for a f I Iter type. ThIs gives
the program a finite set of points to operate with.
13
Input length, type, #bands, grid dens, band
edges, desired function and weight function
(1)
Y.
Set up the dens grid array (2)
•
•
Set up the desired function array (3)
Formulate the equiv. approx. problem (4)
~.
Make an initial guess of the extremal freqs. (5)
~
Remez (7)
~
Calculate the impulse response (8)
.!..
Output the answers (9)
Figure 3.2
Block Diagram of the Filter
Design Algorithm
14
The weight function (3)
point.
Is evaluated at each grid
For the dlfferentiator, the weight function
WTX(J),
FX(J)•O
WTX(J),
FX(J)~O
is
(3.17a)
WATE(f} •
For the other fl Iter types, the weight function
WATE(f) ..
In both cases,
WTX ( J )
(3.17b)
"f" belongs to the jth band.
The desired function
point.
is
(4)
is evaluated at each grid
For the differentiator, the desired function
EFF(f) •
FX(J)
*
f
(3.18a)
For the other fl Iter types, the desired function
EFF (f) •
FX(J)
In both cases,
~f~
is
Is
(3.18b)
belongs to the jth band.
Remember that the error function equation is In the
form
E(f) • W(f)Q(f)
where
* [D(f)/Q(f) - P(f)J
G(F) • Q(F) * P(f).
(3.19)
15
To formulate the equivalent approximation problem (5), the
following equations must be appl led:
W'(f) • W(f) * Q(f)
(3.20a)
D'(f) • D(f) I Q(f)
(3.20b)
where
,4.-
Q(f) =cos( 11 f)
(even symmetry & even length)
,.....
Q(f) • sln(21tf)
(odd symmetry & odd length)
(3.21a)
(3.21b)
.....,
Q(f) • sin( 11 f)
(odd symmetry & even length)
(3.21c)
The weight functions and desired functions are modified
according to the fl Iter type.
An Initial set of extremal frequencies Is required to
begin processing. This set Is obtained by formulating an
initial guess of (n+2) equally spaced points (6).
The Remez Exchange Algorithm Is appl led (7). The
flowchart of the Remez Exchange Algorithm implementation
Is shown In Figure 3.3.
16
Remez
itrmax = 25, niter=O, n=nfcns
Main
iteration
loop
go to calc. coeff. of best
approx.
Calc. the deviation using
LaGrange formula and save
sign
Calc. the magnitude response, D(f)
error
Figure 3.3
Block Diagram of the Remez
Exchange Algorithm
17
Set upper (kup) & Lower (klow) grid pt limits for 1st
extremal freq
Search Loop
go to endpt search
Calc magnitude
response
0(iext(i)+1)
Calc. Dev (j)
Figure 3.3 cont. Block Diagram of
the Remez Exchange Algorithm
18
B
Search k+2, k+3, ... , kup until local max
(where D(j+ 1)-D(j) change sign) is found
Exchange
points
___.
Change the extremal freq. to that found
ie: new iext(j)
,,
Update klow and kup variables for next
iteration
,,
jchnge = jchnge + 1
(# of extremal freqs
that have changed)
I
!
I
go to search loop
Figure 3.3 cont.
Block Diagram of
the Remez Exchange Algorithm
19
Calc D(j) and devU)
Search k-2,
k-3, ... ,
klow until
local max is
found
Update
klow and
kup
Continue search k-2,
k-3, ... ,klow for local max
where devU) > or = dev
found
go to
exchange
points
go to
search
loop
go to
exchange
points
not found
Update klow
and kup
Search k+2, k+3, ... , kup for
local max
where devU) > or = dev
found
not found
~------------------~
go to
search
loop
go to
search
loop
go to exchange
points
Figure 3.3 cont.
Block Digram of the
Remez Exchange Algorithm
20
Endpt Search
Search indices < min(iext(1 ), new iext(1 )) for error
with sign -dev(1) and > error at new iext(n)
not
found
found
Store index & error value
Search indices > max(iext(n), new iext(n)) for local
max of error curve with sign -dev(n) and error > or =
error at new iext(n) and error found above
iext( 1)
new iext(2)
iext(n-1) = new iext(n)
iext(n) = index just found
Figure 3.3 cont.
Block Diagram of the
Filter Design Algorithm
21
iext(1) = index found
above
iext(2)
new iext( 1)
iext(j) = new iext(j)
for all "j"
iext(n) = new iext(n-1)
go to main
iteration
loop
Calc coeff of
best approx
Calc D(f) for "f"
belonging to
(0, 1 /(2n-1 ), ... ,
(n-1 )/(2n-1))
Inverse DFT to obtain best cosine
coefficients
Figure 3.3 cont.
Block Diagram of the
Filter Design Algorithm
22
After Initial lzlng the iteration count, the main
iteration loop is entered.
This main loop is where a
maximum of 25 Iterations of exchanging and trying extremal
frequency points wi I I be performed.
(Main Iteration Loop) The deviation of the error is
calculated using the LaGrange interpolation method
described in Equations 3.14 and 3.15.
With this error
value, the frequency response error Is determined using
Equation 3.10.
The deviation is tested,
if it isn't
decreasing, then a settlement Is made of the best
approximation of the coefficients.
If the deviation is
decreasing, the process of exchanging points is continued.
If 25 Iterations has been reached, the best approximation
of the coefficients Is used.
The actual manipulation of
the extremal frequency points Is performed In the search
loop.
(Search loop) For each grid point frequency,
a search
Is conducted between the adjacent lower and upper grid
points.
Initially, the first grid point's search boundary
Is scanned for the local maxima, where the error changes
sign.
The lower and upper bounds are searched to find
the direction In which a decreasing error occurs.
When
the proper direction of the search Is determined, the
search Is continued In that direction untl I the best local
23
maxima Is found, preferably where the signed error is
greater then the present deviation.
(Endpoint search) When the exchange process is
completed, an endpoint search Is required since the
endpoints were excluded previously.
There are guaranteed
extremal frequencies at the endpoints.
The two endpoints
are searched for each local maxima.
The extremal
frequencies have been obtained.
At
these extremal points, the frequency response Is
evaluated and the Inverse Discrete Fourier Transform
Is
performed to obtain the coefficients of the best cosine
approximation.
From the frequency response, the impulse response
coefficients (8) are obtained.
The Impulse response, the set of extremal
frequencies, the deviation obtained In each band and the
original
Input fl Iter specifications are stored In the
appropriate fl le.
4.
INPUT PARAMETER ESTIMATION ALGORITHM
To improve the fl Iter design program in the case of a
lowpass fl Iter, an algorithm was developed to optimize the
input Information.
parameters;
This algorithm allows one of the five
I .e., fl Iter length, passband frequency,
stopband frequency, passband or stopband deviations,
be estimated.
to
The passband and stopband frequencies and
deviations are defined In Figure 4.1.
24
25
h(w)
passband deviation
+
stopband deviation
+
Figure 4.1
Definitions of Stopband
and Passband Variables
w
26
To estimate one of the parameters, an Initial guess
Is obtained.
The fi Iter design program is Invoked.
resultant deviation is tested.
The
If the deviation does not
meet the value specified, the parameter is optimally
adjusted and the fi Iter design program Is Invoked again.
The general form of this process Is i I lustrated in Figure
4.2.
27
Case 1-5
Input 4 out of 5 parameters
Guess the 5th parameter
Invoke the filter design algorithm
END
Modify 5th parameter
Figure 4.2
General Block Diagram of the
Input Estimation Algorithm
.
'
28
S I nee there are five parameters, f I Iter
I ength,
passband frequency, stopband frequency, passband
deviation and stopband deviation, there are five cases in
which to deal with. Before explaining each case In detai I.
some variables need to be defined,
fp
fs
fd
pd
sd
•
•
•
•
•
passband
stopband
passband
passband
stopband
frequency
I
frequency
to stopband transition band
deviation
deviation
These variables and notations wi I I be used for the
remaining text
In this section.
The first case is to est Imate the f i Iter
I ength.
Initial guess Is obtained by the equation
N'
•
DA(pd,sd)/fd - FA(pd,sd)*fd2
( 4. 1 )
where DA and FA are functions defined as follows:
DA(x,y) •
[ a1 log2x + a21ogx + a3 J * logy
+ a41og2x + a51ogx + as
(4.2)
a1 • 0.005309, a2 • 0.07114, a3 • -0.4761
a4 • -0.00266, a5 • 0.5941, a4 • -0.4278
FA(x,y) • b1 + b21ogx - b21ogy
b1 -
11 . 01 2 1 7. b2 - 0. 51 244
(4.3)
The
29
N, fs, pd. sd
Calculate fd'
fp' = fs-fd'
fst = fd/1 0
jd = 0
Invoke the optimal design
algorithm
fst =
fs t* 3
fp'
fp'-fst
fp'
= fp' + fst
Figure 4.4
Block Diagram for the
Estimation of the passband frequency
30
With the Initial guess of the fl Iter
length, the fl Iter
design program Is Invoked and the deviatIon Is tested.
the deviation is not within specifications, N'
If
Is modified
as shown In Figure 4.3 and the fi Iter design program Is
executed again.
This process continues untl I the desired
deviation is obtained.
The next two cases, estimating passband and stopband
frequencies, are similar and wi I I be described
simultaneously.
The actual variable estimated In these
cases is the frequency band between the passband and
stopband cutoff frequencies.
(4.4)
fd • fs - fp
The equation for estimating the frequency band Is
(4.5)
n-1
fd'
-
----------
2*FA(pd,Sd)
I
* !
I
\ \
4*FA(pd,sd)*DA(pd,sd) \112 \
+ --------------------
(n-1)2
I
-1
I
From the estimated frequency band variable, the estimated
passband and stopband frequencies are determined from
Equation 4.4:
fp' • fs - fd'
(4.6)
fs' • fp + fd'
(4.7)
31
N, fs, pd, sd
Calculate fd'
fp' = fs-fd'
fst = fd/1 0
jd = 0
Invoke the optimal design
algorithm
=
<
y
fst =
fst/3
fst =
fs t* 3
y
n
fp'
=
fp'-fst
n
fp' = fp'+fst
Figure 4.4
Block Diagram tor the
Estimation of the passband frequency
32
N, fp, pd, sd
Calculate fd'
fp' = fp+fd'
fst = fd/1 0
jd = 0
Invoke the optimal design
algorithm
fst =
fst* 3
fs'
~
fs'+fst
fs'
fs'-fst
Figure 4.5
Block Diagram for the
Estimation of the stopband frequency
variable
33
With the Initial guess of the cutoff frequency, the filter
design program is Invoked and the deviation is tested.
If
the deviation is not within specifications, the variable
in focus
Is modified as shown in Figure 4.4 and Figure 4.5
for passband and stopband, respective I y and the f i Iter
design program is executed again.
This continues unti I
the desired deviation Is obtained.
The estimation of the passband deviation is acquired
using Equation 4.8.
2
1/2
pd - 10 [ -g2/2 + (g2 /4-g3)
J
(4.8)
where
g1 • b1-b21og(sd)
g2 • (e2-b2*fd*fd)/e1
g3 • (e3-g1*fd*fd-(n-1)fd)/e1
b1,b2 are obtained In Equation 4.3
e 1 • a 1 I og <sd) + a4
e2 • a21og(sd) + a5
e3 • a31og(sd) + as
a1 through as are obtained In Equation 4.2
With the Initial guess of the passband frequency, the
fi Iter design program Is invoked and the deviation Is
tested.
If the deviation is not within specifications,
the passband deviation Is modified as shown In Figure 4.6
and fl Iter design program Is executed again.
This process
continues untl I the desired deviation Is obtained.
34
N, fp,fs, sd
Calculate pd'
k' = pd'/sd
km = 2.0
jd = 0
Invoke the optimal design
algorithm
=
<
y
km =
km/2
km =
km/2
y
n
k' = k'(1 +km)
n
k' = k'/(1 +km)
Figure 4.6
Block Diagram for the
Estimation of the passban~ deviation
variable
35
The stopband deviation Is estimated using Equation
4.9.
I
sd' •
10 *
\
(n-1)fd + d1*fd*fd- c2 \
(4.9)
------------------------1
c1 + b2fd2
I
where
c1 - a11og2(pd) + a21og{pd) + a3
c2 - a41og2{pd) + a51og(pd) + as
c3 - b1 + b21og(pd)
al & bj coefficients are obtained In
Equations 4.2 and 4.3
With the initial guess of the stopband deviation, the
fi Iter design program Is invoked and the deviation is
tested.
If the deviation Is not within specifications,
the stopband deviation is modified as shown
In Figure 4.7
and the fi Iter design program Is executed again.
This
process continues untl I the desired deviation Is obtained.
For alI
five cases, when the last iteration has been
reached, the output data of the fi iter wl i i be aval lable
In the appropriate file.
There Is no need to run the
fi Iter design program with the new set of
parameters.
Input
36
N, fp,fs, pd
Calculate sd'
k' = pd/sd'
km = 2.0
jd = 0
Invoke the optimal design
algorithm
=
<
y
km =
km/2
km =
km/2
y
n
k'
=
k'{1 +km)
n
.,.
"
k'/(1+km)
Figure 4.7
Block Diagram for the
Estimation of the stopband deviation
variable
5. PLOTTING THE FREQUENCY RESPONSE
5.1 Evaluating The Frequency Response
To obtain the frequency response plot data, first the
frequency response Is calculated from the impulse
response coefficients.
The frequency response is related
to the Impulse response by Equation 5.1.
N-1
H(eJw) •
.L
h(n)e -jwn
( 5. 1 )
n-o
From this equation, the real part and the imaginary part
can be found
In an Iterative manner using Euler's
formulas.
R (f)
I(f )
-
N-1
~
h ( n) cos ( -2
f n)
(5.2)
h(n)sln(-211 fn)
(5.3)
N-1
~
n-o
,....
11
~
From the real and imaginary parts, the magnitude and
phase Is defined In Equations 5.4 and 5.5.
P(f) • arctan [
1/2
2
2
M(f) • [ R(f)
+
I (f)
(5.4)
]
l(f)/R(f) ]
37
(5.5)
38
The group delay
Is obtained by taking the slope of the
phase response.
G(f) • dP(f)/df
(5.6)
5.2 Program Implementation
The flowchart
in Figure 5.1
illustrates the flow of
the frequency response calculation program.
For a f I Iter,
the impulse response, the real part and Imaginary part of
the frequency response along with the magnitude, the logmagnitude, the phase and group delay are stored in
separate files.
From these files,
the plot program can
plot any of the above mentioned functions for the fi Iter.
39
' .
40
input fscale, f2, tn, h(n) for n = 1 TO tn
fmax = nint(fscale*f2)+ 1
rscale = 1If scale
pold = o
do f = 1,fmax
r(f) = 0, i(f) = 0
do n = 1, tn
r( f)
i (f)
=
=
r(f)+h(n)cos[-2*pi*(f-1 )rscale(n-1 )]
i(f)+h(n)sin[-2*pi*(f-1 )rscale(n-1 )]
output h(n)
m(f) = sqrt [r(f)*r(f) + i (f)* i (f) l
if r(f) <> 0, p(f)=arctan (i (f)/r(f))
else if i(f)<O, p(f)=-pi/2, else p(f)=pi/2
g(f) = (p(f)-pold)/2*pi*rscale,
pold = p(f)
output m(f), log[m(f)], p(f), g(f), r(f), i(f)
Figure 5.1
Block Diagram for Calculating
the Frequency Response from the Impulse
Response
APPENDIX A: PROGRAM MECHANICS
In Figure A1, the design program structure Is
i I I ustrated.
program.
Each program is accessed from the main
The entire program is menu-oriented.
The menus
are written so required inputs to the program are selfexplanatory during execution.
Menu selections are made by
entering the corresponding letter In parentheses.
used to exit a menu.
"X"
is
A I I Inputs requIre a <CR>, carriage
return.
From the main program, the user must first choose
the program to design the fi Iter coefficients.
If the
fi Iter type to be designed Is that of a low pass filter,
the user may use the Input parameter estimation program
to optimally approximate one of the Input parameters.
The
Input parameters are stored In a "*.FIX" fl le specified by.
the user.
The "*.FIX" files can tater be used by the
f I Iter design program as an Input.
The output from the
f i Iter coefficients design program consists of the
Impulse response coefficients and Is stored In the
"*.FOX" f I I e corresponding to the "*.FIX" f II e.
The next step Is for the user to choose the program
which calculates the frequency response.
This program
wl I I read the Impulse response coefficients from the
41
42
FILTER
DESIGN PROGRAM
TERMINAL INPlfTS
'.FIX
FILES
USER DEFINED COEFFICIENTS
Figure A1
Filter Design Program Structure
43
"*.FOX" file specified by the user.
With these
coefficients, the program wl I I calculate the frequency
response In the form of the real part,
magnitude,
imaginary part,
I og-magn I tude, phase and group de I ay.
As we I
as the impulse response, each set of data wi I I be stored
in it's corresponding file:
"*.LOG",
"*.REA", "*.IMG", "*.MAG",
"*.PHA", "*.GRD", "*.IMP", respectively.
In the final step, the user chooses the program which
plots the data sets stored In each of the fl les.
From
the plot program, any of the data sets can be plotted on
the screen by specifying the corresponding filename.
APPENDIX B: DATA FILES AND PLOTS
A set of FIR filters designed by the program Is given on
the following pages.
specification sheet
set of
Each example contains the
completely describing the fi Iter, the
Impulse response coefficients obtained by the
fl Iter design program and the plot(s) of the fi Iter.
44
45
**************************************************************
Finite impulse response (fir)
linear phase digital filter design
remez exchange algorithm
multiple passband/stopband
Filter length= 24
***** Impulse response *****
H( 1) = .33743830E-02 = H(
H( 2) = .1494690DE-01 = H(
H( 3) = .1056464DE-01 = H(
H( 4) = .2544311DE-02 = H(
H( 5) = -.1592129DE-01 = H(
H( 6) = -.34077760E-01 = H(
H( 7) = -.38112380E-01 = H(
H( 8) = -.14629380[-01 = H(
H( 9) = .40085650E-01 = H(
H( 10) = .11540570Et00 = H(
H(11)= .18850380Et00=H(
H( 12) = .23354570Et00 = H(
Lower band edge
Upper band edge
Desired value
Weighting
Deviation
Deviation in dB
Extremal frequencies
.0000000
.0364583
.1738888
.2051387
.3787492
.4273601
24)
23)
22)
21)
20)
19)
18)
17)
16)
15)
14)
13)
Band 1
Band 2
.000000000
.160000000
.080000000
.500000000
1. 000000000
.000000000
1. 000000000
1. 000000000
.012460740
.012460740
-38.089130000 -38. 089130000
.0659722
.2450691
.4759710
.0800000
,2884717
.1600000
.3318743
**************************************************************
Band
Mag
FILE NAME:
1924b2
PLOT TYPE: MAGNITUDE
1.~12496
I
9999586
.1
~lj-=- --- -
- -
- --
-
- -
-
~
(j')
47
**********************************************************************
Finite Impulse response (fir)
I lnear phase digital fi Iter design
remez exchange algorithm
multiple passband/stopband
Fi Iter length = 31
***** Impulse response *****
H( 1) = -.43548390E-02 = H(
H( 2) = .19293090E-01 = H(
H( 3) = -.56706780E-02 = H(
H( 4) = .52362520E-01 = H(
H( 5) = .31495380E-02 = H(
H( 6) = .43499720E-01 = H(
H( 7) = .11642860E-01 = H(
H( 8) = -.3788716DE-01 = H(
H( 9) = .34420910E-02 = H(
H( 10) = -.87576720E-01 = H(
H( 11) = -. 10984870E-01 = H(
H( 12) = .44461220E-01 = H(
H( 13) = -.69043850£-02 = H(
H( 14) = .31144210E+00 = H(
H( 15) = .96790660E-02 = H(
H( 16) = .45296030E+00 = H(
Lower band edge
Upper band edge
Desired value
Weighting
Deviation
Deviation in dB
Extremal frequencies
.0000000
.1570565
.2820563
.4623387
31)
30)
29)
28)
27)
26)
25)
24)
23)
22)
21)
20)
19)
18)
17)
16)
Band 1
Band 2
Band 3
.000000000
.150000000
.420000000
.100000000
.350000000
.500000000
1. 000000000
.000000000
1. 000000000
1. 000000000 50. 000000000
1. 000000000
.144147700
.002882955
.144147700
-16.823840000 -50.803250000 -16.823840000
.0403226
.1762097
.3092740
.5000000
.0776209
.2004032
.3324595
.1000000
.2266128
.3500000
.1500000
.2548386
.4270564
**********************************************************************
Band
I.
FILE NAME:
1931h3
PLOT TYPE: MAGNITUDE
Mag\
(
Li44168 ( \
v
.9999133
._!
•1
\
.2
.3
J .4
eye/sa~~
+:>
(X)
49
**********************************************************************
Finite Impulse response (fir)
Iinear phase digital fi Iter design
remez exchange a Igor ithm
multiple passband/stopband
Fi Iter length = 32
***** Impulse response *****
H( 1) = -.57447340E-02 = H(
H( 2) = .10073480E-02 = H(
H( 3) = .75460730E-02 = H(
H( 4) = -.65532810E-02 = H(
H( 5) = .13960540E-01 = H(
H( 6) = .23156110E-02 = H(
H( 7) = -.19921270E-01 = H(
H( 8) = .71668100E-02 = H(
H( 9) = -.39710130E-01 = H(
H( 10) = . 11193220E-01 = H(
H( 11) = .66155690E-01 = H(
H( 12) = -.10461110E-01 = H(
H( 13) = .85256380E-01 = H(
H( 14) = -. 12018580E+00 = H(
H( 15) = -.29677990E+00 = H(
H( 16) = .30399600E+00 = H(
Band 1
Lower band edge
Upper band edge
Desired value
We lght ing
Deviation
Deviation In dB
.OOOOOOOJO
.1000000JO
.000000000
10.000000000
.001517165
-56.379340000
32)
31)
30)
29)
28)
27)
26)
25)
24)
23)
22)
21)
20)
19)
18)
17)
Band 2
Band 3
.200000000
.425000000
.350000000
.500000000
.000000000
1. 000000000
1.000000000 10 . 000000000
.015171650
.001517165
-36.379340000 -56. 379340000
Extremal frequencies
. 0000000
. 1000000
.3142578
. 4503906
. 0263672
. 2000000
.3386719
. 4796875
.0517578
.2195313
.3500000
.0751953
.2517578
.4250000
.0927734
.2839844
.4318359
**********************************************************************
Band
Mag
FILE NAME:
1932h3
PLOT TYPE: MAGNITUDE
1.~1518
.3 _ _ _ _
.1.....-;__ _ _.2_ _ _ _ _
.9999412 _ _
eye/sa~~
_ _ . , ; ; a . . __ __
01
0
51
**********************************************************************
Finite Impulse response (fir)
linear phase digital filter design
remez exchange algorithm
multiple passband/stopband
Fi Iter length
=
50
***** Impulse response *****
H( 1) = .15690930E-02 = H(
H( 2) = .30874090E-02 = H(
H( 3) = -.31803400E-02 = H(
H( 4) = -.62088770E-02 = H(
H( 5) = .74384460E-02 = H(
H( 6) = .98493040£-02 = H(
H( 7) = -. 11102330E-01 = H(
H( 8) = -.10110550£-01 = H(
H( 9) = .89912700E-02 = H(
H( 10) = .29016600E-02 = H(
H( 11) =
.26657310E-02 = H(
H( 12) = . 12023170E-01 = H(
H( 13) = -.20650430E-01 = H(
H( 14) = -.27187130E-01 = H(
H( 15) = .32322320E-01 = H(
H( 16) = .28297670E-01 = H(
H( 17) = -.20904180E-01 = H(
H( 18) = -. 18645190E-02 = H(
H( 19) = -.22835830E-01 = H(
H( 20) = -.53935940E-01 = H(
H( 21) = .90470440E-01 = H(
H( 22) = . 12315660E+00 = H(
H( 23) = -. 15637510£+00 = H(
H( 24) = -. 17731950E+00 = H(
H( 25) = . 19075910E+DD = H(
Band 1
Lower band edge
Upper band edge
Desired value
Weighting
Deviation
Deviation in dB
•OOOOOIXXlO
. 150001XXlO
•OOOOOIXXlO
10. OOOOOIXXlO
.003715133
-48.600511XXlO
50)
49)
48)
47)
46)
45)
44)
43)
42)
41)
40)
39)
38)
37)
36)
35)
34)
33)
32)
31)
30)
29)
28)
27)
26)
Band 2
Band 3
. 200000000
.350000000
.300000000
.500000000
1. 000000000
.000000000
1. 000000000 100. 000000000
.037151330
.000371513
-28.60051 0000 -68. 60051 0000
Extremal frequencies
. 0000000
.1011999
.2096002
.3000000
.3928007
.4896024
. 0204000
.1219998
.2288005
. 3500000
.4104010
.04041XXl
. 1412001
.2500008
.3536001
.4288013
.0607999
.1500000
.2716012
.3632002
.4484017
.0807999
.2000000
.2908015
.3768004
.4688020
Band
52
c
c
~
(,)
::r'l
(,)
~
.::::.
-
"d"
::::::1
IE-4
z
:
(.;I
>=c=--.._____
>
..:
z:
(.;I
0
~
--
I
../
~
a:a..
("1";1
::t-1
IE-4
IE-4
0
~
a:a..
I
\("1.:1
....
-
....
("1";1
I
53
**********************************************************************
Finite impulse response (fir)
I inear phase digital filter design
remez exchange a Igor Ithm
multiple passband/stopband
Fi iter length = 55
***** impulse response *****
H(
H(
H(
H(
H(
H(
H(
H(
H(
H(
H(
H(
H(
H(
H(
H(
H(
H(
H(
H(
H(
H(
H(
H(
H(
H(
H(
H(
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)
=
.10719440E-02
= .35607050£-02 = H( 53)
= -.9060785DE-02 = H(
= -.90823620£-02 = H(
= .29162600E-02 = H(
= .39652270£-02 = H(
= .11171430£-01 = H(
= .11650430E-01 = H(
= -.99733340£-02 = H(
= -.92476470£-02 = H(
= -.20384100E-01 H(
= -.19465300E-01 = H(
= .31229910E-01 = H{
= .63285300E-02 = H(
= -.20484120E-01 = H(
K
= .65552290E-02 = H(
= -.11189740E-02 = H(
= .41966560£-01 = H(
= .35788310£-01 = H(
= .34726490£-01 = H(
= .71501740£-01 = H(
• -.17136550E+00 = H(
=
-.18256780Et00 "' H(
= .74055420£-01 • H(
• -.10315350E+00 = H(
= .25711340£-01 • H(
= .37811400£+00 = H(
Band 1
Lower band edge
Upper band edge
Desired value
Weighting
Deviation
Deviation In dB
= H( 55)
= .63709710£-02 = H( 54)
Band 2
52)
51)
50)
49)
48)
47)
46)
45)
44)
43)
42)
41)
40)
39)
38)
37)
36)
35)
34)
33)
32)
31)
30)
29)
28)
Band 3
Band 4
.000000000
.100000000
.180000000
.300000000
.050000000
.150000000
.250000000
.360000000
.000000000
1.000000000
.000000000
1.000000000
10. 000000000
1.000000000
3. 000000000
1.000000000
.003448157
.034481570
.011493860
.034481570
-49.248260000 -29. 248260000 -38 .790680000 -29.248260000
' .
,, .
54
Lower band edge
Upper band edge
Desired va Iue
Weighting
Deviation
Deviation in dB
Extremal frequencies
.0000000
.1000000
.1800000
.2436376
.3506480
.4453886
Band 5
.410000000
.500000000
.000000000
20. 000000000
.001724079
-55.268860000
.0162338
.1094155
.1855196
.2500000
.3600000
.4632453
.0318182
.1266233
.1985068
.3000000
.4100000
.4814266
Band
.0444805
.1425327
.2140916
.3120127
.4151947
.5000000
.0500000
.1500000
.2300009
.3321420
.4285060
**********************************************************************
dB
I
FILE NAME:
1Q55h5
PLOT TYPE: LOG MAGNITUDE
QQQ6964 --~---r-·-------......__...._.~---~,.---
I.
I
r
I
I'
I.
I' •
-99 .• 63149
I
I
tn
tn
56
**********************************************************************
Finite Impulse response (fir)
I inear phase digital fi Iter design
remez exchange a Igar ithin
different iator
Fi Iter length = 32
***** Impulse response*****
H( 1) = -.62640810E-03 = -H(
H( 2) = .85609670E-03 = -H(
H( 3) = -.42432670E-03 = -H(
H( 4) = .39867770E-03 = -H(
H( 5) = -.43467000E-03 = -H(
H( 6) = .50076920E-03 = -H(
H( 7) = -.59680050E-03 = -H(
H( 8) = .73320150£-03 = -H(
H( 9) = -.93015320E-03 = -H(
H( 10) = . 12265650£-02 = -H(
H( 11) = -.17008700E-02 = -H(
H( 12) = .25268870£-02 = -H(
H( 13) = -.41599720£-02 = -H(
H( 14) = .81295790E-02 = -H(
H( 15) = -.22539400£-01 = -H(
H( 16) = .20266570Et00 = -H(
Band 1
.000000000
.500000000
1.000000000
.006203195
Lower band edge
~per band edge
Weighting
Deviation
Extremal frequencies
.0009766
.1650391
.3300781
.4863281
.0332031
.1982422
.3623047
.5000000
.0664063
.2304688
.3955078
32)
31)
30)
29)
28)
27)
26)
25)
24)
23)
22)
21)
20)
19)
18)
17)
Band
.0986328
.2636719
.4277344
. 1318359
.2968750
:4589844
**********************************************************************
I
Mag
FILE NAME:
diff32
PLOT TYPE: MAGNITUDE
_____................---
.4~68568
..............---
.------------
__.-··
--------·
....-__,....-------
___
....-------------
______
//
//
--....-----------.1
.2
.3
.4
eye/sa~~
9~------------------------------·----------------------------------------------------------------------
U"1
"'-.J
58
**********************************************************************
Finite impulse response (fir)
I inear phase digital fi Iter design
remez exchange algorithm
hilbert transform f i Iter
Filter length= 20
***** Impulse response *****
H( 1) = .16016350£-01 = -H(
H( 2) = .14167600E-01 = -H(
H( 3) = .20452090E-01 = -H(
H( 4) = .28734700E-01 = -H(
H( 5) = .39842050E-01 = -H(
H( 6) = .55333790E-01 = -H(
H( 7) = .78548800£-01 = -H(
H( 8) = . 11823810£+00 = -H(
H( 9) = .20665790£+00 = -H(
H( 10) = .63476410£+00 = -H(
Lower band edge
Upper band edge
Desired value
Weighting
Deviation
Extremal frequencies
.0500000
.0650000
.2949998
.2449998
.5000000
Band 1
.050000000
.500000000
1.000000000
1. 000000000
.020559660
.1025000
.3449998
20)
19)
18)
17)
16)
15)
14)
13)
12)
11)
Band
.1474999
.3974997
.1949999
.4474997
**********************************************************************
59
~
.....
........
n;;
(.)
::r'l
(.)
"=:~"~
~
.:::::::.
::::::1
IE--I
z:
c,.::t
<I:
:E
--
~
lo':l-4
=--IE--I
IE--I
0
...:I
lo':l-4
\
)
\
C'l')
APPENDIX C: PROGRAM LISTING
Follows is alI the command files and source files which
make up this entire design program.
60
•
61
"FDESIGN.BAT"
echo off
cis
fdmenu
•
,,
62
"FDMENU.FOR"
program fdmenu
c
write(*, 10)
10
format(//,
1
2
3
4
5
c
end
'FIR Filter Design/Plot menu:',//,
' (D)esign filter coefficients',/,
' Galculate frequency (R)esponse for plotting',/,
' Filter (P)Iot',//,
'Enter selection ... ',$)
63
"D.BAT"
echo off
cis
echo Available fi Iter parameter files:
di r *. f ix/w
fdesgn
fdesign
64
"R.BAT"
echo off
cis
echo Available impulse response coefficients files:
dir *. fox/w
pf i les
fdesign
65
"P.BAT"
echo off
CIS
fplots
fdesign
66
PROGRAM fdesgn
c
C This Is the main program for fi Iter designing. It executes an
C editor for the fi Iter input parameters and sequences the events
c necessary for designing the fi Iter and saving the results.
COMMON /block1/ dev,pi,pi2
COMMON /block2/ ad,x,y,grid,des,wt,alpha,iext
COMMON /block3/ lgrid,nfcns,ngrid
DIMENSION
DIMENSION
DIMENSION
DIMENSION
iext(1027),ad(1027),alpha(1027),x(1027)
des(8216),grid(8216),wt(8216),y(1027),h(1027)
edge(20),fx(10),wtx(10),deviat(10)
ctype(4)
DOUBLE PRECISION ad,dev,x,y,change,pi,pi2
CHARACTER*1 char,bflrfl(10)
CHARACTER*10 firfil
CHARACTER*26 ctype
INTEGER odflag,err,fflag,cc, Ifirst
EQUIVALENCE (bfirfl,firfi I)
pi=4.0*DATAN(1.000)
pi 2=2.000*p i
C Initialize the control flags.
lflrst=O
err=O
odflag=O
ff lag=O
init=O
C Set
up
the fi Iter names
ctype(1)
ctype(2)
ctype(3)
ctype(4)
for the data files.
= 'none
= 'multiple passband/stopband'
= 'differentiator
= 'hilbert transform fi Iter '
C Main menu loop.
94
CONTINUE
IF(init.EO.l)CALL cis
lnlt=l
lchang=D
C Display the menu to select the source of the fi Iter Input parameters.
WRITE(*,98)
98
FORMAT{/,lX,'FIR Fi Iter design menu:',//,
67
"FDESGN.FOR"
1X,' (R)ead in fi Iter parameters "*.FIX" file',/,
1X, • (C)reate new (or edit oId) f i Iter parameters·)
2
91
93
WRITE(*,93)
FORMAT(/,2X,'Selection {(X)=exit} => • ,$)
C Read the selection of the source.
READ(*,92)char
92
FORMAT(A1)
IF (char .NE. 'R' .AND .char .NE. 'r' .AND .char. NE. 'C' .AND.
char .NE .' c' .AND. char .NE. 'X' .AND.char. NE. 'x' )GO TO 91
IF(char.EO.'X' .OR.char.EO. 'x')GO TO 995
C If new or edit old option, set the appropriate flags.
IF (char .EO. 'C' .OR .char. EO.' c' )THEN
newf i 1:1
IF(ifirst.EO.O)iflrst:1
C If a file Is to be read, set the appropriate flags and open the file.
ELSE
00 11 ce:1,10
11
bf Irf I(cc ): ' '
97
FORMAT(I,lX, 'Enter the file base name:
96
FORMAT( SA)
~ITE(*,97)
• ,$)
READ(*,96)(bfirfl(cc),cc:1,6)
bf Irf I(7)=' .'
bfirfi(S)='F'
bf Irf I(9)=' I'
bf lrf l(lO)='X'
newt 11=0
OPEN(2,FILE:firfi I,STATUS:'OLD')
END IF
c
C Maximum fi Iter length a 2048. To change this: redimension variables
C iext,ad,aipha,x,y,h to be nfmax/2+2 and variables des,grld,wt to be
C lS(nfmax/2+2).
NFMAX=2049
100
CONTINUE
IF(init.EQ.O)Jtype = 0
--
-
..
i'
68
"FDESGN.FOR"
C When first starting ·up and no file is specified, initialize the variables.
IF(newfl I.EQ. 1)THEN
IF(init.EQ.O)THEN
nf i lt=2048
lgrid=16
ri>ands= 1
edge(1)=0.0
edge(2)=0.0
fx(1)=0.0
wtx(1)=0.0
ELSE
END IF
c If a file input is requested, read the Input parameters from the file.
ELSE
READ(2,800)nf i It, jtype, lgr id,nbands
DO 802 I= 1,nbands
READ(2,801)edge(2*i-1),edge(2*i),fx(i),wtx(i)
CONTINUE
FORMAT(//, 18X, 13,/, 18X, 11,/, 18X, 13,/, 18X, 12,/)
FORMAT(10X,F10.4,3X,F10.4,4X,F10.4,7X,F10.4)
CLOSE(2)
END IF
802
800
801
c Display
10
903
902
900
901
the parameters
CONTINUE
CALL cis
WRITE(*,900)nfl lt,jtype,ctype(jtype+1), lgrid,nbands
WRITE(*,903)
FORMAT(1X,'band t' ,3X,' lower edge' ,3X,'upper edge' ,3X,
1
'band flllctlon' ,3X, 'weight flllCtion')
DO 902 I= 1,rbands
WRITE(*,901)i,edge(2*i-1),edge(2*i),fx(i),wtx(i)
CONTINUE
FORMAT{!, 1X ,'Present f i Iter parameters are as follows·:',
1
/,1X,'fllter length: ',13,
2
I, lX,'f liter type:
', 11 ,' ',A26,
3
/,1X,'grid density:
',13,
4
/,1X,'nl.lllberofbands: ',12)
FORMAT(lX,' t', 12,5X,F10.4,3X,F10.4,4X,F10.4,7X,F10.4)
C Display the menu for selecting parameters to edit.
WRITE(*,905)
905
FORMAT(///, 1X,'FIR filter edit/design menu:',
1
//, lX,' Alter the f liter parameters: (L)ength,',
2
' (T)ype, (G)rld, *of (B)ands, (A) I I',
3
/,lX,' (D)es I!Jl the f liter',
4
/,1X,' (E)stlmate an Input parameter and desi~ the',
4
' f liter',
'
69
"FDESGN. FOR"
5
392
C
//, 1X,'Selectlon {(X)=exlt} =>
READ(* ,392)char
FORMAT(A1)
',$)
If done with Inputs, exit this section and proceed to execute
IF (char. EO .'X' .OR .char. EO. 'x' )GO TO 997
C If user wants to design the fi Iter, go to that section of the code.
IF (char. EO .'0' .OR .char. EO.' d' )GO TO 996
C If user wants to estimate the parameters, go to that section.
IF (char. EO. 'E' .OR .char. EO.' e' )GO TO 992
laii=O
C If user wants to change alI the parameters, set the flag.
IF(char .EO.' A' .OR .char .EO.' a') Ia 11=1
C Fetch the fl Iter length
lf(char .EO .'L' .OR .char .EO.' I' .OR .Ia II.EO .1 )THEN
WRITE(*,101)nfi It
!save •nfllt
101
FORMAT(/, 1X,' length= ',13,1X,
' ... new value (max=2048 no change=O):
READ(*, *)nf i It
lf(nfl lt.LT.3.0R.nfl lt.GT.2048)THEN
YIR ITE(* ,102)
102
FORMAT( 1X, 'no change')
nfilt=lsave
ELSE
IF(isave.NE.nfl lt)lchang=1
END IF
ELSE
END IF
',$)
C Fetch the fi Iter type
IF(char .EO. 'T' .OR .char .EQ. 't' .OR .Ia ll.eQ .1 )THEN
WRITE(*,201)Jtype
Isave =Jtype
201
FORMAT(!,1X,'type .. ',11,1X,
' .•. new value (1=bandpass 2=dlff 3=hllb):
READ(*,*) Jtype
IF(Jtype.LT.1.0R.jtype.GT.3)THEN
WRITE(* ,102)
Jtype=lsave
ELSE
IF(Isave.NE.Jtype)lchang=1
END IF
ELSE
END IF
',$)
70
"FDESGN.FOR"
C Fetch the grid density
IF(char .EO. 'G' .ffi .char .EO .'g' .OR. ia II.EO .1 )THEN
WRITE(*,301)1grid
isave =lgrid
301
FORMAT(I,1X,'gr id dens lty = ',13,1X,
' ... new value (max=2048 no change=O):
READ(*,*)Igrid
lf(lgrid.LT.1.0R. lgrid.GT.2048)THEN
WR ITE(•, 102)
lgrld=lsave
ELSE
IF (I save .NE. lgr Id) Ichang= 1
END IF
lf(lgrld.LE.O}Igrld=16
ELSE
END IF
C Fetch the number of bands
IF (char .EO. 'B' .ffi .char. EO .'b' .OIUa II. EO .1 )THEN
WRITE(*,401)nbands
lsave=nbands
401
FffiMAT(/,1X,'number bands=' ,12,1X,
' ... new value (max=10 no change=O):
-READ(•,•)nbands
IF(nbands.LT. 1.0R.nbands.GT.10)THEN
WRITE(*, 102)
nbands= isave
ELSE
IF(Isave.NE.nbands)ichang=1
END IF
IF(nbands.LE.O)nbands=1
Jb=2*nbands
menu for selecting a band 11 to edit parameters
C Note: If new 11 of bands Is less than old; edittlng Is forced
IF ( isave.L T.nbands .OR. ia II.EO. 1)THEN
lchar=10
ELSE
IF(nbands.EO. 1)1char=1
IF(nbands.NE. l)THEN
WRITE(* ,402)
FORMAT(/, lX, 'Select band t (no change=O all=lO):
402
READ( 0 ,492)1char
FeRMAT( 12)
492
END IF
END IF
',$)
',$)
C Show
C If no change, exit band parameters edltting
IF(Ichar.LE.O)THEN
',$)
71
"FDESGN.FOR"
WRITE(*, 102)
GO TO 499
ELSE
END IF
C Set up loop to edit band parameters
IF(ichar.GE. 10)THEN
lm ln=1
imax=nbands
Ia 11=1
ELSE
lmin=ichar
lmax=ichar
Ia 11=0
END IF
C Edit the band parameters
max=999.0
DO 490 i=ImIn , imax
C Display the menu to select the band parameter to edit
IF ( ia II. EO .O)THEN
llfliTE(*,500)i
500
F~MA.T(I,1x,'Select filter parameter for bandil',
1
12,/, 1X, '(L)ower edge, (U)pper edge, '
2
'(F)unction, (W)eight function, '
3
'(A)II -> ',$)
READ(*,592)char
592
FORMAT( A1)
ELSE
char=' A'
END IF
C Fetch the lower edge
501
IF(char .EO.'L' .~.char .EQ. '.1'.
OR.Iaii.E0.1)THEN
WRITE(*,501)1,edge(2*1-1)
save =edge(2*i-1)
F~MAT(/,1X, 'lower edge 1' ,12,' = ',F10.4,1X,
' ... new value (no change=999): ',$)
READ(*,*)edge(2*i-1)
IF(edge(2*1-1).EO.max)THEN
WRITE(*,102)
edge(2*i-1)=save
ELSE
IF(save.NE.edge(2*i-1))ichang=1
END IF
ELSE
END IF
72
"FDESGN.FOR"
c Fetch the upper edge
601
IF (char. EO. 'U' .OR .char. EO .'u'.
ffi. iaii.EO.l)THEN
WRITE(*,601)i,edge(2*i)
save =edge(2* I)
FORMAT(!, 1X, 'upper edge tl', 12,' = ',F10.4, 1X,
' ... new value (no change=999): ',$)
READ(*,*)edge(2*i)
IF(edge(2*i).EO.max)THEN
WRITE(*, 102)
edge(2*i)=save
ELSE
IF(save.NE.edge(2*i))ichang=1
END IF
ELSE
END IF
C Fetch the desired function (slope If dlfferentiator)
IF (char .EO. T .OR .char .EO.' f'.
ffi . ia I I. EO. 1) THEN
WRITE(*,701)1,fx(i)
save = fx( I)
701
FORMAT(/, 1X, 'band function*', 12,' = ',F10.4, 1X,
' ... new value (no change=999): · ,$)
READ(*,*)fx(l)
IF(fx(I).EO.max)THEN
WRITE(*,102)
fx( I)=save
ELSE
IF(save.NE.fx(i))ichang=1
END IF
ELSE
END IF
C Fetch the weight function
751
IF(char .EO.'W' .ffi.char .EO.'w'.
ffi . ia II .EQ. 1)THEN
WRITE(*,751)i,wtx(l)
save = wtx( I)
FORMAT(/, lX, 'weight function*' ,12,' = ',F10.4,1X,
' ... new value (no change=999): ',$)
READ(*, *)wtx( I)
IF(wtx(I).EQ.max)THEN
WRITE(* ,102)
wtx( I)=save
ELSE
IF(save.NE.wtx(l))lchang=1
END IF
ELSE
END IF
73
"FDESGN.F~"
490
499
CONT INUE
CONTINUE
ELSE
END IF
GO TO 10
C Estimate the parameters section.
992
CONT INUE
IF(Ichange.EQ.O)THEN
IF(ifirst.EO. 1)THEN
WRITE(*,*)' ***ERROR ... 1Ilegal inputs***'
CALL pause
GO TO 10
ELSE
END IF
ELSE
END IF
C If trying to estimate a fl81ter not supported, displayed.
IF(jtype.NE. 1.0R.nbands.NE.2.0R.fx(1).LE.fx(2))THEN
Vii ITE(* ,991)
991
FORMAT(/, 1X,'Estimating Inputs is only avai !able for',
1
' IOwPaSs f II ters .')
CALL pause
GO TO 10
ELSE
END IF
C Display the parameters.
CALL cis
WRITE(*,900)nfl lt,jtype,ctype(Jtype+1),lgrid,nbands
WRITE(*,903)
DO 912 1=1,nbands
WRITE(*,901)l,edge(2*1-1),edge(2*l),fx(l),wtx(i)
912
CONTINUE
odflag=1
C Ca II the est Imat Jng program.
CALL flpest(nfl lt,edge,fx,wtx,h,err)
IF(err.NE.O)GO TO 10
lchang=1
C Fetch the fl lename to save the Input parameters.
996
CONT INUE
CALL cis
IF(Ichang.EQ. 1)THEN
DO 110 cc.. 1, 10
74
"FDESGN.FDR"
110
993
bfirfi(CC)=' '
I'IRITE(*,993)
FORMAT(lX,'Enter the file base name to save these ',
'parameters: ',$)
REA0(*,96)(bfirfl(cc),CC=l,6)
bf irf I(7)=' .'
bf irf 1(8)='F'
bf irf 1(9)=' 1'
bfirfl(lO)='X'
C Write the input parameters in the specified file.
CALL skip I
OPEN(2,FILE=firfi I,STATUS='NEW')
WRITE(2,900)nfi lt,jtype,ctype(jtype+l), lgrid,nbands
WRITE(2,903)
DO 702 l=l,nbands
WRITE(2,90l)l,edge(2*i-1),edge(2*i),fx(i),wtx(i)
702
CONT INUE
CLOSE(2)
C flag the error of trying to design with i I legal inputs.
ELSE
lf(iflrst.EO.l)THEN
WRITE(*,*)' ***ERROR ... i I legal inputs***'
CALL pause
GO TO 10
ELSE
END IF
END IF
C Execute the design program If only designing the fi Iter.
lf(odflag.EO.O)CALL cis
lf(ODFLAG.EO.O)WRITE(*,444)
444
FORMAT(/, 1X, 'Executing . . . ',$)
lf(odflag.EO.O)CALL flrrea(nfllt,Jtype,nbands,edge,
fx,wtx,h,nz,err)
CALL skipl
lf(err.NE.O)GO TO 94
c Wr 1te
the Inpu Ise response and output data to the specifIed f II e.
CALL flrwrt(nfl lt,jtype,nbands,edge,fx,wtx,
h,nz,deviat,bfirfl)
c Return
to the first menu.
odflag=O
GO TO 94
997
C Exit.
995
CONT I NUE
75
"FDESGN. FOR"
END
SUBROUTINE flpest(n,edge,fx,wtx,h,err)
C This program estimates the fi Iter inputs using the Rabiner equations.
COMMON /blockl/ dev,pl ,pl2
COMMON /block3/ lgrid,nfcns,ngrid
DIMENSION edge(20),fx(10),wtx(10),h(1027)
DOUBLE PRECISION accur,fs,fp,sd,pd,dev,pi,pi2
INTEGER n,select,err,cc
CHARACTER*l char,ans,bfirfl(lO)
CHARACTER*lO flrfl I
CHARACTER*26 ctype
EQUIVALENCE (firfi l,bfirfl)
C Initialize the flags.
err=O
C Initialize the variables.
fp=DBLE(edge(2))
fs=DBLE(edge(3))
ctype = 'llllllt lp le passband/stopband'
C Check for error conditions.
IF(n.EO.l)THEN
err=l
WRITE(*,*)' ***Divide by zero error: tsamples=l***'
CALL pause
RETURN
ELSE
END IF
IF(fp.EQ.fs)THEN
err=l
WRITE(*,*)' ***Divide by zero error: stopband=passband***'
CALL pause
76
"FDESGN.FOR"
RET~N
ELSE
END IF
C Display the menu for selecting which variable to estimate.
120
WRITE(*, 100)
110
WRITE(*,101)
100
FORMAT(I,1X,'FIR fi Iter parameter estimation program menu:',
1
I ,1X,'Est imat ion var table = ?',
1,1X,' *of samples (N) in the Impulse response',
' duration',
1
I ,1X,' (P)assband cutoff frequency',
2
3
I ,1X,' (S)topband cutoff frequency',
4
l,lX,' passband (D)evlatlon',
I ,lX,' stopband (d)eviat ion' ,I)
5
FORMAT(1X,'Selectlon {(X)•exlt} •> ',$)
101
C Read the selected variable to estimate.
READ(*, 102)char
102
FORMAT(A1)
. C Or exit.
IF(char.EQ. 'X' .OR.char .EQ. 'x')THEN
err=1
RETURN
ELSE
END IF
2
IF(char .NE. 'N' .AND.char .NE. 'n' .At«J.char .NE .'P' .AND.
char. NE. 'p' .At{) .char .NE. 'S' .AND.char .NE. 's' .AND.
char.NE. 'D' .AND.char.NE. 'd')GO TO 110
CALL cis
c If not estimating the passband deviation, fetch it from the user.
IF(char .NE. 'D')THEN
180)
FORMAT(!, 1X, 'passband deviation: ',$)
READ(*,162)pd
FORMAT(f16.8)
IF(pd.EQ.O.O)THEN
err=1
WRITE(*,*)' ***Divide by zero error: passband dev=O***'
CALL pause
RETI.RN
ELSE
END IF
ELSE
END IF
~ITE(*,
180
162
77
"FDESGN.FOR"
C If not estimating the stopband deviation, fetch it from the user.
IF (char. NE. 'd' )THEN
WRITE(*, 190)
190
FffiMAT(!,1X, 'stopband deviation: ',$)
READ(* ,162)sd
ELSE
END IF
C Determine the accuracy needed.
WRITE(*,182)
182
FORMAT(/,1X, 'Enter the% accuracy criterion=>',$)
READ(*, 162)accur
accur=accur/100.0
IF(accur.EO.O.O)THEN
err=1
WRITE(*,*)' ***Divide by zero error: %accuracy=D***'
CALL pause
RETURN
ELSE
END IF
C Execute the appropriate subroutine.
CALL cis
l'IRITE(*,111)
111
FORMAT(/,' Executing ... ',$)
c Estimate fi Iter length.
IF(char .EO .'N' .llLchar. EO .'n')
CALL case1(n,fp,fs,pd,sd,accur,edge,fx,wtx,h,err)
C Estimate passband cutoff freQUency.
IF(char .EO. 'P' .~.char .EO. 'p')
CALL case2(n,fp,fs,pd,sd,accur,edge,fx,wtx,h,err)
C Estimate stopband cutoff frequency.
IF(char. EO. 'S' .OR.char .EO. 's')
CALL case3(n,fp,fs,pd,sd,accur,edge,fx,wtx,h,err)
c Estimate passband deviation.
IF(char.EO.'D')CALL case4(n,fp,fs,pd,sd,accur,edge,fx,wtx,h,err)
C Estimate stopband deviation.
IF(char.EO.'d')CALL case5(n,fp,fs,pd,sd,accur,edge,fx,wtx,h,err)
c Restore the global variables.
edge(2)•sngl(fp)
edge(3)=sngl(fs)
78
"FDESGN.FOR"
RETURN
END
SUBROUTINE case1(n,fp,fs,pd,sd,accur ,edge,fx,wtx,h,err)
c This
subroutine estimate the fl Iter length.
COMMON /block1/ dev,pi,pl2
COMMON /blook3/ lgrid,nfcns,ngrid
DIMENSION h(1027),edge(20),fx(10)
DIMENSION wtx(10),devlat(10)
DOUBLE PRECISION fd,fp,fs,pd,sd,sdnew,dev,pi,pi2
DOUBLE PRECISION da,fa,nx,accur,k,sdO,sdl,sd2
INTEGER n,jd,err
EQUIVALENCE (dev,sdnew)
c Calculate
N
fd ..fs-fp
nx:(da(pd,sd)/fd)-(fa(pd,sd)*fd*fd)
n=D INT (nX+ 1. 00+0)
C Clear the direction parameter
Jd:O
C Optimal estimation loop
100
CONTINUE
C Initial lze the Input parameters
k=pd/sd
edge(l)=O.O
edge(2):SNGL(fp)
edge(3)=SNGL(fs)
edge(4) .. 0.5
fx(l):l.O
fx(2) .. o.o
wtx(1).SNGL((1.0D+0)/k)
wtx(2):1.0
C Invoke the opt irna I des IbTl program
~ITE(*,111)
111
FORMAT(/, 1X)
CALL flrrea(n,2,2,edge,fx,wtx,h,nz,err)
IF(err.NE.O)RETURN
79
"FDESGN. FOR
II
C Compare the actual stopband deviation with that given within the tolerance
C If they don't compare, adjust Naccordingly
sdl}:sdnew
sd1=(sd-(sd*accur))
sd2=(sd+(sd*accur))
IF(sdO.LT.sd1)THEN
IF(jd.EC.1)GO TO 200
n=n-2
Jd=-1
ELSEIF(sdO.GT.sd2)then
n=n+2
IF(jd.EC.-1)GO TO 200
jd=1
ELSE
GO TO 200
END IF
GO TO 100
C The deviation constraints are satisfied, exit.
200
CONTINUE
CALL skip!
RETURN
END
SUBROUTINE case2(n, fp, fs,pd,sd,accur ,edge, fx,wtx,h,err)
C This subroutine estimates the passband cutoff frequency.
COt.MlN /b lock1/ dev ,p l,p 12
COMMON /block3/ lgrid,nfcns,ngrld
DIMENSION h(1027),edge(20),fx(10)
DIMENSION wtx(10),devlat(10)
DOUBLE PRECISION fd,fp,fs,pd,sd,accur,sdnew,dev,pi,pi2
DOUBLE PRECISION da,fa,nx,fdx,fdy,fst,k,sdO,sd1,sd2
INTEGER n,jd,err
EQUIVALENCE (dev,sdnew)
c ca IcuI ate passband cutoff frequency
nx=OBLE(n-1)
fdx=nx/(2.0*fa(pd,sd))
fdy=(4.0*fa(pd,sd)*da(pd,sd))/(nx*nx)
fd•fdx*(DSQRT(1.0+fdy)-1.0)
fp=fs-fd
80
"FDESGN.FOR"
C Initialize the frequency step size and direction parameter
fst=fd/10.0
jd=O
C Optimal estimation loop
100
CONTINUE
c Initialize the Input parameter variables
k=pd/sd
edge(1)=0.0
edge(2)=SNGL(fp)
edge(3)=SNGL(fs)
edge(4)=0.5
fx(1)=1.0
fx(2)=0.0
wtx(1)=SNGL((1.0Dt0)/k)
wtx(2)=1.0
C Invoke the optimal design program
WRITE(*, 111)
111
FORMAT(/,lX)
CALL firrea(n,2,2,edge,fx,wtx,h,nz,err)
IF(err .NE.O)RETURN
C Compare the actual stopband deviation with that given withlng the tolerance
C If they do not compare, adjust the passband cutoff freq. accordingly
sciO=sdnew
scl1=(sd-(sd*accur))
sci2=(Sdt(sd*accur))
IF(sdO.LT.scll)THEN
IF(jd.EC.l)fst=fst/3.0
fP=fP+fSt
Jd=-1
ELSEIF(sdO.GT.sd2)THEN
IF(jd.EQ.-l)fst=fst/3.0
fP=fp-fst
Jd=l
ELSE
GO TO 200
END IF
GO TO 100
c Constraints are satisfied, exit.
200
CONT IItlE
CALL sklpl
RETURN
END
SUBROUTINE case3(n,fp,fs,pd,sd,accur,edge,fx,wtx,h,err)
81
"FDESGN .FOR"
C This subroutine estimates the stopband cutoff frequency
COMMON /block1/ dev,pi,pl2
lblock3/ lgrid,nfcns,ngrid
CO~N
DIMENSION h(1027),edge(20),fx(10)
DIMENSION wtx(10),deviat(10)
DOUBLE PRECISION fd,fp,fs,pd,sd,accur,sdnew,dev,pi,pi2
DOUBLE PRECISION da,fa,nx,fdx,fdy,fst,k,sdO,sdl,sd2
INTEGER n, jd,err
EQUIVALENCE (dev,sdnew)
c Estimate the stopband cutoff freq.
nx=DBLE(n-1)
fdx=nx/(2.0*fa(pd,sd))
fdy=(4.0*fa(pd,sd)*da(pd,sd))/(nx*nx)
fd=fdx*(DSORT(1 .Otfdy)-1.0)
fs=fP+fd
c Initialize
the frequency step size and direction parameter
fst .. fd/10.0
Jd=O
C Optimal estimation loop
100
CONT INUE
C lnltil lze the Input parameters
k=pd/sd
edge(l ).. o. 0
edge(2)=SNGL(fp)
edge(3) .. SNGL(fs)
edge(4)=0.5
fx(1 )= 1.0
fx(2)=0.0
wtx(1)=SNGL((1.00+0)/k)
wtx(2)= 1.0
C Invoke the optimal design program
~ITE(*, 111)
111
FORMAT(/,1X)
CALL flrrea(n,2,2,edge,fx,wtx,h,nz,err)
IF(err.NE.O)RETURN
C Compare the actua I stopband dev Iat Ion wIth that g Ivem wIthIn the to Ierance
C If they do not compare, adjust the stopband freq. accordingly
82
"FDESGN.FOR"
sdD=scilew
sd1=(Sd-(sd*accur))
sd2=(Sd+(sd*accur))
IF(sdO.LT.sd1)THEN
IF(jd.EC.1)fst=fst/3.0
fs=fs-fst
Jd=-1
ELSEIF(sdO.GT.sd2)THEN
IF(jd.EC.-1)fst=fst/3.0
fs=fs+fst
Jd=1
ELSE
GO TO 200
END IF
GO TO 100
200
CONTINUE
C Constraints were satisfied, exit.
CALL skipl
RETURN
END
SUBROUTINE case4(n,fp,fs,pd,sd,accur,edge,fx,wtx,h,err)
C This sUbroutine estimates the passband deviation.
COMMON /block1/ dev,pl,pl2
COMMON /block3/ lgrld,nfcns,ngrid
DIMENSION h(1027),edge(20),fx(10)
DIMENSION wtx(10),deviat(10)
DOUBLE
DOUBLE
DOUBLE
DOUBLE
DOUBLE
PRECISION
PRECISION
PRECISION
PRECISION
PRECISION
fd,fp,fs,pd,dev,sd,accur,pi,pi2
a1,a2,a3,a4,a5,b1,b2
e1,e2,e3,g1,g2,g3,nx
dlogsd,beta,sdO,sd1,sd2
sdnew,k,km
INTEGER n,jd,err
EQUIVALENCE (dev,sdnew)
C Estimate the passband deviation
a1=5.3090-3
a2c7 .1140-2
a3--4. 7610-1
a4..-2.660-3
83
"FDESGN.FOR"
a&=-5 .9410-1
aS=-4.2780-1
b1=11.01217
b2=0.51244
logsd=DL0010(sd)
e1=(a1*1ogsd)+a4
e2=(a2*1ogsd)+a5
e3=(a3*1ogsd)+a6
g1=b1-(b2*1ogsd)
fd=fs-fp
g2=(e2-(b2*fd*fd))/e1
nx=DBLE(n-1)
g3=(e3-(g1*fd*fd)-(nx*fd))/e1
beta=(-g2/2.0)+DSORT((g2*g2/4.0)-g3)
Dd=10.0**beta
C Initialize the frequency step size and direction parameter
km=2.0
Jd=O
C Optimal estimation loop
100
CONT INUE
C Initialize the Input parameters
k=pd/sd
edge(l)=O.O
edge(2)~SNGL(fp)
edge(3)=SNGL(fs)
edge(4)=0.5
fx(1 )= 1.0
fx(2)=0.0
wtx(1)=SNGL((1.0e+0)/k)
wtx(2)=1.0
C Invoke the optima I des ig1 program
IIR ITE(*, 111)
111
FORMAT(/, lX)
CALL flrrea(n,2,2,edge,fx,wtx,h,nz,err)
IF(err .NE.O)RETURN
C Compare the actual stopband deviation with that given within the tolerance
C If they do not compare, adjust the deviation accordingly
sdO=sdnew
sdl•(sd-(sd*accur))
sd2=(sdt(sd*accur))
IF(sdO.LT.sdl)THEN
IF(jd.EQ.l)km=km/2.0
84
"FDESGN .FOR"
k=l</ (1.O+km)
Jd=-1
ELSEIF(sdO.GT.sd2)THEN
IF(Jd.E0.-1)km=km/2.0
k=k*(1.0tkm)
jd=1
ELSE
GO TO 200
END IF
GO TO 100
C The constraints are satisfied, exit
200
CONTINUE
CALL skipl
RETURN
END
SUBROUTINE case5(n,fp,fs,pd,sd,accur,edge,fx,wtx,h,err)
C This subroutine estimates the stopband deviation.
COMMON /block1/ dev,pi,pi2
COMMON /block3/ lgrid,nfcns,ngrid
DIMENSION h(1027),edge(20),fx(10)
DIMENSION wtx(10),DEVIAT(10)
DOUBLE
DOUBLE
DOUBLE
DOUBLE
DOUBLE
PRECISION
PRECISION
PRECISION
PRECISION
PRECISION
fp,fs,pd,sd,dev,pl,pi2
alpha,nx, logpd,slogpd
a1,a2,a3,a4,a5,a6,b1,b2
c1,c2,d1,accur,pd0,pd1,pd2
sdnew,k,km
INTEGER n,jd,err
EOU IVALENCE (dev ,sdnew)
a1=5.309D-3
a2=7 .1140-2
a3--4.761D-1
a4=-2.660-3
aS=-5.9410-1
aS=-4.2780-1
b1=11.01217
b2=0.51244
lo~d=DL0010(pd)
s Iogpd•l ogpd* Iogpd
cls(al*slogpd)+(a2*1o~d)+a3
85
"FDESGN .FOR"
c2=(a4*slogpd)+(a5*1ogpd)+a6
dl=bl+(b2*1ogpd)
nx=DBLE(n-1)
fd=fs-fp
alpha=((nx*fd)+(d1*fd*fd)-c2)/(c1+(b2*fd*fd))
sd= 10 .O**a lpha
C Initialize the frequency step size and direction parameter
km=2.0
Jd=O
C Optimal estimation loop
100
CONTINUE
C Initialize the Input parameters
k•pd/Sd
edge(l )=0. 0
edge(2)=SNGL(fp)
edge(3)aSNGL(fs)
edge(4)=0.5
fx(l )= 1.0
fx(2)=0.0
wtx(1)=SNGL((1.00+0)/k)
wtx(2)=i .0
C Invoke the opt irna I des Ig1 program
~ITE(*,111)
111
FORMAT(/, 1X)
CALL flrrea(n,2,2,edge,fx,wtx,h,nz,err)
IF(err .NE.O)RETURN
C Compare the actual stopband devlat lon with that given within the tolerance
C If they don't compare, adjust the devlat lon accordingly
pdO.sdnew/DBLE(wtx(1))
Ddl=(Pd-(pd*accur))
pd2=(Pd+(pd*accur))
IF(pdO.LT.pd1)THEN
IF(jd.EQ.1)km-km/2.0
kzk*(1.0+km)
Jd·-1
ELSEIF(pdO.GT.pd2)THEN
IF(jd.EQ.-1)km=km/2.0
k•k/{l.O+km)
Jd=l
ELSE
GO TO 200
END IF
GO TO 100
86
"FDESGN .FOR"
C Constraints were satisfied, exit
200
CONTINUE
CALL skipl
RETURN
END
DOUBLE PRECISION FUNCTION da(pd,sd)
DOUBLE PRECISION pd,sd,a1,a2,a3,a4,a5
DOUBLE PRECISION logpd, logsd,slogpd,slogsd
a1=5.309D-3
a2=7 .1140-2
a3=-4.761D-1
a4=-2.66D-3
a5=-5.941D-1
aS=-4. 2780-1
log:Jd=DLOG10(pd)
s Iogpd= Iogpd* Iogpd
logsd=DLOG10(sd)
slogsd=logsd*logsd
da=(((a1*slogpd)+(a2*1ogpd)+a3)*1ogsd)
+((a4*slogpd)+(a5*1ogpd)+a6)
RETURN
END
DOUBLE PRECISION FUNCTION fa(pd,sd)
DOUBLE PRECISION pd,sd,b1,b2, logpd, logsd
b1= 11.01217
b2=0.51244
logpd=DLOG10(pd)
logsd=DLOG10(sd)
fa=b1+(b2*1ogpd)-(b2*1ogsd)
RETURN
END ·
87
"FDESGN.FOR"
SUBROUTINE flrrea(nfi lt,jtype,nbands,edge,
fx,wtx,h,nz,err)
C This program is used for the design of I inear phase finite impulse
C response (fir) fl lters using the remez exchange algorithm (rea) by
C Jim McCiel ian, Rice University, Apri I 13,1973. Three types of
C filters are included: bandpass filters, differentiators and hilbert
C transform fl lters. This program (firrea) is a modified version of
C the program found In IEEE trans. audio eletroacoust., vol .au-21,
C pp. 50~526, Dec. 1973, "a computer program for designing optimum FIR
C linear phase digital filters".
COMMON /block1/ dev,pi ,pi2
COMMON /block2/ ad,x,y,grid,des,wt,alpha, iext
COMMON /block3/ lgrid,nfcns,ngrid
DIMENSION
DIMENSION
DIMENSION
DIMENSION
DIMENSION
lext(1027),ad(1027),alpha(1027),x(1027),y(1027)
h(1027)
des(8216),grid(8216),wt(8216)
edge(20),fx(10),wtx(10)
ctype( 4)
DOUBLE PRECISION pi2,PI
DOUBLE PRECISION ad,dev,x,y,change
INTEGER err
err=O
C calculate 11 of approximating flllctions (depends on fi Iter type
C and fi Iter length) nfcns .. length/2 (rounded up to even If bandpass)
neg=1
IF(Jtype.E0.1)neg=O
IF(MOO(nfi lt,2).EO. 1)nodd=1
IF(nfl lt.EO.O)THEN
WRITE(*,*)' Divide by zero error: fi Iter length=
CALL pause
err=1
RETURN
ELSE
END IF
o·
88
"FDESGN.FOR"
nfcns =nf II t/2
IF(nodd.EO.l.AND.neg.EO.O)nfcns=nfcnstl
C Set up dense grid: (*points In grid= fi Iter length+ l)*grid dens/2
grid(l)=edge(l)
IF(FLOAT(Igrid*nfcns).EO.O.O)THEN
WRITE(*,*)' Divide by zero error: grid density or',
' *approx fncts = 0'
CALL pause
err=l
RETURN
ELSE
END IF
delf=0.5/FLOAT(Igrid*nfcns)
C grld(l)
= larger (edge(1) and delf) If not bandpass
lf(neg.NE.O)THEN
IF(edge(l).LE.delf)grid(1)=delf
ELSE
END IF
J=l
lz1
lband=1
140
fup:edge(l+1)
C calculate the
Con the grid
145
desired magnitude response and the weight fl.Ulction
temp=grid(J)
des(J)=eff(temp,fx, lband,Jtype)
wt(J)=wate(temp,fx,wtx, lband,Jtype,err)
IF(err.NE.O)RETURN
J· J+ 1
grld(J)=temp+delf
IF(grld(j).LE.fup)GO TO 145
grld(J-l)=fup
des(j-l)zeff(fup,fx,lband,jtype)
wt(J-1)=wate(fup,fx,wtx,lband,jtype,err)
IF(err.NE.O)RETURN
Jband=lband+l
1·1+2
IF(Iband.GT.nbands)THEN
ngrld=J-1
IF(neg.EQ.nodd)THEN
IF(grld(ngrld).GT.(0.5-delf))ngrld=ngrid-1
89
"FDESGN.FOR"
ELSE
END IF
ELSE
grld(j)=edge(l)
GO TO 140
END IF
C Set up a new approximation problem which Is equivalent to the
problem
IF(neg)170,170, 180
C original
C passband type; even length
170
IF(nodd.E0.1)GO TO 200
DO 175 J=1,ngrld
change=DCOS(pi*DBLE(grid(j)))
IF(change.EO.O.O)THEN
WRITE(*,*)' Divide by zero error: cos(pi*grid(' ,j, ') = 0'
CALL pause
errz1
RETURN
ELSE
END IF
des(J)-des(J)/change
wt(j)zwt(j)*change
175
CONTINUE
GO TO 200
C differentiator or hilbert; even length
180
lf(nodd.E0.1)GO TO 190
DO 185 J=1,ngrld
change=DSIN(pi*DBLE(grld(j)))
lf(change.EO.O.O)THEN
WRITE(*,*)' Divide by zero error: sln(pl*grid(' ,j,') = 0'
CALL pause
err=1
RET~N
ELSE
END IF
des(j)-des(J)/change
wt(J)=wt(j)*change
185
CONTI NUE
90
"FDESGN.FOR"
GO TO 200
c diff or hilbert: odd length
190
DO 195 j=1,ngrid
change=DSIN(pi2*DBLE(grid(j)))
IF(change.EO.O.O)THEN
WRITE(*,*)' Divide by zero error: sin(pi*2*grid(' ,j, ') = 0'
CALL pause
err=1
RETURN
ELSE
END IF
des(j)=des(j)/change
wt(j)=wt(j)*change
195
CONTINUE
C Initial guess for the extremal fequencies--equal ly spaced along
C The grid. The extremal frequencies location Is recorded by keeping
C track of the Index of the frequency in the grid array. The Indices
Care stroed in the lext array. Initial guess is nfcnst1 equally
C spaced Index values
200
CONTINUE
IF(FLOAT(nfcns).EO.O.O)THEN
WRITE(*,*)' Divide by zero error: •approx. fncts = 0'
CALL pause
err=1
RETURN
ELSE
END IF
temp=FLOAT(ngrid-1)/FLOAT(nfcns)
DO 210 J=l,nfcns
lext(j)=IFIX(FLOAT(j-1)*temP+1.0)
210
CONTINUE
lext(nfcnst1)=ngrid
nm1=nfcns-1
nz=nfcnst 1
C Cal I the remez exchange algorithm to do the approximation problem
CALL remez(err)
c calculate the
Impulse response
IF(neg)300,300,320
91
"FDESGN. FOR"
C passband; odd length
300
IF(nodd.EQ.O)GO TO 310
00 305 J= 1,rwn 1
h(J)=0.5*alpha(nz-J)
CONTINUE
305
h(nfcns)=alpha(1)
TO 350
GO
C passband; even length
310
h(1)=0.25*alpha(nfcns)
00 315 J=2,rwn1
h(j)=0.25*(alpha(nz-j)talpha(nfcnst2-j))
315
CONTINUE
h(nfcns)=0.5*alpha(1)+0.25*alpha(2)
TO 350
GO
C diff or hi !bert; odd length
320
IF(nodd.EQ.O)GO TO 330
h(1)=0.25*alpha(nfcns)
h(2)=0.25*alpha(nm1)
00 325 J=3,rwn1
h(J)=0.25*(alpha(nz-j)-alpha(nfcnst3-j))
325
CONT INUE
h(nfcns)=0.5*alpha(1)-0.25*alpha(3)
h(nz)=O.O
GO TO 350
C diff or hi !bert; even length
330
335
h(1)=0.25*alpha(nfcns)
00 335 J=2,rwn1
h(J)=0.25*(alpha(nz-J)-alpha(nfcnst2-J))
CONT INUE
h(nfcns)=0.5*alpha(1)-0.25*alpha(2)
C Return
350
CONT INUE
RETURN
END
92
"FDESGN .FOR"
FUNCTION eff(temp,fx, lband,jtype)
C Function to calculate the desired magnitude response as a
C function of frequency
DIMENSION fx(5)
c passband or hI Ibert
IF(jtype.NE.2)THEN
eff=fx( Iband)
C differentiator
ELSE
eff=fx(lband)*temp
END IF
RETURN
END
FUNCTION wate(temp,fx,wtx,lband,jtype,err)
C Function to calculate the weight function as a function
C of frequency
DIMENSION fx(5),wtx(5)
INTEGER err
C passband or hilbert
IF(jtype.NE.2)THEN
wate=wtx( Iband)
C differentiator
ELSE
IF(fX(Iband).GE.O.OOOl)THEN
IF(temp.EQ.O.O)THEN
WRITE(*,*)' Divide by zero error: weighted freq. = 0'
CALL pause
err=l
RET~N
ELSE
END IF
wate=wtx( lband)/temp
ELSE
wate=wtx( Iband)
END IF
END IF
93
"FDESGN.FOR"
RETURN
END
SUBROUTINE remez(error)
C This slbrout ine imp Iaments the remez exchange a Igor ithm for the
c weighted chebychev approximation of a continuous function with a
c sum of cosines. Inputs to the subroutine are a dense grid which
C replaces the frequency axis, the desired function on the grid,
C the weight function on the grid, the number of cosines, and an
C initial guess of the extremal frequencies. The program minimizes
C the chebychev error by determining the best location of the
C extremal frequencies (points of maximum error) and then calculates
C the coefficients fo the best approximation.
COMMON /block1/ dev,pi,pl2
COMMON /block21 ad,x,y,grid,des,wt,alpha, lext
COMMON /block3/ lgrid,nfcns,ngrid
Dlt.fNSION
DIMENSION
DIMENSION
DIMENSION
edge(20)
lext(1027),ad(1027),alpha(1027),x(1027),y(1027)
des(8216),grid(8216),wt(8216)
a(1027),p(65),Q(65)
DOUBLE PRECISION pi,pl2,dnum,dden,dtemp,a,p,Q
DOUBLE PRECISION ad,dev,x,y,devl
DOUBLE PRECISION d,gee
INTEGER error
error=O
C The program allows a maximum number of Iterations of 25
ltrmax=25
dev 1=-1. 00+00
nz..nfcns+1
nzz=nfcns+2
C Initialize Iteration count
nlter=O
C Main Iteration loop
100
CONT IN.JE
lext(nzz)-ngrld+1
nlter=nlter+1
C Go to calculate coefficients of best approximation
IF(nlter .GT.Itrmax)GO TO 400
94
"FDESGN. FOR"
C Calculate the abscissae for the lagrange Interpolation, where grid(iext(j))
Care the extremal frequencies
00 110 J=1,nz
x(j)=DCOS(DBLE(grid(iext(j)))*pi2)
110
CONTINUE
c Calculate lagrange interpolation coefficients
jet=(nfcns-1)/15+1
00 120 J=1,nz
ad(J)=d(j,nz,jet,error)
IF(error.NE.O)THEN
WRITE(*,*)' Divide by zero error: d function'
CALL pause
error=1
RETURN
ELSE
END IF
120
CONT INUE
C Calculate the current value of the deviation
ctlt.JII=O. 00+00
dclen=O .00+ 00
k=1
00 130 J=1,nz
I= iext(j)
dhum=dnum+ad(J)*DBLE(des(l))
IF(DBLE(wt(I)).EO.O.O)THEN
WRITE(*,*)' Divide by zero error: weight funct(', I,') = 0'
CALL pause
error=1
REMN
ELSE
END IF
dden=dden+DBLE(FLOAT(k))*ad(j)/DBLE(wt(l))
k=-k
130
CONT INUE
c Record sign of deviation and take the absolute value of deviation
IF(dden.EO.O.O)THEN
WRITE(*,*)' Divide by zero error: dden = 0'
CALL pause
error=1
RETURN
ELSE
END IF
dev•dhum/dden
IF(dev.GT.O.OD+OO)THEN
no..-1
.
'
95
"FDESGN.FOR"
ELSE
no"1
END IF
dev=-DBLE(FLOAT(no))*dev
C Calculate ordinates for lagrange Interpolation
k=rxl
00 140 J=1 ,nz
I= lext(J)
IF(DBLE(wt(I)).EO.O.O)THEN
WRITE(*,*)' Divide by zero error: weight funct(', I,') = 0'
CALL pause
error=1
REMN
ELSE
END IF
y(J)•DBLE(des(I))+DBLE(float(k))*dev/DBLE(wt(l))
k·-1<
140
CONT I NUE
C Deviation is less than last deviation; go to calculate coefficients
C of best approximation
IF(dev.LE.devi)THEN
CALL ouch
GO TO 400
END IF
C For the 1st extremal freq, set upper kup and lower klow I imits
Con the grid points to be searched
150
deY I=deY
Jcmge=D
k1•1ext(1)
knz=lext(nz)
kiOW=O
rut=-no
jo:1
C Search for the extrema I frequencies of the best approximation
C (search loop)
200
CONT I NUE
MUTE(*, 111)
111
FORMAT('.',$)
IF(j.EO.nzz)ynz=eomp
IF(j.LT.nzz)THEN
C Evaluate freq response at grid pt adjacent to the Jth extremal
C freq then calculate the weighted error
96
"FDESGN .FOR"
Iext( J+ 1)
l=lext(j)+1
ll.lt=-nut
k~=
IF(j.E0.2)y1=comp
comp=dev
C Search L+2,L+3, ... k~ for local maximum. Change this extremal freq.
C and return to top of Iteration loop
IF( I.L T.kup)THEN
err=gee(l,nz,error)
IF(error.NE.O)THEN
WRITE(*,*)' Divide by zero error: function ga'
CALL pause
error=1
RET~N
ELSE
END IF
err=(err-des(l))*wt(l)
C If deviation less than the error at jth extremal freq, calculate
C freq response and weighted error at Index k-1
IF(Il.lt*err.GT.comp)THEN
comp:ll.lt*err
21 0
CONTINUE
IF(I.LT.k~)THEN
err.. gee( l,nz ,error)
IF(error .NE.O)THEN
WRITE(*,*)' Divide by zero error: function gee'
CALL pause
error=1
RETURN
ELSE
END IF
err=(err-des(l))*wt(l)
IF(nut•err.gt.comp)THEN
co~=nut•err
TO 210
ELSE
END IF
GO
ELSE
END IF
215
lext(J)=I-1
Jo:J+l
97
"FDESGN. FOR"
kloW=I-1
jchlge= jchnge+ 1
GO TO 200
ELSE
END IF
ELSE
END IF
C Search L-2,L-3, ... ,klow for local maximum. If found, change this extremal
C freq. and return to top of loop
220
1=1-1
225
1=1-1
IF( I .GT.klow)THEN
err-gee( I,nz,error)
IF(error.NE.O)THEN
WRITE(*,*)' Divide by zero error: function gb'
CALL pause
error=1
RET~N
ELSE
END IF
err=(err-des(l))*wt(l)
IF(nut*err.LE.comp)THEN
IF(jchnge.LE.O)GO TO 225
kloW= lext(j)
J=J+1
GO TO 200
ELSE
END IF
230
235
comp:nut*err
1=1-1
IF(I.GT.klow)THEN
err-gee( l,nz,error)
IF(error.NE.O)THEN
WRITE(*,*)' Divide by zero error: function gc'
CALL pause
error=1
RETURN
ELSE
END IF
err=(err-des(l))*wt(l)
IF(nut*err.GT.comp)THEN
comp=nut*err
GO TO 235
98
"FDESGN. FOR"
ELSE
END IF
ELSE
END IF
240
klOW= lext(j)
lext(J)= I+ 1
j=j+1
Jcmge=jcmge+1
GO TO 200
ELSE
END IF
C Previous searches failed. Increment Index and search either L+2,L+3, ... kup
Cor L-2,L-3, ... klow as teh case may for local maximum
250
l=iext(J)+1
IF(jchnge.GT.O)THEN
lext(J)=I-1
J=J+1
kloW=I-1
jcmge= jcmge+ 1
·oo TO 200
ELSE
END IF
255
1=1+ 1
IF(L.LT.kup)THEN
err=gee(l ,nz,error)
IF(error.NE.O)THEN
WRITE(*,*)' Divide by zero error: function
CALL pause
error=1
gd'
RET~N
ELSE
END IF
errz(err-des(l))*wt(l)
IF(nut•err.GT.comp)THEN
CORP=nut•err
258
1=1+1
IF(I.LT.kup)THEN
err-gee(l,nz,error)
IF(error.NE.O)THEN
WRITE(*,*)' Divide by zero error: function gee'
CALL pause
error=l
99
"FDESGN. FOR"
RETURN
ELSE
END IF
err=(err-des(l))*wt(l)
lf(nut*err .gt.comp)THEN
comp=nut*err
GO TO 258
ELSE
END IF
ELSE
END IF
iext(j)=l-1
J=J+1
klow=l-1
jct'nge=jchnge+1
GO TO 200
ELSE
TO 255
END IF
GO
ELSE
END IF
260
kloW=Iext(j)
J=J+1
GO TO 200
ELSE
END IF
C Search the endpoints for the extremal freqs. of the best approximation
C Start by searching those Indices that are less than the minimum of lext(1)
C and new iext(1) for a local maximum of the error curve with sign -sign(1)
C and greater than the error at new lext(1)
lf(j.LE.nzz)THEN
k1=MIN(k1,1ext(1))
ll.lt 1=nut
ll.lt=-no
1=0
kUP=k1
comp=ynz*(1.00001)
luck=1
310
1=1+1
lf(I.LT.kup)THEN
err-gee(l,nz,error)
lf(error.NE.O)THEN
WRITE(*,*)' Divide by zero error: function ge'
CALL pause
100
"FDESGN. FOR"
error=1
RET~N
ElSE
END IF
err=(err-des(l))*wt(l)
IF(nut•err.lE.comp)GO TO 310
col!lp=nut•err
J=nzz
313
1=1+1
IF(I.lT.kup)THEN
err=gee(l,nz,error)
IF(error.NE.O)THEN
WRITE(*,*)' Divide by zero error: function gee'
CAll pause
error=1
RETURN
ElSE
END IF
err=(err-des(l))*wt(l)
IF(nut•err.gt.comp)THEN
comp=nut •err
GO TO 313
ElSE
END IF
ElSE
END IF
lext(J)=I-1
J=J+1
klaw=l-1
jctllge= jcmge+ 1
GO TO 200
ElSE
END IF
315
luck=B
ElSE
320
IF(Iuck.GT.9)THEN
kn=lext(nzz)
00 360
J= 1, nfcns
fext(J)=fext(J+1)
360
CONTINUE
lext(nz)=kn
GO TO 100
101
"FDESGN. FOR"
E~IF
IF(comp.GT.yl)Yl=comp
kl=iext(nzz)
END IF
C Search those indices greater than the maximum of iext(n) and new iext(n)
C for the local max of the error curve with sign -sign(n) and the error
C greater than or equal to the error at the new iext(n) and the error found
C previously
325
l=ngridtl
klow=knz
nut=-nutl
comp=yl*(l.OOOOl)
330
1=1-1
IF( I.GT.klow)THEN
errcgee( l,nz,error)
IF(error .NE.O)THEN
WRITE(*,*)' Divide by zero error: function gf'
CALL pause
error=l
RET~N
ELSE
END IF
err=(err-des(l))*wt(l)
IF(nut*err.LE.comp)GO TO 330
J=nzz
conp=nut *err
IUck=lucktlO
238
I= 1-1
IF( I.GT.klow)THEN
err=gee(l,nz,error)
IF(error .NE.O)THEN
.
WRITE(*,*)' Divide by zero error: function gc'
CALL pause
error=l
RETURN
ELSE
END iF
errm(err-des(l))*wt(l)
IF(nut*err.GT.comp)THEN
conp=nut*err
GO TO 238
ELSE
END IF
ELSE
END IF
klow-=lext(J)
102
"FDESGN.FOR"
lext(J)=i+1
j=J+1
jchnge= jchnge+ 1
GO TO 200
ELSE
END IF
340
345
IF(Iuck.NE.6)THEN
DO 345 j=1,nfcns
iext(nzz-J)=iext(nz-J)
CONTINUE
iext(1)=k1
GO TO 100
ELSE
END IF
IF(jchnge.GT.O)GO TO 100
C Calculution of the coefficents of the best approximation using
C the inverse discrete fourier transform
400
CONTINUE
nm1=nfcns-1
fsh= 1. 00-06
gternp:gr ld(1)
x(nzz)=-2. 00+00
cn=2*nfcns-1
IF(cn.EQ.O.O)THEN
WRITE(*,*)' Divide by zero error: 2*nfcns-1 = 0'
CALL pause
error=l
RETURN
ELSE
END IF
delf=l.O/cn
1=1
IF(grld(1).LT.0.01.AND.grld(ngrld).GT.0.49)THEN
kkk=1
ELSEIF(nfcns.LE.3)THEN
kkk=l
ELSE
kkkaQ
dtemp..DCOS(pi2*DBLE(grid(1)))
dhum=DCOS(pi2*DBLE(grld(ngrid)))
IF ({dtemp-cil.un) .EO .O.O)THEN
WRITE(*,*)' Divide by zero error: cos(pl2*grid(1))',
'-cos(pl*2*grld(',ngrid, ') = 0'
CALL pause
103
"FDESGN.FOR"
error=l
RET~N
ELSE
END IF
aa=2.0d+OO/(dtemp-dnum)
bb=-(dtemptdnum)/(dtemp-dnum)
END IF
00 430 J=1 ,nfcns
ft=( J-1 )*de If
xt=DCOS(pi2*DBLE(ft))
IF(kkk.NE. 1)THEN
IF(aa.EO.O.O)THEN
WRITE(*,*)' Divide by zero error: 2/(cos(pi2*grid(1))',
'-cos(pl*2*grld(' ,ngrld, ')) = o·
CALL pause
error-1
RET~N
ELSE
END IF
xt=(xt-bb )Iaa
xt1=SORT(1.0-xt*xt)
ft=DBLE(ATAN2(xt1,xt))/pi2
ELSE
END IF
410
xe=x(l)
IF(ABS(xe-xt).LT.fsh)THEN
a(J)=Y(1)
ELSEIF(xt.GE.xe)THEN
gr id(l)=ft
a(J)~gee(l,nz,error)
IF(error.NE.O)THEN
WRITE(*,*)' Divide by zero error: function gg'
CALL pause
error=1
REMN
ELSE
END IF
ELSE
1=1+1
GO TO 410
END IF
IF(I.GT.1)1=1-1
430
CONTI NUE
gr ld(l )=gtenp
104
"FDESGN .FOR"
500
510
IF(DBLE(cn).EQ.O)THEN
WRITE(*,*)' Divide by zero error: 2*nfcns-1 = 0'
CALL pause
error=l
RETURN
ELSE
END IF
dden=pi2/DBLE(cn)
DO 510 j=l,nfcns
dtemp:O.OOtOO
dnum=DBLE(float((j-l)))*dden
IF(nml.GE. l)THEN
DO 500 k= 1,nm1
dtemp=dtempta(ktl)*DCOS(dnum*k)
CONT INUE
dtemp=2.00tOO*dtempta(1)
ELSE
END IF
alp')a(j)=dtemp
CONTINUE
DO 550 J=2,nfcns
IF(DBLE(cn).EQ.O.O)THEN
WRITE(*,*)' Divide by zero error: 2*nfcns-1 = 0'
CALL pause
error=l
RETURN
ELSE
END IF
alpha(j)=2*alp1a(j)/cn
550
CONT INUE
IF(DBLE(cn).EQ.O)THEN
WRITE(*,*)' Divide by zero error: 2*nfcns-1 = 0'
CALL pause
error=l
RETURN
ELSE
END IF
alpha(l)=alpha(l)/cn
IF(kkk.NE. l)THEN
p(l)=DBLE(2.0*alpha(nfcns)*bbtalpha(nml))
p(2)c0BLE(2.0*aa*alpha(nfcns))
q(l)=DBLE(alpha(nfcns-2)-alpha(nfcns))
DO 540 J=2,nnl
IF(j.GE.nml)THEN
105
"FDESGN .FOR"
aa=0.5*aa
bb=0.5*bb
ELSE
END IF
p( j+ 1)=0. OOtOO
DO 520 k= 1,j
a(k)=p(k)
p(k)=DBLE(2.0*bb)*a(k)
520
CONT INUE
p(2)=p(2)+a(1)*DBLE(2.0*aa)
jm1= j-1
00 525 k= 1, jm1
p(k)=p(k)+q(k)+DBLE(aa)*a(k+1)
525
CONTINUE
JP1=J+1
00 530 k=3, jp1
p(k)=p(k)tDBLE(aa)*a(k-1)
530
CONT IM.JE
IF(j.NE.nm1)THEN
DO 535 k= 1, j
q(k)=-a(k)
535
CONTINUE
q(1)=Q(1)+DBLE(alpha(nfcns-1-J))
ELSE
END IF
540
CONTINUE
DO 543 J=l,nfcns
alpha(J)=P(J)
543
CONTINUE
ELSE
END IF
IF(nfcns.LE.3)THEN
alpha(nfcns+l)=O.O
alpha(nfcnst2)=0.0
ELSE
END IF
106
"FDESGN.FOR"
RETURN
END
DOUBLE PRECISION FUNCTION d(k,n,m,err)
C Function to calculate the lagrange interpolation coefficients
C for use in the function gee
COMMON /blockl/ dev,pi,pl2
COMMON lblock2/ ad,x,y,gr id,des,wt ,alpha, iext
COMMON lblock3/ lgrid,nfcns,ngrid
DIMENSION iext(1027),ad(1027),alpha(1027),x(1027),y(1027)
DIMENSION des(8216),grid(8216),wt(8216)
DOUBLE PRECISION ad,dev,x,y
DOUBLE PRECISION q
DOUBLE PRECISION pi ,pl2
INTEGER err
d=l.CD+OO
Q=X(k)
1
2
3
D031=1,m
DO 2 J=l ,n,m
IF(j-k)1,2, 1
D=2.00+00*0*(0-X(J))
CONTINUE
CONTINUE
IF(d.EQ.O.O)THEN
err=l
RETURN
END IF
d-1 . CD+OO/d
RETURN
END
DOUBLE PRECISION FUNCTION gee(k,n,err)
C FunctIon to evaluate the frequency response using the Iagrange
C interpolation formula In the barycenlrlc form
COMMON /blockl/ dev,pi,pl2
COMMON lblock2/ ad,x,y,gr ld,des,wt,alpha, iext
COMMON /block3/ lgrld,nfcns,ngrid
107
"FDESGN .FOR"
DIMENSION lext(1027),ad(1027),alpha(1027),X(1027),y(1027)
DIMENSION des(8216),grid(8216),wt(8216)
DOUBLE PRECISION p,c,d,xf
DOUBLE PRECISION pl,pi2
DOUBLE PRECISION ad,dev,x,y
INTEGER err
c
c
c
P=0.00+00
xf-DBLE(grid(k))
Xf=DCOS(pi2*Xf)
ci=O. 00+00
DO 1 J= 1,n
C=Xf-X(j)
IF(SNGL(c).EC.O.O)THEN
err= 1
write(*,*)' divide by c'
RETURN
e=l.OD-10
END IF
ezad(J)/c
(l..d+c
P=P+C*y(j)
CONTINUE
IF(SNGL(d).EC.O.O)THEN
err=2
write(*,*)' divide by d'
RETURN
END IF
gee=p/d
c
c
c
RETURN
END
SUBROOT INE OOCH
CALL cis
~ITE(*,l)
c
c
.
fORMAT(//,' ************FAILURE TO CONVERGE************',/,
1
1X,'Probable cause Is machine rounding error',/,
2
1X, 'The Impulse response may be correct',/,
3
1X, 'Check wIth a f re~ency response' )
CALL pause
RETURN
END
SUBROOT INE SK IPl
108
"FDESGN. FOR"
c
IIIRITE(*, 10)
10
c
c
c
c
10
FORMAT{!)
RETURN
END
SUBROUTINE CLS
WRITE(*, 10)
FORMAT(26{1))
RETURN
END
SUBROUTINE flrwrt(nfi lt,Jtype,nbands,edge,fx,wtx,
h,nz,devlat,xfirfl)
COMMON /b Iock 1/ dev ,pi, Pi 2
COMMON /block2/ ad,x,y,grld,des,wt,alpha,iext
COMMON lblock3/ lgrid,nfcns,ngrid
DIMENSION
DIMENSION
DIMENSION
DIMENSION
iext(1027),ad(1027),alpha(1027),x(1027),y(1027)
h(1027)
des(8216),grld(8216),wt(8216)
edge(2D),fx(10),wtx(10),DEVIAT(10)·
DOUBLE PRECISION ad,dev,x,y,change,pi2,pi
CHARACTER*1 char,bfirfl(10),xfirfl(10)
CHARACTER*10 flrfil
CHARACTER*26 ctype{4)
INTEGER cc
EQUIVALENCE (flrfll,bfirfl)
10
DO 10 CC=1, 10
bflrfi(CC)=Xflrfi(CC)
ctype(1)
E
'none
109
"FDESGN. FOR"
ctype(2) = 'multiple passband/stopband'
ctype(3) = 'differentiator
ctype(4) = 'hilbert transform fi Iter '
neg=1
IF(jtype.E0.1)neg=O
IF(MOD(nfl lt,2).EO. 1)nodd=l
nfcns -nf i It/2
IF(nodd.E0.1.AND.neg.EO.O)nfcns=nfcns+1
C Program output section
350
CONTINUE
bfirfi(B)='F'
bflrfl(9)='0'
bfirfi(10)='X'
OPEN(l,FILE=firfi I,STATUS='NEW')
IIIRITE(1,360)
360
FORMAT(1H1,70(1H*),//,25X, 'Finite Impulse response (fir)',/,
1
25X,'IInear phase digital filter design',/,
2
25X ,' remez exchange a Igor ithm' ,/)
WRITE(1,365)ctype{jtype+1)
365
FORMAT(25X,A26,/)
WRITE(1,378)nfl It
378
FORMAT(15X,'FIIter length= ',13,/)
WRITE(*,379)flrfl I
379
FORMAT{/, 1X,' Impulse response Is being stored in file: '
1
A10,' ... ')
WRITE( 1, 380)
380
FORMAT(20X, '***** Impulse response *****')
00 381 jz 1,nfcns
k·nfllt+1-J
neg=1
IF(jtype.E0.1)NEG=0
IF(neg.EO.O)WRITE(1,382)j,h(j),k
IF(neg.EO.l)WRITE(1,383)j,h(j),k
381
CONTINUE
382
FORMAT(20X,'H(', 13,') .. ',E15.8,' = H(' ,14, ')')
383
FORMAT(20X,'H(',I3,') = ',£15.8,' "'-H(',I4,')')
IF(neg.EQ. 1.AND.nodd.EO.l)WRITE(1,384)nz
384
FORMAT(20X, 'H(' ,13, ') .. 0.0')
00 450 k=l,roands,4
klJPKk+3
IF(kup.GT.roands)kup=nbands
WRITE(1,385)(J,J•k,kup)
385
FORMAT{/,24X,4('Band' ,13,8X))
WRITE(1,390){edge(2*j-1),J=k,kup)
390
FORMAT(2X, 'Lower band edge' ,5F15.9)
WRITE(1,395)(edge(2*j),J•k,kup)
395
FORMAT(2X, 'l.pper band edge' ,5F15.9)
IF(jtype.NE.2)WRITE(1,400)(fx(j),j=k,kup)
110
"FDESGN .FOR"
400
410
420
425
430
435
450
455
460
c
FDRMAT(2X,'Deslred value',2X,5f15.9)
WRITE(1,410)(wtx(j),J=k,kup)
FORMAT(2X,'Welghting',6X,5F15.9)
00 420 J=k,kup
deviat(j)=dev/DBLE(wtx(j))
CONTINUE
WRITE(l,425)(deviat(j),J=k,kup)
FORMAT(2X,'Deviatlon' ,6X,5F15.9)
IF(jtype.EQ.1)THEN
00 430 J=k ,kup
devlat(J)=20.0*ALOG10(deviat(J))
CONTINUE
WRITE(1,435)(deviat(j),j=k,kup)
FORMAT(2X,'Devlatlon In dB',5f15.9)
ELSE
END IF
CONTINUE
WRITE(1,455)(grid(lext(J)),J•1,nz)
FDRMAT(/,2X, 'Extremal frequencies' ,/(2X,5F12.7))
WRITE(l,460)
FORMAT(/, 1X,70(1H*),/,1H1)
CLOSE( 1)
RETURN
END
SUBROOT I NE pause
CHARACTER*1 ans
10
20
r.R ITE(*, 10)
FORMAT(lX,'Hit <Cr> to continue ... ·,$)
READ(*,20)ans
FORMAT(A1)
RETURN
END
111
"PF JLES.FOR"
PROGRAM PF ILES
REAL FTEt.P ,I TEMP ,RSCALE
REAL f1,F2,REALSQ, IMAGSO
REAL PI, 1(1000),R(1000),H(1000)
REAL ~(1000),P(1000),G(1000)
INTEGER TN,N,F,FMAX,TNT,NN,NEWFIL,CC,FSCALE
CHARACTER*26 CTYPE
CHARACTER CHAR
CHARACTER*10 FJRFIL
CHARACTER*1 BFIRFL(10)
EQUIVALENCE (BFIRFL,FIRFIL)
Pl=3.141592654
c
C.... Fetch the input f i le name
IN IT=O
94
CONTINUE
IF(INIT.EO. 1)CALL CLS
IN IT= 1
~ ITE(* ,91)
91
FORMAT(/, 1X, ·~nu to create plot fl Jes: select source of'.
1
' Impulse response coefficients ... ' ,II,
1
' (R)ead coefficients from a "*.FOX" file',/,
2
' (C)reate new coefficients')
192
WRITE(*,90)
90
FORMAT(/,' Selection {(X)=exit} => '$)
READ(*,92)CHAR
92
FORMAT(A1)
IF(CHAR .NE. 'R' .AND .CHAR .NE .' r' .AI{l.CHAR.NE .' C' .AND.
CHAR.NE.'c' .AND.CHAR.NE. 'X' .AND.CHAR.NE. 'x')GO TO 192
IF(CHAR.EO.'X' .OR.CHAR.EO.'x')GO TO 1000
lf(CHAR. EO. 'C' .OR .CHAR .EO. 'c' )THEN
NEWFIL=1
ELSE
00 11 CC=1,10
11
BF JRFL(CC)=' '
YIRITE(*,97)
97
FORMAT(I,1X, 'Enter the f II e name: ',$)
READ(*,96)(BFIRFL(CC),CC=1,6)
96
FORMAT(SA)
BF IRFL(7)='.'
BFIRFL(S)='f'
BFIRFL(9)='0'
BFIRFL(10)='X'
NEWF IL=O
END IF
c
C.... Open and read the file
c
112
"PF ILES .FOR"
IF(NEWFIL.NE. 1)THEN
c
20
40
45
OPEN(UNIT=2,FILE=FIRFIL,STATUS='OLD')
READ(2,20)CTYPE,TN
FORMAT(6(/),25X,A26,2(/),31X,I3,//)
IF(CTYPE.EO.'multlple passband/stopband')THEN
NFLAG=O
ELSE
NFLAG=1
END IF
TNT =(TN+ 1)/2
DO 45 N= 1, TNT
READ(2,40)H(N)
FORMAT(29X,E15.8)
NN=(TNt1)-N
IF(NFLAG.EQ.O)H(NN)=H(N)
IF(NFLAG.NE.O)H(NN)=-H(N)
CONTINUE
CLOSE(2)
c
600
634
633
602
635
632
640
645
ELSE
CALL CLS
IIIRITE(*,600)
FORMAT(/,' Create the Impulse response ... ' ,II,
1
1X, '11 of samp Ies : ' ,$)
READ(* ,602)TN
WRITE(*, 634)
FORMAT(!,' Is this filter a bandpass filter? (y/n) ',$)
READ(* ,633)CHAR
FORMAT(A1)
NFLAG=O
IF(CHAR.EQ. 'N' .OR.CHAR.EO.'n')NFLAG=1
TNT=(TN+ 1)/2
FORMAT(I3)
00 645 N=1,TNT
NN=(TN+ 1)-N
IF(NFLAG.EQ.O)WRITE(*,635)N,NN
IF(NFLAG.EQ.1)WRITE(*,632)N,NN
FORMAT(1X, 'Sa~le 11', 13,' (', 13, ') = ',$)
FORMAT(1X,'Sa~le 11',13,' -(',13,') = ',$)
READ(*,640)H(N)
FORMAT(F15.8)
H(NN)=H(N)
IF(NFLAG.EQ.1)H(NN)=-H(NN)
CONTINUE
c
END IF
c
86
CALL CLS
WRITE(*,78)
113
"PF ILES .FOR"
78
79
84
81
FORMAT(1X, 'Enter the maximum frequency in rad/sec ',
1
'(~default{0.5})=> ',$)
READ(*,79)F2
FORMAT(F17.5)
WRITE(*,81).
FORMAT(1X,'Enter the frequency resolution factor:
1
'f(n)=f(n)*(fmax/nmax)=> nmax = ',$)
READ(*,*)FSCALE
IF(FSCALE.EQ.O)GO TO 84
RSCALE=1 .0/FLOAT(FSCALE)
c
F1=0.0
IF(F2.EO.O.O)F2=0.5
IF((FLOAT(FSCALE)*F2).GT. 1000.0)THEN
WRITE(*,*)' ***Array overflow: max freq ',
'* freq resolution> 10,000 ***'
GO TO 86
ELSE
END IF
c
700
CALL CLS
WRITE(*, 700)
FORMAT(!, 1X, 'Executing . . .
',$)
c
FMAX=NINT(FLOAT(FSCALE)*F2)+1
POLD=O.O
00 100 F= 1,FMAX
~ITE(*,
111
FORMAT('.
111)
',$)
R(F)=O
I(F)=O
00 80 N=i, iN
R(F)=R(F)+H(N)*COS(-2*PI*(F-1)*RSCALE*(N-1))
I(F)=I(F)+H(N)*SIN(-2*PI*(F-1)*RSCALE*(N-1))
80
CONTINUE
RTEMP=R(F)
REALSQ=RTEMP*RTEMP
!TEMP= I(F)
IMAGSO=ITEMP*ITEMP
M(F)=SORT(REALSO+IMAGSQ)
IF(R(F).NE.O.O)THEN
P(F)=ATAN2(1(F),R(F))
ELSE
IF(I(F).~T.O.O)THEN
P(F)=( _p:J/2.0}
ELSE
P(F)=(PI/2.0)
END IF
END IF
G(F)=(P(f)-POLD}/(2*PI*RSCALE)
114
"PF ILES.FOR"
POLD=P(F)
100
c
CONT INUE
BF IRFL(7)='.'
CALL CLS
c
166
c
266
120
140
c
c
366
BF IRFL(8)=' I'
BFIRFL(9)='M'
BFIRFL(10)='P'
WRITE(*, 166)fiRFIL
FORMAT(/,' Storing the Impulse response plot file into '
1
,A10)
OPEN(UNIT=2,FILE=FIRFIL,STATUS='UNKNOWN')
WRITE(2,*)' 1'
WR ITE(2,120)TN
WR ITE(2, 140)(H(N) ,N=1 ,1N)
CLOSE(2)
BFIRFL(8)='M'
BFIRFL(9)='A'
BFIRFL(10)='G'
WRITE(*,266)FIRFIL
FORMAT(/,' storing the magnitude of the frequency',
'response plot file into ',A10)
1
OPEN(UNIT=2,FILE=fiRFIL,STATUS='UNKNOWN')
WRITE(2, 120)FSCALE
WRITE(2, 120)FMAX-1
WRITE(2,140)(M(F),F=1,FMAX)
FORMAT(I8)
FORMAT(F12.7)
CLOSE(2)
BFIRFL(B)='L'
BFIRFL(9)='0'
BFIRFL(10) .. 'G'
OPEN(UNIT=2;fllE=fiRFIL,STATUS='UNKNOWN')
WRITE(*,366)FIRFIL
FORMAT(/,' Storing the log magnitude of the frequency ',
1
'response .plot file Into ',A10)
WRITE(2,120)fSCALE
WRITE(2, 120)FMAX-1
WRITE(2,140)(20.0*LOG10(~(f)),f=1,FMAX)
c
466
CLOSE(2)
BFIRFL(B)='P'
BFIRFL(9)='H'
BFIRFL(10)='A'
WRITE(*,466)FIRFIL
FORMAT(!,' Stor lng the phase .angle of the frequency ',
115
"PF ILES.FDR"
'response plot file into ',AlO)
OPEN(UNIT=2,FILE=FIRFIL,STATUS='UNKNOWN')
WRITE(2, 120)FSCALE
WRITE(2, 120)FMAX-1
WRITE(2, 140)(P(F),F=l,FMAX)
CLOSE(2)
c
566
c
666
c
766
c
1000
c
c
c
c
BFIRFL(8)='G'
BFIRFL(9)='R'
BFIRFL(lO)='D'
WRITE(*,566)FIRFIL
FORMAT(/,' storing the group delay',
1
'plot file into ',AlO)
OPEN(UNIT=2,FILE=FIRFIL,STATUS='UNKNOWN')
WRITE(2, 120)FSCALE
WRITE(2, 120)FMAX-1
WRITE(2, 140)(G(F),F=l,FMAX)
CLOSE(2)
BFIRFL(B)='R'
BFIRFL(9)='E'
BFIRFL(lO)='A'
WRITE(*,666)FIRFIL
FORMAT (I,' star Ing the rea I part of the frequency ' ,
1
'response plot file Into ',AlO)
OPEN(UNIT=2,FILE=FIRFIL,STATUS='UNKNOWN')
WRITE(2,120)FSCALE
WRITE(2,120)FMAX-1
WRITE(2,140)(R(F),F=l,FMAX)
CLOSE(2)
BF IRFL(8)=' I'
BFIRFL(9)='M'
BfiRFL(lO)='G'
WRITE(*,766)fiRFIL
FORMAT(/,' Storing the Imaginary part of the frequency ',
1
'response plot file Into ',A10)
OPEN(UNIT=2,fiLE=fiRFIL,STATUS='UNKNOWN')
WRITE(2, 120)FSCALE
WRITE(2, 120)FMAX-1
WRITE(2,140)(1(f),f=1,FMAX)
CLOSE(2)
CONT I NUE
END
116
"PFILES.FOR"
c
SI.BROUT INE CLS
~ITE(*,
10
c
c
FORMAT(26(/))
RETURN
END
10)
117
"FIRPLT.BAS"
1000
SCREEN 0,0,0 REM Put the screen in non-graphics mode
1010
DIM SC(4047) REM Setup the array for the Image
1020
VRES=160: HRES=600 REM Setup the resolution constants
1030
ROW=20: COL=75
1040
XOFFS=25: YOFFS=25
1050
YES=(1=1) REM Set yes= true
1060
N0=(1=0) REM Set no= false
1070
SS=NO REM Old image saved flag= false
1080
PRINT" FIR PLOT PROGRAM ... "
1090 PRINT" "
1100
Ll NE INPUT " Do you need a I i st of the f II es? Y/N/0 ";ANS$
1110
CLS
1120
IF ANS$="N" THEN GOTO 1280
1130
IF ANS$="n" THEN GOTO 1280
1140
IF ANS$= "C" THEN END
1150
IF ANSS="q" THEN END
1160
PRINT" "
1170
PRINT "Type Which file type: *.(file type) or N for none:"
1180
PRINT " "
1190
PRINT " File types: IMPulse response, REAl part, IMaGinary part,"
1200
PRINT "
LOG magnitude, MAGnitude, PHAse, GRoup Delay"
1210
PRINT " "
1220
LINE I tf'UT " -> ";FTYP$
1230
IF FTYP$ "' "N" THEN GOTO 1280
1240
IF FTYP$ = "n" THEN GOTO 1280
1250
CLS
1260
FILES FTYP$
1270
GOTO 1160
1280
REM
1290
LINE ltf>UT " Name the IJ'l)ut file (<Cr> to quit)=> ";FI$
1300
IF FIS=NU$ THEN END REM If no fl le, exit
1310
RFI$=RIGHT$(FI$,3) REM determine the kind of file
1320
XXX=1
1330
IF RFI$="LOG" THEN TYPE$="LOO MAGNITlllE ":UNIT$="dB "
1340
IF RFI$="1og" THEN TYPE$="LOG MAGNITlllE ":UNITS="dB "
1350
IF RFI$="LOO" THEN XXX=2
1360
IF RFIS="Iog" THEN XXX=2
1370
IF RF I$=" IMP" THEN TYPE$=" IMPULSE RESPONSE" :UN IT$="mag"
1380
IF RF 1$=" illf)" THEN TYPE$=" IMPULSE RESPONSE" :UN ITS="mag"
1390
IF RFI$="1MP" THEN XXX=3
1400
IF RFI$="111f>" THEN XXX=3
1410
IF RFI$="REA" THEN TYPES="REAL PART
":UNITS="mag"
1420
IF RFIS="rea" THEN TYPES="REAL PART
":UNITS="mag"
1430
IF RFI$="1~" THEN TYPES="IIdAGINARY PART ":UNIT$="mag"
1440
IF RFI$="1mg" THEN TYPE$="11dAGINARY PART ":UNITS="mag"
1450
IF RF I$="MAG" THEN TYPES="MAGN !TUDE
":UN IT$="mag"
1460
IF RFI$="mag" THEN TYPE$="MAGNITUDE
":UNITS="mag"
1470
IF RFI$="PHA" THEN TYPE$="PHASE
":UNIT$="rad"
1480
IF RFI$="pha" THEN TYPE$="PHASE
":UNIT$="rad"
1490
IF RFI$="GRD" THEN TYPE$="GROUP DELAY
":UNITS="sec"
118
"F IRPLT .BAS"
1500
1510
1520
1530
1540
1550
1560
1570
1580
1590
1600
1610
1620
1630
1640
1650
1660
1670
1680
1690
1700
1710
1720
1730
17 40
1750
1760
1770
1780
1790
1800
1810
1820
1830
1840
1850
1860
1870
1880
1890
1900
1910
1920
1930
1940
1950
1960
1970
1980
1990
IF RFI$="grd" THEN TYPE$="GROUP DELAY
":UNIT$="sec"
CPEN Fl$ FOO ltf?UT AS 1 REM Open the data file as unit 1
REM
INPUT 111,FSCALE
INPUT 111,N REM Read the 11 of values
PRINT Fl$; " Contains ";N; "values"
REM
INPUT 111,Y REM Read the first value
MINY=Y: MAXY=Y: MIDY=Y REM Initially set the first value to max &min
FOR K=1 TO N-1
INPUT t1l,Y
IF Y>MAXY THEN MAXY=Y
IF Y<MINY THEN MINY=Y
IF ABS(Y)<ABS(MIDY) THEN MIDY=Y
NEXT K REM This loop determines the max and min values
REM
CLOSE
REM Close the fda file
PRINT " Values range from ";miny; "to "; maxy
PRINT " "
IF SS=NO THEN PRINT "Press any key to continue";
IF SS=NO THEN WHILE INKEY$=NU$: WEND
IF SS=NO THEN GOTO 1750
LINE ltf?UT " Overlay previous plot? Y/N "; ANS$
IF ANS$="N" THEN SS=NO
IF ANS$= "n" THEN SS=NO
REM
REM
YSCALE=(VRES-1)/ABS(MAXY-MINY) REM calculate the vertical scale
XSCALE=(HRES-1)/(N) REM calculate the horizontal
RSCALE=(ROW)/ABS(MAXY-MINY)
CSCALE=(COL)/(N)/FSCALE
REM
CLS REM Clear the screen
SCREEN 2 REM Set the screen to high resolution graphics
REM
OOAW "BM 0,0"
IF SS THEN PUT (O,O),SC
CPEN F1$ FOO ltf?UT AS 1 REM Open the data f lie
INPUT 11 , FSCAL E
INPUT t1,N REM llllut the t1 of values
INPUT ti1,Y REM Read the first value
PSET ((D+XOFFS),(199-YOFFS-((Y-MINY)*YSCALE)))
FOR X=1 TO N-1
I~T t11,Y
LINE -(((X-1)*XSCALE+XOFFS),(199-YOFF5-((Y-MINY)*YSCALE)))
NEXT X REM This loop plots the remaining points as a continuous plot
CLOSE REM Close the data file
GET (0,0)-(639,199),SC REM Save the Image
REM
KEY Off
119
"F IRPLT .BAS"
2000
2010
2020
2030
2040
2050
2060
2070
2080
2090
2100
2110
2120
2130
2140
2150
2160
2170
2180
2190
2200
2210
2220
2230
2240
2250
2260
2270
2280
2290
2300
Z3i0
2320
2330
2340
2350
2360
2370
2380
2390
HMIDL=199-(MIDY-MINY)*YSCALE
LINE ((D+XOFFS),(D+YOFFS))-((OtXOFFS),(199-YOFFS))
LINE ((O+XOFFS),(HMIDL-YOFFS))-((649-XOFFS),(HMIDL-YOFFS))
t.t.!IDL=ROW-1-(MIDY-MINY)*RSCALE
LMIDL=(199-YOFFS-(MIDY-MINY)*YSCALE)*(RSCALE/YSCALE)
REM
If XXX=2 THEN GOTO 2080
LOCATE 5,1: PRINT MAXY
IF LMIDL<2 THEN LMIDL=2
IF MMIDL<2 THEN t.t.!IDL=2
LOCATE LMIDL,1: IF MIDY < MAXY THEN PRINT MIDY
LOCATE LMIDL,1: IF XXX=2 THEN PRINT MIDY
LOCATE 21,1: IF MINY < MIDY THEN PRINT MINY
REM
DIV=(N)/5
IF XXX=3 THEN RDIV=DIV ELSE RDIV=DIVIFSCALE
LOCATE MMIDL+2,1*DIV*CSCALE*FSCALE: PRINT RDIV*1
LOCATE MMIDL+2,2*DIV*CSCALE*FSCALE: PRINT RDIV*2
LOCATE MMIDL+2,3*DIV*CSCALE*FSCALE: PRINT RDIV*3
LOCATE MMIDL+2,4*DIV*CSCALE*FSCALE: PRINT RDIV*4
LOCATE MMIDL+2,5*01V*CSCALE*FSCALE: PRINT RDIV*5
IF XXX=3 THEN YAXIS$="samples" ELSE YAXIS$="rad/sec"
LOCATE MMIDL+1,5*01V*CSCALE*FSCALE-5: PRINT YAXIS$
REM
REM
LOCATE 1,10: PRINT "FILE NM£: "
LFI$=LEFT$(FI$,6) REM get the file base name
LOCATE 1,23: PRINT Lfl$
LOCATE 1,40: PR INT "PLOT TYPE : "
LOCATE 1,52: PRINT TYPES
IF XXX=2 THEN LOCATE 1,1 ELSE LOCATE 2,1
PRINT UNIT$
REM
REM
WHILE INKEYSzNU$: WEND REM Walt for any key to exit from the plot
SS=YES REM Set the Image saved flag to true
SCREEN 0,0,0 REM Return the screen to normal text mode
REM
GOTO 1090
END
REFERENCES
[1] Thomas W. Parks and James H. McClellan, "Chebyshev
Approximation for Nonrecurslve Digital Filters with Linear
Phase", IEEE Trans. Circuit Theory, vol. CT-19, pp.
189-194, Mar. 1972.
[2] James H. McCiel lan, Thomas W. Parks and Lawrence R.
Rabiner, "A computer program for Designing Optimum FIR
Linear Phase Digital Filters", IEEE Trans. Audio Electroacoust., vol. AU-21, pp. 508-526, Dec. 1973.
[3] Lawrence R. Rabiner, "Approximate Design Relationships
for Lowpass FIR Digital Filters", IEEE Trans. Audio
Electro-acoustic., vol. AU-21 pp. 456-460, Oct. 1973.
[4] Robert J. Sclamanda, "Another Approach to Data
Compression", BYTE, the Small Systems Journal, vol. 12,
no. 2, pp. 140-142, Feb. 1987.
[5] Alan V. Oppenheim and Ronald W. Schafer, "Digital
Signal Processing", Prentice-Hall, Inc., Englewood Cliffs,
N.J., 1975.
[6] R. W. Hamming, "Digital Fl lters, 2nd Ed.", PrenticeHa I I , Inc. , Eng I ewood C I Iff s, N.J. , 1983.
[7] Kenneth Steigl ltz, "An Introduction to Discrete
Systems " , John WI I ey & Sons , I nc . , U . S . A . , 1 9 7 4 .
[8] Ronald N. Bracewel I, "The Fourier Transform and Its
Appl !cations, 2nd Ed.", McGraw-Hi I I Book Co., U.S.A.,
1978.
120