Understanding FFT Applications Second Edition
Understanding FFT Applications Second Edition
SECOND EDITION
EXTENSIVEL Y REVISED
Applications
A Tutorial for
Students & Working Engineers
Anders E. Zonst
Citrus Bess
Titusville, Florida
Publishers Cataloging-in-Publication Data
lonst, Anders E
Understanding FFT Applications-A Tutonal for Laymen, Students, Technicians and
Working Engineers/ A E lonst
p em
Includes bibliographical references and index
1 Fourier analysis 2 Founer transfonnations
3 Digital signal processing I Title
AZ - 30 April 2003
CONTENTS
Prologue vii
Foreword IX
APPENDICES
Bibliography 277
Index 278
PROLOGUE
A. E. Zonst
INTRODUCTION
1.0 INTRODUCTION
where: Ao = AmplItUde
4> = Phase
f = frequency
understand here-it hides the transition from argument to sine function.
To understand the intriguing phenomena of Fourier's transform we must
understand this transition, so let's go back to the beginning and trace the
path that gets us to an understanding of this profound relationship.
1 . 1 SINUSOIDS
YIR=Sin'
XIR = Cos,
R = Hypotenuse
If the hypotenuse is of unit length, the side opposite the angle will
be exactly equal to the sine-and the adjacent side will equal the cosine.
In Figure 1.2 the unit hypotenuse is also the radius of a circle; so, if we
rotate this radius (like the spoke of a wheel), ¢ will move through all
Rotatloa Sample PoiaQ
-- . -
Startiag
Positloa
Figure 1.2 - Sinusoid Generation
possible angles. We may plot the successive values of the sine function
(i.e., the vertical component of the radius) against the value of ¢ as the
radius rotates. [Figure 1.2 also illustrates a characteristic ofdigital (i. e.,
discrete data) systems. While the angle ¢ changes continuously. and the
sine function is also continuous, we can only sample a finite number of
points in a digital system. Note the symmetry of the sampled data-we
have 16 (i. e., 24) data points. This will be useful in illustrating various
points, but 2N data points (where N is an integer) is completely composed
offactors of 2, which provides maximum efficiency in the FFT. Its not
imperative, however, that 2N data points be used in a DFT.]
The term sinusoid includes both the sine and cosine function. The
cosine may be generated by starting with the radius in the vertical position
(Figure 1.3). It's apparent the only difference between Fig. 1.2 and 1.3 is
Sia(.+ 90')a Cos •
. ---
thephase (i.e., the starting angle) of the functions. The phase angle can be
any value from zero to 211", of course, and we include all possible phase
angles under the term sinusoid.
We may also note that, if the rate of rotation is constant, the angle
cp may be expressed as a function of time. We need only convert this time
variable into radians, scaled to 211" for each revolution:
cf>(t) = wt (1.3)
where: w = 211"f or 211"IT
We will talk more about this argument of sinusoids later, but for now let's
continue our present discussion.
111)- Sln(ml++)
-1-
iXi = ;2 = -l
so that:
i =v-l ---------------------------- (1.5)
of complex numbers .. but after Casper Wessel ( 1 745-1 8 1 8) Long before Argand and
Wessel, Rafael Bombelli (born ca. 1 530) saw the necessity of recognizing the existence
of numbers such as v-I but allowed they were "imaginary numbers " Hamilton ( 1 805-
1 865) did much to clear up the confusion with complex numbers. Arnold Dresden has
suggested removing the stigma of the term imaginary by replacing it with normal.
Sinusoids s
MACLAURIN'S SERIES
where ftO).!'tO), etc. are the 1st, 2nd, etc., derivatives, evaluated for an
argument of zero. We will show how simple it is to use this series via
three examples. The first is e'-the function whose derivative is equal to
itself. Now, anything raised to the zero power is equal to 1. 0, so this
makes Maclaurin's series for e' very easy to evaluate---every derivative
will yield a coefficient of 1.0 (when evaluated for an argument of zero) so:
You may prove this is correct by taking the derivative of this series of
tenns-the result will be the same as the original series-the derivative of
this function is equal to itself.
The next function we approximate is the sine function. We know
the value of a sine function is 0.0 at an argument of zero, and its derivative
(a cosine function) is 1.0 for an argument of zero. The second derivative
(a minus sine function) again yields 0.0 (for an argument of zero) and the
third derivative (a minus cosine) is -1.0 (at zero), etc., etc. We therefore
obtain a Maclaurin series for the sine function:
Note that the sine function contains all the odd powers of x while the
cosine contains all the even powers. Most interestingly, e' contains both
the odd and even tenns, which suggests we might somehow obtain the e'
function by summing the sine and cosine functions. In fact, we find these
functions combine very neatly as follows:
---------- - - (2. 7)
and:
isin(x) = ix - ix3/6 + ixs/l 20 + . . . . ------ - - ---- (2.8)
That is:
/y
Ae i8 = ACos(O) + i ASin(O) - - - - - - - - - - - - - (2.10)
We frequently write j{O) as A !.fl. but it's apparent ei8 is a much better
expression for a phasor (or vector). The i in front ASin(O) implies this
component lies on the imaginary axis while ACos(O) lies on the real axis
we are dealing with the Argand mode of projection-the same we used in
Fig. (1.1) on the first page to define the sine and cosine functions.
Note that the angle goes in the exponent of ei8 _ in vector
multiplication we will multiply the magnitudes and add the angles.
and:
10 Chapter 2
------------------ (2.14)
*
9=90+c!>
(see Fig. 2.3). -1"'------+-
(2.15)
[Note: it' s apparent that the hypotenuse of the little triangle (on which we
base our equation for slope) in Fig 2.3 is equivalent to a radius that has
rotated past 90°. The vertical projection of a radius is
the sine of the angle cp, but I:!.y/I:!.cp is the change in the
F----\---., sine function divided by the change in the independent
variable-it's the slope of the sinusoid. Furthermore,
as is apparent from Figs. 1.2 and 1.3, the sine of (cp +
90°) is a cosine function. The slope, or derivative, of
a sine wave is just a cosine wave.
(2.16)
F= Kilx (2.20)
2
physicists sincerely search for an equation of everything, and we sincerely
wish them well; but, here, in this model of mechanical motion (if we only include the
element of fnction) we very nearly have a mechanical motion equation for everything.
Nature of Sinusoids 15
2.3 EIGENFUNCTIONS
2.4 STURM-LIOUVILLE
where the constants M and K are not necessarily mass and springs. In this
form we see (since M and K are constants), the second derivative off(x) is
equal to itself (modified at most by a multiplied constant)!
Now, we know a function whose 2nd derivative is equal to itself
multiplied by a constant. Not only does the second derivative of a sine
wave equal itself (multiplied by a constant -1) but so does the 2nd
derivative of a cosine wave. On successive differentiations, these functions
are self-generating! You may begin to see where all this is going, but let's
Y 'i Y
Rc= RoCos,
R.= RoSin,
Figure 3.2 - Rotating Phasor Components
'As a phasor's components rotate they too may be broken into components.
Clearly the summation of a phasor's components must yield the original phasor regardless
of how these are rotated; therefore, the sum ofy axis projections of the components must
equal the y axis projection of the phasor at all rotations.
The Unique Sinusoid 19
components o f static phasors will lie along the horizontal and vertical axes
and therefore we may add the phasor components (which lie on the same
axis). We may thus add sinusoids of arbitrary phase by complex addition
--:t:Jts;�=��:';$-
:::
(i.e., add imaginary components to imaginary, and real with real).
Complex
_ Sinusoid
The above scheme for addition works well when we deal with
sinusoids of the same frequency; however, it will obviously not work when
the sinusoids to be added are of different frequency (since the phase will
constantly change). This is illustrated in Fig. 3.4--note that the value of
the summation constantly changes as we progress along the wave.
This remarkable result is
fI
known as "beat frequency modula
tion." As the phase between the two
sinusoids slowly walks, the ampli
tudes first add, then subtract, then
add again. Having completed 3600
of phase shift they can only repeat
v
this cycle. Note that the resultant
(
fI waveform is still essentially a sinu
A soid, albeit an amplitude modulated
f\ A A sinusoid. The term "beat frequency"
These two sinusoids start in-phase but immediately begin walking out-of
phase. The next time they will be back in-phase will be when the higher
frequency sinusoid has completed one full cycle more than the lower
frequency sinusoid, but this equation becomes much more interesting when
we consider its trigonometric identity:
=
Cos(A +B)+Cos(A -B ) (3 .3 A)
Cos(A)Cos(B)
2
Eqns. (3.2) and (3.3) show the same thing. W hen we multiply sinusoids,
two new side band sinusoids are produced (note that the original sinusoids
disappear and the two new sidebands are ofdifferentfrequency than either
of the sinusoids multiplied together).
This unique aspect of sinusoids has profound significance. This
curious result (i.e., of sinusoid multiplication) is the underlying mechanism
of convolution (which plays such a prominent role in understanding and
applying Fourier analysis). It will be worth digging a little deeper here.
+ 21!JJt
many times to sum it into the accumulator.
According to our development of complex = 6mt
addition, we would take our 2 on the Y axis 2 Angle is not multiplied
and sum it into the pot three times yielding
6 on the Yaxis. It would be silly to think +----3----
the angle should be multiplied by 3. Figure 3.7
If we perform this multiplication
the other way around and sum 3 on the X axis into the pot 2 with 7r/2
radians rotation times, then before we throw 3 on the X axis into the pot,
we must rotate it by 7r/2 radians. The thing we are throwing into the pot
then is 3 at 7r/2 radians-the only way we can actually sum quantities is if
they are aligned (in the same direction). So, first we take 3 10, rotate it by
7r/2, throw it into the pot; then we take 3 10 again, rotate it by 7r/2, and sum
it in-yielding 6 at 7r/2.
Clearly, then, if we have 3
on the Yaxis multiplied by 2 on 3M
the Yaxis we must sum 3 at 90°
i n t o the accu m u lator 2 at 9 0 °
times. We must rotate 3 at 90° by 2M. x 3M (2 x 3)�'
=
-.....
Magnitude = 0.5
'\
pairs (i.e., the angles of the pair belonging to either cosine are the negatives
of each other), the angles of the products will be the sums and differences
of the multiplied phasors' angles (see Fig. 3 .9). We will obtain two new
complex conjugate pairs of phasors (i.e., two new cosine functions) which
are superimposed (i.e., summed together-Fig 3.9). Note carefully that,
if one angle is large and the other small, the two new phasors will group
themselves about the larger angle (i.e., they must be spaced equally ahead
of and behind the larger angle by + and - the smaller angle).
Ok ay, now we rep l ac e the static ang l e cP by our k i n e t i c
representation of an angle wt (i.e., 21fjt) so that our phasors rotate. If the
two frequencies are different, and we multiply our two cosine functions
point by point as the phasors rotate, we will be performing the operation
we described above as modulation. Again, note that the faster moving
phasor (Le., higher frequency of rotation) will immediately obtain a larger
angle, and the products of the multiplication will group themselves about
this faster moving phasor. The frequency of rotation of one complex
conjugate pair will be equal to the higher frequency plus the lower-the
24 Chapter 3
2
Cos(27T'(fo + II)t) + Cos(27T'(fo - II)t) (3 . 4)
Cos(27Tfcl)Cos(27Tflt» -
2
or, in exponential fonn:
= Y.. [e,"21r(fo+fl)t + e-,"21r(fo+fl)t] + Y.. [e,"21r(fo-fl)t + e-,"21r(fo-fl)t] (3 .5 )
Cos(wt + <Pt)Cos(wt + �) =
[(ei(WI + ,pI) + e-i(wl + ,pl) (ei(WI + I/ll) + e-i(WI + I/ll) ]/4 (3.6)
[We have omitted magnitudes here since they always factor out and
contribute nothing to the illustration.] You may easily show that the right
hand side of (3.6) reduces to:
[(el(2WI + ,pl + ,p2) + e-i(2wl + ,pl + ,p2)]/4 + [ei(,p1-</>2) + el(,p2-</>1) ]/4 (3.7)
e
l(q,1-4>2) + e i(q,2-4> 1 ) = [2COS(<P1 -<P2)] ------------ (3 .8)
These are unit vectors at plus and minus the difference between the phase
angles and are thus complex conjugates (i.e., they generate equal and
opposite imaginary components which cancel-see footnote p. 10).
This relationship may be more apparent from Fig. 2. 1 (p. 1 0).
This cosine term [i.e., Eqn.(3 . 8)] is, in Eqn.(3 . 7), an added constant
proportional to the cosine of the phase difference. When the two sinusoids
are in phase this will be a maximum of 2.O--when they are 90° out of
phase the added constant is zero. Now, when we take the average of the
complete expression (i.e., Eqn. 3.7) over 211" radians, any sinusoid will
contribute zero to the average value, and the added constant [ i . e . ,
Eqn.(3.8)] will be the only thing remaining. It is therefore apparent that
when we multiply a waveform (composed ofa single complex sinusoid) by
a cosine wave ofthe samefrequenc)t the cosine component in the complex
waveform will yield an average value the sine term will yield none. If we
multiply this complex waveform by a sine wave. the sine component ofthe
waveform will yield an average and the cosine will yield none. Equation
(3.7) reveals everything-frequency doubling, phase angles adding,
amplitude divided by two... even orthogonality. A magnificent expression.
We must consider one last technicality here-the multiplication
of a sinusoid by the complex exponential:
------ (3.9)
This is an important exercise, for it represents the most common fonn in
which the Fourier Integral is written:
+ ...
----- (3. 1 1 )
so multiplication of a real sinusoid by the complex exponential ei(wt) results
in a phasor which rotates at 2wt and has a phase angle equal to cPl ' Now,
there is also a vector constant e-H/>112 which, we recognize, breaks into the
components [COS(cPl) - iSin(cPl)]I2. If the sinusoid inf(t} has magnitude
Mn, this operator breaks it into real and imaginary phasor components
(divided by 2).
The integral finds the area under the curve described by the right
hand side of Eqn. (3. 1 1 ), and the oscillating nature of the first complex
exponential will contribute nothing to the final value. Integration of
Equation (3. 1 1 ) then, must ultimately yield only the phasor of the complex
sinusoid of f(w}. That, of course, is precisely what we expectfrom the
Fourier transform.
Cos(wt + tPt)Cos(wt + �) =
[(e;(WI + 1/>1) + e·;(WI + 1/>1) (e;(WI + 1/>2) + e-;(WI + 1/>2) ]/4 (3.6)
[(e
'f2W1 + 1/>1 + 1/>2) + e-i(2"" + 1/>1 + 1/>2) ]/4 = �Cos(2wt + cJ>.r) (3. 12)
and the right-most two terms yield a constant determined by the phase
difference between the two sinusoids:
(3. 13)
28 Chapter 3
From our introductory remarks, the sinusoid [Eqn. (3. 1 2)] contributes
nothing to the average value of this function, but the average value of a
constant term is j ust the constant term. So, then, the average value of the
product of two sinusoids will depend on the phase relationship between the
two sinusoids-if the two are in phase (i.e., cJ>. � = 0) we will obtain an
average value equal to one-half the product of the amplitudes of the two
sinusoids! If, howeve� the phase relationship is 7r/2 (i.e., cJ>.-<P2 = 90°) the
average value of the product will be zero! This l atter result is the
phenomenon of orthogonality between sine and cosine functions!
So, what about orthogonality between sinusoids of different
frequencies then (which is the real question here)? Returning to our
consideration of the average value of a sinusoid, we reiterate that it is
necessary to deal withfull cycles of a sinusoid for the average value to be
zero. We can always satisfy this requirement by scaling the frequency term
so that exactly one full sinusoid fits into the domain of the function we are
working with. That is, if our sinusoid is given by Cos(27rfot), and the
domain is t = 0 to To, we simply makefo = lITo. When t = To then, and we
are at the end of the domain, the value of the argument will equal 27r and
we will have constructed one full cycle of sinusoid (within the domain of
defin ition). It's also apparent that if we limit frequencies to integer
multiples of fo, then every allowed frequency will provide an integer
number of full cycles within the domain, and every sinusoid at an allowed
frequency will have an average value of zero (see Fig. 3 . 9) .
Allowing only integer multiple frequencies then, what happens
when we multiply two sinusoids? Equation (3 .38) must become:
(3.18)
Both the left-most pair of exponentials and the right-most pair are cosine
functions! Both of these have an average value of zero! The average value
of the product of a sinusoid multiplied by a second sinusoid of twice the
frequency is zero!
Surely you see what happens when n = 3. No?? Sacre bleu!
[( ei{4W1)J + e-i{4W1J + ei{2W1J + e-i{2W1J)]/4 (3.19)
We still get two cosine waves. Only the frequencies are different. The
product of two sinusoids with a 3: I frequency ratio yields an average value
of zero again.
By now you can probably see where all of this is going. You
do?? Oooooh wow! You recognized the general relationship for the
product of two sinusoids of different frequency (fl and £2) is two other
sinusoids whose frequencies equal (fl + £2) and (fl - £2)? You saw that
only when fl = £2 will the difference equal zero, and only a frequency of
zero yields a constant term? That otherwise, we can keep increasing n until
Arizona freezes over, and we will never again get an average value out of
the product of two sinusoids of integer multiple frequencies? You see,
30 Chapter 3
2
Catholic in the sense of universal-broad in sympathies and interests.
CHAPTER 4
NETWORK ANALYSIS
�
consequently several voltages associated with these elements, but there can
be only one currentflowing in any leg.
Node
With the above fundamentals t
Branch
v" (Ieg)
we can write equations to determine
1/ . I, .
the current that will flow, and the In
t
voltage drops that will result, in terms
of the voltage applied to a circuit and
the voltage dropping elements of the
circuit. Figure 4.1
I
Electncal engineers will be old friends with the frequency domain. Much of
their education is presented directly in the frequency domain, and so they are pre-disposed
to accept the reality of Fourier transforms.
32 Chapter 4
© R2 ®
: Vbatt Rl
@ Rl ® R2 ©
Figure 4.2 - Voltage Drop
We may write the following equation for the circuit of Figure 4.2:
That is, the sum of the voltage drops across the resistors equals the applied
voltage (i.e., the battery voltage) or, stated the other way around, when the
battery voltage is included on the right hand of the equal sign, the voltage
drops around the loop sum to zero:
Now, most often, we would know the voltage applied and the
values of the resistors, and would determine the current by an inversion of
Eqn. (4.2C):
--------------- (4.3)
We could then determine the voltage drop across either ofthe two
resistors by simply applying Ohm's law. The configuration of Fig. 4.2 is
commonly used to attenuate a signal and is referred to as a "voltage
divider" (i.e., the two resistors divide the applied voltage between
themselves). The voltage across RI and R2 is given by:
RJ R4 R4
�
R7
/\R4
13 .-/
Figure 4 .4 Mesh Analysis
-
of change (i.e., the derivative) of the current flowing through it, and the
voltage across a capacitor is proportional to the accumulation (i.e., the
integral) of the current flowing into it (or, if you prefe� the charge):
It's remarkable that these three elements are the fundamental elements of
passive, linea� time-invariant circuits.6 The above equations are for the
time domain response of these elements, but undergraduate students are
taught about these components in the frequency domain.
XL = j27rjL (4.7)
and: Xc I/j2 7rjC
= (4.8)
where: XL = inductive reactance
Xc = capacitive reactance
L = inductance
C = capacitance
j = i = v- I
S
For Joseph Henry. Born in 1 797, fourteen years after the Amencan
Revolution Ahe was one of the first world class American scientists (after Franklin).
�
Named in honor of the illustrious Michael Faraday, of course.
6
See Chapter 2, SectIon 2.2.
7
For sinusoids at any given frequency, it's obvious the rate of change is
proportional to amplitude. The quantity of charge accumulated over any half cycle is
likewise obviously proportional to the amplitude and, being discharged on the second half
cycle, must necessanly yield an ac current proportional to amplitude.
36 Chapter 4
The j accounts for the 90° phase shift, of course, and we combine these
reactive elements with resistance just as we combine real numbers with
imaginary numbers. The resulting complex quantity is given the name
impedance and is designated by the symbol Z. For ac circuits, impedance
, ********************************************
, * ACNET 05 - 0 1 - NETWORK MESH ANALY S I S *
I ********************************************
10 SCREEN 9, 1 : COLOR 1 5 , 1 : CLS
1 2 LOCATE 1 , 20 : P R I NT "NETWORK MESH ANALYS I S " : PR I NT PR I NT
20 PI = 3 . 1 41 592653589793#: P I 2 = 2 * P I : E R F L G = 0
30 PR I NT "A UN I T VOLTAGE SOURCE I S ASSUMED FOR MESH 1 1 "
40 I NPUT "ENTER DESI RED FREQUENCY RANGE ( FM I N , FMAX ) " ; FMN , FMX
50 I NPUT "ENTER NUMBER OF FREQUENCY STEPS PER DECAD E " ; D E L F
6 0 FBOT = I N T ( LOG ( FMN ) / LOG ( 1 0 » , COMMON LOG FMN
70 FTOP = I N T ( LOG( FMX) / LOG( 1 0 » , COMMON LOG FMX
80 I F LOG( FMX) / LOG ( 1 0 ) - FTOP > . 000001 THEN FTOP = FTOP + 1
90 FSTEP = 1 0 A ( 1 / DEL F ) , FREQ STEP MULT I P L I E R
1 00 I NPUT "ENTER THE NUMBER OF MESHES"; KB
1 1 0 D I M COMP ( 3 , KB, KB ) , Z ( 2 , KB, KB+1 ) , F ( 2 , « FTOP- FBOT ) * D E L F )+ 1 )
1 20 GOSUB 20000 ' I NPUT C I R CU I T ELEMENTS
130 GOSUB 1 0000 ' CALCULATE X FER I MPEDANCE
140 CLS : LOCATE 2, 20: PR I NT "OUTPUT MENU"
150 P R I NT P R I NT " 1 = PLOT DATA"
155 PR I NT : PR I NT " 2 = PR I NT DATA ( TO SCREE N ) "
160 P R I NT : PR I NT " 3 = OUTPUT T O P R I NTER"
1 70 LOCATE 1 8 , 3: PR I NT "9 = EX I T "
180 PR I NT : PR I NT " I NPUT SELECTON"
1 90 AS = I NKEYS: I F AS = " " THEN 190
200 ON VAL (AS) GOSUB 800, 1 0090, 600, 235 , 235 , 235 , 235 , 235 , 240
38 Chapter 5
230 GOTO 1 40
235 RETURN
240 END
I ***********************************
, * PR I NT OUTPUT TO PR I NTER *
I ***********************************
600 I NPUT "C I RCU I T NAME "; AS
605 LPR I NT DATES, T I ME S : LPR I NT : LPR I N T AS : LPR I NT LPR I NT
610 FO = 1 0 � FBOT : F = F O : PLC = 52
615 FOR I = 1 TO « FTOP - FBOT ) * DEL F ) + 1
620 LPR I NT US I N G "##### . ##_ " ; F ; , PR I NT FREQUENCY
625 LPR I NT U S I NG "##. ####_ "; F ( 1 , I ) * RTERM ; , PR I NT VOLTAGE
630 JS = "+j " : I F SGN ( F ( 2 , I » < 0 T HEN JS = " - j "
635 LPR I NT J S ; : LPR I NT US I NG "##. ####_ "; ABS ( F ( 2 , I )*RTERM ) ;
640 MAG = SQR ( F ( 1 , I ) � 2 + F ( 2 , I ) � 2 )
642 I F MAG = 0 T HEN LPR I N T " 0 . 00000 I 0 . 00" : GOTO 660
644 LPR I NT US I NG "##. ####_ # "; RTERM * MAG; : LPR I NT " I " ;
646 I F F ( 1 , I ) = 0 AND F ( 2 , I ) >= 0 T HEN BTA = P I I 2 : GOTO 655
648 I F F ( 1 , I ) = 0 AND F ( 2 , I ) < 0 T HEN BTA = - P I I 2: GOTO 655
650 BTA = ATN ( F ( 2 , I ) I F ( 1 , I » : I F F ( 1 , 1 ) <0 THEN BTA = BTA+P I
655 LPR I NT U S I NG "### . ##" ; BTA * 1 80 I PI ' PR I NT DEGREES PHASE
660 I F I < PLC T HEN 690 ' CHK END O F PAGE
665 LPR I NT " (MOR E ) "
670 LPR I NT CHRS( 1 2 ) , FORM FEED
675 PLC = PLC + 5 2 ' SET END O F NEXT PAGE
680 FOR LCTR = 1 TO 3 : LPR I NT : NEXT ' PR I NT HEAD SPACE
690 F = FO * FSTEP � I ' SET NEXT FREQUENCY
692 NEXT I ' P R I NT NEXT L I NE
694 LPR I NT CHRS( 1 2 ) , FORM FEED F I NAL PAGE
698 RETURN
, ***********************************
, * PLOT X FER FUNCT I ON *
I ***********************************
800 SCREEN 9, 1 : COLOR 1 5 , 1 : CLS ' SELECT 640 x 350 RESOLUT I ON
810 XO = 2 0 : YO = 20 : YX = 320 : XX = 620 ' 600 x 300 GRAPH
812 L I NE ( X O , Y O ) - ( X O , YX) , DRAW COORD I NATES
814 L I NE ( X O , YX ) - (XX , YX)
820 FREQNO = ( FTOP - FBOT ) * DEL F : YMAX = 0
822 FOR I = 1 TO FREQNO
824 Y1 = SQR ( F ( 1 , I ) � 2 + F ( 2 , I ) � 2 )
826 I F Y 1 > YMAX T HEN YMAX = Y 1
828 NEXT I
830 KX = 600 I FREQNO: KY = 200 I (YMAX * RTERM)
832 LOCATE 8 , 1 : PR I NT YMAX * RTERM
834 Y 1 = RTERM * SQR ( F ( 1 , 1 ) � 2 + F ( 2 , 1 ) � 2 )
836 L I NE ( X O , YX - ( KY * Y1 » - ( XO , Y X - ( KY * Y 1 »
840 FOR I = 1 TO FREQNO
842 Y1 = RTERM * SQR ( F ( 1 , I ) � 2 + F ( 2 , I ) � 2 )
Circuit Analysis Program 39
844 L I NE - ( XO + ( I - 1 ) * KX , YX - ( KY * Y 1 »
846 NEXT
848 LOCATE 24 , 2: P R I NT 1 0A F BOT; : LOCATE 24 , 73 : P R I N T 1 0A F TOP;
850 LOCATE 25, 30 : P R I NT " FREQUENCY"; : LOCATE 25, 2
852 I NPUT AS
854 RETURN
, ***********************************
, * CALCULATE XFER FUNCT I ON *
, ***********************************
1 0000 FO = 1 0 A FBOT : F = F O : FREQ = 0 ' FREQ = DATA PO I NT COUNTER
1 00 1 0 GOSUB 1 1 000 ' CALC I MPEDANCE MAT R I X
1 0020 GOSUB 1 2000 ' SOLVE FOR CURRENTS
1 0040 ' GOSUB 1 3000 ' PR I NT OUT CURRENTS
1 0050 FREQ = FREQ + 1 ' SET FOR NEXT DATA PO I NT
1 0060 F ( 1 , FREQ) = Z( 1 , KB , KB + 1 ) : F ( 2 , FREQ) = Z ( 2 , KB , KB+1 ) , SAVE
1 0070 F = FO * FSTEP A FREQ ' GET NEXT FREQUENCY
1 0080 I F F <= FMX + . 1 T HEN 1 00 1 0 ' JOB DONE?
1 0090 CLS : PLC = 20 ' CLEAR SCREEN FOR PR I NTOUT
1 0 1 00 F = 10 A FBOT ' GET BOTTOM FREQUENCY
1 0 1 1 0 FOR I = 1 TO « FTOP - FBOT ) * DEL F ) + 1
1 0 1 20 PR I NT U S I N G "##### . ##_ "; F; , PR I NT FREQUENCY
1 0 1 30 PR I NT U S I N G "##. ####_ "; F ( 1 , I ) * RTERM; , ? REAL COMPONENT
1 0 1 40 J S = "+j " : I F SGN ( F ( 2 , I » < 0 T HE N J S = " - j "
1 0 1 50 PR I NT " "; JS;
1 0 1 60 PR I NT US I NG "#. ####_ "; ABS( F ( 2 , I )*RTERM ) ; ' PR I NT I MAG I NARY
1 0 1 70 MAG = SQR ( F ( 1 , I ) A 2 + F ( 2 , I ) A 2 ) , F I ND MAGN I TUDE
1 0 1 80 I F MAG = 0 THEN PR I NT " 0 . 0000 10 . 00" : GOTO 1 0240
1 0 190 PR I NT US I NG "#. ####_ # "; RTERM * MAG; : PR I NT " I " ;
1 0200 I F F ( 1 , I ) = 0 AND F ( 2 , I ) >= 0 T HEN B T A = P I I 2 : GOTO 1 0230
1 02 1 0 I F F ( 1 , I ) = 0 AND F ( 2 , I ) < 0 T HEN BTA = - P I I 2: GOTO 1 0230
1 0220 BTA = ATN( F ( 2 , 1 )1 F ( 1 , I » : I F F ( 1 , 1 ) <0 T HEN BTA = BTA+P I
1 0230 PR I NT USING "### . ##" ; BTA * 1 80 I PI ' P R I NT PHASE I N D E G .
1 0240 I F I < P L C T HEN 1 0270 ' SCREEN FULL?
1 0250 PLC = PLC + 20 : PR I NT " (MORE ) "
1 0260 I NPUT "ENTER T O CONT I NUE"; A S ' WA I T FOR USER
1 0270 F = FO * FSTEP A I ' NEXT FREQ
1 0280 NEXT I ' P R I NT NEXT COMPONENT
1 0290 I NPUT "ENTER TO CON T I NUE"; AS ' WA I T
1 0300 RETURN ' WERE OUTA HERE
, ***********************************
, * COMPUTE I MPEDANCE *
I ***********************************
1 1 000 CLS : WF = P I 2 * F ' GET ANGULAR FREQUENCY
1 1 0 1 0 FOR I = 1 TO KB ' FOR ALL MESHES
1 1 020 Z( 1 , I , I ) = COMP O , I , I ) , GET RES I STOR VALUE
1 1 030 I F COMP ( 3 , 1 , 1 ) = 0 T HEN Z ( 2 , 1 , 1 ) = WF*COMP ( 2 , 1 , 1 ) : GOTO 1 1 060
1 1 040 I F F = 0 T HEN Z ( 2 , 1 , 1 ) = - 1 E+ 1 8 : GOTO 1 1 060
1 1 050 Z ( 2 , I , I ) = WF * COMP ( 2 , I , I ) - 1 I ( W F * COMP ( 3 , I , I »
1 1 060 FOR J = I + 1 TO KB ' GET MUTUAL I MPEDANCES
40 Chapter 5
I ***********************************
, * SOLVE FOR CURRENTS *
I ***********************************
1 2000 FOR J = 1 TO KB ' STEP THROUGH ALL D I AGONAL LOCAT I ONS
1 2200 ' GET A " 1 " COE F F I C I EN T ON T HE D I AGONAL
1 2 2 1 0 I F Z ( 2 , J , J ) = 0 T HEN ZRE = 1 / Z ( 1 , J , J ) : Z I M = 0 : GOTO 1 2300
1 2220 I F Z ( 1 , J , J ) = 0 T HEN ZRE = 0 : Z I M = - 1 / Z ( 2 , J , J ) : GOTO 1 2300
1 2230 BTA = ATN ( Z ( 2 , J , J ) /Z( 1 , J , J » : I F Z ( 1 , J , J ) <0 T HEN BTA = BTA+PI
1 2240 BTA = . 5 * S I N ( 2 * BTA)
1 2250 ZRE = BTA / Z ( 2 , J , J ) : Z I M = - BTA /Z ( 1 , J , J ) ' I NVERT Z ( x , J , J )
1 2300 FOR K = J TO KB + 1
1 23 1 0 Z1 = Z( 1 , J , K ) * ZRE - ( Z ( 2 , J , K) * Z I M )
1 23 1 5 Z ( 2 , J , K ) = Z ( 1 , J , K ) * Z I M + ( Z ( 2 , J , K ) * ZRE ) : Z ( 1 , J , K ) = Z 1
1 2320 NEXT K
1 2330 FOR I = 1 TO KB ' GET 0 I N T H I S COL . FOR ALL OTHER EQN S .
1 2340 I F I = J T H E N 1 2400
1 2350 XT = Z ( 1 , I , J ) : YT = Z ( 2 , I , J ) : I F XT =0 AND YT =0 THEN 1 2400
1 2360 FOR K = J + 1 TO KB + l ' DON ' T BOTHER W I T H "ZERO" POS I T I ONS
1 2370 Z( 1 , I , K ) = Z( 1 , I , K) - ( Z ( 1 , J , K ) * XT - ( Z( 2 , J, K ) * YT»
1 2380 Z ( 2 , I , K ) = Z(2, I , K) - ( Z( 1 , J, K) * YT + ( Z( 2 , J, K ) * XT »
1 2390 NEXT K
1 2400 NEXT I
1 2 4 1 0 NEXT J ' REPEAT FOR ALL D I AGONAL POS I T I ON S .
1 2420 RETURN
I ***********************************
, * P R I NTOUT CURRENTS *
I ***********************************
1 3000 CLS : PR I NT
1 30 1 0 FOR I = 1 TO KB
1 3020 PR I NT " 1 " ; I ; "=";
1 3030 PR I NT U S I N G "### . ###" ; Z ( 1 , I , KB + 1 ) ;
1 3040 PR I NT "+j " ;
1 3050 PR I NT U S I N G "### . ###" ; Z ( 2 , I , KB + 1 )
1 3060 NEXT I
1 3080 RETURN
Circuit Analysis Program 41
I ***********************************
, ***********************************
, * *
I N STRUCT I ONS
, ***************************************
Ikn
;3../
Figure 5. 1 - 6 Pole Butterworth (fco = 1 6 Hz)
capacitance). The mutual impedance between meshes I and 2 (Z I 2) is
input 0, 0, 1 7.42E-6. Z I 3 is, of course, 0, 0, 0 because there is no mutual
impedance between these loops (that is, the current flowing in loop 3 will
introduce no voltage into loop I , so we multiply 13 by zero in the loop I
equation. Similarly with Z14. We now come to the self impedance of
loop 2 (Z22) and here we have 0, 1 5 .47, and 7.082E-6. The 7.082E-6 is
the series sum ofthe two capacitors in this loop! Z23 is 0, 0, 1 1 .94E-6 and
Z24 is, again, 0, 0, o. Okay, you know how to input the rest of the data.
[Note that we didn't have to input Z2 1 because it's identical to Z 1 2. You
also recognize that the terminating resistance is 1000 ohms.]
No provision is made for entering voltage sources into any of the
loops (since our primary intention is to analyze filter circuits), but this
provision can easily be added (lines 1 1 1 50 - 1 1 1 70). We also assume that
the output will be taken from the last loop (not necessarily true in the
general case). Furthermore, to get the output in terms of voltage, we must
input the termination resistance mentioned above. That is, this program
solves for mesh currents and, to get things back in terms of voltage, the
output mesh current must be multiplied by the filter terminating resistance
(see for example, lines 625, 635, 644, 832, 834, 842, l O BO, 1 0 1 60 or
1 0 1 90). If you want to see the current, simply make this value equal to
1 .0. The frequency range must be specified (in Hz-line 40) and the
number of steps within each decade of frequency must also be specified
(typically 8 to 1 00-line 50). In this program the frequencies are
computed logarithmically to keep the data from becoming excessive;
however, when using this program with the FFT we will have to calculate
frequency in an arithmetic progression.
Circuit Analysis Program 43
VxJZx = I, +0 (S.4A)
Vy/Zy =0 +12 (S 48)
VziVz =0 +0 (S 4C)
where Vx, Zx, Vy, Zy, etc., will be determined in the process. To do this
we employ only the rules that we may multiply both sides of an equation
by equals without changing the equation, and we may add equals to both
sides of an equation without changing the equality. We start with:
where Z l 1 , Z22 and Z33 (along the diagonal) are the self impedances of
the loops and Z 1 2, Z 1 3, etc., are the mutual impedances between the loops
as explained earlieI Very well then, we start by dividing both sides of
Eqn. (5. 5A) by Z I 1 . VI becomes V l iZ 1 1 , the coefficient of I . becomes
unity, the coefficient of 12 becomes Z 1 2/Z 1 1 and the coefficient of 13
becomes Z 1 3/Z I I :
where V I ', Z 1 2 ' and Z 1 3 ' are the coefficients shown in Eqn. (5.5A l We
then do the same thing to Eqn.(5.5C) (i.e., we reduce Z3 1 to zero by
subtracting a copy of (5.5A) multiplied by Z3 1 ) yielding:
We now tum to Equation (5.6B) and divide all of its terms (on both sides)
by Z22', normalizing the 12 coefficient to unity:
V2" = 0 (5 58")
Finally, we normalize Z33", subtract from the other two eqns., and:
V I '" = I, (5.8A)
V2'" 0 (5 88)
·
V3 ' = 0 (5 8C)
imaginary component will be zero and, if this is so, our task is greatly
simplified (lines 1 22 1 0 and 1 2220)-otherwise, there is nothing left but to
crunch the numbers. Note that complex quantities make everything
complicated here. To obtain the reciprocal of a complex number we must
convert to polar coordinates; howeve� since we need the result back to
rectangular format, we take short-cuts (lines 1 2230 - 1 2250).
Once we have a " I " on the diagonal we must then get a "0" in this
column for all other equations. We do this, as we said, by subtracting a
copy of the normalized equation (after mUltiplying by the coefficient in the
Jth column) from each equation (lines 1 2330 - 1 24 1 0). We do not want to
subtract the normalized equation from itself, howeve� as that would get a
zero on the diagonal; so, when I = J (line 1 2340) we skip this routine.
Furthermore, we can speed things up considerably by avoiding needless
computation so, if the element is already zero, we forego all the arithmetic
(end of line 1 23 50). Also, we know that all the elements preceding this
column are already zero, and all the elements in this column will be zero
after we perform this routine, so we needn't actually do the arithmetic on
any columns to the left of J + 1 (line 1 2360). [Note: all voltage sources
are located at row position KB + I -see routine at lines 1 1 140- 1 1 1 70]
When we come out of this routine we will have the mesh current (for every
loop) in the KB+ 1 location of each row. [The real part of the current is
located in the " 1" side of the array and the imaginary part in the "2" side.]
The data output routines are pretty much what they appear to be
so we will not take up time and pages with this trivia. You might want to
run this program and analyze the Butterworth filter shown in Fig. S. l-just
to see if it works.
This program does what all network analysis programs do-it
gives the transfer function ofthe circuit. If, some day, the big work station
is in use, and you need a quick answe� it could come in handy. For us,
howeve� its real purpose will become apparent in the next chapter
CHAPTER 6
If you run this program for 100 and 1 000 frequency components,
you will get the results shown above. Except for "scaling" of the time
interval, there is little difference between these two runs. On adding more
components still, we find the overshoot remains a constant 8 .9% of the
transition amplitude (the transition in the above example is from - 1 to + 1).
Only the time scale will change.
This is known as the Gibbs phenomenon. The overshoot will
apparently remain at 8 . 9%-even when the number of components
increases without limit! This would seem to indicate a series of sinusoids
will never converge to a perfect reproduction of a discontinuous function;
still, we should not lose sight of the fact that as the number of components
approaches infinity, even though the amplitude of the overshoot remains
constant, the duration of the transient approaches zero, and zero duration
implies non-existence.
50 Chapter 6
1 32 NEXT I
1 34 KT = KT + 2 : NEXT J
1 36 NEXT M
140 ' *** B I T REVERSAL FOR F I NAL DATA ***
142 FOR I = 1 TO Q1 : I NDX = 0
1 44 FOR J = 0 TO N 1
1 46 I F I AND 2 A J T H E N I NDX = I NDX + 2 A ( N 1 - J )
1 48 NEXT J
1 50 I F I NDX > I THEN SWAP C ( I ) , C ( I NDX ) : SWAP S( I ) , S ( I NDX )
1 52 NEXT I
1 54 GOTO 200
270 FOR I = 0 TO Q -
272 YP = C ( I )
274 I F K6 = - 1 THEN YP = SQR ( C ( I ) A 2 + S ( I ) A 2 )
276 L I NE - ( XO + XSF * I , Y O - Y S F * Y P )
278 NEXT I
279 GOTO 222
280 ' *** F I ND Y SCALE FACTOR FOR FREQ DOMA I N PLOT ***
281 YO = 300 ' SET Y AX I S OR I G I N
282 FOR I = 0 T O Q - 1 ' F I ND LARGEST VALUE I N ARRAY
284 YP = SQR ( C ( I ) A 2 + S ( I ) A 2 ) : I F YP > YPX THEN YPX = YP
286 NEXT I
287 YSF = 200 I YPX I SET SCALE FACTOR
288 L I NE (XO + 1 0 , YO - 200 ) - (XO, YO - 200 )
289 LOCATE 8, 1 : PR I NT USI NG "### . ##" ; YPX : GOTO 265
,
298 *** PR I NT DATA TO CRT ***
300 PR I NT US I NG "###" ; Z; : PR I NT " ";
310 PR I NT US I NG "+##. #####" ; C ( Z ) ; : PR I NT " II ,.
II .
312 PR I NT US I NG "+##. #####" ; S ( Z ) ; : PR I N T " I
320 PR I NT US I NG "###" ; Z + Q2; : PR I NT " ";
II .
322 PR I N T US I NG "+##. #####" ; C ( Z + Q2 ) ; : PR I NT " ,
324 PR I N T US I NG "+##. ##### " ; S ( Z + Q2)
330 RETURN
***************************************
990 STOP : END
***************************************
54 Chapter 6
I ***************************************
1 000
1 002 I * *
MOD I FY SPECTRUM
1 004 I ***************************************
1 040 CLS : R T F L G = 0
1 042 I F K6 <> - 1 THEN 1 096
1 05 0 PR I NT SPC ( 30 ) ; "MOD I FY SPECTRUM MENU" : PR I NT : PR I NT
1 060 PR I NT SPC ( 5 ) ; " 1 = TRUNCATE SPECTRUM" : PR I NT
1 062 PR I NT SPC( 5 ) ; "2 = BUTTERWORTH RESPONSE " : PR I NT
1 064 PR I NT SPC ( 5 ) ; "3 = GAUSS I AN RESPONSE" : P R I NT
1 068 PR I NT SPC ( 5 ) ; "6 = EX I T " : PR I NT
1 070 PR I NT SPC( 1 0 ) ; "MAKE SELECT I ON : " ;
1 080 AS = I NKEYS: I F AS = " " THEN 1 080
1 082 A = VAL (AS ) : ON A GOSUB 1 1 00, 1 200, 1300, 1 990 , 1 990 , 1990
1 084 GOSUB 200
1 088 I F R T F L G = 1 THEN RETURN
1 090 GOTO 1 040
1 096 I NPUT "MUST TRANSFORM S I GNAL BEFORE MOD I FY I NG SPECTRUM"; AS
1 098 RETURN
1 1 02 CLS : PR I NT : PR I NT
1 1 04 I NPUT "ENTER CUTOFF FREQUENCY"; AS
1110 FCO = VAL ( AS ) : I F FCO = 0 OR FCO > Q3 THEN 1 1 04
1 1 30 FOR I = FCO TO Q - FCO
1 1 40 C ( I ) = 0: S ( I ) = 0
1 1 50 NEXT I
1 1 94 R T FLG = 1 : RETURN
imaginary array (Le., set S(I) = 0 clearing any data left over from
previous operations). This loop steps from 0 to Q/2 -1 (i.e., the first half
of the array). At line 640 we do the same thing for the second half of the
array except that we set C(I) to + 1 . We then jump down to the forward
transform routine (line 680), and when we come back to line 690, the
frequency domain function will be ready for our experiments.
' ***************************************
1 000
' * *
1 002 MOD I FY SPECTRUM
' ***************************************
1 004
1 040 CLS : R T F L G = 0
1 042 I F K6 <> - 1 T HEN 1 096
1 050 PR I NT SPC ( 30 ) ; "MOD I FY SPECTRUM MENU" : PR I NT : PR I NT
1 060 PR I NT SPC ( 5 ) ; " 1 = TRUNCATE SPECTRUM" : PR I NT
1 062 PR I NT SPC ( 5 ) ; "2 = BUTTERWORTH RESPONSE " : PR I NT
1 064 PR I NT SPC ( 5 ) ; "3 = GAUSS I AN RESPONSE" : PR I NT
1 068 PR I NT SPC ( 5 ) ; "6 = EX I T " : PR I NT
1 070 PR I NT SPC ( 1 0 ) ; "MAKE SELECT I ON : " ;
1 080 AS = I NKEYS : I F AS = " " THEN 1 080
1 082 A = VAL ( AS ) : ON A GOSUB 1 1 00, 1 200, 1 300, 1 990 , 1 990 , 1 990
1 084 I F RTNFLG = 1 THEN GOSUB 200 : RETUR N ' PLOT DATA AND E X I T
1 090 GOTO 1 040
1 096 I NPUT "MUST TRANSFORM S I GNAL BE FORE MOD I FY I NG SPECTRUM"; AS
1 098 RETURN
Type this program into your BASIC application software and run
it. The FFT can handle reasonably large transforms (in a matter of
seconds) so select 2"10 components to provide plenty of resolution. First,
simply transform the step function and reconstruct (to verify the original
time domain function is obtained). The results will be as shown in Figure
6.3 below-note that Gibbs' phenomenon does not appear when we do not
modify the spectrum. Next, repeat the transform and select the MODIFY
SPECTRUM routine. Remove a large portion of the spectrum (e.g., from
Transient Analysis 57
Reconstruction from
Uncorrupted Spectrum
/
No Oversboot
the 60th harmonic up). Reconstruct the time domain signal and you will
observe that Gibbs' phenomenon is present. Visually, things may not be
very clear; nonetheless, Gibbs' phenomenon is apparent.
Repeat the illustration, but this time remove everything from the
1 6th harmonic up. Now when we reconstruct the time domain the
oscillations at the transitions are readily apparent (Figure 6.4). So, Gibbs'
phenomenon does not appear simply because we transform and reconstruct
the data; howeveI; when we drop off the higher frequency components, it
definitely does appear [Did you notice anything peculiar? We will have
to come back to th is. Th ere are a couple of things in th e above
illustration that aren t completely on the up-and-up, but they have a
perfectly good reason for being there, and we 'U discuss them in detail
shortly.]
58 Chapter 6
6.5 FILTERS
3
Much of this was covered in the last chapter of Understanding the FFf
Transient Analysis 59
� eo
R.
el
el
T
eo = e l �
J21t1c
RI -JX<
-j X< -
the cutoff frequency, fCO while leaving harmonics below fco unchanged.
'
For physically realizable filters, however; the amplitude rolls-ojJ over
some range of frequencies. We must therefore decide where cutojJtakes
place-where the pass band ends and the reject band begins. We might
select the frequency where the capacitive reactance equals the resistance
as a sort of half-way point; however, because of the q u adra tu re
relationship between an imaginary reactance and a real resistance, this
attenuates the signal to 1 1\12 = 0.707. At this attenuation the power
2
delivered to a resistive load is attenuated to half (P V /R). Equating
=
But the circuit of Fig. 6.5 is not a very good filter. We might
improve things by connecting several RC sections end-to-end, with each
section sort of repeat-filtering the output of the preceding section; but,
while the notion of increasing the number of sections (Le., number of
po/es4) is indeed valid, cascading RC sections will never produce a truly
high performance filter. High performance filters require both inductors
and capacitors in each section (or active components-i.e., op amps).
Butterworth
0.9 Frequency
0.8 Response
1:1 0.7 vs.
,gII 0.6 Number o f Poles
� 0.4
; 0.5
..
0.3
0.2
0.1
Frequency
4
For the filters we will be considering here, the number ofpoles will be equal to
the number of reactive elements in the circuit The Transfer Function of a circuit may be
expressed as the ratio of two frequency domain polynomials The roots ofa polynomial are
the values of the independent vanable for which the function goes to zero. The roots of the
numerator are the zeros of the transfer function while the roots of the denominator are
poles-'nufT said
Transient Analysis 61
We know all real systems are band l imited, but we also know that
overshoot and ringing does not appear on every signal that has a rapid
transition. Why not? Obviously we still don't have the whole story here.
0.9 Gaussian
0.8 Frequency
Response
= 0.7 0.707
�II 0.6
� 0.4
; 0.5
..
0.3
0.2
0.1
feo 2 feo
Frequency
filters (or any circuit). Once we obtain this transferfunction, we may use
it to modify the spectrum of a square wave (or any other wave). This
requires nothing more than multiplication of the spectrum of the square
wave by the frequency response of the filter (complex multiplication of
course). By simply performing the inverse transform then, we obtain the
transient response of the circuit.
The two programs ( i . e . , ACNET02-0 1 and FFT06-0 1 ) are
combined or merged in Appendix 6. 1 to obtain a program which provides
the transient response of a circuit. The program will work pretty much as
the Gibbs Phenomenon program did, except that now we can enter a
circuit (from the ModJfy Spectrum Menu) and multiply the circuit transfer
function with the spectrum of the step function. Just as before, we find
the transient response via an inverse transform of the results. [Note:
While the program given is acceptable for the purpose of illustration, you
might want to expand it so that it can perform either frequency response
analysis or transient analysis. It's not such a difficult task, and on today's
high speed personal computers the execution time is not unacceptable
(especially with compiled BASIC). Even if you have access to heavy
weight circuit analysis programs, you might still find this application
handy on occasions. With only a modest effort you can add dependent
generators, which will allow inclusion of active components.
Before we leave this section, let's consider one of the peculiar
aspects of our Gibbs phenomenon discussion. When we considered
Butterworth filters, for example, we found the ringing and overshoot
started before the transition! Now, that's not how it should happen in the
real world- if overshoot and ringing are caused by a rapid transition they
should occurfollowing the driving transient.. . . What's wrong here?
The reason for the prescient ringing and overshoot in the above
is that we used only the amplitude response of the filter and ignored the
phase. The reason we ignored the phase response was that we had a
simple but accurate equation for amplitude response and it was easy to
modify the spectrum by this equation.6 There's a better way, of course.
When we determine the transient response via circuit analysis, we
automatically include the phase response, and the result gives a much more
truthful picture of the transient response.
6
There, s another reason for using only the amplitude response-it's sometimes
desirable to filter signals without disturbing phase, and we have illustrated how the FIT
allows that operation This technique might come in handy some day
Transient Analysis 65
6.10 ALIASING
If you generate this input and then immediately perform the inverse
transform, you will find Gibbs' phenomenon prominently displayed at the
comers-not because we have taken the transform, but simply because we
have started out with the incomplete, band limited, square wave spectrum.
Note that, even though ourfirst input signal was "corrupted" by
deliberate aliasing, the inverse transform still produced the "correct"
66 Chapter 6
7
Ifwe look at what the summation of these hannonics produce in the continuous
world, we find that it doesn't really produce a "flat-topped" time domain function either We
will investigate the technique of looking between the data points a little later
CHAPTER 7
7.0 INTRODUCTION
It's well known there are faster FFT algorithms than the classic
Cooley-Tukey derivation. "Tricks" that may be used to speed up the
transform become apparent when we analyze the FFT mechanics. For
example, in the first two stages, where we are dealing with one and two
point DFTs, the twiddle factors have values of + 1 , - 1 , and zero, and we
need not evaluate a Taylor series for these-nor perform a multiplication
at all. In fact, these first stages yield no sine components at all. We can
save time here by writing simplified routines for the first two stages of
computation and then revert to the conventional algorithm for the
remainde[ The modification will look something like this:
1 32 FOR M=2 T O N - 1 : • • • • •
[Note: The validity of this trick may not yet be apparent but we will
consider it in more detail later (under small array FFTs)-for now I
recommend you try this modification in the FFT routine of the previous
chapter; however, keep in mind that this modification works only for the
forward transform.]
This will cut off a little processing time and, as we said, every
little bit helps; however, there are other, much more significant, ways to
speed things up.
68 Chapter 7
4 . _ Nyqulst
Negative
2 Frequendes
(Omitted)
Time Domain
I"
Negative
Frequendes
Time Domain Nyquist (Omitted)
. .. . · .t--
Frequency Domain (Mag)
real sampling rate. So, then, even though we are only finding the positive
frequencies (relative to the real sampling rate), we will see the positive
and negative side bands about this half frequency pseudo sampling rate.
The unique thing here is that stretching a function (when only the positive
frequencies are involved) creates a mirror image of the un-stretched
spectrum above the pseudo sampling rate. This is the same mechanism
(but not quite the same result) as the simple repeat effect we obtained in
our original development of the FFT. [Note: In both cases the negative
frequency components are complex conjugates of the positive frequency
components, but when you take the complex conjugate of a complex
conjugate you get the original expression. Ifyou are retired, with more
time than pension, or ifyou are incurably curious, it may be worth your
time to work this out-see The Stretching Theorem, Appendix 5.3, in
Understanding the FFT. Ifyou understand Figs. 7. 1 and 7.2, however,
that's all you need to know to create a Positive Frequency FFT.]
Once again, we start with an 8 point data array [You might want
to read the FFT development given in Understanding the FFT before you
begin this presentation] :
We separate the odd and even data elements creating two stretched arrays:
1 DATA l ' 1 = 1 Do, 0, D2, 0, D4, 0, D6, ° 1 --- - -- -- - (7. 1)
1 DATA2' 1 = 1 o, D" 0, DJ, 0, Ds, 0, D7 1 - ---- - - - - (7.2)
We remove the zeros separating the data points in array I D.AJ'A l ' I ' and
obtain the array:
- - - - - - - - - - - - - - - -- - (7.4)
2
While I frown on mindless creation of acronyms, this mindless creatIon is an
exception. The "P" is silent (as in swimming) yielding a slightly aspirated "ffftl "
The PFFFT 71
(7.5)
where Fo, F\ and F2 are the same in both Eqns. (7.3) and (7.5).
Very well then, we must transfonn two arrays (i.e., the odd and
even data points), and add the spectrums obtained. As before, we must
rotate the phase of the odd data transform (due to the shifting phenome
non) before adding it to the even data transform. That is, the frequency
components from the odd data transfonn are properly phase-shifted and
..
Z!
D2 F I_I FO-I
4 Point «(
...
Xform '"
D4 F I_2 FO-l ...
;;
:c
..
..
D6 -
�
j
DI F2 _0 FO-J
DJ
4 Point F2_1 F(1.4
Xform
Ds Fl_2
o No Data
D7 No Data * = Complex Conj ugate o No Data
summed with the frequency components of the even data transfonn; but,
this time, the virtual components are created as the mirror image of the
direct components (i.e., the complex conjugates are summed in reverse
order). This is important so let's walk through the software for this
algorithm just to make sure we understand how it really works.
As before: Q = the number of data points.
K l = 2·PVQ
Q2 = Q/2: Q4 = Q/4
J counts the frequency components.
I counts the data points.
Y(I) = input data
C(J) = cosine components of frequency
S(J) = sine components of frequency
72 Chapter 7
We have now taken the transform of each half of the data base in
(Q/4+ 1) • Q/2 or Q2/8 + Q/2, for a grand total of Q2/4 + Q (as opposed to
Q2 operations in the DFT or Q2/2 for the conventional FFT). When Q is
large this cuts processing time in half again ... almost.
Okay, to obtain the complete transform we will sum the two
partial transforms as follows:
1 39 ' *** SUM ODD AND EVEN TRANSFORMS ( F I NAL STAGE ) ***
1 40 FOR J = 0 TO Q4 : J1 = J +Q 2 : J I = Q2- J : K2 = K1 * J : K3 = K 1 * J I
142 CTEMP1 = C ( J ) + C ( J 1 ) * COS(K2) - S ( J 1 ) * S I N ( K2 )
1 44 STEMP1 = S ( J ) + C ( J 1 ) * S I N (K2) + S ( J 1 ) * COS(K2)
1 46 CTEMP2 = C ( J ) + C ( J 1 )*COS(K3) + S ( J 1 ) * S I N ( K3 )
1 48 S ( J I ) = C ( J 1 ) * S I N ( K3 ) - S( J 1 ) * COS ( K3 ) - S ( J )
1 50 C ( J ) = CTEMP1 : S( J ) = STEMP 1 : C ( J I ) = CTEMP2
152 NEXT J
The PFFFT 73
We simply sum the frequency components of the even DFT with the
rotated components of the odd DFT. The virtual components are formed
(lines 1 46- 1 48) by summing the components in a mirror image (note that
the index n handles the virtual components in reverse order while storing
the data in the location from which it came). For example, the first two
components computed are the zero frequency and Nyquist components,
both of which can be put back into the locations from which they came.
If you step through this routine you will find the direct components may
always be placed in the locations from which they came while the virtual
components are always placed in empty locations. You may type this into
your computer and verify that it works if you like.
! L. L
: � } r . : � } r. . ::� } r-.
�
. Data Data Data
�} � }
.
c.os cos
.
f= 1 f = Nyq
}
Sm . S�
�
Cos
. • • f= Nyq
Sm
noted earlieI; with four data points (or less) the sines and cosines only take
on values of ±1 and 0, greatly simplifying things.
Figure 7.4 shows a 4-point DFT (vertically at the left), a 2-point
74 Chapter 7
DFT (running down the middle of the picture) and a I-point DFT (right).
We obtain the frequency components of the DFT by mUltiplying each data
point by the corresponding point from the cosine and sine function (at the
integer multiple frequencies-only positive frequencies are shown). It's
obvious that, while we must compute the value of the sine and cosine for
every data point in larger data arrays, calculating sines and cosines for
these small arrays is hardly necessary (they are either + I , - I , or 0). This
is the situation we took advantage of in the speedup technique discussed at
the beginning of this chapteI [Note that, in Fig. 7.4, the 2-point DFT
analyzes the even data points of the 4-point array (i.e., the 0 and 2nd data
points). For the 8-point array we have been considering in this PFFFT
illustration, the 4-point array would be the even data points of the whole
array. If we were building a full FFT there would have to be a second
DFT for the odd data points, of course (see Appendix 7.2).]
DO F o.o
D4 F (I. \
D:Z
D6 F (l.l
DI
DS F (l.J
D3
D7 F (I.o4
For this illustration the 2-point DFTs will look like this:
When we sum these four partial transforms (to form the two 4-
point transforms), our positive!requen cytransform (for 4 points) will only
have three components. The phase shifts for the components from the odd
DFT are still mUltiples of PU2 (i.e., 90°) which yields:
1 26 CTMP = ( C ( 0 ) +C ( 2 » / 2 : S ( 0 ) =0 I FOR f = 0 NO S I NE
1 27 C( 1 ) = C ( 1 )/2 : S ( 1 )=C(3 )/2
1 28 C(2) = ( C ( 0 ) - C ( 4 » /2 : S ( 2 )=0 I FOR f = NYQ NO S I NE
1 30 C(O) = CTMP
1 32 CTMP = ( C ( 4 ) +C(6» / 2 : S(4 )=0 I NO S I NE FOR f = 0
1 33 C(S) = C ( S ) /2 : S ( S )=C(7)/2
1 34 C(6) = ( C ( 4 ) - C ( 6 » / 2 : S ( 6 )=0 I NO S I NE FOR f = NYQ
136 C(4) = CTMP
Note that we ignore the sine components when forming the next higher
transform, and that we form only three positive frequency components.
From here we may tack-on the final stage summation routine at the bottom
of p. 72 and obtain the complete transform.
We ll and good but, carrying the FFT scheme to its logical
conclusion, we must consider the transform subdivision process through
one more step-until we are dealing with I -point DFTs.
Oata Rotate & Sum Rotate & Sum Stage II Rotate & Sum Stage III Output
DO � F240 �
/\:
�FI _
7 FI-II-I
\t }
� Fo.o
F�I
F�z
04 WIl Fl-O-I
02 � F:z..I-O
FI-II-z
06 WIl F:z..I-I
D1 � F:z..� �
/\:
�F I_I-O
7 FI_I_I
05 WIl FZ-l-I
03 � F:z..z-o
FI_I_z
F�3
F0-4
07 WIl F:z..z_I
n
:r
�
"CI
Figure 7.6 - Complete 8-Data Point PFFFT (Triple Butterfly) -
n>
.,
-...J
The PFFFT 77
apparent that we are performing the same rotation and summing routine
at every stage of computation, although it's also apparent the first two
stages don't actually need the excess baggage of calculating sines and
cosines (except to determine the signs of the summations). Let's look at
the shifting and summing process for the first stage of computation: to
form the zero frequency component of a 2-point transform we simply add
the two data points required (see Fig. 7.6). Note that, since there's no sine
component for a zero or Nyquist frequency component, the Nyquist
frequency term is formed by negating the higher order data point and
summing again (i.e., subtracting the two terms). For example, in lines 1 1 0
and 1 1 2 below, we do this for the 2-point transform o f data points D O and
D4, and store them in locations C(O) and C( 1 ).
132 FOR J = 0 TO 1 : JO = J + Q4
1 34 J 1 = J + Q4 + Q8: J I = Q4 + Q8 - J : K2 = 4*K 1 * J : K3 = 4*K1 * ( J I - Q4 )
1 36 CTEMP1 = C ( J O ) + C ( J 1 )*COS ( K2 ) - S( J 1 )*S I N ( K2 )
1 38 STEMP1 = S ( J O ) + C ( J 1 )*S I N ( K2 ) + S ( J 1 )*COS(K2)
1 40 CTEMP2 = C(JO) + C ( J 1 )*COS(K3) + S ( J 1 )*S I N ( K3 )
142 S( J I ) = ( C ( J 1 ) *S I N ( K3 ) - S ( J 1 )*COS ( K3 ) - S ( J O » /2
1 44 C ( J O ) = CTEMP 1 / 2 : S ( J O ) = STEMP 1 / 2 : C ( J I ) = CTEMP2/2
1 46 NEXT J
The PFFFT 79
148 FOR J = 0 TO 1 : JO = J + Q2
150 J1 = J + Q2 + Q8: J I = Q2 + Q8 - J : K2 = 4*K 1 *J : K3 = 4*K1 *( J I - Q 2 )
152 CTEMP1 = C ( J O ) + C ( J 1 )*COS(K2) - S ( J 1 )*S I N ( K2 )
1 54 STEMP1 = S ( J O ) + C ( J 1 )*S I N ( K2 ) + S ( J 1 ) *COS(K2)
156 CTEMP2 = C(JO) + C ( J 1 )*COS ( K3 ) + S ( J 1 )*S I N ( K3 )
1 58 S( J I ) = ( C ( J 1 )*SI N ( K3 ) - S ( J 1 )*COS ( K3 ) - S ( J O » /2
1 60 C ( J O ) = CTEMP 1 / 2 : S ( J O ) = STEMP 1 / 2 : C ( J I ) = CTEMP2/2
1 62 NEXT J
exactly how they change from stage to stage as well as between partial
transforms-they "j ump-out at you . " Now, this book is about FFT
applications; but, the FFT is j ust a computer algorithm. A l l FFT
applications are applications of an algorithm . . . . Listen carefully-it's
apparent there are many ways to write FFT software, and if you continue
working with the FFT you will eventually come to see that, for many
applications, it's best to write your own software (this will be amply
illustrated in the following chapters). When the day comes that you tackle
your first FFT, you will be well advised to lay-out all the loops as we have
done above. Let's look at the example before us:
From Figs. 7.3, 7.5 and 7.6; and the partial routines presented so
far, it's apparent that in each succeeding stage the J counter will count
from 0 to 1 ; 0 to 2; 0 to 4; etc. (remember we have already done the zero
stage). With the number of data points Q = 2N we know there will be N
stages of computation; so, if we let M count 1 to N, it will count the stages
of computation. Furthermore, QP = 2M will equal the size of each partial
transform in each stage, and QPI = 2(N-M) will equal the number of partial
transforms at that stage. If we review the 1 6 point transform on the
preceding page (with all its individual loops) we will find that these two
variables are most of what we need to produce a complete PFFFT. Having
made these observations it will be apparent that the new algorithm we are
looking for can be written as follows:
1 1 6 NEXT J
1 1 8 C ( 1 2 ) = (S( I NDX) + S( I NDX+o2» /2: C( 1 2+1 ) = (S( I NDX) - S( I NDX+o2» /2
1 20 NEXT I
1 22 FOR I = 0 TO Q1 : S( I ) = 0 : NEXT I ' CLEAR S I NE COMPONENTS
, ********* SUM REMA I N I NG STAGES ************
1 24 FOR M = 1 TO N 1 : QP = 2AM : QPI = 2 A ( N 1 - M ) : KO = QPI *K1
1 26 FOR K = 0 TO QPI - 1
1 28 FOR J = 0 TO QP/ 2 : JO = J + ( 2*K*QP)
1 30 J 1 = JO + QP : J I = J 1 - ( 2*J ) : K2 = KO*J : K3 = KO*J I
1 32 CTEMP1 = C ( J O ) + C ( J 1 )*COS(K2) - S ( J 1 ) *S I N (K2)
1 34 STEMP1 = S ( J O ) + C ( J 1 )*S I N (K2) + S ( J 1 ) *COS(K2)
1 36 CTEMP2 = C ( J O ) + C ( J 1 )*COS(K3) + S ( J 1 )*S I N ( K3 )
1 38 S( J I ) = ( C ( J 1 ) * S I N ( K3 ) - S( J 1 ) *COS ( K3 ) - S ( J O » /2
140 C ( J O ) = CTEMP1 /2: S ( J O ) = STEMP1 /2: C ( J I ) = CTEMP2 /2
142 NEXT J
144 NEXT K
146 NEXT M
148 FOR J = Q2+1 TO Q1 : C ( J ) = 0 : S ( J ) = 0 : NEXT J ' CLEAR UPPER ARRAY
150 SK1 = 2 ' SET P R I NT SCALE FACTOR
, **********
900 STOP
82 Chapter 7
8.0 INTRODUCTION
r- FI-I-O ------�----O FO - O
00
02 r- F l -I-I --------�.---_r--O FO - I
04
r- FI-I-2 --------�rT--r__r--O FO - 2
06 8 POINT
POSITIVE
08 FREQUENCY r- FI-I-3 --------��.-r__r--O FO - 3
FFT
010
r- FI-I-4 --------�����_r--O FO - 4
012
014
@ = Complex Conjugate
01
03
r- FI-2-O L--1---cr__r--.f, 14-�,-{)FO - 5
05
015
'--______---'r- FI-2-4
(8. 1 )
and:
( 8 .2)
where the * indicates complex conjugates and [08 ] indicates the usual
I
phase rotation necessary for the time-shifted transform. Similarly, the
other components are formed as follows:
I
We ignore the requirement of dividing the summed components by 2 in the
forward transfonn-following this derivatlon will be difficult enough!
86 Chapter 8
Very well then, we have eliminated the F,_ , _ , term from these equations
we now need to look at what's left [i.e., the FI_2_1 terms]:
,81 and ,87 are the twiddle Jactor angles, and because of the way we
generate these angles, ,87 is equal to 7r - ,8 1 (i.e., they are generated by
dividing 7r into N equal increments). Furthermore, Cos(7r -,8 1 ) = -Cos( ,8 1 )
and Sin(7r - ,81) = Sin( ,81)' Substituting these Eqn.(8. 14) becomes:
We know the imaginary parts of FI_I_I * and FI_2_1* are just the negated
imaginary parts of F , _ , _ , and FI _2_1 . Therefore, (8. 1 7) becomes:
To remove the F, _ , _ , term from these equations it's apparent that we must
add (8. 1 6) and (8. 1 8):
This, of course, is the component of phasor F. _2_1 that's projected onto the
The Inverse PFFFT 87
Re {F, 2 d = [Re{Fo d - Re{Fo 7} ]COS{31 + [1m {Fo d+lm {Fo 7} ]Sin{31 (8.2 1 )
Im {FI 2 1 } = [lm {Fo I} + Im{Fo 7}]Cos{3l - [Re {Fo 1 } -Re {Fo 7}]Sin{31 (8.22)
while, from (8.2 1 ) and (8.22) the inverse transform for FI _2_1 is given by:
(8.23)
These are similar equations but, once we know the coefficients for F 1-2- 1
the equation for FI _ I _ I is even closer:
(8.24)
1 . Subtract the real (i.e., cosine) term of F0-(8-x) from the real term
of Fo_x : MCT = Re { Fo_x } - Re { Fo-{8_X)} ·
2. Add the imaginary (i.e., sine) part of FO-{8-x) to the imaginary
part of Fo_x : MST = Im { Fo_x }+ Im { Fo-{8_X) } .
3. The cosine term for F I_2_X then equals MCTCos{3x + MsTSin{3x.
4. The sine term is equal to MSTCos{3x - McTSin{3x.
88 Chapter 8
Note: Steps 3. and 4. are simply the operation of rotating the phasor
backward by {3x. FI_I_x is then obtained by subtracting the rotated phasor
FI_2_X from the phasor Fo_x as follows:
8 I ********************************************
10 I *** F FT08 - 0 1 *** POS I T I VE FREQUENCY FFT ***
11 I ********************************************
12 CLS : P R I NT " I NPUT NUMBER OF DATA POINTS AS 2"N"
14 I NPUT "N = "; N
16 Q = 2"N : N1 = N - 1 : Q1 = Q - 1 : Q2 = Q/2 : Q3 = Q2 - 1 : Q4 = Q/4
18 Q5 = Q4 - 1 : Q8 = Q/8: Q9 = Q8 - 1 : Q34 = Q2 + Q4 : Q 1 6 = Q/ 1 6
20 D I M Y C Q ) , C C Q ) , S CQ ) , KC C Q2 ) , KSCQ2)
30 P I = 3 . 1 41 59265358979#: P2 = P I * 2 : K1 = P2/Q
32 FOR I = 0 TO Q3 : KCC I ) = COS C I * K1 ) : KSC I ) = S I N C I * K1 ) : NEXT I
*******************************
******** MA I N MENU ********
*******************************
40 CLS : PR I NT SPC ( 30 ) ; "MA I N MENU" : P R I N T : P R I NT
60 P R I NT SPC( 5 ) ; "1 = ANALYZE Q/2 COMPONENT FUNCT I ON " : P R I NT
62 P R I NT SPC( 5 ) ; "2 = I NVERSE TRANSFORM " : P R I NT
64 PR I NT SPC ( 5 ) ; "3 = P R I NT RESULTS" : P R I NT
66 PR I NT SPC ( 5 ) ; "4 = EX I T " : PR I NT
70 PR I NT SPC ( 1 0 ) ; "MAKE SELECT I ON : " ;
80 AS = I NKEYS : I F A S = " " THEN 80
82 PR I NT AS
90 A = VAL C AS ) : ON A GOSUB 600, 200 , 350, 900
92 GOTO 40
The Inverse PFFFT 89
98 ' *****************************
1 00 ' *** FORWARD TRANSFORM ***
1 02 ' *****************************
1 06 ' *** TRANSFORM STAGE 1 ***
1 08 T9 = T I ME R
110 C ( O ) = ( S( O ) + S(Q2» /2 : C ( 1 ) = ( S( O ) - S(Q2» /2
112 FOR I = 1 TO Q3 : 12 = 2 * 1 : I NDX = 0
1 14 FOR J = 0 TO N 1 : I F I AND 2AJ THEN I NDX = I NDX + 2 A ( N - 2 - J )
1 16 NEXT J
1 18 C ( 1 2 ) = ( S ( I NDX)+S ( I NDX+Q2» / 2 : C ( 1 2+ 1 ) = ( S( I ND X ) - S ( I NDX+Q2 » /2
1 20 NEXT I
1 22 FOR I = 0 TO Q1 : S( I ) = 0 : NEXT I ' ZERO S I NE TERMS
********* REMAI N I NG STAGES **********
1 24 FOR M = 1 TO N 1 : QP = 2AM: QPI = 2 A ( N 1 - M )
1 26 FOR K = 0 TO Q P I - 1
1 28 FOR J = 0 TO QP/2 : JO = J + ( 2 * K*QP ) : J 1 = JO + Q P : K2 = QP I * J
1 30 J I = J1 - ( 2* J )
132 CTEMP1 = C ( J O ) + C ( J 1 ) * KC ( K2 ) - S ( J 1 ) * KS(K2)
1 34 STEMP1 = S ( J O ) + C ( J 1 ) * KS(K2) + S ( J 1 ) * KC ( K 2 )
1 36 CTEMP2 = C ( J O ) - C ( J 1 ) * KC ( K2 ) + S ( J 1 ) * KS(K2)
1 38 S ( J I ) = ( C ( J 1 ) * KS(K2) + S( J 1 ) * KC (K2) - S ( J O » /2
140 C ( J O ) = CTEMP 1 / 2 : S ( J O ) = STEMP 1 / 2 : C ( J I ) = CTEMP2/2
142 NEXT J
144 NEXT K
146 NEXT M
148 FOR J = Q2 + 1 TO Q1 : C ( J ) = 0 : S ( J ) = 0 : NEXT J
150 T9 = T I ME R - T9 : SK1 = 2
152 RETURN
REM ****************************************************
REM * I NVERSE TRANSFORM *
REM ****************************************************
200 P R I NT : T9 = T I ME R : SK1 = 1
202 FOR M = N1 TO 1 STEP - 1 ' LOOP FOR STAGES OF COMPUTAT I ON
204 QP2 = 2 A ( M ) : QP = I NT ( QP2/2 ) : QP4 = 2* QP2 : QP I = 2 A ( N 1 - M )
206 FOR I = 0 T O Q - (QP2 ) STEP QP4
208 FOR J = 0 TO QP : KI = J + I : KT = J * QPI : KJ = QP2 + K I
212 MCT = C ( J + I ) - C ( I + QP2 - J ) : MST = S ( J + I ) + S ( I + QP2 - J )
214 CTEMP = MCT * KC( K T ) + MST * KS( KT )
216 STEMP = MST * KC ( K T ) - MCT * KS( KT )
218 CTEMP2 = ( 2 * C ( J + I » - CTEMP * KC ( K T ) + STEMP* KS(KT )
220 S ( K I ) = ( 2 * S ( J + I » - CTEMP * KS( KT) - STEMP* KC ( KT )
222 C(KJ ) = CTEMP : S ( K J ) = STEMP : C ( K I ) = CTEMP2
224 NEXT J
226 NEXT I
228 NEXT M
229 ' ******** F I NAL STAGE ********
230 FOR I = 0 TO Q3 : 1 2 = 2* 1 : I NDX = 0
232 FOR J = 0 TO N 1 : I F I AND 2AJ THEN I NDX = I NDX + 2 A ( N - 2 - J )
234 NEXT J
236 S ( I NDX ) = C ( 1 2 ) + C ( 1 2 + 1 ) : S ( I NDX + Q2) = C ( 1 2 ) - C ( 1 2 + 1 )
238 NEXT I
240 FOR I = 0 TO Q1 : C ( I ) = 0 : NEXT I
242 T9 = T I ME R - T9
244 RETURN
90 Chapter 8
********************************
******* P R I NT OUTPUT *******
********************************
350 CLS : PR I NT SPC ( 1 6 ) ; "***** "; NMS; " *****"
352 P R I NT " FREQ F ( COS) F(SIN) FREQ F ( COS)
F(SIN)"
354 PR I NT
362 TZT = 20 : FOR Z = 0 TO Q4 ' P R I NT OUTPUT
364 PR I NT U S I N G "####" ; Z ; : P R I NT " ";
366 P R I N T U S I N G "+##. #####" ; SK1 * C ( Z ) ; : P R I N T " ";
368 P R I N T U S I N G "+##. #####" ; SK1 * S ( Z ) ; : P R I NT " ";
370 P R I NT U S I N G "######" ; Z + Q4; : P R I NT " ";
II .
372 P R I NT U S I N G "+##. #####" ; SK1 * C ( Z + Q4 ) ; : P R I NT " ,
374 P R I N T U S I N G "+##. #####" ; SK1 * S ( Z + Q4 )
376 I F Z > TZT THEN TZT = TZT + 20 : I NPUT AS
378 NEXT Z
380 P R I NT : P R I NT "T = "; T9: I NPUT "ENTER TO CONT I NUE"; AS
382 RETURN
9.0 INTRODUCTION
Spectrum analysis brings us into conflict with the real world, for
here we don't have the luxury of analyzing mathematical functions that are
made-to-order (so to speak). In the practical realm of spectrum analysis,
the signals are generated by and for completely independent criteria, and
the customer who purchases an instrument to analyze his spectrum expects
it to accurately measure and display the spectrum of his signal-not
artifacts of the DFT process.
We begin our investigation using the program we developed in the
last chapter (i.e., FFT08 . 0 1 ). We will study a simple input-a single
sinusoid (a sine wave) with its frequency selected by the user. This single
sinusoid input is ideal for our task-it produces a single point in the
frequency domain so it will be easy to understand what we see. So, what
could possibly go wrong?
At this early stage in our discussion we need waste no time on
long-winded theoretical discussions-as they say in show-biz, let's cut
directly to the chase' .
'
Hollywood discovered long ago that, after struggling intenninably with no story
at all, a 1 0 minute "chase scene" guaranteed a box office success Shortly thereafter
everyone figured out what they were doing-hence the expression. These expressions can't
stand up to the overuse they're given but, used sparingly, they're effective
92 Chapter 9
TIME - 5 . 8 5 9 3 7 5 E - 02
Okay, we know the DFT (and FFT) can only analyze waveforms
into orthogonal components. That is, frequencies that fit perfectly into the
time domain interval; howeveJ; we also know that, in the real world, the
s inusoids we input to our spectrum analyzer may be any frequency
whatsoever! So, what happens when we analyze some frequency that's not
a perfect match for our computer algorithm? What happens when we
analyze a sinusoid that doesn't match-up with the harmonics used in the
analysis?
Run FFT08.0 1 again and this time select a frequency of 16. 1 .
Pretty messy, hey? Messy, but nonetheless, the basic sine wave component
is still there . . . we knew something would have to "give." Look at it this
way: it would seem not unlikely that the amplitude of a sinusoid (whose
frequency was between the frequencies of our analytical scheme) would be
sort of averaged between the frequencies on either side. Okay, but what's
that garbage all over the spectrum?
Run the an alysis yet again, and sel ect a frequency of 1 6. 2
Ummm. . .things are not getting better We can still tell there's a signal in
there somewhere, but the garbage is getting worse.... Let's cut directly to
the chase here. Run the analysis with a frequency midway between the two
analyzing components-run it at 16. 5 Hz. The result of this analysis is
shown in the printout below, and it's not good!
The Spectrum Analyzer 93
TIME . 0 0000
We are generating a sine wave to test our FFT analyzer, but the sine
component has disappeared completely. There's nothing but cosine
garbage spattered all over the spectrum. We're supposed to sell this thing
to people who want to see the sinusoidal components in their signal, but it
has completely lost our input component. I wouldn 't buy this thing
myself1 Ahhhhhhh . .Mission Control, we may have a problem.
.
Why does the transform of a single sinusoid come out this way?
Well, that is the question; but, before we rush off on that tangent let's
complete the above illustration. If you reconstruct the 1 6.5 Hz signal from
the garbage shown above, you will get a faithful reproduction of the
original input! Even though no sine component appears at 1 6.5 Hz (there
is no 1 6.5 Hz component), the cosine component garbage yields a sine
wave on reconstruction. Apparently this is a legitimate transform(?}, but
it's like our FFT has "busted a spring" trying to come up with a transform
for a frequency it doesn't have ! That kind of commitment is admirable,
but this busted spring transform is useless as a spectrum analyzer.
Ahhhhh . . Mission Control, we do have a problem.
.
Now let's look at why. First of all, the size of the data array (e.g.,
64 data points, 1 28 data points, etc.,) isn't completely arbitrary. Sampling
rate must be twice the highest frequency in the signal (we will talk about
this in detail later) and we must digitize long enough to include the lowest
frequency ofinterest. Other things will influence the decision (e.g., the use
of a round number 2N data points); still, the lowest and highest frequencies
of concern will determine the minimum data array size (i.e., sampling rate
multiplied by data interval-see Fig. 9. 1 ) . Now, from the orthogonality
requirement, we must analyze this data using harmonics whichfit into the
data interval (i.e., we must use integer multiples of the fundamental
94 Chapter 9
sinusoid that fit exactly into the domain of the digitized function-Fig.
9 . 1 ) ; however, it's clear the problem w ith the N+ 1 I2 Hz frequency
components dropping out is related to the use of these integer multiple
frequency components. . . hmmm.
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 - Samplia£ Pulses
J. = Samplbrg Rate
Fundamental
-- for Analysis
.'
the second half with zeros). We know we can take the FFT of this 1 28
point array just as we did the 64 point array, and we know the inverse
transform will reproduce the data we started with (i.e., 64 data points
followed by 64 zeros). Let's ignore the 64 zeros for now and look at what
has happened to the relationship ofthe fundamental (and harmonics) of the
analytical configuration. Look at Fig 9.2 again-as far as the 64 data
points are concerned we are now analyzing the data every 1 12 Hz!
We can see where this is going; but, is this transform the same as
the one we obtained before? Obviously it isn 't-we have frequency
components we didn't have before, but is this a "correct" spectrum for our
64 data points? There can be no question this is a legitimate transform . . .
but i s i t the true spectrum o f the input signal? Will i t accurately display
sinusoid components that really exist in the input signal (and not display
components that don t exist in the input signal)? A more immediate
question is whether this 1 /2 Hz transform will actually recover the
fractional frequency we lost in the conventional transform?
Let's cut to the chase scene here. What we really want to know
is this: "Can we develop a 1 /2 Hz FFT that will cause our spectrum
analyzer to detect half-frequency components?" Well, we can certainly
make an analyzer that will lookfor 112 Hz components! All we need to do
is increase the array size to the next power of2 and fill the second half of
the array with zeros. [There is, of course, another way, but let's keep it
simple-at least until we can figure out if this is going to work.] The real
question is whether this will accurately display 112 Hz components.
Okay, let's cut to the chase here-just run program FFT9.02
(Appendix 9.4). This program starts-up with a menu of fractional
frequency modes. Any selection jumps to a spectrum analyzer display
(with a single frequency input signal). We can increment or decrement the
frequency by 0. 1 Hz increments using the cursor control (arrow) keys.
Okay, let's find out what we really want to know-select the second
illustration which implements 112 Hz analysis of the data. We note that
now, with this 112 Hz analysis, even with a nice round integer frequency
the garbage is present.. .but let's not worry about that now. . . . Using the
cursor key, step the frequency up 0.5 Hz (i.e., press the right cursor arrow
five times). Watch the spectrum change with each step. Even though the
amplitude still changes with each step, it only varies by a few percent-it
certainly never vanishes. Okay, we just might be moving forward here . . . .
96 Chapter 9
Let's pause here to talk about this extended array data and its
transform. Fig. 9.2 (and a little mental juggling) reveals that the even
components of the 1 28 point analysis are identical to the components of the
original 64 point analysis (recognizing an amplitude scalefactor change of
course). So, if the even frequency components are identical to the original
analysis, what are the odd frequency components doing? If the even
frequency components represent the original spectrum, how much do these
additional odd components distort the spectrum?
[Consider this: if we input an integer frequency sinusoid, the even
components will be identical to the original analysis, but on reconstruction
the transform must know to stop generating data after the first 64 data
points. There must be something in the spectrum which starts and stops
the reconstructed data-something that makes it appear in the first half of
the 1 28 data point array. If the even components are identical to the
original transform components, it would appear the odd components must
contain the information about starting and stopping in the front half; but,
do these odd frequency components make this spectrum unusable as a
spectrum analyzer?]
Run FFT09.02 again and select illustration 3. This time we make
the array four times the length of the data being analyzed-we are now
analyzing the data at 1 14 Hz intervals. If we scan the spectrum the
amplitude varies even less than before; but, also, the garbage is beginning
to look coherent.. .hey guys, is that a light I see at the end of the tunnel?
Enough of this-let's really cut to the chase. Run illustration 5
and now you will see that the garbage actually has a well-defined structure
�::
(Fig. 9.3). These bumps are (to a good approximation) the absolute value
!�
•
Is = Signal Frequency
J; = 1/Io
fNyquist
( nf, = Integer
�
1
1 \,
Harmonics
", ,, I :·
.
: :
1\ • :
. f\... \
Figure 9.3 - 1 1 1 6 Hz Transform
of a sine function. What's really going on here?
We all see that this sinc function appears because we have packed
the aft end of the array with zeros, but some suppose it's generated by an
The Spectrum Analyzer 97
Note ' You may have recognized that, while we 're ostensibly
illustrating how the FFT is applied, we spend a lot of time kicking around
fundamentalphenomena underlying this technology � introduce the sine
function here, but we will return to this profound little gizmo repeatedly in
the following chapters.
Let's cut directly to the chase-what the heck's a sinc function?
A sinc function is defined as Sin(x)/x. That is, it's a sine wave divided by
its own argument so the amplitude
decreases as the argument increases.
Interestingly, although the sine func
tion starts with a value of zero, the
value of Sin(x)/x starts at 1 .0 (we ex
plained why this happens back on p. 6
at the end of Chapter 1 ), but there are Figure 9.4 - The Sinc Function
98 Chapter 9
Tab l e 9 . 1
4 1 2 S(I) = 1
•
•
transform of a rectangle wave is a sinc
•
•
function-rectangle functions do little
more than start and stop (i.e., if we
•
- Integer
•
f,
o 1 Harmonics multiply a second function by this
·
. .!\. /�
I \'\." �
serve s o n l y to start and stop t h e
•
•
9.S MODULATION
sidebands of the modulating wave. So, it's apparent that, in our fractional
frequency transform, this is what we have done. We have multiplied the
signal by a rectangle function (in the time domain, see Fig. 9. 8), and the
sinc function spectrum must appear about the harmonics of the input
function in the frequency domain.
Clearly, then, whenever we start and stop data (as in a fractional
frequency transform) we are effectively multiplying by a rectangle
function, and the sinc function must always appear in the resulting
spectrum. But wait-this is true whether we analyze with fractional
frequencies or not! When we start and stop data, mathematically we are
multiplying by a rectangle function, and the sinc function spectrum will
appear about the harmonics of the sampled data function (Fig. 9.3). In a
sense, these sinc function sidebands are the frequency domain start and
stop. . .the beginning and end of the sampled data interval.
[Note: If the function we sample is more complicated than a single
2
We should note that, if the modulating function contains a constant term (i e ,
a DC component), the sinusoid being modulated will not disappear from the product
The Spectrum Analyzer 101
Our second problem with the sinc function is this: the sinc
function side-bands are of non-negligible amplitudes. These side lobes ride
on the signal components and wreak havoc on quantitative analysis. Now,
in a practically useful instrument we expect to resolve signals more than
100 db down. To see the severity of
• t-----,:It--- -.�::- this effect we must display the spec
- ....
- 1-----.."II\-fII..-------
----::c trum in the way it's most often dis
. I- played on spectrum analyzers-as the
•
logarithm of amplitude ratios (i.e., in
..
. _ ---
• . OR
II
. It j ust might be possible to
produce improvements over the old
• analog instruments. Don't be misled by
•
• the s l owness of these pro grams
..
BASIC is notoriously slow ! 6 With
•
·Chip Sets" are available from major manufacturers which run a 1024 point FFT
6
1 0.0 INTRODUCTION
.. .. . . .'
/�
.
Negative Alternation
� 1_
�
Sample
Pulses
I
I
Two I
- Sample -J
Pulses
Figure 1 0.2 - Nyquist Samples per Cycle
Lets clear a hole in thefog here: in discussing this phenomenon
we will sometimes refer to the highest frequency component in the signal
as the Nyquist frequency while the sampling rate will be referred to as the
Nyquist sampling rate. This confusing terminology comes about simply
because of the way we specify Signal frequency (as opposed to sampling
rate) The "Hertz" implies a number offull sinusoids (two alternations in
a cycle)per unit time while Samples per Second implies a number ofSingle
events (samples) per unit time. If we were to define the frequency of
sinusoids by the number of alternations per unit time, then the 2: 1 ratio
would become 1 : 1. The point is this: in a sense the sampling rate is
actually the same thing as the Nyquist signal frequency-its just that we
only count one sample per "cycle" of sampling rate, but we count two
alternations for a cycle of signal (compare Figure 10. 1 to Figure 10.2).
So, it appears there's nothing mysterious about the Nyquist
sampling criterion of 2: I . . . but there's a bit more to it than just this two
alternations vs. one sample per cycle. Surely 2: 1 is imperative if we hope
to capture the highest frequencies, but is 2: 1 enough? Can we realIy (in the
practical world) capture and reproduce a high fidelity replica of any signal
at 2: I ? Let's investigate some of the sampling rate implications that may
not be immediately obvious.
Sampling 107
f\ f\ f\
�
I � at Zero C rossover
I I I I� I I
Figure 1 0.3 - Out-of-Phase Sampling
in Fig. 1 0.2, we will capture every fluctuation in the signal; howevet; what
if the signal and sampling pulses are shifted in phase? Suppose things are
as shown in Fig. 1 0.3? Here the digitized data will indicate no signal
when, in fact, a perfectly good sinusoid is present at the input.
Actually, the situation shown in Fig. 1 0.3 is of little practical
consequence. In a practical system we will never have a sinusoid of
significant amplitude exactly at the Nyquist frequency. Our discussion of
filters in Chapter 6 makes it obvious that, if there are to be no components
above the Nyquist, then (due to the finite roll-off rate in physically
realizable filters) the harmonics must already be of negligible amplitude at
the Nyquist frequency. What we must worry about are the components just
below the Nyquist-here we will have harmonics of significant amplitude.
So, what happens to sampled data in this part of the spectrum?
Once again, let's use a short computer program:
10 REM *********************************************
11 REM * NYQU I ST TEST 1 *
12 REM *********************************************
20 PI = 3 . 1 4 1 592653589793# : F 1 = 0 . 9 ' F1 = X NYQU I ST
30 FOR N = 0 TO 20
32 X = N * F 1 *P I ' SOLVE FOR ARGUMENT
40 Y = COS ( X ) , SOLVE FOR COS I NE
48 PR I NT N , : PR I NT US I NG n ## . #####n ; y
50 NEXT ' D O T H I S 2 0 T I MES
60 END
108 Chapter 10
Note: The basic scheme here is to sample a sinusoid at rates near the
Nyquist. but since we are only concerned with the output of the AID
converter we need only generate numerical values of the sine function at
the sampling points.
If F 1 had been set to 1 .0 in line 20 we would simply obtain the
alternating values of + 1 and - 1 [i.e. Cos(Nr), where N integer values =
- '" �N 1\ "
/
/
/ , f\ f\ f/
" /
,
,
,
, /
I II
I \
/
/ \
,
"
V V \ /
V V
,
.� �.
r� 1\ !� ./\'
.. .. .: .. ,
v
. oJ
,' .. :. ',
V V
Sinusoids are pretty simple things when you get to know them
(never met a sinusoid I didn't like); so, when you see a distinctive pattern
(e.g., Figs. 1 0.4 and 1 0.5) you can't help but suspect that, under that flimsy
disguise, it's the same guy (or at least a close relative). 2 Now, if we had
digitized the sum of the two sinusoids shown in Fig. 1 0. 5 we would expect
the result to look like Fig. 1 0.4; but, we only digitized a single sinusoid!
Let's run another experiment.
Instead of setting F 1 to 0.9, let's make it 1 . 1 . This is an illegal
condition according to Claude and Harry-it's 1 0% above the Nyquist
limit-but let's just see what happens when we break this rule. ( We can
make life easier here with a little modification to our computerprogram:)
N F1 = 0.9 F1 = 1.1
o 1 . 00000 1 . 00000
1 - 0 . 95 1 06 - 0 . 95 1 06
2 0 . 80902 0 . 80902
3 - 0 . 58778 - 0 . 58778
4 0 . 30902 0 . 30902
5 - 0 . 00000 0 . 00000
6 - 0 . 30902 - 0 . 30902
7 0 . 58778 0 . 58779
8 - 0 . 80902 - 0 . 80902
9 0 . 95 1 06 0 . 95 1 06
10 - 1 . 00000 - 1 . 00000
11 0 . 95 1 06 0 . 95 1 06
12 - 0 . 80902 - 0 . 80902
13 0 . 58779 0 . 58778
14 - 0 . 30902 - 0 . 30902
15 0 . 00000 0 . 00000
16 0 . 30902 0 . 30902
17 - 0 . 58778 - 0 . 58779
18 0 . 80902 0 . 80902
19 - 0 . 95 1 06 - 0 . 95 1 06
20 1 . 00000 1 . 00000
3
Summing sinusoids from above the Nyquist into the digitized data is tenned
aliasing, and obviously compromises the sampled data.
Sampling 111
the digitization routine given above (i.e., Nyquist Test 1 ), you will find that
sampling ratios of less than about 8: I frequently give poor replicas of the
input sinusoids, and we must wonder if this sort of thing is acceptable.
Worse yet, the presence of a beat frequency modulation envelope hints
there may be sinusoids in the digitized data that were not in the original
signal-that this modulation is really intermodulation distortion.... So, is
Shannon's 2: 1 ratio really sufficient?
4
lbe data extends in time and/or space, and the conversion process must
include this extension to some extent, but the numbers know nothing of this. We may use
them in mathematical developments that assume they are extended in time, but the
numbers know of that either'
112 Chapter 10
**************************************************
* APPL I CAT I ONS 1 0 . 2 - I MPULSE FUNCT I ON TEST *
**************************************************
12 I NPUT "NUMBER OF TERMS AS 2AN " ; N ' NUMBER OF S I NUSO I D S TO SUM
20 Q = 2 A ( N+4 ) ' MUL T I PLES OF 16 FREQUENCY COMPONENTS
22 P I = 3 . 1 4 1 59265358# ' NO I DEA BUT CAN ' T STOP NOW • • •
30 FOR I = 0 TO 2 * P I STEP P I I 8 ' COMPUTE EACH DATA PO I NT
32 Y = 0 ' I N I T I AL I ZE ACCUMULATOR
40 FOR J = 0 TO Q - 1 : Y = Y + COS ( I * J ) : NEXT ' SUM ALL HARMON I CS
50 PR I N T US I NG "#### . #####" ; Y ' PR I N T DATA PO I NT
60 NEXT I ' DO NEXT DATA PO I NT
62 I NPUT A$' WA I T HERE , TRUSTY 286 • • •
When you run this routine you will notice that the single point at the origin
has a value equal to the number of sinusoids summed (Table 1 0.2). The
reason is fairly obvious-the value of a cosine is 1 .0 when the argument
is zero. If we sum Q cosine waves, which all start with an argument of
zero (regardless of their frequency), they will all have an initial value of
1 .0; therefore, the initial data point must have a value of Q. Table 1 0.2
shows the value of each of the harmonic components (i.e., FOO, FO 1, etc.)
at the data sample times (TOO, TO 1, etc.). [It took a lot of time to create
this picture so I would appreciate it if you perused it carefully. ]
So, the first data point is 1 6.000, but how did all the other data
points get to be exactly zero? Let's step through the operation of this
routine and see what really happens. In line 40 we accumulate Cos(l*J) for
Sampling 1 13
all Q hannonics. When 1 = 0 (i.e., the initial data point) Cos(l*J) will be
l .0 regardless of the value of J (J = hannonic number). The next data
point, howeveI; gives I a value of riS. Now, as J steps through each of the
1 6 hannonics, I *J steps through 2r radians, and Cos(I*J) will give a
sinusoidal pattern of values-the summation of which is zero (see column
T01). At the next data point the value of I will be 2rlS and we will only
have to step through S hannonic numbers to get a null summation (which
we will do twice). Table l O.2 shows the summation of 16 hannonics ( FOO
through F 1 5 ) for the first S data points ( TOO through T08) . This is simple
but important-these summations simply must come out to zero. [You
recognize, of course, that we 're playing with loaded dice again; but, this
is the very configuration we use in the DFT and FFT. Now, while we are
playing with loaded dice in the above illustration, it's important to
understand that this phenomenon (i.e., a large number of sinusoids
summing to zero under ,(onditions of symmetry) occurs naturally (i.e., even
when the dice are not loaded). ]
For now, we should note that these sinusoids will fail to yield a
zero summation whenever ! = 1 6rN/S [where N, is any integer (including
zero obviously)]. At these points Cos(I*J) = 1 .0 regardless of the integer
value of J (as we said J represents hannonic numbers, or frequency);
howeveI; the domain of our consideration extends only from I 0 to one =
data point less than 1 2 *PI. Note: The cosine waves we have been
=
1 14 Chapter 1 0
discussing here. all of equal amplitude and constant phase. represent the
transfonn ofthe impulsefunction. ofcourse. Ifwe take the transform ofan
impulse we will obtain a perfectlyflat spectrum of cosine waves
So, the impulse function has an amplitude equal to Q (where Q is
the number of hannonics in the frequency domain function), but if we
divide each hannonic by Q, they will all sum up to unity, and this function
will fulfill our requirements for a single sample pulse.
recognize this is the only way we can stretch the spectrum with our DFT
loaded dice scheme. This result is illustrated in Table 1 0.3, where you will
note that a minor miracle takes place-we get a second pulse in the T08
column (which, we recognize, would be halfway through the time domain
data array). This is the frequency domain equivalent of the time-based
stretching we used when we developed the FFT algorithm in Understand
ing the FF1). [Note: A superior intellect would see this phenomenon
must happen for any function if we stretch its spectrum, but for you guys
we will extend this exposition in the next chapteI; making it obvious why
the time domain function repeats itself when the spectrum is stretehed.]
You may have noticed that Table 1 0.3 and Table 1 0.2 are actually
pictures. They're not the beautiful panoramas of an Ansel Adams, of
course . . . the beauty of these pictures lies in the details . . . and meaning.
They're pictures of sinusoids (of different frequency going down the page)
taken at succeeding points in time (going across the page}-they are
pictures of the Fourier transform! Table 1 0.3 is, in fact, just an expanded
frequency representation of 1 0.2. Our sampling times are unchanged
between these two pictures, so, in 1 0.3 we get only half as many sample
points in a cycle at each frequency. If we generate additional data points
between the ones shown in 1 0.3, howeveI; it becomes identical to 1 0.2
(ignoring scaling). [Note: It's worth repeating that all of the harmonics
of the waveform have doubled in frequency. If we turn a basso profundo
into a chipmunk by playing the record fasteI; all the harmonics of that
great voice must be scaledproportionally. Ifwe play the recordfaster. the
time domain is compressed and the frequency domain is expanded!]
Okay, take a sip of coffee here because we're about to reveal a
profound truth! In Table 1 0.3 we have stretched the frequency domain
function and so the time domain is compressed-the impulses are closer
togetheI: But pulses spaced on shorter time intervals are higherfrequency
Compression in time is expansion in frequency This most profound of
theorems is only profound if we don t think about what we 're doing.
So, stretching the spectrum (i.e., only summing even frequency
cosines) causes the time domain function to repeat itself (within the same
time interval). Ifwe stretch this spectrum again (i.e., we only sum in every
fourth harmonic), we will getfour impulse points. Stretching the spectrum
to every eighth harmonic yields eight impulses, and this begins to look
very much like a train of sampling pulses. In every case, howeveI; the
116 Chapter 10
relative relationship between the harmonics and impulse rate remains the
same! As more pulses occur in a given time, the harmonic frequencies
increase-the spectrum of this pulse train (made up of identical cosine
waves whose frequencies are integer multiples ofthe fundamental sampling
rate) must obviously "stretch."
where: K, = constant
f(t) = time domain function
g(t)A.Cos(t) = UoA.Cos(t)+U,Cos(t)A,Cos(t)+V,Sin(t)A,Cos(t)+
U2Cos(2t)A,Cos(t)+V2Sin(2t)A,Cos(t) + .... ( 1 0.6A)
And, as we know, this replaces the harmonic component of f(t) with side
band components [i.e., a replica of the spectrum ofg(t) appears about each
and every component off(t)]. This is all pretty obvious, but it's important;
so, let's return to our FFT program to illustrate our point.
Sampling 117
• • • • • • • •
has happened here: each of the harmonics of g(t) must multiply each and
every harmonic of f(t). We produce a set of sidebands about each of the
sampling pulse train harmonics. We see clearly how sampled data (i.e.,
modulated sample pulses) relates to modulated sinusoids and, more
importantly, how this relates to the sinusoids spaced equally above and
below the Nyquist frequency.
Harmonics of
Modulating Waveform
Modulation
Sidebands
• • • • • • • • • •
• • • • • • • • • • • • • • • •
• • • • • • • • • • • • • • • •
is a single sinusoid at 3 1 99 Hz. From what we have just seen, the digitized
signal will look something like Fig. 1 0.4. That is, the negative frequency
component, extending downward from the fundamental of the sampling
impulse functionf(t}, will only be 2 Hz away from the single sinusoid of
our 3 1 99 Hz signal. Our digitized signal will be (for all practical purposes)
a summation of two sinusoids. One at a frequency of 3 1 99 Hz (i.e., the
original input signal) and another at 3201 Hz. We can hear both of these,
of course, and it will sound awful (like hitting c and c# on a piano-only
worse)! I f we can filter off the 3201 Hz component, however; everything
will then be as it should-the beatfrequencymodulation will disappear and
we will only hear the single high-pitched tone-but that's not very
realistic. Go back to the filter curves ofp. 60 and estimate the amount of
attenuation provided by the best of those curves when cutoff is exactly at
the Nyquist frequency.
The point is this: Shannon says the lower limit for the sampling
rate is twice the highest signal frequency. From what we have seen, ifwe
can filter off the negative frequencies above the Nyquist when we
reconstruct the signal, then Shannon's criterion is sufficient. Speaking
realistically, we must always leave a little room for ovemead in our system
design. Only ifwe can filter off the sidebands extending downward from
the first harmonic of the sampling pulse train can we faithfully reproduce
the original digitized signal.
Clearly, Shannon's criterion is a theoretical limit, and we will
seldom obtain that limit. We will look at the practical application of this
in detail in the next chapter
• •
Negative Positive
Frequencies Frequencies
• •
• •
• •
• •
. .
... - - ...
t
Nyquist Frequency
Fig. 1 0. 1 3 - 1 : 1 Sampling Spectrum
(
Modem CD players spec the low end at 5 Hz and the high end at 20 kHz.
2
This is very simplified. Actually, the hannonics of a piano wire are not exact
integer multiples of the fundamental, causing problems for piano tuners, but causing much
greater problems for engineers building Piano Tuners Each note must be tuned to a sort
of weighted average of the hannonics in each tone.
Digital Audio 123
on the CD, we will select a data rate of, say, 44. 1 kHz !-) Now, from Fig.
JJ. 4, this is definitely equal to a Nyquist signal frequency of 22.05 kHz.
We know (Chapter 1 0) there will be
beat-frequency modulation near the
Nyquist, implying unwanted sinusoids
above the Nyquist. Furthermore, from HttHtttttHttHtttltltl1ftttb"'tHtHttH-Itt
what we know about filters, rolling
off the passband between 20 kHz and
22.05 kHz will be difficult.3
The way we handle this is by
Fig . 1 1 .5 - Beat-Frequencies
oversampling. Oversampling refers to the technique of introducing extra
�
data words into the data stream. For example, we might generate 2: 1 over
AVerage Valne
�. _ Oversampling
sampling by summing each word with its
successor, dividing by two, and inserting the
. . resultant between the digitized data points
1 ��
"I t · I··· (dotted lines Fig. 1 1 .6). By inserting average
values between data samples, we generate twice
Fig. 1 1 . 6 - Oversampling as many steps per unit time (i.e., the data rate,
or sample rate is doubled to 88.2 kHz).
3
Modem filters do a pretty good job, still, we only have from 20 kHz up to
22.05 kHz to achieve about 1 00-120 db attenuation.
1 24 Chapter 11
I
frequency (that has been oversampled) of
Fig. 1 1 . 1 3 is, of course, incompatible
with high fidelity sound-this will never I
work, but why does this fractional fre- � \.. -------------------
.
i\ !\ f\ LJfj f
!\
illustrated Gibbs phenomenon-the upper
�
1 5/ 1 6 of this spectrum is packed with
V V VVV
--+--i-
I- ---<I--+- - -
---\---;f--+--I---;� zeros . If we had actually sampled a 4.5
Hz signal at the oversampled rate the
spectrum wouldn't simply truncate at
Fi g. i 1 . 1 3 _ 4.5 Hz Oversampied 1 / 1 6 of the available frequency space.
Apparently the distortion we see in Fig.
1 1 . 1 3 is a close relative of Gibbs ! Now, we have seen how we can
alleviate this problem in previous chapters, and we will investigate that
p o s s i b i l ity s hortl y ; however, we w i l l never b e very good at FFT
applications unless we understand it's internal workings. We have a
golden opportunity to look inside the Fourier transform here, so let's listen
to ole' Yogi and take this "fork in the road."
(Note that thefirst column is replaced with the data that would naturally
occur at the phase shift given to each sinusoid.)
In general, each harmonic is shifted by 27rF/Q (where F is the
harmonic number and Q is the number of data points). Furthermore, it's
relatively obvious this relationship must hold between any time-shifted
function and its frequency domain counterpart-it's completely general-it
applies to any function.7 It's also bilateral (as are all the theorems). That
is, if we shift a data point in time the harmonics must shift linearly in
phase; and, conversely, the equivalent phase shifts in the frequency domain
must move the data points in time. The linear phase shift phenomenon is
nothing but the result of shifting all of the different frequencies by the
same time increment! No magic here-it's simply what happens.
that are integer multiples of the fundamental, the phase shift between each sampled point
will always be as descnbed above (i.e., 211"FIN). Ifwe time-shift any function the sinusoids
of its frequency domain representation must shift by the rules given above.
Digital Audio 129
First of all, the advent of the DVD format makes solving this
problem (as we have currently framed it) unnecessary, I I but there are other
reasons for pursuing this technology. For example, when loud-speakers are
driven outside oftheir optimum frequency range the overall sound tends to
be degraded. Speakers are sometimes connected using filter networks to
attenuate frequencies outside the optimum band, but it's difficult to do this
effectively. In our ultimate system we could separate frequencies via the
FFT and drive the speakers with an optimum band of frequencies, but here
oversampling would be unavoidable.
Now, every sinusoid in an audio signal
Save last 512 words - must have some function's sidebands (since the
Load 512 new words for data starts and stops); but, when we transform
1024 data word array
blocks of data we introduce sine function side
I
Multiply by
bands. When we look between the original
Weighting data points (i.e., we oversample) we will see
Function
these (except when they cancel each other).
One way to minimize this phenomenon, we
PFFFT know, is to use a weighting function before
taking the transform. As we saw with our
20-80 \�"...
80-320 1 280-5120 Hz
Hz
spectrum analyzer, this produces tight little
sidebands... which, hopefully, will not be too
320-1280 Hz distorted if we oversample and reconstruct.
Oversampling
Reconstructor
There is one prob lem-when we use this
technique what we transform is not the same
thing as the original audio [ i . e . , we w i l l
D/A, Filter
& Speaker transform (for example) a cos2 weighted version
Fig. 1 1 . 1 8
of the audio]. But this is no big problem-on
reconstruction we can de-weight the recon
structed signal (i.e., divide by the weighting function). So, when we
oversample this weighted function spectrum, the effect we encountered
earlier should be greatly reduced. We find, in fact, with weighting and de
weighting, distortion is attenuated, but not completely eliminated. There
II
DVDs alleviate this problem by storing data at a rate of 96 kHz. Via a
compressed data format they provide two hours of 5 channel, 24 bit words at this 96
kHz-truly high fidelity sound. We will discuss the benefits of using 24 bit words shortly,
but we should see the eminent demise of the CD format in this.
Digital Audio 133
are sti ll vestiges of the problem near the beginning and end of the
reconstructed signal. So, how shall we solve this problem?! !
[If we, say, load 1024 data words into a buffer and oversample as
we described above, on reconstruction we know we will get distortion at
the seams of these buffers. If, however; we have really fast hardware, we
can transform, oversample, reconstruct, and then throw away thefirst and
last quarters of the reconstructed data. 1 2 Only the middle half would go to
the DIA . We would then load the second half of the data to the first half
of the data array, load the next 5 12 data points into the (last half) of the
array, and repeat the process. If the transient distortion has decayed far
enough after 256 data points, we get practically seamless output data.]
, * *
APS1 1 . 00 OVERSAMPLE I L LUSTRAT I ON
I **************************************************
plus and minus 20 data points. If you take the trouble to extend this
algorithm to ± 30 data points you will find the approximation to the
original sinusoid is considerably better. If, however, we suppose
harmonics generated by the distortion in the reconstructed oversampled
signal are integer multiples of the sinusoid itself, we realize this distortion
will be well above the audible range.
o
1"
We must talk a little about digitized signals, and the best way to
do that is to review how a typical AID converter digitizes a signal. 1 3 A
simplified 3 bit "flash converter" is shown below:
V
REF
R8
.AAA
-y v T r-
�»
(
D {;I
"AN D" GATE
r C (l
R7 : � r- +'- � "OR" GATE
?- -
�
�
D {;I
r- C Q r-- I
R6 : '\
...
?- D Q �
V D IGITAL
I- C Q 0 UTPUT
R5 : �
?- - D
2
-
2
>
•
r- C Q
R4 :
� r- +
- D Q
-
>
•
�
r- C Q
R3
� - D Q
"-
�
�
•
r- C Q
:� �
r--
R2 -;
+'-
-
�
D Q �
�
I- C Q
R l :�
� SAMPLE
-....
CLOCK
Figure 1 1 .20 - Flash AID Converter
There are eight identical resistors (RI through Rs) in the "divider string."
If VREF is equal to 8 volts (and circuit loading is negligible) the voltage
comparators will have thresholds increasing incrementally in 1 volt steps
(the triangle shaped functional blocks compare two voltages and output a
" 1 " if the + side is greater-otherwise "0"). Now, during normal operation,
the analog input voltage will vary between 0 and 8 volts. When the
comparison voltage is exceeded, the comparator will output a " 1 ." The
clock pulses occur at a constant, precise rate, strobing the states of the
comparators into a series of "latches" or "Flip-Flops" (i.e., bistable
multivibrators). Note that when the input voltage exceeds the threshold of
some comparator, turning it on, all comparators below it will also be turned
on. The logic to the right of the latches encodes the state of the latches
13 AIDstands for analog to digital There are many different ways to convert
analog to digital data but a flash converter is easy to understand
Digital Audio 137
ttl
S
=-
I l O
S 101
0 1 00
..J
� 01 1
G 010
-
Q 00 1
000 1 1 I I 1
1 . 00 2.00 3.0() 4.00 S.O() 6.00 7.00
INPUT VOLTAGE
Figure 1 1 .2 1 - AID "Transfer" Curve
volts, etc. , etc. This represents a form of truncation error, where the
fractional portion of the input is truncated, resulting in the digital output
always being lower than the input (except when the input exactly equals a
transition voltage). If the input varies continuously the output will be, on
average, 112 of a least significant bit (LSB) too low. When working with
a 1 6 bit AID converteJ; this error (which corrupts the 5th decimal place)
may be negligible. If, on the other hand, we're working with an 8 bit
converteJ; where the error corrupts the 3rd decimal place, it just might be
a problem (in our 3 bit converter it's a serious problem).ls
This problem can be alleviated by changing the threshold voltages.
If the two end resistors in the voltage divider string of Fig. 1 1 .20 are made
1/2 the value of all the others, and the reference voltage is reduced to 7
volts, the first threshold will be at 0.5 volts, the next at 1 .5 volts, the 3rd
14
Jt works like this J only if both inputs are J
AND gates (flat backs) put out a
(i.e., one input is a J AND the other input is a J ) OR gates (concave backs) put out a J if
either input is a J (i e., one input is a J OR the other input is a J ) The little circles negate
the input (i e , tum J into 0 and 0 into J. Most often a J is 3 to 5 volts and a 0 is less than
0 5 volts
IS
When we perfonn cumulative computations on data the truncation error may be
significant in any digital system The error accumulates at an average rate of 1/2 LSB per
computation Obviously, then, the nght-most digit is compromised immediately, and ten
successive computations may discredit the second decimal place. Even if your software
returns 7 decimal digits. if you're doing a lot of computatJon, the last two digits may be
inaccurate (if you need more accuracy you may want to use double precision anthmetJc).
138 Chapter 1 1
at 2.5 volts, etc., etc. The output will now be 00 1 for any input from 0.5
volts to 1 .5 volts, 0 1 0 (binary 2) for inputs from 1 .5 to 2.5, etc. The
average values are now correct, but we haven't completely solved the
problem; in fact, we have exposed a more fundamental error-the
digitization error. When we convert continuous variables into discrete
numbers, we encounter this digitization error. We can no longer "see"
what the input is doing below the least significant digit. In binary systems
this introduces an error of ± 112 of the least significant bit. 1 6
So, then, how many bits do we need in our AID converter? An
8 bit converter splits the input signal into 256 increments, and a 1 6 bit
converter yields 65,536 different values. High fidelity compact discs use
1 6 bits for each sound channel and provides pretty good audio-True Color
digital pictures use 8 bits for each of three primary colors. Our present
example is digital audio, so let's look at this 1 6 bit digital word.
120 _ Threshold
of Pain
The dynamic range of the human ear is about 1 20
110 - db (from the threshold of hearing to the threshold of pain).
100 --.--
Now, playing music at the threshold of pain must surely
imply masochistic tendencies-one would think we need
90 -
not produce levels greater than about 1 20 db (above the
80 - threshold of hearing).
70 -
Unfortunately, the quasi-coherent nature of the
harmonics in real music cause them to add in ways that
60 Mnslc ?
-
16
Tuming the average - 1 /2 LSB truncation error into a ± 112 bit error doesn't
completely eliminate the cumulative error. With a ± 1 /2 bit error the cumulative error then
wanders orrin a random walk. Fortunately, 5 decimal digits is more than adequate for most
applications.
Digital Audio 139
the loudest in the 1812, and when we 32 harmonics of equal amplitude Phases
play a disc with more than one selec- are randomized in the lower waveform
tion, most people would find this unacceptable. So, we lose 20-30 db here
(so far as noise and distortion are concerned). 18 We consider this in greater
detail late[
So, we need about 1 3 0 db of distortionless dynamic range (i.e.,
practical considerations expand the range between 20 and 1 1 0 db
considerably). [Note: "distortionless" doesn't mean no distortion at all!
We need only guarantee that distortion is below the threshold of hearing.
65,536, so the largest peak to peak sinusoid that can be stored with this
word size must fit between zero and 6 5 , 5 3 5 (effectively the most
significant bit is a sign bit and the sinusoids vary between ±32,767 but let's
not get bogged-down in details). The smallest possible peak to peak signal
would be a change of 1 bit, yielding a dynamic range of 20 LoglO(65,535)
=
96.3 db. Okay, it's apparent where they get the 96 db, but let 's not kid
ourselves-a signal switching between zero and one is a square wave (see
Fig. 1 1 .24). Surely this is a very low sound level (about 20 db above the
threshold of hearing if the max. amplitude 1 20 db, but if you reach over
=
17
Again, the real situation is complicated-threshold of heanng is frequency
dependent, loud sounds "mask" soft ones, etc. (see, for example, Chapter 2 of Ballou's
Handbook of Sound Engineering, Howard W Sams).
18
Things could be greatly improved with an embedded "loudness" (i.e , gain)
word which could be updated every 0 . 1 seconds or so.
140 Chapter 1 1
and tum up the volume on your amplifier; this will not sound musical (if
your voice sounds like this don't bother sending your resume to the "Met").
Now, we 're not s o m u c h c oncerned w i t h s u c h a s i mp l e-minded
determination of dynamic range, but with distortionless dynamic range!
Let's look at this just a little bit closer
Let's look at how digitiza-
tion error (see Fig. 1 1 .2 1 ) affects
digital sound. Not only do we
acquire an error from the finite
increment of the digital steps but f= 1.45kHz
we digitize at precise times, and the level 40 dblthreshold
,---'
=
, *********************************
, * GENERATE FUNCT I ON & ANALYZE *
, *********************************
600 CLS : PR I NT : PR I NT
602 I NPUT "SPE C I FY AMPL I TUD E , FREQUENCY ( E . G . , . 8 , 1 6 ) " ; AMP, F8
604 F9 = F8 * FRACF ' CONVERT TO FRACT I ONAL ARRAY DOMA I N S I ZE
606 PR I NT "PREPAR I NG DATA I NPUT - PLEASE WAI T ! "
61 2 GOSUB 400 ' GENERATE S I NUSO I D
6 1 4 GOSUB 1 00 ' ANALYZE S I GNAL
19As we discussed earlier (Section 1 1 .7 and Fig. 1 1 .22), the maximum amplitude
will generally be less than - 1 0 db (and in many cases much lower), making the ratio of the
signal to distortion/noise about 1 0 db less (i e . worse).
142 Chapter II
, . U1I I2S
digital output simulation into our
29
spectrum analyzer (we need only
40
add it to the spectrum analyzer of
60
the previous chapter) to see what
89
distortion is generated. We find, as
100
U\J\..AJVV\.A
I I I I I I should have been expected. that
129
Li
non-linear distortion shows up as
harmon ics that were not in the
Fig. 1 1 .25 - Synchronized Signal
original signal (intermodulation
distortion, on the other hand, creates side band components). The sum
total of all these distortion harmonics must be below audible levels for a
perfect system. You will find the worst distortions occur when the signal
is an integer sub-multiple of the sampling rate (Fig. 1 1 .25). This happens
because the digitization non-linearity locks in and is identical for each
cycle (as we explained on p 140); however, if the digitization error is not
identical i n sequential cycles, it shows up as digitization noise.
Fortunately, this lock-in phenomenon is rare.
. mn�
n------ ...
You will want to see what
• ---
happens over both the frequency 21
range and the dynamic range. The &I lII----
routine given in Appendix 1 1 .3 will " ift------
step through the chromatic scale, • !it-----
allowing amplitude to be changed . '-'- __________
I ,
so you may observe signal to noise +-��-:----:-----:--
and distortion ratios for various 2I ����������in
I
u Fig. 1 1 -2 7
, I ,
12.1 SIMILARITY
1 ------
You never hear anyone say "Woman wasn't meant to have such knowledge!"(?)
2
Ifchanging the playback-rate shifts the frequency, it's more or less obvious we
will need this-it's just not apparent how we can use it to break the law.
146 Chapter 12
1.0
L
•
•••• • • •••••••••••••••••••••••• Ifwe transfonn a con-
1.0 .
stant amp l Jtu' d e function we
get a zero frequency com
ponent only (Figure 1 2. 1 left).
Tim e Freq uency This is simply what a constant
Fig. 1 2 . 1 j(x) 1 - = function is (forget about the
sinc function for now-we're in deep swampwater here ! ).
Okay, suppose we shift this zero frequency component up one
notch in the frequency domain-up to the first harmonic position. We
know this spectrum will reconstruct as a single sinusoid (Fig. 1 2.2). We
will tum a constant amplitude Shifted
1.00
L
into a sinusoid by shifting its o.so -+
• • • • ••
• •
• •
•
• •
. .
transform (from zero) up one . .
..
. . . .. .
harmonic number. If we shift . '.
. .
t h is h arm o n i c up another
notch, w e will turn a singl e Frequency Time
3
Time domain theorems have frequency domain equivalents which, because of the
similanty of forward and inverse transforms. are always similar. The similanty between the
time and frequency shilling theorems may not be apparent as we have presented them;
however, mathematically the time shifting theorem is wntten:
In Section 1 1 .4.2 we investigated time domain shifting sufficiently to know that Eqn. ( 1 2. 1 )
says a time shift is a phase shift o fthe frequency components. Keeping in mind that the time
domain function isjust the summation of the harmonics, it becomes apparent that Eqn. ( 1 2.2)
says a frequency domain shift produces a frequency shift of the time domain function. Note
that Eqn. ( 1 2.2) multiplies the time domain function by a sinusoid-this is modulation--the
equivalent of side bands in the frequency domain That, of course, is exactly what we have
produced in Figs 1 2.3, 1 2 4 and 1 2 5 above
148 Chapter 12
I
ms. interval so the fundamental
loA �...,.u� d�U�Aa�.
Ot' vtQ4•viiWi.,
frequency appears to be around 4.5
T
. V. ,,1 r .,4...
kHz. With a sampling rate of 1 1 i\. f' 1,"
kHz (Nyquist = 5 . 5 kHz) we're
marginal here. If we look at the
first half of "this" word under the Fig. 1 2.9 - The "sss" Sound
same microscope, we find its dominant frequency is about 400 Hz.
Okay, let's now look at the spectrum of this word. In Fig. 1 2. 10
we see there are four major frequency components (ignoring the very
lowest component). Each frequency tick on the x axis is 550 Hz. so the
dominant component is around 400 Hz. Note that at about 4.5 kHz, we
have something that looks like wide band noise. This, of course, is the
" sss" sound shown in Fig 1 2.9. Finally, you will note the major harmonics
Playback Rate 149
-
are approximately integer multiples of
the lowest major harmonic frequency.
These are produced by the voice-box,
and must, of course, vibrate according
to the laws of physics. It's unlikely
they would not be integer multiples
(approximately).
�w.������-
We m ay s ep arat e - o u t t h e
Fig 1 2 . 1 0 Transform of "This"
-
So, what was our intention when we went out into that swamp?
Well, what we want is to get the opera over in less time so we can go to
bed. We can do this by playing the 33V3 record at 45 RPM; but, that basso
will turn into a soprano (i.e., the frequency domain wIll be expanded as the
time domain shrinks.) Right-but why couldn't we just use the frequency
152 Chapter 12
shifting theorem to shift him back down to bass again? In fact, we can, but
it's not completely straightforward. When we speed up the record, every
frequency will be increased proportionally to the increase in playback rate
(i.e., if we double the speed of the record, every frequency that was
recorded will be multiplied by 2), but frequency shifting shifts all the
frequencies by a constant increment. That is, the result of shifting is:
Frequency Stretched
Shifted & Shifted
to be one of our sound elements). Using what we have discussed, how can
we speed up this triangle wave without changing the frequency? Well, if
we had shifted the stretched waveform of Fig 1 2. 1 5 by the same amount
we shifted the original function (i.e., to the same center frequency see Fig
1 2. 1 6 above), then when we reconstruct this spectrum we will surely get
the waveform shown on the right hand side of Fig. 1 2 . 1 7 . Consider this
carefully for in this case all we have done is stretch the modulation
sidebands-the carrier frequency is unchanged. We get twice as many
cycles in the same time interval but the frequency is the same . . . that sure
looks like a light at the end of the tunnel.
• •
Frequency Stretched
Shifted & Shifted
Fig. 1 2 . 1 7 - Stretched & Shifted & Reconstructed
Okay, so, how do we use this trick to speed up the playback and
keep our basso basso? Well, we have seen how we can decompose spoken
words into elements based on their frequency domain function, and these
components can then be recombined to yield the original words (see Fig.
1 2. 1 1 ). These major components are simple enough that they are not
significantly different from the triangle wave of Fig. -
1 2 . 1 7 . I f, then, we tran s form any one o f the
components of Fig. 1 2. 1 1 , stretch the sidebands
about the component's dominant frequency (there's
no need to shift it down to zero, stretch it, and shift
it back up again) then, when we reconstruct, these
components will occur twice in the same time period
Figure 1 2. 1 8
(we will j ust throw away the back half). If we
perform this operation for all major components and then recombine them
(from superposition we can do that in either the frequency domain or time
domain), we will have our basso singing the opera twice as fast, and he
will still be a basso. [We can go to sleep in two hours instead of.. one?}
But this raises another problem-if they gallop through the opera
at twice the tempo that's too much (Figaro might be interesting though).
What we really need is something maybe 1 2.5% faster (that could still
1 54 Chapter 12
5
Strictly speaking even this is true only for tones that are near each other, for,
as we move up and down the scale on any instrument, the tone changes. As noted earlier
this implies a new harmonic model (for any instrument) must be created for every octave
(at least). Altemauvely, we could map the harmonic structure of an instrument over its
tonal range and create a dynamic model which changes with every note
Playback Rate 1 57
chaptet It's the next technological step in synthetic sound (i.e., to improve
on the fidelity of recorded/reconstructed sounds-and to recover high
fidelity replicas of the very early attempts to record sound).
There is much to do here. We need to understand the structure of
the harmonic element side bands. Surely the modulation envelope
determines a significant part of the s idebands, but the asymmetry of these
side bands tells us there's considerably more here than just the modulation
of a harmonic carrier sinusoid. We need to know how distinctly different
sounds are carried in these side bands (e.g., in the case of vocal sounds
how the vowel sounds are carried via these sidebands). We also need to
know how the individual harmonic elements combine to produce the
distinctive sounds of various instruments, voices, etc.
In this project it's clear the Fourier transform will be both an
instrument of analysis and synthesis. We have included a routine to
perform afine line analysis of the sounds which employs a DFT fractional
frequency algorithm. It's slow in execution, of course, but once we have
determined the location of the major harmonic elements we need not
analyze a broad band of frequencies. This is one of the major themes of
this book-if you want to use the tool of Fourier analysis in practical
research and development you will need to improvise, design, and write
variations of the FFT (and DFT) which can only be done if you understand
these tools.
If we can build a synthesizer that generates a truly high fidelity
replica of musical instruments, we can eliminate the insurmountable
problems of trying to recom live performances. Such a synthesizer would
create the music note by note, which allows control of the piece at the time
of creation (just as with a live performance). This feature, as we said
before, would eventually allow the real time synthesis of a high fidelity
orchestra, but understanding the nature of sound itself will open whole new
fields in the 2 1 st Century.
CHAPTER XIII
13.0 INTRODUCTION
individual data lines. If, howeve� we replaced each data line of the
original picture with its transfonn (as we nonnally do), we would then
have a new array of data such as is shown in Figure 13.3.
Let's pause here and talk
about what we've done so far. We
now have transfonns ofevery line of
the original picture (which is, inci
dentall� the picture of Fig. 13.2).
It's apparent we had no good reason
for taking the transfonns of the Fig. 13.3 - Row Transfonns
The old standby FFT routine is located at line 200, but to perform the
transform in 2-D we must take the FFT of both rows and columns. That
is, after performing all the horizontal transforms, we then transform each
column [note: when we do the inverse transform we must transform the
columns first-then the rows-but, we'll talk about that later). We need
two separate routines to handle these forward/reverse and row/column
requirements. The high-level forward routine (line 100) looks like this:
**********************************************
REM
* *
REM XFORM FUNCT I ON
**********************************************
REM
1 00 CLS : K6= - 1 : SK1 = 2 : XD I R= 1 : T9= T I MER ' XD I R : 1 = FWD , O = I NVERSE
1 02 GOSUB 200 ' DO FORWARD ROW X F ORMS
1 04 GOSUB 300 ' DO FORWARD COLUMN X F ORMS
1 06 T9 = T I MER - T9 ' CHECK T I ME
1 08 RETURN
, * *
THE ROUT I NE BELOW I S FOR A ROW
I ***********************************
to do with the transfonn but on larger arrays it may take a while to perfonn
a transfonn, and we need something to indicate the computer is still
running; therefore, the number of each row is printed as it is transfonned
(line 206). KRTST is used to control the printed line length (line 208).
We will explain why line 204 is presented as a comment shortly.
At line 2 1 4 we come to the familiar FFT routine. We process the
row of data as we would in any FFT, emerging from this row transform
routine at line 248, and looping back for the next row at line 250. When
all rows are transfonned we display the data (line 252) and then return to
the high level routine (line 1 04) [where we will immediately jump down
to transfonn the data by columns]. The difference between row and
column is the reversal of row and column addressing in the butterflys (lines
2 1 8 thru 226). The column routine is shown in the appendix (starts line
300).
There are differences, of course, in the routines to generate 2-D
data, plot the data, etc., but we discuss all of that in Appendix 1 3 . 1 (where
a complete listing is provided).
128, but that's as far as you will be able to go in this Basic language
(without writing software to shuffle data back and forth to the hard drive).
The reason is that DOS has always been hamstrung by the original (in my
opinion poorly conceived) addressing scheme of the 8080 p.processot;
which basically provided for one mega-byte of memory. Today DOS
requires more than half of its one meg. When you load the application you
want to run (QuickBasic™ for example), that takes up a big chunk of
what's left, and the program you write in QuickBasic™ takes up another
chunk, and you wind up with only a few hundred kilo-bytes left over to
hold data. On my computet; when the program FFT l 3 . 1 is loaded and
running [you can add the line of code PRINT FRE(- 1 ) both before and
after dimensioning the arrays], there is 329,960 bytes available for data
arrays (this number is affected by such remote things as the number of
BUFFERS in the CONFIG.SYS) file. Now, floating point (i.e., real)
numbers take 4 bytes of memory each, and we have two 2-D arrays of data
(one for the cosine component and another for the sine). There are also
two smaller arrays (Q quad-bytes for each twiddle factor array) and a
"spare data array" of QxQ quad-bytes, which adds up to:
IfQ 64 this will require 49,664 bytes; but, when Q 128 this
= =
demands 197,632 bytes. Q 256 requires 788,480 bytes so, if you want
=
to work with arrays larger than 128 by 1 28 pixels, you will have to resort
to more desperate techniques-sorry about that.
Execution times have become a moot point. Modem computers
are so fast that lousy system software doesn't matter for the above array
sizes. On a 500 MHz machine, you can do a 1 28 x 128 transform in about
1 0 sec; still, if we could see things clearly, we would see that, rightly, the
value of software (assuming it works) is inversely proportional to: 1 ) the
execution time, 2) number of keystrokes (or mouse operations) required to
perform its tasks and, 3) quantity of code. When we pay hundreds of
dollars for a piece of software, howevet; we want it to contain a lot of code
that takes a lot of time to load and run ... and the software companies oblige
us with stuff that's optimized primarily to make a profit.
The last thing we need to talk about is the frequency domain
arrangement that results from the conventional 2-D transform. In Figure
1 64 Chapter 13
1 3.4 we see the transform comes out in four heaps in the corners of the
plot.. .and some see this to be a gross distortion of reality. As everyone
knows, the negative part of the X axis should be to the left of zero while
the positive should be to the right (just as North should be up on a map),
and the positive Y axis should be above the X axis (East should be to the
right). As we all know, when we take the conventional transform, we get
the negative frequencies above the positive ones (Le., they are plotted to
the right of the positive spectrum) and, if you review the mechanism
described in Section 1 3.2 of this chapteJ; you will see how the negative
frequencies come out "right and below" the positive components-but
running in the wrong directions from each end. Now, if you can mentally
shift the quadrants around where they should be (the preceding comments
will be an immense help here), you will see the four heaps are really part
of the same thing-a single heap in the center (Fig. 1 3.5)'
Okay, we could go all the way back to the original DFT program,
write it such that the negative frequencies come out at the bottom, and then
develop our FFT algorithms to give the same results; but, that would be an
awful lot of (painful) work. There's anotheJ; relatively simple correction
that we can introduce here---one that will straighten things out.. ..
In Chapter 10 we noted that if the sampling pulse train was
composed of alternately positive and negative pulses, the required
sampling rate would then be equal to the Nyquist frequency in the signal
(Section 1 0.8). We also saw this eliminates the zero-order harmonics, puts
the negative frequency components in that space, and the positive
frequencies will therefore be above the negative frequencies. Now, if we
go through the data and negate every-other data point, we will have
accomplished this sort of 1 : 1 sampling-and transforming this data will
then yield a spectrum with the positive and negative frequencies in correct
relative position. Ifwe take the sampling frequency as the zero frequency
in this 1 : 1 modulation scheme, everything will work-out in our graphics.
, ****************************************
, * *
MOD I FY ROW SAMPL I NG
, ****************************************
, * *
MOD I FY COLUMN SAMP L I N G
I ****************************************
Two Dimensional FFT 165
We will need to add code in the transfonn routine to jump to these routines
before perfonning the transfonn-we need to modify the sampling prior
to perfonning the FFT:
Ifwe are doing aforward transform we jump down to the modify sampling
routines but we must perfonn the reverse modification on reconstruction.
Furthennore, when we reconstruct the signal, we must perfonn the inverse
transfonn before we modify the sampling-that will properly undo what
we did during the forward transfonn. The lines of code required are:
On reconstruction we undo the columns first. Also note that, i n either case,
we use the same routine-negating every other data point twice yields the
original data. We then add the final undoing of the data:
TRANSFORM FAMILIARIZATION
14.0 INTRODUCTION
which results in a diagonal pattern ofFig 14 7. Note that there are 3 sine
waves clearly visible on both the x and y end patterns]. The horizontal
transforms (Fig. 14.8) come out pretty much as we expect. [Note: we have
fudged this resultfor the row trans
forms by showing only th e sine
component (to illustrate ourpoint).
The actual magnitude of any row
transform is the RSS of both sine
and cosine components which yields
Fig. 14.8 - Row Transforms a constant magnitude for the col
umn of3rd harmonic components.]
Transform Familiarization 169
BESSEL II
Nothing less than a perfectly flat-topped function will do; but how
shall we find it? Well, we know how to generate individual harmonic
components-we learned how just two sections back using sinusoidal bars.
We can use a technique similar to
the one we used back when we first
discussed Fourier series, and write a
routine to sum sinusoidal bars at the
frequency components where we
want them-and leave zeros where
we don't. That's how we do it in Fig. 1 4. 1 3 - Bessel II Xform
the Bessel II routine in FFT 1 4.0 1 .
You will note that Fig. 14. 1 3 is indeed what we are looking for (it could
hardly be otherwise), but is Fig. 14. 1 4 what we see in our telescopes?2
[Note: This is not the best way to generate this particularfunction-it just
illustrates that we can create a modestly complex picture by piecing
together sinusoidal bars (see discussion p. 169 and Bessel II in Appendix
14. 1). This is obviously a legitimate interpretation ofthe 2-D transform.]
There's a much easier way
to find this aperture function. We
can simply generate a constant
amplitude function over the desired
aperture in the frequency domain,
take the inverse transform, and
Fig. 14.14 - Bessel II
voila! We have also included this
in our function generator sub-routine (Bessel III). It's easy to generate,
illustrates the same thing as all those 2-D sinusoids, and represents what we
really want. [At this point you should probably load program FFTl4.01
into your computer and experiment with these functions a little.]
0ne way to see this is to cut a small hole in the lens cap of your refracting
2
telescope (using, for example. a leather punch) and project a laser beam (expanded to a I "
to 2 " diameter) onto this "stopped-down" aperture. The bright spot at the center o f this
pattern is know as the Airy disk (for Sir George Biddell Airy, 1 80 1 - 1892). It's amazing
how few people have actually seen this pattern. but you can purchase a "key-chain" laser for
about $ 10.00 (target the discount stores---8utomotive accessories-{)r Edmund Scientific).
Transform Familiarization 1 73
aperture to a point at the focal plane . . . and from all our foregoing
discussions we recognize this point to be a 2-D impulse function. Now,
from our earlier discussions of impulse functions, we know their
transforms are constant amplitude cosine harmonics, and you might
anticipate that the 2-D transform of this star function (i.e., impulse
function) will generate perfectly equal amplitude harmonics across the
whole 2-D frequency domain. That is, the Fourier transform of this star
is very much like the relationship between the image created by a lens and
the equal intensity parallel rays that fall on the lens aperture . . . we'll
certainly have to discuss this further!
15.1 WAVES
15.2 INTERFERENCE
1 5.4 DIFFRACTION
phase wave front increments are summed together at a point in space, the
result will be an interference null (it's an interesting exercise to complete
this laser/slit/diffraction illustration) .... 4
/
__
_
�___
4
That is, as we have left this explanation, the 8 directed wave front increments
are not supenmposed. Light, however, is leaving the slit at all angles, of course.
5Known as paraxial rays.
6
The Pleiades were the seven daughters of Atlas, the nymphs of Diana (Jupiter
turned them into a constellation to aid their escape from Onon); but, we only see six stars
The Pleiad Electra left her sisters, unable to gaze upon the fall of Troy, which had been
founded by her son Dardanus (this stuff is important so let's get it nght)
Wave Optics Mechanism 179
superposition holds for light rays at different angles! All rays at the same
angle, regardless of where they enter the aperture, will focus into the
same image point)! Note that each bundle of rays at any angle will have
a wavefront (Fig. 1 5 . I l )! The light from each star in the Pleiades will
enter the aperture as a wavefront slightly tilted from the others.7 Let's
also point out that, as two points come closer togetheI; their wavefronts
approach the same angle, and tend to be focused into the same image
point-we will discuss this in more detail shortly.
I n FFT 1 5 . 0 0 w e h ave a
function generator that generates a
single star (impulse) which may be
p l aced at any p o i n t in the image
domain. We will consider a point on
the optical axis for our first example (i.e., at position 0,0 in the image
domain). Now, from our discussion
. of impulse functions (back in Ch. 1 0,
pp. 1 20- 1 2 1 ) the row transfonn of a
single star, being an impulse, will
g ive us a flat spectrum of cosine
phasors. Now, when we do the vertical transfonns, all of these equal
amplitude cosine components will
themselves transfonn as if they were
impulses, and w e w i l l get a 2 - D
matrix of equal amplitude cosine
hannonics. Furthennore, we know
the magnitude of the impulse must be
equal to the square of the magnitude of each hannonic.
Let's now consider stars that are not on the optical axis. As we
know, if we shift the position of our star along the row, the phases of the
harmonics will shift proportionally. That is, we will still get the same
equal amplitude components in the frequency domain, but now they will
7
This illustration is just a limited example of all image fonnatio�very point
on the surface of illuminated objects is a point source of light. Note carefully that the
infonnation here is not simply the light, but the location and intensity of the light-the
infonnation is the spatial modulation of the light!
180 Chapter 1 5
As we expected the phases of all the harmonics are zero-these are all
cosine components. If we now shift the star one position along the row
the phases of the harmonics will be as shown in the table below (their
amplitudes remaining constant). We only show the first three rows and
columns about the 0, 0 axis but this is sufficient to show what we need
to know. Note that all the phase shifts are the same across the rows.
In the next table we show the phase shifts of the harmonic components
for a shift of one position along the column.
We get the same results except the wave front now tilts in the vertical
direction. Perhaps you're beginning to see where this is heading; so, let's
stop skirting the issue and come directly to the point!
Wave Optics Mechanism 181
extracting different frequency components from the image. It's not clear
at this point why these two processes arrive at the same end result, or
even that the results are really the same. We must keep in mind that the
FFT is not modeling the physical mechanics of an optical system-it's
describing another process, another mechanism; nonetheless, these
mechanisms are so intimately connected they corresponds to the same
thing. That is, under the supposition of a sinusoidal basis function (i.e.,
waves) for both light and the Fourier transform, things like diffraction
and refraction through a lens are equally captured by both processes. A
specific example might be of help here:
The components near the outer periphery of a transform are the
high-frequency components which provide sharp definition in the image,
but it's not clear that rays through Tnuronl plan. H,.. tnq ..cy .
• CO.poMIIb
the outer periphery of an aperture
contribute more to sharpness and .,;8i--=1H-l=:=-
definition than rays on the axis.
Are these really the same?
As the Airy disk grows
oport .. _
.
smaller, definition and sharpness
increase. A careful review of Figs.
1 5.9 and 1 5 . 1 0 will reveal that as the aperture increases, the angle of the
null point decreases, and the diameter of the Airy disk decreases. Now,
we can surely write an equation for the diameter of the Airy disk vs. the
diameter of the aperture (see Fig. 15.9}--and just as surely we can find
an equation for the highest frequency component resolvable for a given
size Airy disk, and by combining these we can show that the high
frequency components in an image are indeed equivalent to (i.e.,
dependent on) rays passing through the outer periphery of the aperture
(i.e., the size of the aperture).
Still, we are only showing an equivalence between the two--we
are interpreting the Fourier transform in two different ways. The rays at
the outer periphery of the aperture are equivalent to high frequency
components of a 2-D Fourier transform. One is dependent on the other.
The Fourier transform models the light modulation at the aperture.8
8
you may not find it easy integrating all of the pieces we have discussed into this
magnificent insight. but trust me, the mathematical exposition is no cake-walk either As you
know by now, these books are notfor dummies books, but if you have made it this far you
can surely handle this .. and it's okay to go back and review the individual parts
Wave Optics Mechanism 183
FOURIER DIFFRACTION
RESOLVING POWER
all of the increments along the wavefront will be in-phase, and the light
moving in this direction will be unattenuated. As 0 moves to either side
of this forward direction, howevet; some of wavefront increments (as
illustrated in Fig. 1 5 . 10) will not be in-phase, and therefore the total light
intensity will be diminished. As the angle increases this attenuation will
become greater and greater until we
reach a null (as described on p. 1 77) r
when 0 equals sin· 1 (AID) where A and t
D are as shown in Fig. 1 5. 10. Note � �=:::;�-/::::::
:= :::"
:::::;7"""=:::""
this phenomenon arises because of the 8 •
Huygens point source model o f Fig. 1 5 . 1 7 - Diffraction Pattern
radiation from a wavefront and the
9
Those familiar with OptiCS will note that we are also working with the amplitude
function-not intensity
1 84 Chapter 15
CONCLUDING REMARKS
IO
For Sir George Biddell Airy ( 1 80 1 - 1 892)
CHAPTER XVI
IMAGE ENHANCEMENT
CONVOLUTIONIDECONVOLUTION
16.0 INTRODUCTION
l o ca t i o n o f t h e image ( i f there i s no \
aperture proper, the diameter of the lens
will always provide one). So, if the lens is \
t
focusing an image at the location indicated Source Aperture
in Fig. 1 6 . 1 , we might (assuming the Fig. 1 6. 1 - Imaging System
imaging process is bilateral) suppose the
transform of that image will be found at the aperture. This being so, it's
apparent the transfonn of the image is multiplied by the aperture in the
frequency domain... and this frequency domain boundary implies an image
domain phenomenon very similar to the Gibbs phenomenon we know so
well! In fact, it's the source of the diffraction pattern we call the Airy
disk.
Tlf) = !!JJl
Alf) ( 1 6. 1)
[The (f) in these equations, as usual, implies all frequencies in the signal.
An impulse function serves nicely for this evaluation, of course.]
Image Enhancement 1 87
The transfer function includes those things that modify the input
signal. If we have a perfect lens, for example, the transfer function will
include only the aperture function we just described. If, howevet; you're
using a cheap camera with a mass-produced, untested plastic lens-or you
get mud on the lens of the expensive camera you borrowed from a rich
friend-the transfer function will include a great deal more. In any case
the transfer function will multiply the image transfonn (Fig. 1 6. 1), and the
image will be the convolution of this transfer function with the object. So,
2
We understand, of course, that when we limit our discussion to linear systems
we disallow things like backlash, pump valves, diodes, logarithmic converters, etc.
188 Chapter 16
just as the aperture creates the Airy disk (yielding a less than perfect
image), lens aberrations degrade the result. When we deal with this sort
of problem it's a great simplification to think in terms of multiplication of
transforms in the frequency domain. Clearly, if the distortion is caused by
some action in the aperture plane (e.g., mud on the lens), the distorting
function will be convolved with the image and becomes part of the transfer
function. To remove this sort of distortion we must divide the frequency
domain function by the transfer function! In fact, if we knew the exact
equation for the transfer function we could exactly reproduce the input!
That's what Eqn. 1 6. 3 says! {Unfortunately. you can t divide by zero! The
aperture function multiplies everything outside the aperture by zero so
theres no way to get that portion of the input back... (sighJ.)
That's all it takes ...(well, you will also have to change the Dimension
190 Chapter 16
statement, etc., etc., etc... ). Run the identical illustration as above under
double precision and it will work.
The reason it takes double precision to get good arithmetic in the
above illustration is that a Gaussian function greatly attenuates the higher
frequencies (Fig 1 6.7). We are multiplying the high frequencies of our
transform by very small numbers and
t h e n p e rfo r m i n g a m u l t i t u d e o f
0" - 1
multiplications and additions on these
-ZD
numbers (i.e., the FFT operations).
When we later divide the transform
-40
by these same small numbers we find
the original data has been lost in -60 .'"'1...., ....
our 8-bit AID, and dividing the transform of this 8 bit data by our
mathematical Gaussian transform can't bring back the lost information
it will only create random numbers in the high frequencies-noise.
The message is, in the case of data bits, more is better, but 16 bits
only give 96 db of dynamic range ... and from Fig. 1 6.7 it's apparent that
seriously degraded images will need more than that. [Note: the maximum
resolution frequency is. of course. the Nyquist (indicated as QI2 in Fig
16 7). Our efforts here only concern hannonics below the Nyquist.] You
will get a better feel for this as you work with it.
You can experiment with the effect of data length by converting
the degraded image to various data length words (e.g., 8 - 1 6 bit
resolution) and then try to deconvolve it in the usual way. You will, of
course, get the overgrown "hay" at the high frequencies (you can try this
in double precision if you like, but it's pretty obvious that the problem is
data word length). In Fig. 1 6 . 1 0 we compare attempts to recover images
were obtained with a relatively fixed routine, keeping things the same for
each picture (a not completely successful effort, obviously). The last
picture (bottom right) was obtained after an unjustifiably long effort (as
I said, some are better at this than others). Compare these to Fig. 1 6.4
which is the best we can do with a perfect recovery.
8
The physics of e1ectncity and magnetism make this point strongly as the
ubiquitous nature of stray capacitance and inductance is grasped
Image Enhancement 197
9
Founer analysis can be extended to the third dimension. The way to do this is
more-or-Iess apparent from our discussion ofthe 2-D transform The implication is that 3-D
objects are a superposition of sinusoidal waves . talk about interesting puzzles. It has
occurred to me that a third book on the 3-D transform might be interesting... but so far I
have been successful in putting it out of my mind
APPENDIX 1 . 1
AVERAGE VALUE
---------- (A 1 . 1 )
where n1 is the smaller number and n2 is the larger [e.g., (5-3) = (7-5»). If
we solve (A 1. 1) for Av we find:
- - - - - - - - - - - (A l .2)
The notion of a mean extends to more than two numbers, of course. The
number 5 is also the average of 3, 4, 6 and 7. It's the middle point of this
set of numbers. If you expand this exercise, you will eventually conclude
that the average of any set of N numbers is:
That is, we add up the numbers and divide by the number of data points.
Now, we may use this notion to find the
4 average value of a function over some range. For
•
3 •
example , we would solve the very simple function
y = INT(x) for its average value from zero to 4 as
2 -- - ....
- - -
follows:
•
(0 + 1 + 2 + 3 + 4)/5 = 10/5 = 2
1 2 3 4
Fig. A1.1
The average value of this linear function between 0
and 4 is 2-we included zero--the average value from 1 to 4 is 2.5!
Well and good, but suppose, for example, we want to know the
2
average value of the function y = INT(x )? The above technique yields:
(0 + 1 + 4 + 9 + 1 6)/5 = 30/5 = 6
200 Appendix 1 . 1
I **********************************************************
, * PROGRAM TO COMPARE AVERAGE VALUES OF CURVES OF THE FORM *
, * (Y = XAN ) AS OBTA I NED BY : 1 ) THE MEAN VALUE THEOREM *
, * 2) SUMMAT I ON D I V I DED BY N 3) THE TRAPEZOIDAL RULE *
I **********************************************************
CLS : DE FDBL A - Z
I NPUT " POWER T O RAI SE I NDEPENDENT VAR I ABLE T O I S " ; N
I NPUT " CALCULATE OVER DOMA I N (X MAX . - I NTEGER ) " ; XMAX
1 0 I NPUT " USE A STEP S I ZE OF : ( E . G . , 0 . 5 ) " ; SZ
PR I N T : PR I N T
PR I NT "MEAN VALUE DETERM I NED B Y CALCULUS I S " ; ( XMAXA(N+1 ) / ( N+1 »1 XMAX
PR I NT
I ******************************************************
YT = 0 ' I N I T I AL I ZE ACCUMULATOR
FOR I = 0 TO XMAX STEP SZ ' SOLVE AR I THMET I C MEAN
YT = YT + I A N
NEXT I
YAV = YT I ( XMAX I SZ + 1 ) ' ADD 1 TO COUNT THE "ZERO" DATA POI NT
PR I NT "THE AR I THMET I C MEAN IS " ; YAV: PR I NT
YT = YT - ( XMAX A N I 2 ) : YAV = SZ * YT I XMAX
I ******************************************************
PR I NT "THE AVERAGE DETERM I NED BY THE TRAPEZO I DAL RULE I S "; YAV
PR I NT "MORE ( YIN ) " ;
9 0 A S = I NKEYS : I F A S = " " THEN 90
I F AS = "Y" OR AS = " y" T HE N 1 0
END
If you experiment with this program you will find that as the number of
data points is increased, the arithmetic average approaches the mean as
determined by calculus. Calculus finds the mean of an infinite number of
data points, and as we take more points, the arithmetic mean approaches
the same value. The calculus finds the mean value of a continuous
function-the arithmetic mean gives the average of a finite number of data
points. If you're working with a continuous function you must use the
calculus-the average of a finite set of numbers is given by Eqn. (A 1 .3).
APPENDIX 4.1
DIFFERENTIATION AND INTEGRATION
VIA TRANSFORMS
A4.1 DIFFERENTIATION
(A4. 1 )
1 1 20 CTemp = - KD F * S ( I ) : S ( I ) = KD F * C ( I ) : C( I ) = CTemp
1 1 30 CTemp = KD F * S ( Q - I ) : S ( Q - I ) = - KD F * C(Q- I ) : C ( Q - I ) = CTemp
1 1 40 NEXT I
1 1 42 C(Q2) = 0 : S(Q2) = 0
1 1 50 NMS = "DE R I VAT I V E " : RETURN
So far, in our work with the FFf, we have assum ed a time domain
of unity (i.e., one second). That, of course, will seldom be the case; but,
as far as the FFT is concerned it makes no difference. We will always fit
exactly one sinusoid into the domain for the fundamental, and the
harmonics will always be integer multiples of this fundamental. If, for
example, we study the offset voltage drift of an operational amplifiet; and
take one sample per day for 1 024 days, the harmonics will be in units of
N cycles/ l 024 days. The derivative of this data will be expressed in volts
per 1 024 days. [If we should need the drift rate per day we simply divide
the data by 1 024].
Having noted this, it's apparent the reference period for our
sinusoids (i.e., To) is always equal to the time of the total sample interval,
and that the time between samples (At) must be Tr/Q (where Q is the total
Differentiation and Integration 203
61 0 FOR I = 0 TO Q - 1 : C ( O , I ) = 0: S ( O , I ) = 0
61 2 FOR J = 1 TO Q2 - 1 STEP 2 ' SUM 000 HARMON I CS
620 C( I ) = C ( I ) + ( S I N ( K1 * I * J ) / ( J * J »
630 NEXT J ' SUM I N NEXT HARMON I C ' S CONTR I BU T I ON
640 C ( I ) = C ( I ) * 8 / P I A2 ' CORRECT FOR UN I T AMP L I TUDE
650 NEXT I ' CALCULATE NEXT DATA POINT
If you try this you will find it works fine, but suppose our actual
digitized function was a 1 00 Hz triangle wave of amplitude ± 1 volt? In
that case (assuming we still digitize one cycle in 5 1 2 samples) the total
data interval will be om second, but the data will be identical to the 1 Hz
waveform. The derivative will still yield ± 4 but we must divide by the
0.01 seconds and the correct derivative will then be 400 volts/sec. While
we do not carry the time scale factor into the DFT, we should keep it safe
somewhere so that we can calculate and display the correct ampli
tudeltimelfrequency data for the end user.
A4.3 INTEGRATION
The two programs for Chapter 5 and 6 have been written so that
they can be merged together with a minimum of headaches. First you will
need to load the Mesh Analysis program ACNET05.BAS (p. 37), delete
everything up to line 1 0000, and then save what's left under some
temporary name (e.g., MESHTMP.BAS). Note that, for QuickBasic, you
will have to save this as an ASCII file so that it can be merged late!: Next
load the Gibb's Phenomenon (FFT06-0 1 ) program from Chapter 6 (p. 5 1 )
and immediately save it as TRANALYS.BAS (or some name you prefer).
Then merge MESHTMP.BAS to the end of this new program.
Okay, now we will have to make some modifications:
1 082 A = VAL (A$ ) : ON A GOSUB 1 1 00, 1 200, 1 300 , 1400 , 20000, 1 990
1 0000 F = 1 3 : FREQ = 1 3
10010 GOSUB 1 1 000' CALC I MPEDANCE MAT R I X
1 0020 GOSUB 1 2000 ' SOLVE FOR CURRENTS
1 0030 ATTNR = Z ( 1 , KB , KB+1 )*RTERM :ATTN I = Z ( 2 , KB , KB+1 )*RTERM
1 0090 RETURN
2 REM ******************************************
3 REM ** ( D FT07. 01 ) GENERATE/ANALYZE YAVE FORM **
8 REM ******************************************
1 0 Q = 3 2 : PI = 3 . 1 4 1 592653589793#: P2 = 2*P I : K1 =P2/Q : K2= 1 / P I
1 4 D I M C ( 2 , Q ) , S ( 2 , Q ) , KC ( 2 , Q ) , K S ( 2 , Q )
1 6 C L S : FOR J =O T O Q : FOR 1 = 1 T O 2 : C ( I , J ) =O : S ( I , J )=O: NEXT : NEXT
20 CLS : REM * MA I N MENU *
22 PR I NT : P R I NT : P R I NT " MA I N MENU" : PR I NT
24 P R I NT " 1 = STRETC H I NG THEOREM" : PR I NT
31 P R I NT " 2 = EX I T" : PR I NT : PR I NT
32 PR I NT SPC ( 1 0 ) ; "MAKE SELECT I ON " ;
3 4 A S = I NKEYS : I F A S = " " T H E N 34
36 A = VAL ( AS ) : ON A GOSUB 300, 1 000
38 GOTO 20
40 CLS : N = 1 : M = 2: K5 = Q2: K6 = - 1 : GOSUB 1 08
42 FOR J = 0 TO Q : C ( 2 , J ) = 0: S ( 2 , J ) = 0 : NEXT
44 GOSUB 200 : REM - PER FORM O F T
4 6 GOSUB 1 4 0 : R E M - PR I NT OUT F I NAL VALUES
48 PR I NT : I NPUT "C/R TO CONT I NUE"; AS
50 RETURN
1 00 REM ******************************************
1 02 REM * P ROGRAM SUBROUT I NES *
1 04 REM ******************************************
1 06 REM * P R I NT COLUMN HEAD I NGS *
1 08 P R I N T : PR I NT : I F CORS = "P" THEN 1 1 6
1 1 0 P R I NT " F REQ F ( COS) F(SIN) FREQ F ( COS ) F ( S I N )"
1 1 2 PR I NT
1 1 4 RETURN
1 1 6 PR I NT " F REQ F ( MAG ) F C THETA) FREQ F ( MAG) F C THETA ) "
1 1 8 GOTO 1 1 2
1 37 REM ******************************
1 38 REM * PR I NT OUTPUT *
1 39 REM ******************************
1 40 I F CORS = "P" AND M = 2 THEN GOSUB 1 70
1 4 1 FOR Z = 0 TO Q / 2 - 1
1 42 PR I NT US I NG "## II. z ·
1 44 PR I NT US I NG ,,+### . #11###
'
"; C ( M , Z ) ; S C M , Z ) ;
1 45 PR I NT U S I NG "## II . (2 + Q / 2 ) ·
146 PR I NT USI N G ,,+### . _- "; C ( M, Z + Q / 2 ) ; S C M , Z + Q / 2 )
1 4 7 NEXT Z
148 RETURN
Positive Frequency Stretching 207
10 c ( o ) = ( Y ( O ) + Y ( 2 ) + Y ( 4 ) + Y ( 6 » /4
1 2 C ( 1 ) = ( Y ( O ) - Y ( 4 » /4 : S ( 1 ) = Y ( 2 ) - Y ( 6 » /4
14 C ( 2 ) = ( Y ( O ) - Y ( 2 ) + Y ( 4 ) - Y ( 6» /4
10 T(O) = Y ( 0 )+Y(4 ) : T ( 1 ) = Y ( 2 ) +Y ( 6 )
11 T(2) = Y ( 0 ) - Y ( 4 ) : T ( 3 ) =Y ( 2 ) - Y( 6 )
12 C(2) = ( T ( 0 ) - T ( 1 » /8
13 T(S) = T(O) + T ( 1 )
and:
14 T ( 6 ) = Y ( 1 ) + Y e S ) : T ( 7 ) = Y ( 3 ) + Y ( 7)
15 T ( 8 ) = Y ( 1 ) - Y e s ) : T ( 9 ) = Y ( 3 ) - Y ( 7)
16 T ( 1 0 ) = T(6) + T(7)
17 T ( 1 1 ) = T(8) + T(9) : T ( 1 2 ) = T(8) - T(9)
Note that the divide by 4 operation at the end of each line is removed,
exceptfor C(2) and S(2) which are divided by B.
In the conventional FFT we would combine these odd and even
transforms by the familiar rotate and sum loop; but, to obtain maximum
efficiency, we will combine these via five individual equations:
18 K1 = SQR ( . S ) /8
20 C ( O ) = ( T ( S ) + T ( 1 0 » /8
21 0 Appendix 7.2
22 C( 1 ) = T ( 2 ) + K1 * T ( 1 2 )
24 S(1 ) = T ( 3 ) + K 1 *T ( 1 1 )
26 C(2) = ( T ( O ) - T ( 1 » /8 : S ( 2 ) = ( T ( 6 ) - T ( 7» /8
28 C(3) = T ( 2 ) + K1*T ( 1 1 )
30 S(3) = K1 * T ( 1 2 ) - T ( 3 )
32 C(4) = ( T ( 5 ) - T ( 1 0 » /8
to beat the efficiency and speed of this routine. You should recognize
we're not abandoning the fundamental rotate and sum mechanism of the
FFT-we're only weeding-out superfluous machine cycles.
This approach can be extended to 16 point arrays but it's apparent
that, as the twiddle factors become more complicated, it becomes harder
to obtain significant gains. It might be a worthwhile exercise to see how
far this approach can profitably be extended. In any case, you will find it
difficult to beat the efficiency and speed of the routines given here . . . .
APPENDIX 9.2
- - - - - - - - - - - - (A9 2 0)
odd data transfonn to account for the shifting phenomenon That is to say,
the frequency components from the "odd data" transform are properly
phase shifted and summed into the frequency components of the "even
data" transform. As before, the virtual components are created by
summing the mirror image of the direct components (i.e., the complex
conjugates are summed in reverse order).
Writing a fractional frequency transfonn would be a good exercise
to undertake-it would make much of what we have discussed about the
FFT come sharply into focus. Here is a little help:
The FFT starts from the one point xforms that each data point
represents, shifting and adding through N stages to accomp lish the
transfonn of Q data points (Q = 2N) . Now, in the conventional FFT, we
sum each data point from the lower half of the array with the data point
that is halfan array away (i.e., Do is summed with DQ/2 ' etc.). In the FFT
and PFFFT we start with single point xfonns and generate, in the first
stage, two point xfonns; however, in a half-frequency PFFFT, we must
produce three frequency components in the first stage . . . . In a shifting and
adding algorithm, how is the 112 Hz component generated?
It will help to return to the basic model for the FFT algorithm
discussed in Understanding the FFT, and realize that we are shifting the
"odd" data points so that they will interleave with the stretched "even" data
points. The shifting theorem has shown that the shift of each component
must be proportional to the frequency, and a half Hz frequency wiII only
receive half the shift as a full Hz. It may help to review Fig. 7.4 and think
of the 4-point xfonn as a 2-point, half frequency xfonn, with the last two
data points packed with zeros. It may also help to review Fig 7.6, packing
half of the array with zeros.
You recognize, of course, that this is deliberately presented as a
puzzle. It's a worthwhile puzzle, even if you do not plan to use the 1 12 Hz
configuration. It's apparent that many readers wiII have neither the time
nor inclination to tackle this puzzle . . . but, quite frankly, it is hard to
imagine that, if you have read this far, you wiII not have the ability. At
any rate, if you ever need it, it wiII be here.
APPENDIX 9.3
ONE DECI-BEL
Just as the Ohm is named in honor of Georg Simon Ohm, the Bel
is named in honor of Alexander Graham Bell. I It has a definition:
P I and P2 are power readings. This i s the only definition of the Bel.
By taking the logarithm of the power ratio we can compress a
wide dynamic range into a small scale (the range of one megawatt to one
microwatt is only 1 2 Bels). Fortunately, our senses measure magnitude
logarithmically, allowing us, for example, to see the Sun (almost) and
(later) see very faint stars; consequently, measuring things in Bels
frequently fits much better than a linear scale.
The average person, when she wants an evaluation of some man,
asks her girlfriend; "How would you rate him on a scale of 0 to 1 0?" (The
pat answet; of course, is - 1 .) Engineers, howevet; don't want to be like
"normal" people (fat chance); so, they evaluate everything on a scale of 0
to 1 00. This requires splitting Bels into deci-bels (abbreviated db) which
are obviously defined:
This is still the same definition-we 're just working with 1 / 1 0th units.
Now, power is a good way to measure things-power makes
resistors get hot, etc. Electrical engineers, howevet; don't like to think
about resistors getting hot, so they work mostly with volts. Be that as it
may, it would still be nice to use this logarithmic scale in our work with
voltage. This is no problem-if we know the voltage and the resistance,
we can always calculate the power:
2
Power (in Watts) = y /R (A9.3.3)
I
There is, no doubt, a connection here with the notonous "language skills" of
engineers
214 Appendix 9.3
Frequently, the two resistors will be the same value (when we measure two
voltages at the output of an amplifieJ; for example) and then:
(A9.3.6)
2
Under these conditions we can imagine some hypothetical load impedance
(significantly greater than the internal impedance of the source), across which these
voltage readings do indeed indicate legitimate power ratios, and satisfy Alex.
APPENDIX 9.4
SPECTRUM ANALYZER II
I ***************************************************************
, ***
F F T09-02 *** POS I T I VE FREQUENCY F F T ***
' T H I S PROGRAM ANALYZES T I ME DOMA I N DATA W I T H FRACT I ONAL F REQUENCY
' ANALYS I S . I T S I MULATES T H E PER FORMANCE REQU I RED FOR A FR EQUENCY
' ANALYZER AND I NCLUDES THE LATEST P F F F T ( F FT08 - 0 1 ) .
t ***************************************************************
************************************************
******** ********
MA I N MENU (ANALYZER SETUP)
************************************************
*****************************
*** ***
1 00 ' FORWARD TRANSFORM
*****************************
21 6 Appendix 9.4
I **********************************
, * PLOT SPECTRUM *
I **********************************
350 CLS : L I NE (XO - 1 , 1 1 ) - ( XO - 1 , YO + 320 ) ' DRAW Y AX I S
352 L I NE ( X O , Y O + 1 ) - ( XO + 500, Y O + 1 ) ' DRAW X AX I S
, **** DRAW 20 D B L I NE S ****
354 FOR I = 2 TO 1 4 STEP 2 : YSKT = I NT ( YSF*1 0*LOG ( 1 / ( 1 0� 1 » /KLOG)
355 L I N E ( X O , YO - YSKT ) - ( XO + 500, YO - YSKT)
356 YDB = C I NT ( . 3 + ( YO - YSKT ) / 1 3 . 9 ) : IF YDB > 25 T H E N 360
358 LOCATE YDB, 2 : PR I NT U S I NG "###" i 1 0 * I ;
360 NEXT I
362 YP = SCALE * SQR ( C ( I ) � 2 + 5 ( 1 ) � 2 ) F I ND RSS OF DATA PO I NT
Spectrum Analyzer II 217
I **********************************
I **********************************
, **********************************
*** ***
600 ' SPECTRUM ANALYZER
, **********************************
602 CLS : PR I NT : PR I NT
604 PR I NT SPC ( 20 ) ; "PREPAR I NG DATA - PLEASE WA I T "
61 0 GOSUB 400 ' GENERATE S I NUSO I D
620 GOSUB 1 00 ' ANALYZE SPECTRUM
624 REPT = 0 ' RESET REPEAT FLAG
626 LOCATE 24 , 65 : PR I NT "RETURN TO E X I T " ;
628 A S = I NKEYS : I F AS = " " T H E N 628' WA I T USER I NPUT
630 I F ASC(AS) = 0 THEN GOSUB 650 ' CURSOR HAS LEAD I NG ZERO
632 I F REPT = 1 THEN 620 ' ANALYZE SPECTRUM AGA I N
634 RETURN ' BACK T O MA I N MENU
650 ' *** HANDLE CURSOR KEYS ***
652 A = ASC( R I GHTS(AS, 1 » , WH I CH CURSOR
654 I F A < 75 OR A > 77 OR A = 76 THEN 669 ' NOT A CURSOR KEY
656 I F A = 75 THEN F8 = F8 - . 1 ' I NC FREQUENCY
658 I F A = 77 THEN F8 = F8 + . 1 ' D E C . FREQUENCY
660 F9 = F8 * Q / Q I ' SCALE FOR CURRENT ARRAY S I ZE
662 GOSUB 400 ' GENERATE NEW S I NUSO I D
664 REPT = 1 ' S E T REPEAT FLAG
669 RETURN ' DO IT AGA I N SAM
218 Appendix 9.4
I *********************************************
* *
800 I SETUP FRACT I ONAL FREQUENCY ANALYZER
I *********************************************
, ***********************
I * *
SYSTEM SETUP
, ***********************
920 CLS
922 PR I NT "USE GRAPH I C D I SPLAY ( YIN ) " ;
924 AS = I NKEYS: I F AS = " " T H E N 924
926 I F AS = "N" OR AS = " n " THEN I OFLG = 1 E LSE I OFLG = 2
928 RETURN
I **********************
930 CLS : PR I NT "WE I GH T I NG FUNCT I ON ON (YIN ) " ;
932 AS = I N KEYS : I F AS = " " THEN 932
934 I F AS = "N" OR AS = " n " THEN WT FLG = 1 : WSF = 1 : GOTO 956
936 WTFLG = 2 : PR I NT
938 PR I NT "CHANGE WE I GHT I NG FUNCT I ON EXPONENT?"
Spectrum Analyzer II 219
I ***********************
I ***********************
, ***********************
in the sub-routine at line 970. At line 980 the value entered is checked for
a valid selection, then the fraction denominator FRACF is calculated (line
982). Note: FRA CF is used in the Array Selection (line 852) to set QI.
It s also used in line 855 to set QDT (i e., the length 0/ the actual data),
and also in the Plot Data routine (to set the scale/actor-line 85 7).
3. We need to talk about the Generate Sinewave Function routine
(line 400). At line 4 1 2 we generate two sine waves-the second is 80 db
down and separated by 50% of the primary functions frequency (you may
want to change this). Also, if the weighting function is selected, we jump
to the Weighting sub-routine at line 406. Weighting is handled (lines 452
through 456) by multiplying the data by a half cycle sine wave raised to
the power WEXP (see p. 1 03).
4. We must also talk about the Plot Data routine (line 350-390),
but we we calculate the X and Y scale factors at line 857 (where they are
determined). At line 350-352 we draw Y and X axes. The data is now
displayed in a logarithmic format and, when we draw 20 db attenuation
marks (354-360), we must calculate their logarithms (line 354). Likewise,
when we plot the data (lines 372-386) we must caluclate the logarithm of
that (line 382).
5. Finally, this program is intended for experimentation-to get
a feel for the various configurations and what they can do. It's limited in
speed and size but it can still be helpful in gaining an understandmg of how
spectrum analysis is performed with the FFT and the problems to be
expected. It's also intended for experimentation with the code . . . and
possible routines.... For example, you might want to experiment with
various weighting functions so don't hesitate to make variations.
APPENDIX 10.0
NEGATIVE FREQUENCIES
(AIO. I )
and the argument of a sinusoid 10% below the Nyquist will be:
(A I O.2)
Now, from Fig. A l O.5 below, we see that when we deal with a
complex sinusoid, the relationship we have just described still holds for the
_-- Negative __
T-0 __ Positive --_
Time Time
sine and cosine components of that wave; so, clearly a negative time
argument yields the complex conjugate ofthe positive time argument (that
is, in terms phasors, the negative time domain waveform will be generated
by the complex conjugate of the positive time phasor).
Now, when we deal with the harmonics above the Nyquist in the
FFT, it's obvious we're not dealing with negative time; howevet; it's not
obvious we're dealing with negative frequency-we certainly haven't
deliberately introduced negative frequencies. In Equations (A 1 0. 1 ) and
( A l O.2) above, however, we see that, for each sampling pulse, the
argument steps forward by half a cycle of the Nyquist signal frequency ±
the difference between Nyquist and the sinusoid being digitized:
( A l O.5)
l
in both of these cases there is a symmetry, although it's obviously not the
same kind of symmetry. The symmetry exhibited by the cosine wave is called even while
the symmetry displayed by the sine wave is called odd If the hannonic components of
a function are composed of only sine waves, the whole function will display this odd
symmetry (i.e., for negative arguments the function itself will be equal to the negative of
its positive counterpart). Functions composed of only cosine components will exhibit
even symmetry. A square wave constructed of sine waves, for example, exhibits odd
symmetry 1 Functions containing both sine and cosine components (i.e., complex
sinusoids) will exhibit neither form of symmetry discussed above; nonetheless, the odd
and even condition stili holds for the sine and cosine components of these functions.
224 Appendix 10.1
OVERSAMPLE DEMONSTRATION
OVERSAMPLING II
I **********************************
, * *
PLOT SPECTRUM
, **********************************
, * *
GENERATE S I NE WAVE
, **********************************
602 CLS : PR I NT : P R I NT
604 I N PUT "PLEASE SPEC I FY START I NG FREQUENCY"; F8
606 F9 = F8 * FRACF * Q2 / NYQ
610 GOSUB 400 ' GENERATE S I NUSO I D
61 2 GOSUB 1 00 ' ANALYZE SPECTRUM
614 REPT = 0 ' RESET REPEAT F LAG
620 LOCATE 1 , 40 : P R I NT "ESC TO CHANGE SYSTEM : " ;
622 A$ = I N KEY$ : I F A$ = " " THEN 632 ' E X I T OR RUN?
624 I F ASC(A$ ) = 0 THEN GOSUB 650 ' CURSOR HAS LEAD I NG ZERO
626 I F ASC (A$) = 27 T HEN GOSUB 900
632 I F REPT = 1 THEN 6 1 2 ' ANALYZE SPECTRUM AGA I N
634 GOTO 606 ' BACK T O MA I N MENU
650 ' *** HANDLE CURSOR KEYS ***
652 A = ASC ( R I GHT$(A$, 1 » , WH I C H CURSOR
Digital Audio Analyzer 231
************************************************
******** ARRAY S I ZE MENU (ANALYZER SETUP) ********
********************* ******************* ********
800 CLS : LOCATE 2, 30 : PR I NT "ANALYZER SETUP MENU"
802 LOCATE 6 , l ' D I SPLAY MENU
810 PR I NT SPC ( 5 ) ; "1 = ANALYZE 64 PO I NT ARRAY" : PR I NT
812 PR I NT SPC( 5 ) ; "2 = ANALYZE 1 28 PO I NT ARRAY" : PR I NT
814 PR I NT SPC( 5 ) ; "3 = ANALYZE 256 PO I NT ARRAY " : PR I NT
816 PR I NT SPC ( 5 ) ; "4 = ANALYZE 5 1 2 PO I NT ARRAY" : PR I NT
818 PR I NT SPC ( 5 ) ; "5 = ANALYZE 1 024 PO I NT ARRAY " : PR I NT
820 PR I NT SPC ( 5 ) ; "6 = ANALYZE 2048 PO I NT ARRAY " : PR I NT
822 PR I NT SPC ( 5 ) ; "7 = ANALYZE 4096 PO I NT ARRAY " : PR I NT
826 PR I N T SPC ( 5 ) ; "9 = E X I T MENU" : PR I NT
828 PR I NT SPC( 1 0 ) ; "MAKE SELECT I ON : " ;
830 A$ = I NKEY$: I F A$ = " " THEN 830
832 I F ASC(A$ )<49 OR ASC(A$» 57 THEN PR I NT " I NVAL I D KEY " : GOTO 830
840 A = VAL (A$ ) :
842 ON A GOSUB 850 , 860, 865 , 870, 875 , 880 , 885 , 900 , 990
844 RETURN
through line 30 and then GOSUB to line 900 to allow setup modification
as desired. At line 34 we jump to the array size setup and then jump to
600 to start the analyzer At line 604 we get the initial frequency, F9 gets
the frequency in terms of harmonic number, and we are ready to jump to
the Generate Function routine at line 400. This Generate Function routine
is discussed in the text (p. 1 4 1 ).
Lines 400 - 458 generate the simulated D/A signal. Thefunction
weighting is invoked as a user option. Returning to line 612 we then jump
down to the PFFFT, and the return-jump (to the Plot Transformed Data
routine-lines 350-390) takes place from the PFFFT. The Plot Data
routine has changed slightly-all of the scale factors etc ., are calculated
when they are determined so that they need not be calculated each time we
plot the data. Aside from these changes it's pretty much the display
routine used in Appendix 9.4.
Returning to line 614 we set the Repeat flag to zero and locate the
cursor to the upper right-hand comer of the screen. At line 620 we print
the note that the Escape key may be used to modify the system configura
tion. Let's look at how the user controls the spectrum analyzer
Line 622 checks to see if a key has been pressed-if not the
program continues on. If a key has been pressed, we check to see if it's a
control key (line 624). If so we jump to the Handle Control Key routine
at line 650, but we will discuss this shortly. Line 626 checks to see if an
Escape key has been pressed (ASCII code 27). If so we j ump to line 900
where a System Menu is displayed. The number 1 selection will display
the current status of the system (i.e., the fractional frequency and weighting
function configuration, as well as the size of the data array being used).
This subroutine is located at lines 920-928. [Note that these sub-routines
all return to the System Menu J
Option 2 allows changing the weighting function parameters (at
lines 930-956). The weighting function may be turned "On" or "Off' and
the exponent of the 112 cycle sineN weighting function may be selected.
Option 3 allows changing the fractional frequency configuration
(lines 970-986). This routine jumps to the select the array size routine
again (this is necessary to re-establish the array constants).
Option 4 is the Exit back to the Analyzer
Option 5 allows selecting the array size which is the same routine
used in the previous spectrum analyzers.
Option 6 allows changing the sampling rate (lines 960-964).
Option 9 is the orderly way to terminate this program.
If, back at line 622, a cursor key was entered, the program flow
jumps to line 650. We note that the control keys (i.e. , the cursors) allow
234 Appendix 1 1 .3
changing the amplitude of the signal being analyzed via the Up and Down
cursors (i.e., t and � on the keyboard) which is an interesting illustration
(see lines 630-646). The left and right (i.e., -+ and -) keys increment!
decrement the frequency. You may recognize T I 2 (line 30) as the step
ratio for the chromatic scale.
The reader should be warned this program is a conglomeration of
several test routines and, while it works, it will probably contain bugs.
Handling of scale factors, etc., is sloppy. It would be a good exercise to
write a similar program concentrating on a simple, consistent set of
amplitude, time and frequency scale factors.
APPENDIX 12.1
PLAYBACK SPEEDUP
I ************************************
10 ' *** ( F FT1 2 . 01 ) VO I CE SPEEDUP F F T ***
, ************************************
, DYNAM I C
1 2 C L S : SK1 = 1 : SK3 = 1 : Q = 2 � 1 1 : Q 2 = Q / 2
14 PAT H I S = "C : \ " : PATHOS = PAT H I S
1 6 D I M C ( Q ) , S ( Q ) , KC ( Q2 ) , KS(Q2 ) , HDR(44)
1 8 FOR I = 1 TO 44 : READ HDR( I ) : NEXT I
20 DATA 82 , 73 , 70 , 70 , 1 87 , 23 , 0 , 0 , 87 , 65 , 86 , 69 , 1 02 , 1 09, 1 1 6 , 32 , 1 6 , 0 , 0 , 0 , 1
22 DATA 0 , 1 , 0 , 1 7 , 43 , 0 , 0 , 1 7 ,43, 0 , 0 , 1 , 0 , 8 , 0 , 1 00 , 97, 1 1 6 , 97, 1 5 1 , 23 , 0 , 0
24 P I = 3 . 1 4 1 592653589793#: p 2 = 2 * P I : K 1 = P 2 / Q
30 Q3 = Q2 - 1
32 FOR I = 0 TO Q3 : KC( I ) = COS( I * K1 ) : KS( I ) = S I N ( I * K 1 ) : NEXT I
34 I NPUT " I NPUT DATA SAMPLE RAT E " ; DSR1
36 CLS : GOSUB 1 1 00 ' GET DATA F I LE
40 CLS
50 PR I NT SPC(30 ) ; "MA I N MENU" : PR I NT : P R I N T
52 PR I NT SPC ( 5 ) ; " 1 = F FT " : PR I NT
54 PR I NT SPC ( 5 ) ; "2 = I NV F F T " : PR I NT
56 PR I NT SPC ( 5 ) ; "3 = D I SPLAY MENU" : P R I N T
5 8 P R I NT SPC ( 5 ) ; " 4 = SPEEDUP ROUT I N E " : P R I N T
6 0 PR I NT SPC ( 5 ) ; "5 = F I LE UT I L I TY " : PR I NT
62 PR I NT SPC ( 5 ) ; "6 = F I LTER " : PR I N T
68 PR I NT SPC ( 5 ) ; " 9 = EX I T" : PR I N T
7 0 PR I NT SPC ( 1 0 ) ; "MAKE SELECT I ON : " ;
80 AS = I NKEYS : I F AS = 1111 THEN 80
82 I F ASC(AS) < 49 OR ASC(AS) > 57 THEN 40
90 A = VAL ( AS ) : ON A GDSUB 600, 700,800 , 400 , 1 000 , 3000 , 990 , 990 , 990
95 I F A = 9 THEN 999
97 GOTD 40
, ******************************
, * VO I CE SPEEDUP *
, ******************************
400 CLS : PR I NT : PR I N T
402 PR I NT SPC ( 20 ) ; "VO I CE SPEEDUP MENU" : PR I NT : PR I NT
404 PR I NT SPC ( 5 ) ; " 1 = STRETCH S I DEBANDS 2 = FRACT I ONAL STRETCH"
405 P R I NT SPC ( 5 ) ; "9 = EX I T "
406 A S = I NKEYS: I F A S = " " THEN 406
407 ON VAL (AS) GOSUB 450, 4 1 2 , 4 1 0 , 4 1 0 , 4 1 0 , 4 1 0 , 4 1 0 , 4 1 0 , 4 1 0
410 RETURN
412 ' *** FRACT I ONAL STRETCH ***
414 CLS : I NPUT "SELECT CENTER FREQUENCY " ; FC01
415 FC01 = FC01 * Q / DSR1
416 P R I NT SPC ( 20 ) ; "SELECT SPEEDUP FRACT I ON " : PR I NT PR I NT
417 PR I NT SPC ( 5 ) ; "1 = -3/4 5 = + 1 /4"
418 PR I NT SPC ( 5 ) ; "2 = - 1 /2 6 = +1 /2"
419 PR I NT SPC ( 5 ) ; "3 = - 1 /4 7 = +3/4"
420 PR I NT SPC ( 5 ) ; "4 = 0 8 = N/A"
421 PR I NT SPC ( 5 ) ; "9 = EX I T "
236 Appendix 12.1
************************
* F I L E MENU *
************************
1 000 CLS : PR I NT SPC(30 ) ; " F I L E HANDLER MENU " : PR I NT : P R I NT
1 002 P R I NT SPC ( 5 ) ; " 1 = RELOAD F I LE 2 = SAVE F I L E "
1 004 P R I NT SPC ( 5 ) ; "3 = LOAD NEW F I L E 4 = CHANGE SAMPLE RATE"
1 006 PR I NT SPC ( 5 ) ; "5 = WE I GHT I NG FUNCT I ON "
1 0 1 0 PR I NT SPC ( 5 ) ; "9 = E X I T "
1 0 1 2 A S = I NKEYS : I F AS = " " T HEN 1 0 1 2
1 0 1 4 ON VAL(AS) GOSUB 1 1 1 0 , 1 200 , 1 1 00 , 1 040 , 1 060 , 1 0 1 8 , 1 0 1 8 , 1 0 1 8 , 1 0 1 8
1 0 1 8 RETURN
******** CHANGE I NPUT PATH ***********
1 020 NPATHS = " "
1 022 I NPUT " N E W PAT H " ; NPATHS
1 024 I F NPATHS = "" THEN 1 024
1 026 PAT H I S = NPATHS
1 028 RETURN
, ********* CHANGE OUTPUT PATH ************
1 030 NPATHS = ""
1 032 I NPUT "NEW PAT H " ; NPATHS
1 034 I F NPATHS = "" THEN 1 034
1 036 PATHOS = NPATHS
1 038 RETURN
, ************ CHANGE SAMPLE RATE ************
1 040 NRATE = 0
1 042 I NPUT "NEW SAMPLE RAT E " ; NRATE
1 044 I F NRATE = 0 OR NRATE > 44 1 00 THEN 1 042
1 046 DSRl = NRAT E : TOO = Q I DSR1
1 048 RETURN
, ******** WE I GHT I NG FUNCT I ON **********
1 050 FOR I = 0 TO Q l
1 052 S( I ) = 5 ( 1 ) * ( S I N ( I * P I I Q ) A 6 )
1 054 NEXT I
1 056 RETURN
, ********* WE I GHT I NG ON/OFF ************
1 060 PR I NT "WE I GHT I NG ON ( YIN ) ? " ;
1 062 AS = I NKEYS: I F A S = .111 THEN 1 062
1 064 I F AS = "Y" OR AS = " y" THEN WT FLG = 1
1 066 I F AS = "N" OR AS = "n" THEN WT FLG = 0
1 068 I F AS <> "Y" AND AS <> " y" AND AS <> "N" AND AS <> "n" T HEN 1 060
1 070 RETURN
*******************
*** LOAD F I LE ***
*******************
1 1 00 CLS : NUFLG = 1
1 1 02 PR I NT " F I LENAME ( ' PAT H ' TO CHANGE PATH ) " ; PAT H I S ;
1 1 04 I NPUT NM I S
1 1 06 I F NM I S = "PATH" T H EN GOSUB 1 020 : GOTO 1 1 00
1 1 08 NM I S = PATH I S + NM I S
1 1 09 I F R I GHTS(NM I S , 4 ) < > " . WAV" THEN NM I S = N M I S + " . WAV"
1 1 10 OPEN "R" , #1 , NM I S , 1
1 1 12 F I ELD #1 , 1 AS DAS
1 1 14 QDF = LOF ( 1 ) : QF = QD F - 44
1 1 16 GET #1 , 23 ' GET NUMBER OF CHAN NELS
1 1 18 CHNLS = ASC( DAS ) : I F CHNLS = 2 THEN Q F = QF/2 ' ( MONO?STEREO?
238 Appendix 12.1
1 1 20 G E T #1 , 35
1 1 22 WRO S I Z = ASC(DAS ) : I F WRDS I Z = 16 THEN QF = Q F / 2' TWO BYTES
1 1 26 PR I NT Q F ; " AFTER WORD S I ZE & # CHANNELS"
1 1 28 GET #1 , 26 ' GET SAMPLE RATE H I GH BYTE
1 1 29 SR 1 = ASC(DAS)
1 1 30 SR1 = 256 * SR1 ' F I LE SIR
1 1 32 GET #1 , 25
1 1 34 SR1 = SR1 + ASC(DAS ) : DSR1 = SR1
1 1 36 CLOSE 1
1 1 38 STZ = 1
1 1 42 I F NUFLG = 0 THEN 1 1 52
1 1 44 NUFLG = 0
1 1 45 PR I NT " FREE MEMORY a ll ; FRE( - 1 )
1 1 46 PR I NT "8 * Q F = " ; 8 * Q F ; " PROCEED? " ;
1 1 48 A S = I NKEYS: I F A S = 1111 THEN 1 1 48
1 1 50 I F AS = "N" OR AS = " n " THEN 1 1 94
1 1 52 FOR N = 1 0 TO 1 3 ' F I ND S I ZE OF REQU I RED ARRAY
1 1 54 I F QF <= 2 A N THEN 1 1 58
1 1 56 NEXT N
1 1 57 REPROF = 0 : ' Q F = 2 A ( N )
1 1 58 I F AS = "Q" OR AS = " q " THEN N = 1 4
1 1 60 Q = 2AN : N 1 = N - 1 : Q1 = Q - 1 : Q 2 = Q/2 : Q3 = Q 2 - 1 : Q 4 = Q/4
1 1 61 Q5 = Q4 - 1 : Q8 = Q/8: Q9 = Q8 - 1 : Q34 = Q2 + Q4 : Q 1 6 = Q/ 1 6
1 1 62 RED I M C( Q ) , S ( Q )
1 1 64 TOO = Q / DSR 1 : K1 = P 2 / Q
1 1 66 CLOSE 1
TYPE Dat3
Amp3 AS I N TEGER
END TYPE
1 1 76 D I M WRD AS Dat3
1 1 80 I F WRD S I Z = 1 6 THEN GOSUB 1 300 ELSE GOSUB 1350
1 1 82 CLOSE #1
1 1 84 FOR I = 0 TO Q
1 1 86 C ( I ) = 0
1 1 88 NEXT I
1 1 90 I F WT FLG = 1 THEN GOSUB 1 050
1 1 94 CLS : GOSUB 2000
1 1 96 RETURN
I *******************************************
, * SAVE T I ME DOMA I N F I LE *
, *******************************************
1 200 PR I NT " SET GA I N ( Y/N ) ? "
1 202 A S = I NKEYS : I F A S = " " T H E N 1 202
1 204 I F AS = "Y" OR AS = " y" THEN GOSUB 1 270
1 2 1 0 HDR ( 7 ) = I N T « 2 * Q + 36) / 65536)
1 2 1 2 HDR(6) = I N T « ( 2 * Q + 36) - HDR ( 7 ) * 65536) / 256)
1 2 1 4 HD R ( 5 ) = I N T « 2 * Q + 36) - ( HD R ( 7 ) * 65536) - ( HD R ( 6 ) * 256»
1 2 1 6 HDR(43) = I N T « 2 * Q) / 65536)
1 2 1 8 HDR (42) = I NT « ( 2 * Q ) - ( HD R (43) * 65536» / 256)
1 220 HDR ( 4 1 ) = I NT « 2 * Q) - ( HD R (43) * 65536) - ( HDR(42) * 256»
1 222 HDR(26) = I NT ( DSR1 / 256 )
1 224 HDR ( 25 ) = I NT ( DSR1 - ( HD R ( 26 ) * 256» : DSR2 = 2 * DSR1
1 226 HDR ( 3 1 ) = I NT ( DSR2 / 65536 ) : DSR2 = DSR2 - HDR ( 3 1 ) * 65536
1 228 HDR (30) = I NT ( DSR2 / 256 ) : DSR2 = DSR2 - HDR(30) * 256
1 230 HDR ( 29 ) = I NT ( DSR2)
Playback Speedup 239
1 232 PR I NT : P R I NT " F I LENAME ( ' PAT H ' TO CHANGE PAT H ) "; PATHOS;
1 234 I NPUT NM2S
1 236 I F NM2S = "PATH" THEN GOSUB 1 03 0 : GOTO 1 232
1 238 NMS = PATHOS + NM2S
1 240 OPEN " R " , #2 , NMS, 2
1 242 F I E LD #2 , 2 AS DA2S
1 244 FOR I = 1 TO 43 STEP 2
1 246 HDR1 = HDR( I ) + HDR ( I + 1 ) * 256
1 248 I F HDR1 <32768 THEN LSET DA2S = MK I S ( HDR1 ) : GOTO 1 252
1 250 LSET DA2S = CHRS ( HDR ( I » +CHRS ( HDR ( I +1 »
1 252 PUT #2 , ( I + 1 ) / 2
1 254 NEXT I
1 256 FOR I = 0 TO Q - 1
1 258 LSET DA2S = MK I S ( I N T ( SK3 * S ( I » )
1 260 PUT #2 , ( I + 23 )
1 262 NEXT I
1 264 CLOSE #2
1 266 RETURN
1 270 ' ************ SET GA I N *************
1 272 I T ST = 0 : NGA I N = 0
1 274 FOR I = 0 TO Q - 1
1 276 I F ABS ( S ( I » > I TST THEN I TST = ABS ( S ( I »
1 278 NEXT I
1 280 P R I NT : P R I NT "THE PEAK VALUE I N THE ARRAY I S "; nST
1 282 PR I NT "MAX I UM GA I N = " ; ( 4096 / nST )
1 284 I NPUT "NEW GA I N = "; NGA I N
1 286 I F NGA I N = 0 THEN 1 282
1 288 SK3 = NGA I N
1 290 PR I NT "GA I N = " ; SK3
1 292 I NPUT "C/R TO CONT I NUE " ; AS
1 294 RETURN
1300 , ********* LOAD 16 B I T WORDS ************
1302 OPEN NM I S FOR RANDOM AS #1 LEN = LEN(WRD )
1303 STZ = CHNLS ' : I STOP = 22' ' STEPSI ZE = 2 FOR STEREO
1304 FOR I = 0 TO Q F - 1
1305 GET #1 , ( STZ * I ) + 45 , WRD ' LOCATE STZ* I +45 - - S0 WE STEP
1306 S ( I ) = WRD . Amp3 ' OVER 2 WORDS FOR STEREO
1307 I F CHNLS = 1 THEN 1 3 1 0 ' = MONAURAL
1308 GET #1 , ( STZ * I ) + 46 , WRD ' +46 FOR STEREO
1309 I F I > Q THEN 1 3 1 2 ELSE S( I ) = S ( I ) + WRD . Amp3
1310 NEXT I
1 3 1 2 RETURN
1350 ' ********* LOAD 8 B I T BYTES
1352 OPEN " R " , #1 , NM I S , 1
1 354 F I ELD #1 , 1 AS DAS
1356 FOR I = 0 TO Q F - 1 ' STEP STZ
1 358 GET #1 , ( STZ * I ) + 45
1 360 S( I ) = ASC(DAS) - 1 2 7
1 362 NEXT I
1 364 RETURN
, ***************************************
, * PLOT DATA ( T I ME )
, ***************************************
2000 I F REPOF = 1 THEN RETURN
2001 SCREEN 9 , 1 , 1 : COLOR 7, 1 : MAGX = 1 : XO = 10
240 Appendix 12.1
I DRAW DATA
2900 L I NE (XD, Y D ) - (XD, YO)
2901 FOR I = OFS TO (Q2 I MAGX + OFS)
2902 X5 = ( I - O F S ) * SKX 1 : Y5 = SKY1 * SQR ( C ( I ) A 2 + S( I ) A 2)
2904 L I NE - ( XO + X5 , YO - Y 5 )
2906 N E X T I
, *************************************
I * F I L TER ROUT I NES *
, *************************************
3000 CLS
3002 PR I NT "LOWPASS, BANDPASS OR NOTCH ( L/B/N ) "
30 1 0 AS = I NKEYS: I F A S = 1111 THEN 30 1 0
30 1 2 I F AS = " N " OR AS = " n " THEN 3 1 00 I NOTCH F I LTER
30 1 4 I F AS = "B" OR AS = "b" THEN 3 1 50 I BANDPASS
3016 I F AS <> " L " AND AS <> " l " THEN 3000 I I F NOT LOWPASS TRY AGA I N
3018 PR I NT " GAUSS I AN O R TRUNCATE ( Gi n " I WHAT K I ND O F LOWPASS?
3020 AS = I NKEYS : I F AS = "" T HEN 3020 I GET USER I NPUT
3022 I F AS = "T" OR AS = " t " T HEN 3200 I TRUNCATE?
3024 P R I NT : PR I NT "GAUSS I AN LOW PASS F I LTER ROUT I NE "
3026 I NPUT " I NPUT CUTOF F FREQUENCY " ; FC03
3028 FC03 = FC03 * TOO : FC032 = FC03 * FC03
3030 FOR I = 0 TO Q I 2 I F I LTER THE DATA
3032 KAT = EXP( - . 3453878 * I * I I FC032) I E I GEN VALUE COE F F I C I ENT
3034 C ( I ) = KAT * C ( I ) : S( I ) = KAT * S ( I )
3036 C ( Q - I ) = KAT * C ( Q - I ) : s e Q - I ) = KAT * s e Q - I )
3038 NEXT I
3040 RETURN
We load and save audio files with the routines between l ines
1 000-2000 (which takes up 3 � pages of code). There are two maj or
fonnats for audio: the * .VOC and *.WAV (where the * represents the file
name), but we use only the * . WAY. The file header is critical if these
files are to be used in other programs, and lines 1 8-24 read . WAY header
data into array HDR(44) [required when we save files later] .
We load files at lines 1 1 00- 1 200. A file name may be specified
or the current file may be re-loaded (line 100 1). Lines 1 1 02- 1 1 04 input the
file name [an option to change the path (lines 1 500- 1540) is provided-the
default is for C:\). The file is opened at line 1 1 1 0 and the size of the data
QF detennined by subtracting 44 header bytes from the Length Of File
(line 1 1 1 6). The *. WAY fonnat allows saving the data as either 16 bit
words or 8 bit bytes, which is indicated by the 35th byte of the header (line
1 1 20). This changes everything, of course, as we will see. The sample
rate is indicated by bytes 25 and 26 of the header (lines 1 1 28- 1 1 34). If we
are re-loading the current file (i.e., NUFLG = 0) we can skip down to
1 1 52. If the size of the file is too large to fit into the maximum array size,
there is no sense proceeding, and we can exit at line 1 1 50. [If the file was
created at 44, 1 00 samples /sec. but is too large to fit in the maximum array
size, we can cut it in half by specifying a sample rate of 22,050 and then
loading only every second sample-you must make a decision about
aliasing, of course.] If the file will "fit" we go on to select the next largest
2N file size 1 1 52-56, or if "Q" is input at line 1 1 48 we simply use the
largest file size possible. Lines 1 1 70-76 establish the type of data we
expect to read from the file. At 1 1 80 we jump to either the 1 6 bit word or
8 bit byte input routine and load the data into the data array. Lines 1 1 84-
88 insure the aft end of the array is zeros and at line 1 1 90 weighting the
data is allowed. At line 1 1 94 we display the loaded data, and then our file
is loaded and ready for work.
To save a file (lines 1 200- 1 3 00) we may want to change the
"gain" ( 1 200-subroutine at 1 270). Again, we can specify the name and
244 Appendix 12.1
path (lines 1 232-38); but first, we generate the header (lines 1 2 1 0-20) file
size infonnation. At 1 222-30 we enter the sample rate and at 1 240-54 we
save the header data.
The plot data routines have a couple of features that must be
pointed out: you can expand and offset the time domain display (2044-74)
by changing the variables MAGX (lines 2056-60), XO (lines 2070-74), and
re-plotting the data (the routines are located at lines 2700-2920). We may
exit the display routine via the Esc key.
The Plot Frequency routine allows expanding the display by
pressing "M" (2546), but you can also "shrink" the dIsplay by pressing "m."
Since expanding the display will move the upper frequencies out of view,
you can offset the display ("0" offsets + and " 0" offsets - in lines 2548).
The cursor can be moved across the data using the arrow keys (2544).
Left/right arrows move the cursor ± 1 hannonic and up/down arrows move
the cursor ± 1 0 (routines at lines 2590-2644).
Voice speedup is done in lines 400-470, but before we get there,
the data must occupy no more than 118 of the total array (you must control
sample rate, aliasing, etc.). As an exercise you may digitize a word (your
own voice) and break it into its major components (using the bandpass
filter routine). You then save the individual elements (e.g., as HAR0 1 .wav,
HAR02. wav, etc.), recall them one by one, change the playback rate, save
these new components, and then recombine them. To change playback rate
you must transfonn an element and use the cursor keys to find its center
frequency. You then select Fractional Stretch (lines 402-08), and enter the
component's center frequency (line 4 1 4). Select the speedup fraction
desired (menu 4 1 6-22) and the routine then does the fractional stretching
(see Ch. 1 2, pp. 1 5 1 - 1 55). You must then Inverse Transform this data
(note that the time domain data is automatically expanded to nominal half
array-see lines 432-36) and then save this stretched sound element. In
FFf1 2.03 (next appendix) the File Manager includes a routine for combin
ing two * . wav fi les. With a little effort you could recombine these
stretched fi les and hear yourself say the same thing faster and slower.
[Note: there's a * . wav file included on the software disk (THISF3C. WAV)
for the sound element we expanded in Chapter 1 2.]
APPENDIX 1 2.2
SYNTHESIZER
, *******************************************************
, * FORWARD P F F F T *
, *******************************************************
1 00 ' *** TRANSFORM STAGE 1 ***
1 08 T9 = T I MER
110 C(O) = ( S ( O ) + S ( Q2 » 1 2: C( 1 ) = ( S ( O ) - S(Q2» 1 2
112 FOR I = 1 TO Q3 : 1 2 = 2 * I : I NDX = 0
1 14 FOR J = 0 TO N 1 : I F I AND 2AJ T HEN I NDX = I NDX + 2A ( N . 2 - J )
1 16 NEXT J
1 18 C ( 1 2 ) = ( S ( I NDX)+S( I NDX+Q2» /2: C( 1 2+1 ) = ( S ( I ND X ) - S ( I NDX+ Q2» /2
1 20 NEXT I
1 22 FOR I = 0 TO Q1 : S( I ) = 0 : NEXT I
********* REMA I N I NG STAGES **********
1 24 FOR M = 1 TO N 1 : QP = 2 A M: QPI = 2 A ( N 1 - M )
1 26 FOR K = 0 T O Q P I - 1
1 28 FOR J = 0 TO QP/2 : JO = J + ( 2 *K *QP ) : J 1 = JO+QP : K2 = QP I *J*K1
130 J I = J1 - (2 * J )
132 CTEMP1 = C ( J O ) + C( J 1 ) * COS ( K2 ) - S ( J 1 ) * S I N ( K2 )
134 STEMP1 = S ( J O ) + C ( J 1 ) * S I N ( K2 ) + S ( J 1 ) * COS ( K 2 )
246 Appendix 12.2
1 36 CTEMP2 = C ( J O ) - C ( J 1 ) * COS(K2) + S ( J 1 ) * S I N ( K2 )
1 38 S ( J I ) = ( C ( J 1 ) * S I N ( K2 ) + S ( J 1 ) * COS ( K 2 ) - S ( J O » / 2
140 C ( J O ) = CTEMP 1 / 2 : S(JO) = STEMP 1 / 2 : C ( J I ) = CTEMP2/2
1 42 NEXT J
1 44 NEXT K
1 46 NEXT M
1 48 FOR J = Q2 + 1 TO Q1 : C ( J ) = 0 : S ( J ) = 0 : NEXT J
150 T9 = T I MER - T 9 : SK1 = 2
1 52 RETURN
, **** ************************************************
, * I NVERSE TRANS FORM *
, ****************************************************
200 PR I NT : T9 = T I MER : SK1 = 1 : QP = 2 A N 1
202 FOR M = N 1 T O 1 STEP - 1 ' LOOP F O R STAGES O F COMPUTAT I ON
204 QP2 = 2 A ( M ) : QP = I NT ( QP/2 ) : QP4 = 2*QP2: QPI = 2A ( N 1 - M)
206 FOR I = 0 TO Q - ( QP2) STEP QP4
208 FOR J = 0 TO QP : KI = J + I : KT = J * QPI * K 1 : KJ = QP2 + K I
212 MCT = C ( J+ I ) - C ( I +QP2 - J ) : MST = S ( J+ I ) + S ( I +QP2 - J )
214 CTEMP = MCT * COS ( KT ) + M S T * S I N ( K T )
216 STEMP = MST * COS ( K T ) - MCT * S I N ( K T )
218 CTEMP2 = ( 2 * C ( J + I » - CTEMP * COS ( K T ) + STEMP * S I N ( K T )
220 S ( K I ) = (2 * S ( J + I » - CTEMP * S I N ( K T ) - STEMP * COS ( K T )
222 C ( K J ) = CTEMP : S(KJ ) = STEMP : C ( K I ) = CTEMP2
224 NEXT J
226 NEXT I
228 NEXT M
229 ' ******** F I NAL STAGE ********
230 FOR I = 0 TO Q3 : 1 2 = 2 * I : I NDX = 0
232 FOR J = 0 TO N 1 : I F I AND 2AJ THEN I NDX = I NDX + 2A ( N - 2 - J )
234 NEXT J
236 S ( I NDX ) = C ( 1 2 ) + C ( 1 2 + 1 ) : S ( I NDX + Q2) = C ( 1 2 ) - C ( 1 2 + 1 )
238 NEXT I
240 FOR I = 0 TO Q1 : C ( I ) = 0 : NEXT I
242 T9 = T I ME R - T9
244 RETURN
I ************ **************************
600 ' *** FORWARD F F T ***
, ******** * ** ***************************
602 SK1 = 1 : K6 = 1 : KFB = 2
604 I F REPOF = 0 THEN CLS : LOCATE 1 2 , 20 : PR I NT "PLEASE WA I T "
620 GOSUB 1 00 ' DO P F F FT
621 LOCATE 1 2 , 20 : PR I NT " "
622 GOSUB 2500' PLOT DATA
624 RETURN
, ******* * **************************
700 ' ** * I NVERSE F F T ***
, * ******** *** **********************
710 K6 = - 1 : KFB = 1
71 2 I F REPOF = 0 THEN CLS : LOCATE 1 2 , 20 : PR I NT "PLEASE WA I T "
714 GOSUB 200' D O I NVERSE P F F FT
715 LOCATE 1 2 , 20 : PR I NT " "
716 GOSUB 2000 ' PLOT DATA
718 RETURN
Music Synthesis 247
*************************
800 ' *** D I SPLAY DATA MENU ***
*************************
802 CLS : PR I NT SPC ( 30 ) ; "D I SPLAY MENU " : PR I NT PR I NT
804 PR I NT SPC( S ) ; " 1 = D I SPLAY T I ME ( GRAPH I C ) "
806 PR I NT SPC( S ) ; " 2 = D I SPLAY FREQ ( GRAPH I C ) "
808 PR I NT SPC( S ) ; " 9 = E X I T "
81 0 A S = I NKEYS : I F A S = " " T H E N 8 1 0
81 2 I F ASC(AS) < 4 9 O R ASC(AS) > 57 T H E N 802
814 ON VAL (AS) GOSUB 2000, 2500 , 816, 816, 816, 816, 816, 816, 816
816 RETURN
, *** HAPPY END I NGS ***
990 CLOSE
992 RETURN
999 END
******************* *****
* F I LE MENU *
************* ***********
1 000 CLS : PR I NT SPC(30 ) ; " F I LE HANDLER MENU " : P R I N T : PR I NT
1 002 PR I NT SPC ( S ) ; "1 = RE LOAD F I LE 2 = SAVE F I L E "
1 004 P R I NT SPC( S ) ; "3 = CHANGE NAME & LOAD 4 = SUM I N 2ND F I LE "
1 006 PR I NT SPC ( S ) ; " 5 = CHANGE SAMPLE RATE
1 008 PR I NT SPC ( S ) ; "7 = WE I GHT I NG FUNCT I ON "
1 0 1 0 PR I NT SPC( S ) ; " 9 = E X I T "
1 0 1 2 A S = I NKEYS: I F A S = " " T H E N 1 0 1 2
1 0 1 4 ON VAL (AS) GOSUB 1 1 1 0 , 1 200 , 1 1 00 , 1 400 , 1 040 , 1 0 1 8 , 1 060 , 1 0 1 8 , 1 0 1 8
1 0 1 8 RETURN
1320 , ********* LOAD 2ND F I LE 16 B I T WORDS ******* * ****
1322 OPEN NM I S FOR RANDOM AS #1 LEN = L E N ( WRD )
1323 STZ = CHNLS ' : I STOP = 22' ' STEPSI ZE = 2 FOR STEREO
1324 FOR I = 0 TO Q F - 1
1 325 GET #1 , ( STZ * I ) + 4 5 , WRD ' LOCATE STZ* I +4S - - S0 WE STEP
1326 S ( I ) = S ( I ) + WRD . Aq)3 ' OVER 2 WORDS FOR STEREO
1 327 I F CHNLS = 1 THEN 1 330 ' = MONAURAL
1 328 GET #1 , ( STZ * I ) + 46, WRD ' SAME EXCEPT +46 FOR STEREO
1 329 I F I > Q THEN 1 332 ELSE S ( I ) = S ( I ) + WRD . Aq)3
1 330 NEXT I
1 332 RETURN
1 370 , ********* LOAD 2ND F I LE 8 B I T BYTES
1 372 OPEN " R " , #1 , NM I S , 1
1 374 F I ELD #1 , 1 AS DAS
1 376 FOR I = 0 TO Q F - 1 ' STEP STZ
1 378 GET #1 , ( STZ * I ) + 45
1 380 S( I ) = S( I ) + ASC(DAS) - 1 2 7
1 382 NEXT I
1 384 RETURN
, ***************** **********
,* SUM TWO F I LES TOGETHER *
, ***************************
, ASSUMES F I RST F I LE I S I N S ( I )
1 400 CLS : NUFLG = 1
1 402 PR I NT " F I LENAME ( ' PATH ' TO CHANGE PAT H ) " ; PAT H I S ;
1 404 I NPUT NM I S
1 406 I F NM I S = "PATH" THEN GOSUB 1 020 : GOTO 1 402
248 Appendix 12.2
1 408 N M I S = PAT H I S + NM I S
1410 OPEN "R" , #1 , NM I S , 1
1412 F I ELD #1 , 1 AS DAS
1416 GET #1 , 23 ' GET NUMBER OF CHANNELS
1418 CHNLS = ASC(DAS) ' SET # CHANNELS ( I . E . MONO/STEREO)
1 420 GET #1 , 35
1 422 WRD S I Z = ASC(DAS)
1 426 PR I N T QF; " AFTER WORD S I ZE"
1 428 GET #1 , 26 ' GET SAMPLE RATE H I GH BYTE
1 430 SR1 = ASC(DAS)
1 432 SR1 = 256 * SR1 ' F I LE SIR
1 434 GET #1 , 25
1 436 SR1 = SR1 + ASC(DAS)
1 438 CLOSE 1
1 440 STZ = 1
1 442 I F NUFLG = 0 THEN 1 450
1 444 NUFLG = 0
1 446 TOO = Q / DSR 1 : K1 = P2 / Q
1 450 I F WRDS I Z = 16 THEN GOSUB 1 320 ELSE GOSUB 1 370
1 452 CLOSE #1
1 454 CLS : GOSUB 2000
1 456 I F WT FLG = 1 THEN GOSUB 1 050
1 458 RETURN
***********************************
4000 ' *** SETUP MUS I C SYNTHES I ZER ***
***********************************
4002 CLS : PR I NT "TH I S ROUT I NE W I L L SAVE THE HARMON I CS SELECTED I N "
4004 PR I NT "THE ARRAYS CD ( X ) AND SD ( X ) T O B E USED LATER I N THE"
4005 PR I NT "SYNT HES I S O F A SON G . T H I S ALGOR I T HM WILL SERVE TO"
4006 PR I NT "DEMONSTRATE THE BAS I C PR I NC I PLE I NVOLVED IN A H I GH "
4008 PR I NT "QUAL I TY SYNTHE S I ZER . I NPUT BAS I C I NSTRUMENT SOUND"
4010 I NPUT "ELEMENTS BANDW I D T H FOR ALL ELEMENTS"; BAND
4012 BAND = C I N T ( BAND * TOO)
40 1 4 FOR HAR = 0 TO 10 ' DO FOR 10 HARMON I CS
40 1 6 PR I N T " F REQUENCY CFO FOR ELEMENT"; HAR;
40 1 8 I NPUT C FO
4020 C FO = C I NT ( C FO * TOO ) : C F 1 = C F O : OFSET = 2 * HAR
4022 FOR TF = C F 1 - BAND TO C F 1 + BAND ' TRANSFER TO DATA ARRAY
4024 CD ( T F - C F 1 + ( 1 + OFSET ) * BAND ) = C ( T F )
4026 SD ( T F - C F 1 + ( 1 + OFSE T ) * BAN D ) = S ( T F )
4028 NEXT T F
4030 CD( OFSET * BAND ) = C F 1 ' SAVE HARMON I C FREQUENCY
4032 NEXT HAR ' REBU I LD THE P I ANO BE FORE WE EX I T !
S H I F T HARMON I CS TO DESI RED NOTE
4034 FOR I = 0 TO Q: C ( I ) = 0: S ( I ) = 0: NEX T ' ZERO ARRAY
4036 FOR HAR = 0 TO 1 0 : OFSET = 2 * ( HAR )
4038 CF = CD ( O FSET * BAND )
4040 FSHO = C I NT ( C F * « 1 . 059463 1 # A NOT E ) - 1 »
4042 ' I F HAR = 0 THEN FSHO = 0 ' DON ' T SH I F T THE BOX
4044 FOR I = C F - BAND + 1 TO CF + BAND - 1
4046 C ( I + FSHO) = CD ( I - CF + BAND * ( 1 + OFSE T »
4048 S ( I + F S H O ) = SD ( I - C F + BAND * ( 1 + OFSET »
4050 NEXT I
4052 NEXT HAR
4054 RE TURN
Music Synthesis 249
************************************* ** ***
* SYNTHES I ZE A SONG ( 0 1 / 1 2/03 ) *
******************************************
T H I S ROUT I NE ASSUMES THE F I LE " P IANO . WAV" HAS BEEN LOADED
AND THE HARMON I CS SAVED I N THE CD ( I ) AND SD ( I ) F I LE S ) . I T
ALSO ASSUMES THE NOTE I S "A" . T H I S ROUT I NE CALCULATES
THE NOTE AND I TS DURAT I ON AND APPENDS IT TO F I LE "SONGNMS"
UNT I L THE "END OF SONG" CODE ( I . E . , "Z" ) IS I NPUT .
4080 REPOF = 1 : CLS : PR I NT : PR I N T " I N PUT SONG? ( Y/N ) " ;
4084 AS = I NKEYS: I F AS = " " THEN 4084
4086 I F AS = "Y" OR AS = " y " THEN 6000
4088 RETURN
**** SYNTHES I ZER ****
6000 SCREEN 9, 1 , 1 : CLS : F I LOC = 23
6002 PR I NT "ENTER F I LENAME FOR SONG ( PATH TO CHANGE PATH ) " ; PATHOS;
6004 I NPUT SONGNMS : I F SONGNMS = "PATH" THEN GOSUB 1 03 0 : GOTO 6002
6006 GOSUB 6200 ' OPEN F I LE AND WR I TE HEADER
6008 CLS : COLOR 1 5 , 1 : I SG = 1 : FFO = 469 . 693
60 1 2 LOCATE 1 , 1 : PR I NT SONGNMS; " NOT E " ; I SG
60 1 6 LOCATE 3, 1 : I NPUT "ENTER NOTE AS A, A#, Bb, E TC . " ; AS
6020 A = ASC(AS ) : A1 = ASC ( R I G HTS(AS, 1 »
6021 I F A = 1 1 1 THEN 08 = - 1 : GOTO 60 1 6 ' S H I FT DOWN AN OCTAVE
6022 I F A > 71 THEN 6 1 00 ' CLOSE F I LE
6024 I F A < 65 THEN 60 1 2
6026 LOCATE 4 , 1
6028 I NPUT "ENTER DURAT I ON AS 1 / 2 NOTE = 2, 1 /4 = 4, ETC . ) " ; B 1 9
6030 B = Q / B 1 9 ' : PR I NT " 1 / " ; B 1 9;
6034 ' MULT BY 2 FOR FULL STEPS, EXCEPT AT B - C AND E - F
6036 A = ( A - 65 ) * 2 : I F A > 3 THEN A = A - 1 : I F A > 7 THEN A = A -
6038 I F A > 7 OR 08 = - 1 THEN A = A - 1 2 : 08 = 0
6040 I F A1 = 35 THEN A = A + 1 ' SHARP?
6041 I F A1 = 98 THEN A = A . l ' FLAT?
6042 NOTE = A
6044 GOSUB 4034 ' PRODUCE NOTE
6046 LOCATE 20, 30 : P R I NT " I NVERSE TRANS FORM"
6048 GOSUB 700 ' I NVERSE TRANSFORM
6049 I F B > Q THEN BREM = B - Q : B = Q
6050 FOR I = 0 TO B
6052 LSET DA2S = MK I S( C I NT ( SK3 * S ( I » )
6054 PUT #2 , ( I + F I LOC)
6056 NEXT I
6058 F I LOC = F I LOC + B + 1
6059 I F BREM > 0 THEN GOSUB 6080
6060 LOCATE 20, 3 D : PR I NT " "
6062 I SG = I SG + 1
"
6064 LOCATE 3, 1 : PR I NT "
6066 LOCATE 4, 1 : PR I NT " "
6068 LOCATE I SG , 68: I F I SG > 22 THEN 6070
6069 GOSUB 6072 ELSE LOCATE 24 , 1 : PR I NT : LOCATE 22, 68 : GOSUB 6072
6070 GOTO 60 1 2
6072 PR I NT AS; " , 1 / " ; B 1 9 : RETURN
6080 LSET DA2S = MK I S ( 1 024)
6082 FOR I = 0 TO BREM : PUT #2, ( I + F I LOC) : NEXT
6084 F I LOC = F I LOC + BREM + 1 : BREM = 0
6086 RETURN
250 Appendix 1 2.2
**********************************************
**** UPDATE HEADER DATA & CLOSE F I LE ****
*************** *******************************
6 1 00 F I LC = 2 * ( F I LOC - 23 ) + 36
61 02 HDR ( 8 ) I NT ( F I LC/1 67772 1 6 ) : F I LC = ( F I LC - ( HDR ( 8 ) * 1 67772 1 6 »
6 1 04 HDR ( 7) = I NT ( F I LC/65536 ) : F I LC = F I LC - ( HDR( 7)*65536)
61 06 HDR(6) = I NT ( F I LC/256 ) : F I LC = F I LC - ( HD R ( 6)*256)
6 1 08 HDR ( 5 ) = I NT ( F I LC ) : F I LC = 2 * ( F I LOC - 23)
61 1 2 HDR ( 44 ) = I NT ( F I LC/1 67772 1 6 ) : F I L C = F I L C - ( HD R ( 44 ) * 1 67772 1 6 )
61 1 4 HDR (43) = I NT ( F I L C / 65536) : F I LC = F I LC - ( HD R ( 43 ) * 65536)
61 1 6 HDR(42) = I NT ( F I LC / 256) : F I LC = F I LC - ( HDR(42) * 256)
61 1 8 HDR(41 ) = I NT ( F I LC )
6 1 20 HDR ( 26 ) = I NT ( DSR1 / 256)
6 1 22 HDR ( 2 5 ) = I NT ( DSR1 - HDR(26 ) * 256)
6 1 26 DSR2 = 2 * DSR1
61 28 HDR(31 ) = I NT ( DSR2 / 65536 ) : DSR2 = DSR2 - HDR ( 3 1 ) * 65536
6 1 30 HDR(30) = I NT ( DSR2 / 256 ) : DSR2 = DSR2 - HDR (30) * 256
6 1 32 HDR(29) = I N T (DSR2)
6 1 40 FOR I = 1 TO 43 STEP 2
6 1 42 LSET DA2$ = CHR$( HDR ( I » + CHR$( HDR ( I + 1 »
6144 PUT #2 , ( I + 1 ) / 2
6146 NEXT I
6148 CLOSE #2
6150 REPOF = 0 : RETURN
*************************************
** OPEN SONG F I LE & WR I TE HEADER *
*************************************
6200 NM$ = PATHO$ + SONGNM$
6202 OPEN " R " , #2 , NM$, 2
6204 F I E LD #2 , 2 AS DA2$
6206 FOR I = 1 TO 43 STEP 2
6208 LSET DA2$ = CHR$( HDR ( I » + CHR$( HDR ( I + 1 »
62 1 0 PUT #2 , ( I + 1 ) / 2
62 1 2 NEXT I
62 1 4 RETURN
**********************************
*** F I N E L I NE SPECTRUM ***
**********************************
7000 CLS : PR I NT "CNTR FREQ, BNDWD T H , STEP 1/N ( E . G . 350 , 3 0 , 8 ) "
7002 I NPUT " " ; C F , BAND , F FRAC
7004 CLS : SCREEN 9, 1 , 1 : COLOR 7, 1
7006 YO = 300 : SKY2=SKY 1 / 2 : C F=C I NT ( C F*TOO ) : BAND = C I NT ( BAND*TOO)
7008 SKX1 = 600 / (2 * BAND ) : XO = 1 0 ' SET X SCALE FACTOR
70 1 0 GOSUB 5 1 00 ' SET D I SPLAY SCALE FACTOR = 200 P I X LS/SKY1
70 1 2 L I NE (XO, yO - 200 ) - (XO, YO ) ' DRAW ORD I NATE & ABSCI SSA
70 1 4 L I NE (XO, Y O ) - (600 + XO, YO )
70 1 6 L I NE ( XO + ( BAND * SKX1 ) , YO - 200 ) - ( XO + ( BAND * SKX 1 ) , YO)
701 8 L I N E (XO + SKX1 * BAND , yO + 5 ) - ( XO + SKX1 * BAND , YO - 200)
7020 L I NE (XO, Y O ) - ( X O , YO)
7022 LOCATE 20, 37: PR I NT C F / TOO
7024 LOCATE 20 , 1 : PR I N T ( C F - BAND ) / TOO
7026 LOCATE 20, 70 : PR I NT ( C F + BAND ) / TOO
7028 D E L F = 1 / F FRAC : X5 = 0 : Y5 = 0 : YMAX = 0
7030 COLOR 1 5 , 1
7032 FOR F = CF - BAND TO CF + BAND STEP D E L F
Music Synthesis 25t
The core 2-D FFT is discussed in the text. We will cover the
associated routines required to display 2-D functions here. The routines to
generate 2-D functions are discussed in the next appendix.
*****************************************
******* *******
2D F F T 1 3 . 00 - 2D XFORM
*****************************************
I **********************************************
, * PLOT DATA *
, ************** ** ********** * ******* * ** *********
1 76 CLS : AMP1 = 0 ' F I ND LARGEST MAGN I TUDE I N ARRAY
1 78 FOR I = 0 TO Q - 1
1 80 FOR J = 0 TO Q - 1
1 82 I F XD I R = 0 THEN AMP = C ( I , J ) : GOTO 1 86
1 84 AMP = SQR ( C( I , J ) A 2 + S ( I , J ) A 2 )
1 86 I F AMP > AMP1 T H E N AMP1 = AMP
1 88 NEXT J
1 90 NEXT I
1 92 MAG2 = - 1 30 / AMP1 ' SET SCALE FACTOR
1 94 GOSUB 6000 ' PLOT 2 - D DATA
1 96 LOCATE 1 , 1 : PR I NT " T I ME = "; T9
1 98 RETURN
***********************************
* THE ROUT I NE BELOW I S FOR A ROW *
*********** ************************
210 FOR M = 0 TO N 1 : QT = 2 A (N - M ) ' DO N STAGES
212 QT2 = QT / 2: QT3 = QT2 - 1 : KT = 0
214 FOR J = 0 TO Q1 STEP QT : KT2 = KT + l ' DO ALL FREQUENCY SETS
216 FOR I = 0 TO QT3 : J1 = I + J : K = J1 + QT2' DO FREQUENC I ES IN SET
, ROW BUTTERFLY
218 CTEMP = ( C ( KR , J 1 ) + C ( KR , K )*KC ( K T ) - K6*S ( K R , K ) *KS ( K T » /SK1
220 STEMP = ( S ( KR , J 1 ) + K6*C ( KR , K ) * KS ( K T ) + S ( KR , K ) * KC ( K T » /SK1
222 CTEMP2 = ( C ( KR , J 1 ) + C ( KR , K ) *KC ( KT 2 ) - K6*S ( KR , K ) * KS(KT2» /SK1
224 S ( KR , K ) = ( S ( KR , J 1 ) + K6*C ( KR , K )*KS ( KT 2 ) + S ( KR , K )*KC ( KT 2 » /SK1
226 C ( K R , K ) = CTEMP2: C ( K R , J 1 ) = CTEMP : S ( K R , J 1 ) = STEMP
228 NEXT I ' ROTATE AND SUM NEXT PA I R O F COMPONENTS
230 KT = KT + 2
232 NEXT J ' DO NEXT SET OF FREQUENC I ES
234 NEXT M ' DO NEXT STAGE
, B I T REVERSAL FOR ROW TRANSFORMS
236 FOR I = 1 TO Q1 : I NDX = 0
238 FOR J = 0 TO N 1
240 I F I AND 2 A J THEN I NDX = I NDX + 2 A ( N 1 - J )
242 NEXT J
244 I F I NDX>I THEN SWAP C ( KR , I ) , C ( KR , I NDX ) : SWAP S ( KR , I ) , S ( KR , I NDX)
246 NEXT I
248 ' I F XD I R = 0 THEN GOSUB 400
250 NEXT KR
252 T9 = T I ME R - T9: GOSUB 1 76' USE TO SHOW RESULTS OF ROW X FORMS
254 AS = I NKEYS : I F AS = "" THEN 254
256 CLS : T9 = T I MER - T9: RETURN ' ROW TRANSFORMS DONE
The 2-D FFT 255
*************************************
* THE ROUT I NE BELOW IS FOR COLUMNS *
*************************************
300 KRTST = 1 9
302 FOR KR = 0 T O Q 1 ' XFORM 2 0 ARRAY B Y COLUMNS
304 ' I F XD I R = 1 THEN GOSUB 4 1 0
306 PR I NT US I NG "###_ " ; KR;
308 I F KR = KRTST THEN PR I NT : KRTST = KRTST + 20
310 FOR M = 0 TO N 1 : QT = 2 A ( N - M)
312 QT2 = QT / 2: QT3 = QT2 - 1 : KT = 0
314 FOR J = 0 TO Q1 STEP QT : KT2 = KT + 1
316 FOR I = 0 TO QT3: J 1 = I + J : K = J 1 + QT2
' COLUMN BUTTERFLYS
318 CTEMP = ( C ( J 1 , KR ) + C ( K , K R ) * KC( K T ) - K6* S ( K , KR ) * KS( KT » /SK1
320 STEMP = ( S ( J 1 , K R ) + K6* C ( K , K R ) * KS(KT ) + S ( K , K R ) * KC(KT » /SK1
322 CTEMP2 = ( C ( J 1 , KR ) + C ( K , K R ) *KC( KT2 ) - K6* S ( K , KR ) * KS( KT2 » /SK1
324 S ( K , K R ) = ( S ( J 1 , KR ) + K6* C ( K , K R ) * KS( KT 2 ) + S ( K , KR ) * KC(KT2» /SK1
326 C(K, KR ) = CTEMP2 : C ( J 1 , KR ) = CTEMP : S ( J 1 , KR ) = STEMP
328 NEXT I
330 KT = KT + 2
332 NEXT J
334 NEXT M
, ***** ************* **** ***************
, *
B I T REVERSAL FOR COLUMN TRANSFORMS *
, *************************************
336 FOR I = 1 TO Q1 : I NDX = 0
338 FOR J = 0 TO N 1
340 I F I AND 2 A J T H E N I NDX = I NDX + 2 A ( N 1 - J )
342 NEXT J
344 I F I NDX> I THEN SWAP C( I , KR ) , C ( I NDX , KR ) : SWAP S ( I , KR ) , S ( I NDX , KR )
346 NEXT I
348 ' I F XD I R = 0 THEN GOSUB 4 1 0
350 NEXT KR
352 I F K6 = 1 THEN XD I R = 1
354 T9 = T I MER - T9: GOSUB 1 76 ' USE TO SHOW RESULTS OF COLUMN X FORMS
356 I F K6 = 1 THEN XD I R = 0
358 AS = I NKEYS : I F AS = "" THEN 358
360 CLS : T9 = T I MER - T9: RETUR N ' COLUMN TRANSFORMS DONE
I ****************************************
, * *
MOD I FY ROW SAMP L I NG
I ****************************************
, ****************************************
, * *
MOD I FY COLUMN SAMPL I NG
, ****************************************
4 1 0 FOR I = 1 TO Q1 STEP 2
4 1 2 C( I , KR ) = - C ( I , KR ) : S C I , KR ) = - S C I , KR )
4 1 4 NEXT I
4 1 6 RETURN
256 Appendix 13.1
, *********************************
, * GENERATE FUNCT I ON S *
, *********************************
5000 XD I R = 0
5001 CLS : PR I NT : PR I NT : PR I NT " FUNCT I ON MENU" : PR I NT
5002 PR I NT " 1 = GENERATE S I NCA2 FUNCT I ON 2 = S I NC FUNCT I ON " : PR I NT
5009 PR I NT " 9 = EX I T : " : PR I NT
5010 PR I NT " MAKE SELECT I ON " ;
5012 A S = I NKEYS : I F A S = " " T H E N 5 0 1 2
5014 A = VAL ( AS ) : O N A GOTO 5030, 5500
5016 I F A = 9 THEN RETURN
5018 GOTO 5000
, *********************************
, * S I NCA2 FUN CT I ON *
I *********************************
5030 CLS
5032 I NPUT "W I DT H " ; WDTH1 ' I NPUT F I NCT I ON S I ZE
5034 I F WDTH1 = 0 THEN WDTH1 = 1 ' ZERO I NVAL I D
5036 SKL1 = P I 2 I WDT H 1 : MAG1 = Q ' CONSTANTS
5038 FOR I = 0 TO Q - 1 '
5040 YARG = SKL1 * ( I - Q2 ) : PR I NT "*" ;
5042 FOR J = 0 TO Q . 1
5 044 XARG = SKL1 * ( J • Q2)
5 046 I F YARG = 0 AND XARG = 0 THEN C ( I , J) = MAG1 : GOTO 5052
5 048 ARG = SQR(XARG A 2 + YARG A 2 )
5050 C ( I , J ) = MAG1 * ( S I N (ARG ) I ARG) A 2 : S ( I , J ) = 0
5052 NEXT J
5054 NEXT I
5055 MAG2 = - 1 30 I MAG1
5056 GOSUB 6000 ' PLOT FUNCT I ON
5058 PR I NT "ANY KEY TO CON T I NUE " ;
5060 A S = I NKEYS : I F A S = " " THEN 5060 ' WA I T
5080 RETURN
400 FOR I = 0 TO Q3
402 FOR J = 0 TO Q3
404 1 2 = I + Q2 : J2 = J + Q2
406 SWAP C ( 1 2 , J 2 ) , C ( I , J ) : SWAP S ( 1 2 , J 2 ) , S ( I , J )
410 NEXT J
412 NEXT I
420 FOR I = 0 TO Q3
422 FOR J = Q2 TO Q - 1
424 1 2 = I + Q2 : J2 = J - Q2
426 SWAP C ( 1 2 , J 2 ) , C ( I , J ) : SWAP S ( 1 2 , J 2 ) , S ( I , J )
430 NEXT J
432 NEXT I
434 RETURN
, *********************************
, * S I NC"2 FUNCT I ON *
, *********************************
5030 CLS : T1 = 0 : T O = 1
5032 I NPUT "WIDTH"; WDTH1 ' I NPUT F I NCT I ON S I ZE
5 034 IF WDTH1 = 0 THEN WDTH1 = 1 ' ZERO I NVAL I D
5 036 SKL1 = P I 2 I WDT H 1 : MAG1 = Q ' CALC CONSTANTS
5038 FOR I = 0 TO Q - 1 '
5040 YARG = SKL 1 * ( I - Q2 ) : PR I NT " * " ;
5042 FOR J = 0 T O Q - 1
5044 XARG = SKL1 * ( J - Q2)
5046 I F YARG = 0 AND XARG = 0 THEN C( I , J ) = MAG1 : GOTO 5052
260 Appendix 14.1
, *********************************
I * BESSEL FUNCT I ON *
I *********************************
5600 CLS : o E FoBL o - K
5604 I NPUT " W I D T H " ; WDTH1
5606 I F WD T H 1 < 1 THEN 5604 I M I N I MUM W I D T H
5608 S K L 1 = P I / ( 3 . 6 * WDTH1 * Q / 64)
561 0 FOR I = 0 TO Q - 1
561 2 YARG = SKL1 * ( I - Q2 ) : PR I NT " * " ;
5614 FOR J = 0 T O Q - 1
5616 XARG = SKL1 * ( J - Q2 )
5618 KARG = SQR( XARG � 2 + YARG � 2 )
5620 K A = 1 : K B = 1 : oAT 1 = 1 : KTGL = 1
5622 FOR K = 2 TO 900 STEP 2
5624 KTGL = - 1 * KTGL
5626 KA = KA * K: KB = KB * ( K + 2 ) : oENOM = KA * KB
5628 oAT2 = KTGL * ( WD T H 1 � (K / 2 ) * KARG � K / oENOM )
5630 I F ABS(oAT 2 ) < ABS(oAT 1 ) * 1 E - 1 0 THEN 5640
5632 oAT 1 = oAT 1 + oAT2
The 2-D Functions 261
5634 I PR I NT OAT 1 ,
5636 NEXT K
5638 PR I N T n# n
5640 C( I , J ) = OAT 1 : S ( I , J ) = 0
5642 NEXT J
5644 NEXT I
5646 GOSUB 1 76
5648 I NPUT AS
5650 RETURN
routine [you might want to try that variation in your experiments]. You
should note the difference between Fig. 14. 14 and Fig. 14. 1 1 in the text.
Finally, we can generate the proper image domain function by
using the inverse of the above process. We generate a circular function
in the frequency domain and inverse transform (the Bessel III routine).
l
In Linear Systems, Fourier Transforms, and Optics, lack 0 Gaskill, lohn
Wiley & Sons. So far as I know this is the standard graduate-level text in this field
APPENDIX 15.1
5402 CLS
5404 I NPUT " I NPUT SL I T W I D T H , LENGTH ( I . E . TO 2 , 20 ) " ; DELX , D E LY
5406 MAG1 = Q A 2: ANM = MAG1 * ANP : AN = 0
5408 FOR I = 0 TO Q - 1
54 1 0
FOR J = 0 TO Q - 1
5412 C ( I , J ) = AN : S ( I , J ) = AN
5414 NEXT J
541 5NEXT I
5416 I F DELX = 0 THEN 5450 ' HEXAGON APERTURE
5417 I F DELY = 0 THEN 5468 ' OCTAGON APERTURE
5418 I F DELX > Q2 OR DELY > Q2 THEN 5425
5420 FOR I = -DELX TO (DELX - 1 )
5421 FOR J = -DELY TO (DELY - 1 )
5422 C ( Q2 - I , Q2 + J ) = MAG1
5423 NEXT J
5424 NEXT I
5425 ' C ( O , 0) = MAG1
5426 MAG2 = - 1 20 I Q A 2
5428 GOSUB 1 76 ' D I SPLAY HAND I WORK
5430 I NPUT "C/R TO CON T I NUE " ; AS
5432 RETURN
, ******* *******
HEX APERTURE
5450 FOR I = - 6 TO 6: A = - C I NT(ABS( I ) * . 6285 - 6 . 8667)
5452 FOR J = - ( 1 + A) TO A
5454 C ( Q2 - I , Q2 + J ) = MAG1
5456 NEXT J
5458 NEXT I
5464 GOTO 5426
, ********** *******
OCTAGON APERTURE
5468 FOR I = 4 TO 8
5470 FOR J = - I TO I - 1 : C ( Q2 - 1 2 + I , Q2 + J ) = MAG1 : NEXT J
5472 NEXT I
5474 FOR I = 9 TO 1 4
5476 FOR J = - 8 T O 7 : C ( Q2 - 1 2 + I , Q 2 + J ) = MAG1 : NEXT J
5478 NEXT I
5480 FOR I = 8 TO 4 STEP - 1
5482 FOR J = - I TO I - 1 : C ( Q2 + 1 1 - I , Q2 + J ) = MAG 1 : NEXT J
5484 NEXT I
5486 GOTO 5426
I * *******************************************************
, * PR I NT OUTPUT
I ********* ***********************************************
500 CLS : PR I NT "ONLY DATA ABOUT THE 0 , 0 PO I NT \J I L L BE PR I NTED"
I ********************************************************
540 CLS : PR I NT " ONLY THE PHASES OF THE HARMON I CS ARE PR I NTED " :
PR I NT ' P R I N T SCREEN
542 FOR I = Q2 - 3 TO Q2 + 3
544 FOR ROLOC = Q2 - 3 TO Q2 + 3
546 Y2 = C ( ROLOC , I ) : X2 = S ( ROLOC , I ) : MAG = SQR ( Y2 A 2 + X2 A 2 )
548 ANG = 1 8 0 / P I * ATN ( X2 / Y2 ) : I F Y 2 < 0 THEN ANG = ANG + 1 80
550 PR I N T US I NG "### . ###- "; ANG;
552 NEXT ROLOC
554 PR I N T : NEXT
556 I NPUT AS
558 RETURN
This Print Routine is the one used to generate the tables ofp. 180
(Chapter 1 5 , section 1 5 . 6), and will display only the angle of the
transformed harmonics (for ± 3 rows and ± 3 columns about the 0, 0
location).
APPENDIX 16.1
IMAGE ENHANCEMENT
*****************************************
******* 2D F F T 1 6 . 00 - 20 X FORM *******
*****************************************
10 SCREEN 9, 1 , 1 : COLOR 1 5 , 1 : CLS
1 4 I NPUT "SELECT ARRAY S I ZE AS 2"N . N = " ; N
16 N1 = N - 1 : Q = 2 " N : Q1 = Q - 1
18 ' $DYNAM I C
20 D I M C ( Q , Q ) , S ( Q , Q ) , KC( Q ) , KS( Q ) , DAC ( Q , Q ) , DAS ( Q , Q )
2 2 TYPE P i x L
P I X AS I NTEGER
END TYPE
28 D I M P I XREC AS P i x L
3D Q2 = Q I 2: Q3 = Q2 - 1 : Q4 = Q I 4 : Q5 = Q4 - 1 : Q8 = Q I 8
32 PI = 3 . 1 4 1 592653589793#: P I 2 = 2 * P I : K1 = P I 2 I Q : CLVK = 1
I **** TWIDDLE FACTOR TABLE GENERAT I ON ****
40 FOR I = 0 TO Q: KC( I ) = COS ( K 1 * I ) : KS( I ) = S I N ( K 1 * I )
42 I F ABS(KC( I » < . 0000005 THEN KC( I ) = 0 I CLEANUP TABLE
44 I F ABS ( KS ( I » < . 0000005 THEN KS( I ) = 0
46 NEXT I
48 FOR I = 1 TO Q1 : I NDX = 0
50 FOR J = 0 TO N 1
52 I F I AND 2 " J THEN I NDX = I NDX + 2 " ( N 1 - J )
5 4 NEXT J
56 I F I NDX > I THEN SWAP KC( I ) , KC( I NDX ) : SWAP KS( I ) , KS ( I ND X )
5 8 NEXT I
I ***************
70 CLS : PR I NT : PR I N T : PR I NT " MA I N MENU" : PR I NT
72 PR I NT 1 = LOAD . BMP F I LE " : PR I NT
74 PR I NT 2 = TRANSFORM FUNCT I ON " : PR I N T
76 PR I NT 3 = I NVERSE TRANSFORM " : P R I N T
78 PR I NT 4 = MOO I FY TRANSFORM " : PR I N T
80 PR I NT 5 = SAVE F I LE " : P R I NT
84 PR I NT 6 = GENERATE FUNCT I ONS ": PR I NT
86 PR I NT 7 = MOO I FY SPACE DOMA I N FUNCT I ON " : PR I N T
88 PR I NT 9 = EX I T " : P R I N T : PR I NT
90 PR I NT MAKE SELECT I ON " ;
9 2 A S = I NKEYS: I F A S = " " THEN 92
94 A = VAL ( AS ) : ON A GOSUB 4000, 1 00 , 1 5 0 , 3000 , 4 1 00 , 5000 , 2400
96 I F A = 9 THEN 9999
98 GOTO 70
I ********** X FORM FUNCT I ON GOES HERE *************
2400 I *** MOO I F Y SPACE DOMA I N FUNC T I ON ***
2402 CLS : PR I N T : P R I N T SPC( 1 5 ) ; "MENU"
2404 P R I N T : PR I NT "1 = CHANGE GREY SCALE RANGE"
2406 P R I N T : P R I N T " 2 = MOO I FY CON TRAST"
2408 AS = I NKEYS: I F AS = " " THEN 2408
241 0 ON VAL (AS) GOSUB 2420, 2460
241 2 RETURN
I *** L I M I T GREY SCALE RESOLUT I ON ***
2420 CLS
2422 A = 1 : B = 0 I F I RST F I ND RANGE OF DATA
2424 FOR I = 0 TO Q - 1
2426 FOR J = 0 TO Q - 1
266 Appendix 16.1
I *********************************
3000 CLS : PR I NT : PR I NT "MOO I FY SPECTRUM MENU " : PR I NT PR I NT
3002 PR I NT " 1 - MUL T I PL Y W I T H STOREO SPECTRUM"
3004 P R I NT " 2 - O I VIOE CURRENT/STOREO SPECTRUMS"
3006 P R I N T " 3 - F I LTER SPECTRUM"
3008 P R I NT " 4 - TRUNCATE SPECTRUM"
30 1 0 PR I NT " 5 - SAVE SPECTRUM"
30 14 PR I NT " 9 - EX I T " : P R I N T : PR I NT
30 1 8 PR I NT " MAKE SELECT I ON "
3020 A S = I NKEYS: I F A S = '"' T HEN 3020
3022 I F ASC(AS) < 49 OR ASC(AS) > 57 T HEN 3000
3024 A = VAL ( AS ) : ON A GOSUB 3200, 3300, 3 1 00 , 3500 , 3400
3026 I F A = 9 THEN A = 8
3028 RETURN
, **********************************
, * GAUSS I AN F I LTER
I **********************************
3334 I F COAT = 0 THEN PHDAT = P I /2 : GOTO 3338 ' CAN ' T D I V I D E BY ZERO
3336 PHDAT = ATN ( SDAT / COAT) , GET ANGLE
3338 I F COAT < 0 THEN PHDAT = PHDAT + PI ' CORRECT FOR NEG X AX I S
, *** D I V I DE ( F I NALLY) ***
3340 I F C S F L G = 1 THEN RQOT = R I J/RDAT : P HQOT = P H I J - P HDAT : GOTO 3344
3342 RQOT = RDAT / R I J : P HQOT = PHDAT - P H I J
3344 C ( I , J ) = COS ( P HQOT ) * RQOT ' CONVE RT T O X , Y
3346 S ( I , J ) = S I N ( P HQOT ) * RQOT
3348 NEXT J
3350 NEXT I
3352 GOSUB 1 60 ' D I SP LAY RESULTS
3354 I NPUT AS ' WA I T FOR USER
3390 RETURN
3400 ' **** TEMP SAVE SPECTRUM IN DAX ( I , J ) ****
3402 AMPN = 1
3404 FOR I = 0 TO Q - 1
3406 FOR J = 0 TO Q -
3408 DAC( I , J ) = C( I , J ) : DAS( I , J ) = S( I , J )
34 1 0 NEXT J
34 1 2 NEXT I
34 1 4 RETURN
, **** TRUNCATE SPECT RUM ****
3500 ATTN = 0
3502 I NPUT "TRUNCATE SPECTRUM ABOVE HARMON I C # "; HARX
3504 FOR I = 0 TO Q2
3506 FOR J = 0 TO Q2
3508 I F SQR ( I A 2 + J A 2) < HARX THEN 35 1 8
35 1 0 GOSUB 3900
35 1 8 NEXT J
3520 NEXT I
3522 GOSUB 6000 ' D I SPLAY RESULTS
3524 I NPUT AS ' WA I T
3526 RETURN
3900 ' **** MOD I F Y HARMON I CS ****
3902 1 2 = Q2 + I : J 2 = Q2 + J : 13 = Q2 - I : J3 =Q2 - J
3904 C ( 1 2 , J 2 ) = C( 1 2 , J 2 ) * ATTN : S ( 1 2 , J 2 ) = S(12, J2) * ATTN
3906 C ( 1 2 , J3) = C( 1 2 , J3) * ATTN : S ( 1 2 , J3) = S(12, J3) * ATTN
3908 C ( 1 3 , J3) = C( 1 3 , J3) * ATTN : S ( 1 3 , J3) = S(13, J3) * ATTN
39 1 0 C ( 1 3 , J 2 ) = C ( 1 3 , J 2 ) * ATTN : S ( 1 3 , J 2 ) = S(13, J2) * ATTN
39 1 2 RETURN
I *********************************
, * LOAD . BMP F I L E *
I *********************************
4000 CLS : PR I NT : P R I NT : P R I NT
4002 DMAX = 20 : XD I R = 0 : PATHS = "A : \"
4004 RED I M I MAGE%(DMAX) , * . BMP HEADER ARRAY
4006 I NPUT " F I LE NAME ( . BMP F I L E S ON L Y ) " ; NMS
4008 OPEN PATHS + NMS + " . BMP" FOR RANDOM AS #3 L E N = LE N ( P I XR E C )
4010 PR I NT " LOF = " ; LOF ( 3 )
4012 FOR J = 1 T O 2 0 ' READ HEADER
4014 GET #3, J , P I XREC
4016 I MAG% = P I XREC . P I X
4018 I MAGE%( J ) = I MAG%
4024 NEXT J
4026 I F I MAGE%( 1 ) = 1 9778 AND I MAGE%( 1 0 ) = Q THEN 404 0 ' CHK. BMP& S I ZE
Image Enhancement 269
4030 I NPUT " T H I S IS NOT A LEGAL . BMP F I LE - ENTER TO CONT I NUE " ; AS
4032 CLOSE 3: GOTO 4090 ' CLOSE F I LE & E X I T
4040 STRT = I MAGE%( 6) I 2 + 1 ' GET OATA START LOCAT I ON
4044 L I NX = I MAGE%( 1 0 ) , NO. L I NES
4046 COLX = I MAGE%( 1 2 ) , NO. COL S .
, LOAO F I LE I NTO OATA ARRAY
4048 FOR I = 0 TO Q - 1 : K L 1 = STRT + ( L I NX* 1 / 2 ) ' DATA L I NE ADDRESS
4050 FOR J = 0 TO Q3 STEP 1 : KL = KL1 + J : J 2 = 2 * J ' DATA ADDRESS
4052 GET #3 , KL , P I XREC
4054 I MAG% = P I XREC . P I X
4056 C( I , J 2 ) = I MAG% AND 255 : C ( I , J 2 + 1 ) = ( I MAG% AND 65280 ) I 256
4058 S( I , J 2 ) = 0: S ( I , J 2 + 1 ) = 0
4060 NEXT J
4062 NEXT I
4064 CLOSE 3
4066 MAG2 = - . 025 ' SCALE FACTOR
4068 GOSUB 6000 ' D I SPLAY I MAGE
4074 I NPUT "C/R WHEN READY"; AS ' WA I T FOR USER
4090 RETURN
, *********************************
, * SAVE * . BMP F I LE *
, *************** ******************
4 1 00 CLS : PR I NT : PR I NT
4 1 02 I F XD I R = 1 THEN GOSUB 50 ' TRANSFORM TO SPACE DOMA I N DATA
4 1 04 A = 1 : B = 0 ' F I RST BR I NG DATA BACK I N TO RANGE , A=MAX , B=M I N
4 1 06 FOR I = 0 T O Q - 1
4 1 08 FOR J = 0 TO Q . 1
41 1 0 I F C ( I , J ) > A THEN A = C ( I , J )
4112 I F C ( I , J ) < B THEN B = C ( I , J )
4114 NEXT J
4 1 1 6 NEXT I
4 1 1 8 I F B > 0 THEN B = 0 ' I F > 0 E L I M I NATE FROM CALCULAT I ON S
4 1 20 A = (A - B ) I 2 5 5 ' F I ND 0 - 255 SCALE FACTOR
4 1 22 FOR I = 0 TO Q - 1 ' CONVE RT TO 8 B I T DATA
4 1 24 FOR J = 0 TO Q - 1
4 1 26 C ( I , J ) = I NT « C ( I , J ) - B ) I A )
4 1 28 NEXT J
4 1 30 NEXT I
4 1 32 FOR I = 0 TO 1 : C ( Q , I ) = 0 : C ( I , Q ) = 0 : NEXT I
4 1 34 PTH 1 S = " C : \W I NDOWS\D " : PTH2S = "C : \W I NDOWS\" : NM2S = NMS
4 1 36 I F L E N ( NM2S) > 7 THEN NM2S = LE FTS(NM2S, 7)
4 1 38 OPEN P T H 1 S + NM2S + " . BMP" FOR RANDOM AS #3 LEN = LE N ( P I XR E C )
4 1 4 0 OPEN PTH2S + NMS + " . BMP" FOR RANDOM A S #4 L E N = LE N ( P I XR E C )
4 1 4 2 FOR I = 1 T O STRT - 1
4 1 44 GET #4, I , P I XREC
4 1 46 PUT #3 , I , P I XREC
4 1 48 NEXT I
4 1 50 CLOSE #4
4 1 52 PR I NT "EXPAND F I LE ( YIN ) " ;
4 1 54 AS = I NKEYS: I F A S = " " THEN 4 1 54
4 1 56 I F AS = " n " OR AS = "N" THEN 4300
4 1 58 P I XREC . P I X = I NT ( 2 * Q )
4 1 60 PUT #3 , 1 0 , P I XREC
4 1 62 PUT #3 , 1 2 , P I XREC
4 1 64 FOR I = 0 TO Q - 1 : KS1 = (2 * I * Q ) + STRT
270 Appendix 1 6.1
4 1 66 FOR J = 0 TO Q - 1 : K S = K S 1 + ( J )
4 1 68 P I XE L 1 = C ( I , J ) : P I XE L 2 = ( C ( I , J + 1 ) + C ( I , J » 1 2
4 1 70 P I XE L = I N T ( P I XE L 1 ) + 256 * I NT ( P I X E L 2 )
4 1 72 I F P I XE L > 32767 THEN P I XE L = P I XE L - 65536
4 1 74 P I XRE C . P I X = I N T ( P I XE L )
4 1 76 PUT #3 , K S , P I XREC
4 1 78 NEXT J
4 1 80 KS1 = KS1 + Q
4 1 82 FOR J = 0 TO Q - 1 : KS = KS1 + J
4 1 84 P I X E L 1 = ( C ( I , J ) + C ( I + 1 , J » /2
4 1 85 P I XE L 2 = ( C ( I , J ) + C ( I + 1 , J ) + C ( I , J + 1 ) + C ( I + 1 , J + 1 » /4
4 1 86 P I XE L = I N T ( P I XE L 1 ) + 256 * I NT ( P I X E L 2 )
4 1 88 I F P I XEL > 32767 THEN P I XEL = P I XEL - 65536
4 1 90 P I XREC . P I X = I NT ( P I XE L )
4 1 92 PUT #3 , K S , P I XREC
4 1 94 NEXT J
4 1 96 NEXT I
4 1 98 CLOSE #3
4200 RETURN
4300 ' STORE UN EXPANDED P I CTURE
4302 FOR I = 0 TO Q - 1 : KS1 = ( I *Q/2) + STRT
4304 FOR J = 0 TO Q - 1 STEP 2: KS = KS1 + ( J /2)
4306 P I X E L 1 = C ( I , J ) : P I XE L 2 = C ( I , J + 1 )
4308 P I XEL = I NT ( P I XE L 1 ) + 256* I NT ( P I X E L 2 )
43 1 0 I F P I XEL > 32767 THEN P I XEL = P I XE L - 65536
43 1 2 P I XREC . P I X = I NT ( P I XE L )
4314 PUT #3 , K S , P I XREC
4316 NEXT J
4318 NEXT I
4320 CLOSE #3
4322 RETURN
I *********************************
, * GENERATE FUNCT I ONS *
I *********************************
5000 XD I R = 0
5001 CLS : P R I NT : PR I NT : P R I NT " FUNCT I ON MENU" : PR I NT
5002 PR I NT " 1 = BESSEL FUNCT I ON 2 = GAUSSA I N " : PR I NT
5009 P R I NT " 9 = EX I T : " : PR I NT
5010 PR I NT " MAKE SELECT I ON " ;
5012 A S = I NKEYS: I F AS = " " T H E N 5 0 1 2
5014 A = VAL ( AS ) : O N A GOTO 5600, 5700
5016 I F A = 9 THEN RETURN
5018 GOTO 5000
, *** BESSE L FUNCT I ON ***
5600 CLS : DE FDBL D - K
5604 I NPUT " W I D T H " ; WO T H 1
5606 I F WO T H 1 < 1 T H E N 5604 ' M I N I MUM W I D T H
5608 S K L 1 = P I / ( 3 . 6*WO T H 1 * Q/64 )
561 0 FOR I = 0 TO Q - 1
561 2 YARG = SKL 1 * ( 1 - Q2 ) : PR I N T " * " ;
5614 FOR J = 0 TO Q - 1
5616 XARG = SKL 1 * ( J - Q2)
5618 KARG = SQR (XARGA2 + YARGA2)
5620 KA = 1 : KB = 1 : DAT1 = 1 : KTGL =
5622 FOR K = 2 TO 900 STEP 2
Image Enhancement 271
The majority of routines for this program deal with modifying the
frequency domain functions (a menu is located at lines 3000-3028). Lines
3 1 00-3 1 20 provide a routine to filter the frequency domain function using
a Gaussian response lowpass filter. The argument is found as the RSS of
the harmonic coordinates (line 3 106) and the attenuation is the same as the
Gaussian filter used in the Gibbs phenomenon illustration of Chapter 6.
A routine to multiply the p o i nts o f two spectrums ( i . e . ,
convolution) i s located at lines 3200-3230. Since the data i s used twice
in complex multiplication it's necessary to pull it out of the array and into
temporary variables (lines 3206-08) before forming the products (line
3210). A second spectrum must already be stored in arrays DAC(I,J) and
DAS(I,J) and we get it there via the Save Spectrum subroutine which is
located at lines 3400-34 14 (under control of the Modify Spectrum menu).
Division of two spectrums (deconvolution) is performed in lines
3300-3390. This routine is written as a straightforward complex division
routine, except that you may divide either the stored spectrum by the
current frequency domain function or vice-versa. You must specify the
direction in lines 3302-08 and a flag is set (CSFLG-line 32 10) according
to the selection. At line 3 3 1 4 we start the actual division. Again, we pull
the data from the arrays (line 33 1 8) and then clear the array data. In line
3320 we find the magnitude, and in lines 3322-3328 we find the angle.
In lines 3330-333 8 we do the same thing for the stored data and in lines
274 Appendix 16.1
I ********************************************************
I * PROGRAM TO I LLUSTRATE THE E F FECT OF NOI SE AVERAG I NG *
I * I T R E T R I EVES A S I GNAL BUR I ED ( 20DB DOWN ) I N NO I SE *
, ********************************************************
SCREEN 9, 1 : COLOR 1 5 , 1 : CLS : PI = 3 . 1 4 1 592653589793#
P I 2 = 2 * P I : K1 = P I 2 I 200 : XO = 1 00 : YM = 1 70
D I M 0 1 ( 4 , 1 9 ) , 0 2 ( 2 , 400 ) , XD%( 400)
FOR I = 1 TO 18
FOR J = 1 TO 4
READ A
D1(J, I ) = A
NEXT J
NEXT I
I N PUT " I NPUT AID DYNAM I C RANGE AS 2"N . N = " ; N
276 Appendix 16.2
OY = 1 00 / (2 A N )
FOR M = 1 TO 60000
12 = 0
FOR J = 1 TO 1 8
FOR I = 1 2 T O 0 1 ( 1 , J )
XO%( I ) = I NT ( I - 0 1 ( 2 , J »
02 ( 1 , I ) = 02 ( 1 , I ) + OY * I NT « 0 1 ( 3 , J) + 141 * ( RNO ( 1 ) - . 5 » / O Y )
0 2 ( 2 , I ) = 0 2 ( 2 , I ) + OY * I NT « 0 1 ( 4 , J ) + 1 4 1 * ( RNO ( 1 ) - . 5 » / O Y )
NEXT I
12 = 01 ( 1 , J ) + 1
NEXT J
CLS
FOR I = 1 TO 400
L I NE ( XO + XO%( I ) , YM + 02 ( 1 , I ) / M ) - (XO + XO%( I ) , YM + 0 2 ( 2 , I ) / M)
NEXT I
LOCATE 1 , 5 0 : P R I N T M
I F M > 1 000 THEN 1 200
FOR L = M TO 1 000
FOR KT I ME = 1 TO 1 00 : NEXT KT I ME
NEXT L
1 200 NEXT M
1 333 AS = I NKEYS: I F AS <> "G" THEN 1 333
STOP
DATA 1 00 , 0 , 0 , 0 , 1 03 , 0 , - 6 , 6, 1 1 4 , 0 , - 1 , 1 , 1 1 8 , 0 , - 6 , 6, 1 48 , 0 , 0 , 0
DATA 1 5 2 , 0 , - 6 , 6 , 1 62 , 0 , - 6 , - 5 , 1 72 , 1 0 , - 1 , 1 , 1 82 , 20 , 4 , 6, 2 1 3 , 30
DATA 0 , 0 , 2 1 6 , 30 , - 6 , 6 , 227, 30 , 4 , 6 , 267, 40 , 0 , 0 , 271 , 40 , - 6 , 6, 277
DATA 4 0 , - 6 , - 5 , 283, 46 , - 1 , 1 , 287, 46, - 6, 1 , 40 0 , 46, 0 , 0
Brigham, E.O., The Fast Fourier Transform And Its Applications, Prentice
Hall. This is another good reference for the FFT. Its strongly graphical
presentation (especially of the 2-D FFT) is helpful.
Gaskill, J.D., Linear Systems, Fourier Transforms, and Optics, John Wiley
& Sons. If you are interested in pursuing Fourier Optics this is pretty
much the standard text. The tutorial orientation is especially helpful and
the review of fundamentals is valuable if you have been away from this
material for some time.
Walker, J.S., Fast Fourier Transforms, CRC Press. This book has a lot
of good material, but (for those who can handle the math) I especially
recommend the introduction to Fourier Optics (Chapter 6).
INDEX
AID conversion . . . . . . . . . . . . . . . . . . . . 1 1 1 , 1 40
. . . . . . . . . . . . . . . . .
Argand . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4, 9, 1 0
Argument . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3, 5, 8, 22 1 -224
Array . . . . . . . . . . . . . . . . . 70, 7 1 , 73, 93-97, 1 5 8, 1 59, 209, 2 1 9, 226, 233
Average . . . . . . . . . . 25-29, 75, 1 37, 1 3 8, 1 59, 1 60, 1 66, 1 67, 1 85, 1 99
. . . .
Band-limited . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . .
. . . . . . . 47, 1 1 7
BASIC . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . 64, 1 04,
1 55, 1 62, 1 63, 205
Basis function . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . .
. . . . 36, 1 04, 1 82
Bessel function . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . .
1 7 1 , 1 73, 260-262
Binary . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . .1 37- 1 39, 1 93
. . . . . . .
Eigenfunction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 5 - 1 7
Execution time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64, 1 63
Exponential . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6, 7, 10, 1 7, 2 1 9
.
Frequency domain . . . . 24, 3 1 , 3 5 , 58, 60, 65, 66, 97, 1 14- 1 1 6, 1 24, 1 26- 1 28,
1 29, 1 3 1 , 1 45- 1 47, 1 60, 1 63, 1 70, 1 73, 1 8 1 , 1 85 - 1 89, 1 92, 1 96, 2 0 1 -204
Frequency response . . . . . . . . . . . . . . . . . . . . . . . . 5 8 , 63, 64, 66, 1 2 1 - 123
Frequency shift . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 45- 1 47, 152
Functions . . . . . . . . . . . . . 1 , 1 2 , 16, 22-23, 28-30, 36, 9 1 , 97-98, 1 26- 1 3 1
.
Imaginary number . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4, 36
Impulse function . . . . . . 66, 1 1 2, 1 1 4, 1 26, 1 28 - 1 30, 1 72- 1 73 , 1 79, 1 8 1 , 1 86
Inductor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34-36, 4 1 , 60
Integration . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 6, 26, 1 96, 20 1 -203, 204
Inverse DFT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . .. . 50, 83
.
Laplace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Limit . . . . . . . . . . . . . . . . . . . . 28, 34, 47, 49, 1 09, 1 1 9, 123, 1 73 , 1 87, 1 93
Linear system . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 6, 1 86, 20 1
Logarithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 1 4, 220
Maclaurin series . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
. .
Magnitude . . . . . . . . 6, 7, 2 1 , 23, 26, 98, 1 60, 1 68, 1 79, 1 80, 2 1 3 , 257, 273
Modulation . . . . . . . . . 1 9-23, 69, 99- 1 00, I l l , 1 1 6, 1 47, 1 49, 1 57, 1 8 1 , 1 82
Real function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 2 8
Real part . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 5 , 46, 8 7
280 Appendix 16.2
Sampled data 2, 97, 1 00, 1 07, 1 1 0, I l l , 1 1 7, 1 1 8, 1 20, 1 33, 1 34, 146, 224
. .
Sine function .. . . 96- 1 03, 1 30- 1 34, 146, 149, 1 69, 1 70, 1 73, 1 84
. . . . . . . . .
Stage . . . . . . . 67, 72, 75, 77-80, 83, 84, 88-9 1 , 9 1 , 1 62, 2 1 2, 229
. . . . . . . .
Taylor series . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
. . . . . . . . . . . . . . . .
Transfer function . . . . . . . . . . . . . . 37, 46, 58, 60, 64, 66, 1 86- 1 88, 190
. . .
Transient analysis . . . . . . . . . . . . . . . . . . . . . . . 3 1 , 64
. . . . . . . . . . . . . .
Truncation . . . . . . . . . . . . . . . . . . . . . . . 1 37, 1 3 8 , 1 9 1 , 1 95
. . . . . . . . . .
vector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9, 2 1 , 26
.
Weighting function 1 03, 1 32, 1 43, 1 49, 2 1 9, 220, 227, 228, 233
. . . . . . . . . .