Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Fourier Analysis Using A Spreadsheet

Robert A. Dory, and Jeffrey H. Harris

Citation: Computers in Physics 2, 83 (1988); doi: 10.1063/1.4822799


View online: https://doi.org/10.1063/1.4822799
View Table of Contents: https://aip.scitation.org/toc/cip/2/6
Published by the American Institute of Physics

ARTICLES YOU MAY BE INTERESTED IN

Spreadsheets for physics


Computers in Physics 2, 70 (1988); https://doi.org/10.1063/1.168300

Performing Fourier transforms on extremely long data streams


Computers in Physics 3, 59 (1989); https://doi.org/10.1063/1.168338

Solving coupled, nonlinear differential equations with commercial spreadsheets


Computers in Physics 3, 75 (1989); https://doi.org/10.1063/1.168340

Spreadsheets Tackle Physics ProblemsUsing Lotus 1-2-3 and software from the AAPT bulletin board,
physicists can quickly solve and graph a multitude of problems
Computers in Physics 2, 37 (1988); https://doi.org/10.1063/1.4822723

Feynman’s proof of the Maxwell equations


American Journal of Physics 58, 209 (1990); https://doi.org/10.1119/1.16188

On Feynman’s proof of the Maxwell equations


American Journal of Physics 60, 301 (1992); https://doi.org/10.1119/1.16921
SPREADSHEETS

Fourier Analysis Using ASpreadsheet


Robert A. Dory and Jeffrey H. Harris

or many tasks involving Fourier analysis, a spread- fast Hartley transform (FHT) should be practical.

F sheet is an excellent tool. Using three examples, this


article shows that for data sets involving perhaps a
hundred points and a few tens of coefficients, a
The latter could give an interesting challenge to
readers; an article on the FHT in the April, 1988 issue of
Byte magazine, p. 93, by Mark O'Neill would be a fine
spreadsheet method is practical and quick on personal starting place for development of this process, which
computers such as the Macintosh SE or the IBM PC AT uses only real (non-complex) arithmetic.
and compatibles. With enhanced hardware such as The procedure used here is straightforward integra-
floating-point co-processor chips, the numbers may be tion with the formulas:
extended by about an order of magnitude. This article .fi,x) = "i.fn cos nx + "i.gn sin nx, with n = 0,1,2'00'
presents three spreadsheet templates that should permit
rather easy generalization to other orthogonal basis fn = J .fi,x)cos nx dxlf cos" nx dx, from 0 to XM = 21T,
functions, provided those can be defined in terms of gn = J J\x)sin nx dx/ f sin" nx dx, from 0 to XM = 21T,
standard functions or recurrence formulas.
Fourier analysis is used here to illustrate provision of where XM = 21T is the periodicity interval of .fi,x). With
modularity to spreadsheet element definitions, so that this approach, the basis functions of the expansion could
the available replication processes allow simple exten- be readily changed for application of another basis, such
sion of the ranges of the calculation; in this case, the as Legendre or Bessel functions, especially if the higher
number of harmonics and integration nodes. index elements can be calculated with sufficient accuracy
Fourier analysis of a real function also illustrates using two-term recurrence formulas as in the Fourier
some techniques in spreadsheet use. These include case. Accuracy of this is discussed further under
selective application of array Ivector arithmetic and the Example 2.
important trick of formulating formula blocks that can be The integrations use the trapezoidal rule for non-
replicated (blocks copied with relative references adjust- uniform (arbitrary) grid spacing:
ed appropriately and absolute references unchanged) to
fg(X)dx = 1/2 "i. (xj + 1 - Xj _ 1 ) g(xj )
extend the range or resolution of the calculation.
For a moderate problem, programming and debug- where the nodes are Xj' for j = 0, jM and for the special
ging of functional Fourier analysis templates like the terms j = 0 and jM' the corresponding X_ 1 and XjM + 1
Fourier templates discussed below might take a spread- are to be taken as Xo and XjM' If a new set of basis func-
sheet user less time than finding the user's manual and tions called for a weight function, it could be grouped
the object code for an existing Fourier analysis program. conveniently with the factor (Xj + 1 - Xj _ 1 ).
The reader is assumed to have some experience with A test function is provided. It is given by y" with pa-
Excel or a similar spreadsheet program so the general rameters a" b o' b, and "noise," where
operations given here can be carried out without detailed y, = a , sin,x + b, + b 2 cos 2x + noise' (R - 1/2)
instructions. The examples use the RIC2 option for
addressing the elements of the template; this is a matter and R is the pseudo-random function with uniform
of taste, but has the property that variable names can take distribution in [0,1).
the form aO and bt, which otherwise conflict with the
slightly more compact normal addressing scheme AI, Fourier Template 1
AB137, 00.. Sine and cosine values are tabulated at the integra-
Two templates for the Excel spreadsheet program tion nodes and recurrence formulas are used to obtain
illustrate the trade-off between extensive use of memory the higher harmonic functions sin nx j and cos nx j. The
for possible reduction of calculation time and, converse- accuracy should be adequate for reasonable values of nx j
ly, the deliberate recalculation of data to reduce memory since Excel arithmetic is done to roughly 15 decimal
requirements. places. For small values of nand iM , for example, ten and
Fast Fourier transform (FFT) techniques are not used 50, Excel recognizes that the basis data do not need to be
here because the logic required is not straightforwardly recalculated when changes are made only to test data
well suited to the simpler tools available in the spread- f(xi ). The process is then reasonably speedy. However,
sheet. By development of function macros for retrograde when the number of nodes increases by a factor of three
indexing, formula entry and logic, the FFT or the related or four, tables of dependency become large and Excel
may then not be able to avoid some recalculation.
Robert A. Dory and Jeffrey. H. Harris are Research Physicists at Oak Figure 1 shows one arrangement. We assume that a
Ridge National Laboratory, Oak Ridge, Tenn. limited number of harmonics are needed, but that

COMPUTERS IN PHYSICS. NOV/DEC 1988 83


SPREADSHEOS

Fourier [Hample 1.

21 ooints 0 2 dxO 0 .31 4 bO -0 .25 a1 12 .34 b2 0 .123


1 x dx u 51 co 5i 2 co 2 513 co 3 514 co 4
0 0 .0 00 0 .3 14 -0 .127 0 .000 1.000 0 .000 1.000 0 .000 1.000 0 .000 1.000
1 0 .314 0 .628 3 .663 0 .309 0 .95 1 0 .588 0 .809 0 .809 0 .588 0 .951 0 .309
2 0 .628 0.628 7.041 0 .588 0 .809 0 .951 0 .309 0 .951 -0 .309 0 .588 -0 .809
3 0 .942 0 .628 9 .695 0 .809 0 .588 0 .951 -0 .309 0 .309 -0 .951 -0 .588 - 0 .809
19 5 .969 0 .628 -3 .964 -0 .309 0 .95 1 -0 .588 0 .809 -0 .809 0 .588 -0 .951 0.309
20 6 .283 0 .314 - 0 . 12 7 0 .000 1.000 0 .000 1.000 0 .000 1.000 0 .000 1.000
(011) Non uni for m int~rvals OK. us es Q'Jadratic forms to do int~Q r ations
Co~ffic 1~nt5 0 I 1 1 I 2 2 3 3 I 4 4 I
1. This Is the Fourier analysis template 1. C051n~ fn -0 .250 I 0 .000 1 0 .123 0.0001 0 .000 1
A two-column block from "SI n Co n " Si n~ en 0 .000 11 2.3 4 0 10 .0 0 0 0.000 10.000
nois e 0 .0 I Test function : y = bO + al sin x + b2 cos 2x + noise * (randO- .5)
through " Coe ffici e n ts Cosine and Sine"
2011 011 011 011 011 011 011 011 011
can be copied to blank columns on the
factor 1.00 I norm 12 .566 6.283 6 .28 3 6 .283 6 .283 6 .283 6.283 6 .283 6 .283
right to add calculation for another n
value.

acceptable accuracy may require a substantial number of The "norm" elements are entered in the form
integration nodes ,x-j ' Integration nodes are arranged "I = surntsi' si' dxl j"using the command key and a
downward, with harmonic numbers across, because that carriage return, to make them array function formulas,
fits the screen format best. If the data to be analyzed are which can act, for example, to give the inner product of
gen erate d by another program, Fortran for example, they the elements of the vectors "sin " and "dx" [that is,
can b e stored one number per row, so that they can ~ (sin ) j . (sin), . tdxl] . Function formulas are set off by
con ven ie n tly be pasted into Excel using the Macintosh braces: I···J.
Clipboard. The rows labeled " Cosin e Coefficients" and "Sin e
At the bottom of the column blocks labeled "si n " Coefficients" are filled with similar array function
and "co n " are formulas for the integrations. The formulas having the forms:
numbers labeled "n orm " are the values of j'sin" nx dx and "! = SUM(y' R[ - 23]C:R[ - 3]C' dxl/B] + 3]Cj " and
jcos'' nx dX" multiplied by an overall normalizing number
" facto r." For the present sine and cosine basis and ,, ' = SUM(y' R[ - 24]C:R[ - 4]C' dX"l/R[ + 2]Cj"
assumed integration interval [O,p1T], these values are all where the divisor picks up the value of "n o rm" from the
p1T exce p t those for n = 0, which are 0 and 2p1T. To save same colu m n and the symbol "R[ - 25]C:R[ - 5]C " calls
time, these could be changed to con stan t values 1, 0, and 2 out the basis function vector from the same column.
and " fa cto r " to p1T; they are as indicated to provide for If more integration nodes are needed, one can in sert
use with another set of orthogonal functions. the appropriate number of blank rows somewhere after

Fourier [Hamp le I. Formu las:l


2 3 4 5 6 17
2 =COUNT(n) iooi nt s Ip i2 dx O 1 ~ p *P IO /(po int s - 1)
3 1 x dx III 51 !C O

2. A partial listing of the 'program' for


template 1. As shown, columns 5 and 6, 25 j(p11) 1 jNon- uni for m intervals 10K
are too narrow to give complete formulas
In all rows. The notation "RC:R[2]C(5]"
denotes a 3 X 6 range of cells, from the
cell containing the reference to the cell
five columns to the right and two rows
~~ no;s~ :O ! .
I}~~ t.~.~~.!.j~.~ : ~ ..:::..~Q.lp;~.! ~!.~i~; ~~ ~~.~ ~~ ~ ~~.i . . . ..
down. 31 f act or !1 Inor m :=SUM( dx) 1=SUM( R[ =SUM( R[- 27 ]C:R[-

Fourier [Hample I. Formula s:2


7 8 9
2 =p*p 10I(points-l ) bO 1-0 .25
3 co 2 ,2
.....L =COS(x) =si*RC[-I]+co*RC[-2)
;;;s·i"*·RcF·i · ·i+~·~·*·R·C"F2f· ....··..........·......·....·....·....··..·=co*RC[-2 )-si*RC[-3]
..·......···....·..·....···..·..·
.z., ;;;COS(~)"·"""""·""·"·" ;;·s;·*R d :·l i"~~~ *R cF2T ......·........··..·....·..........·....·..·
;;;~·~·*·RC"F2Fsi*RC ·[:3·j"
......................·......·..
6 '~6)s'(~')""" """""""'''' ·;;·~~· * RC "[:·2· j":S·;·*·R·Ci": 3 f

24 =COS(x ) =5i*RC[-l )+co*RC[-2] =co *RC [-2]-si*RC[-3)


3 . A s e c o n d partial listing of the 25 i uses Quadrati c forms to do int~Qrations do int~Qrations
26 1 I=R [-23 ]C I=R[ -23]C
'program' for template 1. For a nicely
27 = SUH ( u *R[-23)1 , = SUH ( Il.R [-2 3 )C :R [ - 3 )C. dx ) 1
labeled display, row 3, columns 8 and 9,
28 I= SUM ( '1 *R[-24 )C :R [ - 4 I C. dx ) / nor-m'
which contain the values of n , have been
x + b2 cos 2x + noi !se * (randO- .5)
assigned number formats " Si "0 and " Co ~
30
1011·· ··.. ..·....·..·..·..·....·....'(011..·..·..·....·..........·....·........·......................................·....·....·....:·011·....·....·....·........·....·....·....·........................................
I

" 0 which preface the n value with the text =SUM(R[ -27)C :RH - SUM(R[- 27 )C:R[- 7 )C" 2 * dx) ' -SUM (R(-27)C :R[- 7 )C"2 * dx)
31
in quotes.

84 CO MPlJTE IIS IN PHYSICS. NOVIO EC 198 H


Fo uri er EHamp le 2.

21 ooints D 2 (dx ) 0 .314 I ~ 1 1 I bO -0 .25 b2 0 .1231


i x dx II Test function : \I ~ bO + al sin x + b2 cos 2x + noise (randO- .5) I'

0 0 .000 0 .31 4 - 0 . 12 7 I noise 0 .0


1 0.31 4 0 .628 0 . 159 Copfficipnts n I 0 1 2 3 4
2 0 .628 0 .628 0 .376 Co sinp fn -0 .250 0 .000 0 .123 0 .000 0 .000
3 0 .942 0 .628 0 .521 Sinp on 0 .000 1 .000 0 .000 0 .000 0 .000
4 1.25 7 0 .628 0 .602 norm 6 .283 I
5 1.57 1 0 .628 0 .627 4. Fourier analysis template 2 is a space-
19 5 .969 0 .628 -0 .460 saving program that uses half the
20 6 .283 0 .3 14 -0 .127 Usu quadratic forms to do int egr ations Non- equidist ant pts OK memory of the previous example; it also
(p'l1) has half the speed, but the added time
may be unimportant.

the second row of nodes and before the last one. Then the Macintosh II and use of a floating point co-processor chip
relative duplication command ("Fill Down") can be used the calculation time for about a thousand elements and a
to populate the template from the second node down half dozen coefficients is roughly a minute.
through the last. This should provide the required higher The approach here is generally the same as in the
integration accuracy when new data Wi) are added. first example, except that the coefficients are entered as
If another harmonic value is required, the user can the array function formulas
copy the two-column rectangle from "si n co n" down
through the "n orm " line into the two blank columns at
the right of the presently filled zone. To make this
"! = SUMly' SIN (n*X))/norm I" and
possible, the sine and cosine formulas were written in "I =SUMly 'COS(n*X))/normj " where the
mixed form .. = si ' RC[ - 1] + co RC[ - 2]," which
means multiply the element on this row of the vector row array n is defined as labeled, and norm is set to p1T for
named "si" by the element of this row and one column the Fourier transform to eliminate some repetition of the
back, and add the corresponding element of vector "co " sine and cosine calculations . An easily misleading
times the element two columns back. These formulas are complication arose in this example. An initial assumption
not array function formulas because the intent is to proved incorrect: that the proper element of a row array
eva lu ate a column vector row by row using data from the norm, as in Example 1, would be used by the array
same row of other column vectors. The vectors "si" and formula definingfn and Sn- Instead, the first element of
" co " were defined to be columns C6 and C7, elements R4 the norm array was used even for succeeding columns.
through R24 in the present case, and should not be This feature could be called a bug in Excel (version 1.04),
confused with functions SIN(argument) and COS(argu- but might be specified somewhere in fine print in the
ment). manual. The work-around chosen here was to reduce
norm to a scalar and then insert the necessary factor 2 in
Fourier Template 2
the formula for fa.
To reduce memory usage (by about half) in the case Integration nodes could be added to this template in
of large data sets f (;JCj), the basic functions in this example the same manner as in the first example. Providing more
are not determined once and stored. Instead, they are coefficients is also about the same: insert new columns
recalculated for each coefficient and case. Calculation after the n = 1 calculation and replicate the n,fn, gn and
time is about doubled, but still not large; with a norm columns to the right, as far as the last one.

Fourier EH ample 2. Formula s:!


2 I 3 4 I S 6 I 7
2 ~ COUNT( ;) ' Do;nt s !2 (d x) , ~ n * P IO /(no ints - l )
3 x dx u
.!.!~Lf:-!·n.~·!·! ~·n.":·I..··\I..=··~Q··:':..~·~..· ~·!~..~..·
~ R[1 )C[-I I-RC[-I] =bO+al *SI N(x) +
~ Q................. . . ;;;RFi
0
. . i·c·~·R2C'7· ;;;R'i'j"jc(:'i"i=RF,']Cr:Tj' ;;;b'o~'~'l';';'sir~(~):;:'
~ .!...................... .. £~!..f.f!~~!~t~.! .............................................
';;'R'r:Tjc:;:R2C7' ';;bo"~"~'l";s'iN(';;-j'~' ~.~.~.j.~.~...............

t.~ffl~l-tf!~:~;:~·rl~lfff
~ :?....__........ ..
l..._......._....._....._................
tz, .~_.._............... ';;;;RFi·iE~· R·2'c·i
;'Ri': 'j" jC-';:R2C7'
;;;b·ii·~·~·i ;,;·s·i~i(~f.;:·. Si neo
·;;·bo:;::;·i"*s·ii·j·(·~·j·~·
j
,
8 4
5. A partial listing of the 'program' for
1
2 4 20
2S
=R[-1 lC+R2C7 =RC[- 1)- R[- 1lC[-1
!( 0'l1) , !
=bO+al *S IN( x ) +
,.l!.~~.~..9~~.~~.C~.!.t~·i·f.~r.·T.~..!.~..~~..!.n..\~.gr..~. template 2. Columns 5 and 7 are not
shown fully.

Fourie r EHam ple 2. Formu l as:2


8 I 9 I 10 I 11 I 12 T
2 ~I il bO ,- 0 .25 b2 ,0. 12<;
,
~ ··:!:"~:?·i""·""·"""·"""··""·"·"·""""·""·""·"":·"··""·"""..·..··..·..··.._··..·..........·....·..·..·..·..··..·....··..·..··..........··..1....··..··..·....·......··tnoi.e
4 ,0
S n !O 01 ;2 ,3 ,4 6. A second partial listing of the
6 fn ,= SUH ( u + d x ) / n or m /2 ' -SUH ( u *C OS( R[ - I )C +x) +dx)/norm ' - SUH(Il + ' = SUH(u ~~S U H 'program' for example 2. Columns 8, and
7 Qn 10 I~ S U H ( u +S IH( R[ - 2 )C+ x )* dx) / nor m ,- SUH ( u + j =S UH ( u +I-SUH 11 through 13 are not shown fully. See
8
9
norm ,~o *PIO
, ~ .. ........ .. ......... . .... .. . . . .... .... . . . . .. . . . . . . .... . \.... . . . ... . . . . j.. . . . . .........t·.. . ....... the note in the text concerning the
change in use of norm from Example 1.

C.O MPIlT ERS IN PHYSICS, NOV/I)EC 19M 85


SPREADSHEOS

Fouri er [ Hampl e 3.

Test f unction : y = bO + al sin x +


+ b2 cos 2x + noise*(randO- .5) Act on's Comoaction
21 ooints p 2 dj n 0 1 2 3 I
.11 1 bO - 0 .2 5 0.05 Iphin 0 0 .31 42 0 .6283 0 .94 25
b2 0 .12 3 no i se' 0 .0 dphi c phin2 2 .00 1.9 0 1.62 1.18
(il.x) 0 .3 142 km ax 20 0 .3 1 cK p2 0 0 0 0
i x u uf1 k cK p l 0 0 0 0
0 0 .0 0 0 -0 .12 7 - 0. 127 20 c20 - 0 .12 7 -0 .127 -0 .127 - 0 .127
1 0 .3 14 0 .1585 - 0 .46 19 c19 - 0 .7 14 - 0. 70 1 - 0 .66 5 - 0 .609
? n "'7l'l n :>;7Sl'l _n" '0 ~I " _7 1 _ ? nn.:. - I 7 4 <1 _ I :>;l'l l'l

19 5 .969 - 0 .46 0 .158 5 1 cl - 114 .4 32 .361 l E-1 5 - 2E- 16


20 6 .283 - 0 .127 -0 .127 0 cO - 119 .5 30 .65 1.103 -0 .127
( pn) y (f1ipped) csum -5 .12 7 - 0 .127 1.10 3 - 0 .127
Nota bene : Equal int er v al s r equired. dsum 0 10 6E-16 -2E-16
7. Fourier template 3 is a compact Ref : Numerical Met hods that lI'o r k I
fn - 0 .2 5 2 E-15 0 . 123 0 I
analysis as described by F. S. Acton in the F. S. Acto n; Har per , Row ; 19 70 . err 9E- 16 2E- 17
book Numerical Methods That Work. The gnI 0 1 ('E - 17 0 I
method can accept an arbitrary number err -7E-16
of equally space data points.

Fourier [ Hamp le 3 Form ulas : !


4 I s 8 9 I 10 I 11
4 ! Acton's Cornoact ion
:; 10 i2 .~ L n '0 i - RC[- 1)+1
s bO i- 0.25 -I Ikma x loh i n ,-R[- l )C* dohi i - R[- l ]C* dphi
7 no i se1 0 .~ p..h.i c phi n2 -2*COS(phin) i - 2 * COS(phin)
8 kma x! - MAX(") =2 * oi * di cKn2 '0 10

8. A partial listing of the 'program' for


template 3. Notice that a single sine and
cosine pair is evaluated for each
harmonic coefficient. In the second
column shown, the data vector y is
'flipped' to simplify the downward
recurrence for the harmonic
coefficients.

Discussion cos nt/J = (2 cosel-cosrn + Ilt/J - cos(n + 2)t/J


In spreadsheet programming, higher efficiency is and
possible with more careful planning and improved
mathematical technique, The Fourier analysis process
sin nt/J = (2 c0stPl·sin(n + 1)t/J - sin(n + 2)t/J
has been the focus of a great deal of optimization in the with the observation that:
last couple of decades, The FFT process earlier permits K

vel)' rapid analysis, at the expense of substantially more '2.Jikt/Jn ) cos kt/Jn = [co - CI costPn ]
k~o
involved logic, restriction to uniform data spacing and to
a number of data points that can be decomposed into a and
K
product of powers of small prime numbers [z'" ·3" ...].
"Lfikt/Jn) sinkt/Jn = C 1 sint/Jn,
A compact and elegant process arising from a k=o
suggestion in F. S. Acton's book Numerical Methods That
Work, Harper and Row, 1970, provides an intermediate
when ck = (2 cost/Jn ),ck + 1 - ck + 2 + fl.kt/Jn), for
k=K, ...,I,O, with CK+l =CK+2 =0.
level of speed and complexity. It applies to uniformly
spaced data with arbitral)' number of points, and gives
an attractively simple template requiring only one sine
FOR FURTHER READING
and cosine per harmonic. The scaling of calculation time Numerical Methods That Work, mentioned above,
is asjM times nmex rather thanjM InjM for the FFT gives a fine discussion of the merits of using recurrence
process; it can be competitive then when only a few tens formulas to extend tables of standard functions. A more
of data points are needed and the required highest recent book is Numerical Recipes by Press, Flannel)',
harmonic, nmex, is not large. The speed is about twice Teolkowsky and Vetterling, Cambridge Press, 1986.
that of Example I, and four times that of Example 2.; Readers' suggestions for useful or especially effective
storage requirements are slightly (10%) under those for texts on numerical/mathematical methods would be
Example 1, twice that of Example 2. appreciated. They can be sent in care of Computers In
The process is based on recurrence formulas for Physics, American Institute of Physics, 335 East 45th
cosines and sines: Street, New York, N.Y., 10017. •

86 COMPUTERS IN PHYSICS, NOV/DEC 1988

You might also like