3 The discrete Fourier transform Page 1 3.1 Introduction Let f(t) be a function describing a physical process in the time domain. An equivalent representation of the process, F (ν), is possible in the frequency domain, and the two representations are connected by means of the direct and inverse Fourier transforms: F (ν) = f(t) = Z Z f(t) e 2πiνt dt (3.1) F (ν) e 2πiνt dν. (3.2) In the most common situations, the function f(t) is sampled at a finite number of time arguments. Suppose that we have N consecutive sampled values: f f(t ), with t ( 1), =1, 2,...,N, (3.3) where denotes the sampling interval. We consider the N discrete frequencies ν k = k 1 1, k =1, 2,...,N,
2 3. The discrete Fourier transform and approximate the integral (3.1) by a discrete sum: F (ν k ) ' f e 2πiν kt = =1 =1 f exp 2πi ( 1)(k 1). The last sum is called the discrete Fourier transform of the N values f : ( 1)(k 1) F k = f exp 2πi, k =1, 2,...,N. (3.4) =1 The discrete Fourier transform maps N complex numbers, f,inton complex numbers, F k. Having the N values F k of the discrete Fourier transform, the N original values f can be recovered by the discrete inverse Fourier transform: f = 1 N k=1 ( 1)(k 1) F k exp 2πi. (3.5) It is obvious that a routine designed to calculate the discrete Fourier transform can calculate the inverse transform, too. There are two important particular cases of the Fourier transform. If the transformed function f(t) vanishes at the origin (f(0) f 0 =0), than the basis functions should behave similarly and the natural transform in this case is the sine transform: ( 1)(k 1) F k = f sin π, k =1, 2,...,N. (3.6) =1 If the derivative of the transformed function f(t) vanishes at the origin, than the basis functions should show the same behavior, too, and the natural transform in this case is the cosine transform: N 1 X ( 1)(k 1) F k = f cos π + 1 i hf 1 +( 1) k 1 f N, k =1, 2,...,N. 2 =2 (3.7) The cosine transform is its own inverse, N 1 X ( 1)(k 1) f = F k cos π + 1 F1 +( 1) 1 F N, =1, 2,...,N, 2 k=2 (3.8)
3.1 Introduction 3 and the transformed values F k appear in the Fourier expansion of f as weights of the cosine functions with the frequencies ν k = k 1 1, k =1, 2,...,N. (3.9) 2 We remind that t =( 1). The straightforward implementation of the discrete cosine transform (3.7)- (3.9) is:!============================================================================ subroutine FTcos0(f,ft,n)!----------------------------------------------------------------------------! Straightforward implementation of the discrete cosine transform!! f - input data! ft - transformed data! n - no. of data values! Frequencies corresponding to the transformed data:! freq(k) = (k-1) / (2.d0 * dt * (n-1)), k=1,n!---------------------------------------------------------------------------- implicit real(8) (a-h,o-z) real(8), parameter :: pi = 3.1415926535897932385d0 real(8) f(n), ft(n) isgn = 1 fact = pi / (n-1) do k = 1,n theta = (k-1) * fact sum = 0.d0 do = 2,n-1 sum = sum + f() * cos((-1)*theta) ft(k) = sum + 0.5d0 * (f(1) + isgn*f(n)) isgn = -isgn end Due to the repeated evaluations of the cosine function, the above implementation is highly inefficient and the execution time becomes critical when the number of observations N exceeds 10 5.
4 3. The discrete Fourier transform 3.2 Efficient discrete Fourier transform We consider only the sum in expression (3.7) of the cosine transform F k, N 1 X =2 f cos ( 1)θ 0 k, (3.10) with θ 0 k = k 1 π, (3.11) and split it in even- and odd- terms: X f cos ( 1)θ 0 X k + f cos ( 1)θ 0 k. =2,4 =3,5 The addition formula cos [( 1)θ] =cosθcos(θ)+sinθsin(θ) is used to expand the first sum, and the index is changed to +1in the second one: X f Ck cos(θ 0 k )+S k sin(θ 0 k ) + X f +1 cos(θ 0 k ), where =2,4 C k =cosθ 0 k, S k =sinθ 0 k. =2,4 Now, we replace the index by 2 in both sums and group together the sine and cosine functions with the same argument: Here we use the notations: MX [U cos(θ k )+V sin(θ k )]. (3.12) =1 U = C k f 2 + f 2+1, V = S k f 2, (3.13) θ k =2θ 0 k, (3.14) and M = 2 is the integer part of ()/2. (3.15)
3.2 Efficient discrete Fourier transform 5 Next, the arguments of the trigonometric functions will be modified in the even- moiety. To this end, G k is split first into odd- and even- terms, and in the odd- terms the argument θ k is replaced by ( +1)θ k θ k : + =1,3 =2,4 {U cos [( +1)θ k θ k ]+V sin [( +1)θ k θ k ]} [U cos(θ k )+V sin(θ k )]. In the first sum, addition formulas for the modified cosine and sine functions are applied: + + =1,3 =1,3 =2,4 U {cos [( +1)θ k ]cosθ k + sin [( +1)θ k ]sinθ k } V {sin [( +1)θ k ]cosθ k cos [( +1)θ k ]sinθ k } [U cos(θ k )+V sin(θ k )]. Now, all arguments imply even multiples of θ k. In order to be able to group the trigonometric functions, +1is replace by 2 in the first two sums, and by 2 in the last one: X =1 U 2 1 {cos(2θ k )cosθ k +sin(2θ k )sinθ k } + X =1 V 2 1 {sin(2θ k )cosθ k cos(2θ k )sinθ k } + X [U 2 cos(2θ k )+V 2 sin(2θ k )]. =1 Collecting the terms and making the notations: U (1) = U 2 1 cos θ k V 2 1 sin θ k + U 2 V (1) = U 2 1 sin θ k + V 2 1 cos θ k + V 2, (3.16)
6 3. The discrete Fourier transform leads to the following form of G k : [(M+1)/2] X =1 h i U (1) cos(2θ k )+V (1) sin(2θ k ), (3.17) where [(M +1)/2] is the integer part of (M +1)/2. ForoddM, the new coefficients, U (1) and V (1),implyfor =[(M +1)/2] the old coefficients, U M+1 and V M+1, which do not actually exist. These terms should be interpreted as zero. By comparing expression (3.17) of G k with its original form (3.12), it can be seen that at the expense of having to evaluate a new set of coefficients (U (1) and V (1) for =1, [(M +1)/2]), the number of summed terms was condensed by a factor of almost 2 (according to the parity of M). The corresponding sine and cosine functions may be easily evaluated from the duplication formulas: sin(2θ k ) = 2sin(θ k )cos(θ k ), cos(2θ k ) = 2cos 2 (θ k ) 1. (3.18) A careful analysis shows that if such a condensation procedure is repeated N cond times, where N cond =[log 2 (2M 1)], (3.19) then ust two terms remain and one gets the final expression: U (N cond) 1 cos(2 N cond θ k )+V (N cond) 1 sin(2 N cond θ k ), (3.20) from which G k canbeeasilyevaluated. By this condensation procedure, the number of sines and cosines evaluationsisreducedfromm to N cond, or, for instance, from 1000 to 10. Actually, one ust has to evaluate before starting the condensation procedure cos θ k and sin θ k for =1,M, the rest of the sine and cosine functions forming a sequence which can be readily generated by the duplication formulas (3.18). The routine listed below represents the exact implementation of the presented algorithm.!============================================================================ subroutine FTcos(f,ft,n)!----------------------------------------------------------------------------! Calculates the cosine transform of an array f of n real values.! The transformed data is returned in array ft.!---------------------------------------------------------------------------- implicit real(8) (a-h,o-z)
Bibliografie 7 parameter (pi = 3.1415926535897932385d0) real(8) f(n), ft(n) real(8), allocatable :: u(:), v(:) n2 = (n-1)/2 allocate(u(n2+1), v(n2+1)) ncond = int(log(2.d0*n2-1.d0)/log(2.d0)) isgn = 1 fact = pi / (n-1) fn = f(n); f(n) = 0.d0 do k = 1,n theta = (k-1) * fact c = cos(theta); s = sin(theta) do = 1,n2 2 = 2 * u() = c * f(2) + f(2+1) v() = s * f(2) s = 2.d0 * s * c c = 2.d0 * c * c - 1.d0 nterm = n2 do icond = 1,ncond u(nterm+1) = 0.d0; v(nterm+1) = 0.d0 nterm = int((nterm+1)/2) do = 1,nterm 2 = 2* - 1 utmp = c * u(2) - s * v(2) + u(2+1) v() = s * u(2) + c * v(2) + v(2+1) u() = utmp s = 2.d0 * s * c c = 2.d0 * c * c - 1.d0 ft(k) = c * u(1) + s * v(1) + 0.5d0 * (f(1) + isgn*fn) isgn = -isgn deallocate(u,v) end Bibliografie [1] Aubanel, E.E. and Oldham, K.B.., "Fourier smoothing without the fast Fourier transform", Byte, February 1985, p. 207.
8 Bibliografie [2] W.H. Press, S.A. Teukolsky, W.T. Vetterling, B.P Flannery, Numerical Recipes in C, Second Edition (Cambridge University Press, Cambridge, 1992).