2 @cindex Fast Fourier Transforms, see FFT
3 @cindex Fourier Transforms, see FFT
4 @cindex Discrete Fourier Transforms, see FFT
7 This chapter describes functions for performing Fast Fourier Transforms
8 (FFTs). The library includes radix-2 routines (for lengths which are a
9 power of two) and mixed-radix routines (which work for any length). For
10 efficiency there are separate versions of the routines for real data and
11 for complex data. The mixed-radix routines are a reimplementation of the
12 @sc{fftpack} library of Paul Swarztrauber. Fortran code for @sc{fftpack} is
13 available on Netlib (@sc{fftpack} also includes some routines for sine and
14 cosine transforms but these are currently not available in GSL). For
15 details and derivations of the underlying algorithms consult the
16 document @cite{GSL FFT Algorithms} (@pxref{FFT References and Further Reading})
19 * Mathematical Definitions::
20 * Overview of complex data FFTs::
21 * Radix-2 FFT routines for complex data::
22 * Mixed-radix FFT routines for complex data::
23 * Overview of real data FFTs::
24 * Radix-2 FFT routines for real data::
25 * Mixed-radix FFT routines for real data::
26 * FFT References and Further Reading::
29 @node Mathematical Definitions
30 @section Mathematical Definitions
31 @cindex FFT mathematical definition
33 Fast Fourier Transforms are efficient algorithms for
34 calculating the discrete fourier transform (DFT),
38 x_j = \sum_{k=0}^{N-1} z_k \exp(-2\pi i j k / N)
45 x_j = \sum_@{k=0@}^@{N-1@} z_k \exp(-2\pi i j k / N)
49 The DFT usually arises as an approximation to the continuous fourier
50 transform when functions are sampled at discrete intervals in space or
51 time. The naive evaluation of the discrete fourier transform is a
52 matrix-vector multiplication
54 @math{W\vec@{z@}}. A general matrix-vector multiplication takes
55 @math{O(N^2)} operations for @math{N} data-points. Fast fourier
56 transform algorithms use a divide-and-conquer strategy to factorize the
57 matrix @math{W} into smaller sub-matrices, corresponding to the integer
58 factors of the length @math{N}. If @math{N} can be factorized into a
60 @c{$f_1 f_2 \ldots f_n$}
61 @math{f_1 f_2 ... f_n} then the DFT can be computed in @math{O(N \sum
62 f_i)} operations. For a radix-2 FFT this gives an operation count of
65 All the FFT functions offer three types of transform: forwards, inverse
66 and backwards, based on the same mathematical definitions. The
67 definition of the @dfn{forward fourier transform},
68 @c{$x = \hbox{FFT}(z)$}
69 @math{x = FFT(z)}, is,
73 x_j = \sum_{k=0}^{N-1} z_k \exp(-2\pi i j k / N)
80 x_j = \sum_@{k=0@}^@{N-1@} z_k \exp(-2\pi i j k / N)
85 and the definition of the @dfn{inverse fourier transform},
86 @c{$x = \hbox{IFFT}(z)$}
87 @math{x = IFFT(z)}, is,
91 z_j = {1 \over N} \sum_{k=0}^{N-1} x_k \exp(2\pi i j k / N).
98 z_j = @{1 \over N@} \sum_@{k=0@}^@{N-1@} x_k \exp(2\pi i j k / N).
103 The factor of @math{1/N} makes this a true inverse. For example, a call
104 to @code{gsl_fft_complex_forward} followed by a call to
105 @code{gsl_fft_complex_inverse} should return the original data (within
108 In general there are two possible choices for the sign of the
109 exponential in the transform/ inverse-transform pair. GSL follows the
110 same convention as @sc{fftpack}, using a negative exponential for the forward
111 transform. The advantage of this convention is that the inverse
112 transform recreates the original function with simple fourier
113 synthesis. Numerical Recipes uses the opposite convention, a positive
114 exponential in the forward transform.
116 The @dfn{backwards FFT} is simply our terminology for an unscaled
117 version of the inverse FFT,
121 z^{backwards}_j = \sum_{k=0}^{N-1} x_k \exp(2\pi i j k / N).
128 z^@{backwards@}_j = \sum_@{k=0@}^@{N-1@} x_k \exp(2\pi i j k / N).
133 When the overall scale of the result is unimportant it is often
134 convenient to use the backwards FFT instead of the inverse to save
135 unnecessary divisions.
137 @node Overview of complex data FFTs
138 @section Overview of complex data FFTs
139 @cindex FFT, complex data
141 The inputs and outputs for the complex FFT routines are @dfn{packed
142 arrays} of floating point numbers. In a packed array the real and
143 imaginary parts of each complex number are placed in alternate
144 neighboring elements. For example, the following definition of a packed
149 gsl_complex_packed_array data = x;
153 can be used to hold an array of three complex numbers, @code{z[3]}, in
166 The array indices for the data have the same ordering as those
167 in the definition of the DFT---i.e. there are no index transformations
168 or permutations of the data.
170 A @dfn{stride} parameter allows the user to perform transforms on the
171 elements @code{z[stride*i]} instead of @code{z[i]}. A stride greater
172 than 1 can be used to take an in-place FFT of the column of a matrix. A
173 stride of 1 accesses the array without any additional spacing between
176 To perform an FFT on a vector argument, such as @code{gsl_vector_complex
177 * v}, use the following definitions (or their equivalents) when calling
178 the functions described in this chapter:
181 gsl_complex_packed_array data = v->data;
182 size_t stride = v->stride;
186 For physical applications it is important to remember that the index
187 appearing in the DFT does not correspond directly to a physical
188 frequency. If the time-step of the DFT is @math{\Delta} then the
189 frequency-domain includes both positive and negative frequencies,
190 ranging from @math{-1/(2\Delta)} through 0 to @math{+1/(2\Delta)}. The
191 positive frequencies are stored from the beginning of the array up to
192 the middle, and the negative frequencies are stored backwards from the
195 Here is a table which shows the layout of the array @var{data}, and the
196 correspondence between the time-domain data @math{z}, and the
197 frequency-domain data @math{x}.
203 1 z(t = 1) x(f = 1/(N Delta))
204 2 z(t = 2) x(f = 2/(N Delta))
205 . ........ ..................
206 N/2 z(t = N/2) x(f = +1/(2 Delta),
208 . ........ ..................
209 N-3 z(t = N-3) x(f = -3/(N Delta))
210 N-2 z(t = N-2) x(f = -2/(N Delta))
211 N-1 z(t = N-1) x(f = -1/(N Delta))
215 When @math{N} is even the location @math{N/2} contains the most positive
216 and negative frequencies (@math{+1/(2 \Delta)}, @math{-1/(2 \Delta)})
217 which are equivalent. If @math{N} is odd then general structure of the
218 table above still applies, but @math{N/2} does not appear.
221 @node Radix-2 FFT routines for complex data
222 @section Radix-2 FFT routines for complex data
223 @cindex FFT of complex data, radix-2 algorithm
224 @cindex Radix-2 FFT, complex data
226 The radix-2 algorithms described in this section are simple and compact,
227 although not necessarily the most efficient. They use the Cooley-Tukey
228 algorithm to compute in-place complex FFTs for lengths which are a power
229 of 2---no additional storage is required. The corresponding
230 self-sorting mixed-radix routines offer better performance at the
231 expense of requiring additional working space.
233 All the functions described in this section are declared in the header file @file{gsl_fft_complex.h}.
235 @deftypefun int gsl_fft_complex_radix2_forward (gsl_complex_packed_array @var{data}, size_t @var{stride}, size_t @var{n})
237 @deftypefunx int gsl_fft_complex_radix2_transform (gsl_complex_packed_array @var{data}, size_t @var{stride}, size_t @var{n}, gsl_fft_direction @var{sign})
239 @deftypefunx int gsl_fft_complex_radix2_backward (gsl_complex_packed_array @var{data}, size_t @var{stride}, size_t @var{n})
241 @deftypefunx int gsl_fft_complex_radix2_inverse (gsl_complex_packed_array @var{data}, size_t @var{stride}, size_t @var{n})
243 These functions compute forward, backward and inverse FFTs of length
244 @var{n} with stride @var{stride}, on the packed complex array @var{data}
245 using an in-place radix-2 decimation-in-time algorithm. The length of
246 the transform @var{n} is restricted to powers of two. For the
247 @code{transform} version of the function the @var{sign} argument can be
248 either @code{forward} (@math{-1}) or @code{backward} (@math{+1}).
250 The functions return a value of @code{GSL_SUCCESS} if no errors were
251 detected, or @code{GSL_EDOM} if the length of the data @var{n} is not a
255 @deftypefun int gsl_fft_complex_radix2_dif_forward (gsl_complex_packed_array @var{data}, size_t @var{stride}, size_t @var{n})
257 @deftypefunx int gsl_fft_complex_radix2_dif_transform (gsl_complex_packed_array @var{data}, size_t @var{stride}, size_t @var{n}, gsl_fft_direction @var{sign})
259 @deftypefunx int gsl_fft_complex_radix2_dif_backward (gsl_complex_packed_array @var{data}, size_t @var{stride}, size_t @var{n})
261 @deftypefunx int gsl_fft_complex_radix2_dif_inverse (gsl_complex_packed_array @var{data}, size_t @var{stride}, size_t @var{n})
263 These are decimation-in-frequency versions of the radix-2 FFT functions.
268 @comment @node Example of using radix-2 FFT routines for complex data
269 @comment @subsection Example of using radix-2 FFT routines for complex data
271 Here is an example program which computes the FFT of a short pulse in a
272 sample of length 128. To make the resulting fourier transform real the
273 pulse is defined for equal positive and negative times (@math{-10}
274 @dots{} @math{10}), where the negative times wrap around the end of the
278 @verbatiminclude examples/fft.c
282 Note that we have assumed that the program is using the default error
283 handler (which calls @code{abort} for any errors). If you are not using
284 a safe error handler you would need to check the return status of
285 @code{gsl_fft_complex_radix2_forward}.
287 The transformed data is rescaled by @math{1/\sqrt N} so that it fits on
288 the same plot as the input. Only the real part is shown, by the choice
289 of the input data the imaginary part is zero. Allowing for the
290 wrap-around of negative times at @math{t=128}, and working in units of
291 @math{k/N}, the DFT approximates the continuum fourier transform, giving
292 a modulated sine function.
297 \int_{-a}^{+a} e^{-2 \pi i k x} dx = {\sin(2\pi k a) \over\pi k}
303 @center @image{fft-complex-radix2-t,2.8in}
304 @center @image{fft-complex-radix2-f,2.8in}
306 A pulse and its discrete fourier transform, output from
311 @node Mixed-radix FFT routines for complex data
312 @section Mixed-radix FFT routines for complex data
313 @cindex FFT of complex data, mixed-radix algorithm
314 @cindex Mixed-radix FFT, complex data
316 This section describes mixed-radix FFT algorithms for complex data. The
317 mixed-radix functions work for FFTs of any length. They are a
318 reimplementation of Paul Swarztrauber's Fortran @sc{fftpack} library.
319 The theory is explained in the review article @cite{Self-sorting
320 Mixed-radix FFTs} by Clive Temperton. The routines here use the same
321 indexing scheme and basic algorithms as @sc{fftpack}.
323 The mixed-radix algorithm is based on sub-transform modules---highly
324 optimized small length FFTs which are combined to create larger FFTs.
325 There are efficient modules for factors of 2, 3, 4, 5, 6 and 7. The
326 modules for the composite factors of 4 and 6 are faster than combining
327 the modules for @math{2*2} and @math{2*3}.
329 For factors which are not implemented as modules there is a fall-back to
330 a general length-@math{n} module which uses Singleton's method for
331 efficiently computing a DFT. This module is @math{O(n^2)}, and slower
332 than a dedicated module would be but works for any length @math{n}. Of
333 course, lengths which use the general length-@math{n} module will still
334 be factorized as much as possible. For example, a length of 143 will be
335 factorized into @math{11*13}. Large prime factors are the worst case
336 scenario, e.g. as found in @math{n=2*3*99991}, and should be avoided
337 because their @math{O(n^2)} scaling will dominate the run-time (consult
338 the document @cite{GSL FFT Algorithms} included in the GSL distribution
339 if you encounter this problem).
341 The mixed-radix initialization function @code{gsl_fft_complex_wavetable_alloc}
342 returns the list of factors chosen by the library for a given length
343 @math{N}. It can be used to check how well the length has been
344 factorized, and estimate the run-time. To a first approximation the
345 run-time scales as @math{N \sum f_i}, where the @math{f_i} are the
346 factors of @math{N}. For programs under user control you may wish to
347 issue a warning that the transform will be slow when the length is
348 poorly factorized. If you frequently encounter data lengths which
349 cannot be factorized using the existing small-prime modules consult
350 @cite{GSL FFT Algorithms} for details on adding support for other
353 @comment First, the space for the trigonometric lookup tables and scratch area is
354 @comment allocated by a call to one of the @code{alloc} functions. We
355 @comment call the combination of factorization, scratch space and trigonometric
356 @comment lookup arrays a @dfn{wavetable}. It contains the sine and cosine
357 @comment waveforms for the all the frequencies that will be used in the FFT.
359 @comment The wavetable is initialized by a call to the corresponding @code{init}
360 @comment function. It factorizes the data length, using the implemented
361 @comment subtransforms as preferred factors wherever possible. The trigonometric
362 @comment lookup table for the chosen factorization is also computed.
364 @comment An FFT is computed by a call to one of the @code{forward},
365 @comment @code{backward} or @code{inverse} functions, with the data, length and
366 @comment wavetable as arguments.
368 All the functions described in this section are declared in the header
369 file @file{gsl_fft_complex.h}.
371 @deftypefun {gsl_fft_complex_wavetable *} gsl_fft_complex_wavetable_alloc (size_t @var{n})
372 This function prepares a trigonometric lookup table for a complex FFT of
373 length @var{n}. The function returns a pointer to the newly allocated
374 @code{gsl_fft_complex_wavetable} if no errors were detected, and a null
375 pointer in the case of error. The length @var{n} is factorized into a
376 product of subtransforms, and the factors and their trigonometric
377 coefficients are stored in the wavetable. The trigonometric coefficients
378 are computed using direct calls to @code{sin} and @code{cos}, for
379 accuracy. Recursion relations could be used to compute the lookup table
380 faster, but if an application performs many FFTs of the same length then
381 this computation is a one-off overhead which does not affect the final
384 The wavetable structure can be used repeatedly for any transform of the
385 same length. The table is not modified by calls to any of the other FFT
386 functions. The same wavetable can be used for both forward and backward
387 (or inverse) transforms of a given length.
390 @deftypefun void gsl_fft_complex_wavetable_free (gsl_fft_complex_wavetable * @var{wavetable})
391 This function frees the memory associated with the wavetable
392 @var{wavetable}. The wavetable can be freed if no further FFTs of the
393 same length will be needed.
397 These functions operate on a @code{gsl_fft_complex_wavetable} structure
398 which contains internal parameters for the FFT. It is not necessary to
399 set any of the components directly but it can sometimes be useful to
400 examine them. For example, the chosen factorization of the FFT length
401 is given and can be used to provide an estimate of the run-time or
402 numerical error. The wavetable structure is declared in the header file
403 @file{gsl_fft_complex.h}.
405 @deftp {Data Type} gsl_fft_complex_wavetable
406 This is a structure that holds the factorization and trigonometric
407 lookup tables for the mixed radix fft algorithm. It has the following
412 This is the number of complex data points
415 This is the number of factors that the length @code{n} was decomposed into.
417 @item size_t factor[64]
418 This is the array of factors. Only the first @code{nf} elements are
421 @comment (FIXME: This is a fixed length array and therefore probably in
422 @comment violation of the GNU Coding Standards).
424 @item gsl_complex * trig
425 This is a pointer to a preallocated trigonometric lookup table of
426 @code{n} complex elements.
428 @item gsl_complex * twiddle[64]
429 This is an array of pointers into @code{trig}, giving the twiddle
430 factors for each pass.
435 The mixed radix algorithms require additional working space to hold
436 the intermediate steps of the transform.
438 @deftypefun {gsl_fft_complex_workspace *} gsl_fft_complex_workspace_alloc (size_t @var{n})
439 This function allocates a workspace for a complex transform of length
443 @deftypefun void gsl_fft_complex_workspace_free (gsl_fft_complex_workspace * @var{workspace})
444 This function frees the memory associated with the workspace
445 @var{workspace}. The workspace can be freed if no further FFTs of the
446 same length will be needed.
449 @comment @deftp {Data Type} gsl_fft_complex_workspace
450 @comment This is a structure that holds the workspace for the mixed radix fft
451 @comment algorithm. It has the following components:
453 @comment @table @code
454 @comment @item gsl_complex * scratch
455 @comment This is a pointer to a workspace of @code{n} complex elements,
456 @comment capable of holding intermediate copies of the original data set.
461 The following functions compute the transform,
463 @deftypefun int gsl_fft_complex_forward (gsl_complex_packed_array @var{data}, size_t @var{stride}, size_t @var{n}, const gsl_fft_complex_wavetable * @var{wavetable}, gsl_fft_complex_workspace * @var{work})
464 @deftypefunx int gsl_fft_complex_transform (gsl_complex_packed_array @var{data}, size_t @var{stride}, size_t @var{n}, const gsl_fft_complex_wavetable * @var{wavetable}, gsl_fft_complex_workspace * @var{work}, gsl_fft_direction @var{sign})
465 @deftypefunx int gsl_fft_complex_backward (gsl_complex_packed_array @var{data}, size_t @var{stride}, size_t @var{n}, const gsl_fft_complex_wavetable * @var{wavetable}, gsl_fft_complex_workspace * @var{work})
466 @deftypefunx int gsl_fft_complex_inverse (gsl_complex_packed_array @var{data}, size_t @var{stride}, size_t @var{n}, const gsl_fft_complex_wavetable * @var{wavetable}, gsl_fft_complex_workspace * @var{work})
468 These functions compute forward, backward and inverse FFTs of length
469 @var{n} with stride @var{stride}, on the packed complex array
470 @var{data}, using a mixed radix decimation-in-frequency algorithm.
471 There is no restriction on the length @var{n}. Efficient modules are
472 provided for subtransforms of length 2, 3, 4, 5, 6 and 7. Any remaining
473 factors are computed with a slow, @math{O(n^2)}, general-@math{n}
474 module. The caller must supply a @var{wavetable} containing the
475 trigonometric lookup tables and a workspace @var{work}. For the
476 @code{transform} version of the function the @var{sign} argument can be
477 either @code{forward} (@math{-1}) or @code{backward} (@math{+1}).
479 The functions return a value of @code{0} if no errors were detected. The
480 following @code{gsl_errno} conditions are defined for these functions:
484 The length of the data @var{n} is not a positive integer (i.e. @var{n}
488 The length of the data @var{n} and the length used to compute the given
489 @var{wavetable} do not match.
493 @comment @node Example of using mixed-radix FFT routines for complex data
494 @comment @subsection Example of using mixed-radix FFT routines for complex data
496 Here is an example program which computes the FFT of a short pulse in a
497 sample of length 630 (@math{=2*3*3*5*7}) using the mixed-radix
501 @verbatiminclude examples/fftmr.c
505 Note that we have assumed that the program is using the default
506 @code{gsl} error handler (which calls @code{abort} for any errors). If
507 you are not using a safe error handler you would need to check the
508 return status of all the @code{gsl} routines.
510 @node Overview of real data FFTs
511 @section Overview of real data FFTs
512 @cindex FFT of real data
513 The functions for real data are similar to those for complex data.
514 However, there is an important difference between forward and inverse
515 transforms. The fourier transform of a real sequence is not real. It is
516 a complex sequence with a special symmetry:
532 A sequence with this symmetry is called @dfn{conjugate-complex} or
533 @dfn{half-complex}. This different structure requires different
534 storage layouts for the forward transform (from real to half-complex)
535 and inverse transform (from half-complex back to real). As a
536 consequence the routines are divided into two sets: functions in
537 @code{gsl_fft_real} which operate on real sequences and functions in
538 @code{gsl_fft_halfcomplex} which operate on half-complex sequences.
540 Functions in @code{gsl_fft_real} compute the frequency coefficients of a
541 real sequence. The half-complex coefficients @math{c} of a real sequence
542 @math{x} are given by fourier analysis,
546 c_k = \sum_{j=0}^{N-1} x_j \exp(-2 \pi i j k /N)
553 c_k = \sum_@{j=0@}^@{N-1@} x_j \exp(-2 \pi i j k /N)
558 Functions in @code{gsl_fft_halfcomplex} compute inverse or backwards
559 transforms. They reconstruct real sequences by fourier synthesis from
560 their half-complex frequency coefficients, @math{c},
564 x_j = {1 \over N} \sum_{k=0}^{N-1} c_k \exp(2 \pi i j k /N)
571 x_j = @{1 \over N@} \sum_@{k=0@}^@{N-1@} c_k \exp(2 \pi i j k /N)
576 The symmetry of the half-complex sequence implies that only half of the
577 complex numbers in the output need to be stored. The remaining half can
578 be reconstructed using the half-complex symmetry condition. This works
579 for all lengths, even and odd---when the length is even the middle value
580 where @math{k=N/2} is also real. Thus only @var{N} real numbers are
581 required to store the half-complex sequence, and the transform of a real
582 sequence can be stored in the same size array as the original data.
584 The precise storage arrangements depend on the algorithm, and are
585 different for radix-2 and mixed-radix routines. The radix-2 function
586 operates in-place, which constrains the locations where each element can
587 be stored. The restriction forces real and imaginary parts to be stored
588 far apart. The mixed-radix algorithm does not have this restriction, and
589 it stores the real and imaginary parts of a given term in neighboring
590 locations (which is desirable for better locality of memory accesses).
592 @node Radix-2 FFT routines for real data
593 @section Radix-2 FFT routines for real data
594 @cindex FFT of real data, radix-2 algorithm
595 @cindex Radix-2 FFT for real data
597 This section describes radix-2 FFT algorithms for real data. They use
598 the Cooley-Tukey algorithm to compute in-place FFTs for lengths which
601 The radix-2 FFT functions for real data are declared in the header files
602 @file{gsl_fft_real.h}
604 @deftypefun int gsl_fft_real_radix2_transform (double @var{data}[], size_t @var{stride}, size_t @var{n})
606 This function computes an in-place radix-2 FFT of length @var{n} and
607 stride @var{stride} on the real array @var{data}. The output is a
608 half-complex sequence, which is stored in-place. The arrangement of the
609 half-complex terms uses the following scheme: for @math{k < N/2} the
610 real part of the @math{k}-th term is stored in location @math{k}, and
611 the corresponding imaginary part is stored in location @math{N-k}. Terms
612 with @math{k > N/2} can be reconstructed using the symmetry
613 @c{$z_k = z^*_{N-k}$}
614 @math{z_k = z^*_@{N-k@}}.
615 The terms for @math{k=0} and @math{k=N/2} are both purely
616 real, and count as a special case. Their real parts are stored in
617 locations @math{0} and @math{N/2} respectively, while their imaginary
618 parts which are zero are not stored.
620 The following table shows the correspondence between the output
621 @var{data} and the equivalent results obtained by considering the input
622 data as a complex sequence with zero imaginary part,
625 complex[0].real = data[0]
627 complex[1].real = data[1]
628 complex[1].imag = data[N-1]
629 ............... ................
630 complex[k].real = data[k]
631 complex[k].imag = data[N-k]
632 ............... ................
633 complex[N/2].real = data[N/2]
634 complex[N/2].imag = 0
635 ............... ................
636 complex[k'].real = data[k] k' = N - k
637 complex[k'].imag = -data[N-k]
638 ............... ................
639 complex[N-1].real = data[1]
640 complex[N-1].imag = -data[N-1]
643 Note that the output data can be converted into the full complex
644 sequence using the function @code{gsl_fft_halfcomplex_unpack}
645 described in the next section.
648 The radix-2 FFT functions for halfcomplex data are declared in the
649 header file @file{gsl_fft_halfcomplex.h}.
651 @deftypefun int gsl_fft_halfcomplex_radix2_inverse (double @var{data}[], size_t @var{stride}, size_t @var{n})
652 @deftypefunx int gsl_fft_halfcomplex_radix2_backward (double @var{data}[], size_t @var{stride}, size_t @var{n})
654 These functions compute the inverse or backwards in-place radix-2 FFT of
655 length @var{n} and stride @var{stride} on the half-complex sequence
656 @var{data} stored according the output scheme used by
657 @code{gsl_fft_real_radix2}. The result is a real array stored in natural
664 @node Mixed-radix FFT routines for real data
665 @section Mixed-radix FFT routines for real data
666 @cindex FFT of real data, mixed-radix algorithm
667 @cindex Mixed-radix FFT, real data
669 This section describes mixed-radix FFT algorithms for real data. The
670 mixed-radix functions work for FFTs of any length. They are a
671 reimplementation of the real-FFT routines in the Fortran @sc{fftpack} library
672 by Paul Swarztrauber. The theory behind the algorithm is explained in
673 the article @cite{Fast Mixed-Radix Real Fourier Transforms} by Clive
674 Temperton. The routines here use the same indexing scheme and basic
675 algorithms as @sc{fftpack}.
677 The functions use the @sc{fftpack} storage convention for half-complex
678 sequences. In this convention the half-complex transform of a real
679 sequence is stored with frequencies in increasing order, starting at
680 zero, with the real and imaginary parts of each frequency in neighboring
681 locations. When a value is known to be real the imaginary part is not
682 stored. The imaginary part of the zero-frequency component is never
683 stored. It is known to be zero (since the zero frequency component is
684 simply the sum of the input data (all real)). For a sequence of even
685 length the imaginary part of the frequency @math{n/2} is not stored
686 either, since the symmetry
687 @c{$z_k = z_{N-k}^*$}
688 @math{z_k = z_@{N-k@}^*} implies that this is
691 The storage scheme is best shown by some examples. The table below
692 shows the output for an odd-length sequence, @math{n=5}. The two columns
693 give the correspondence between the 5 values in the half-complex
694 sequence returned by @code{gsl_fft_real_transform}, @var{halfcomplex}[] and the
695 values @var{complex}[] that would be returned if the same real input
696 sequence were passed to @code{gsl_fft_complex_backward} as a complex
697 sequence (with imaginary parts set to @code{0}),
700 complex[0].real = halfcomplex[0]
702 complex[1].real = halfcomplex[1]
703 complex[1].imag = halfcomplex[2]
704 complex[2].real = halfcomplex[3]
705 complex[2].imag = halfcomplex[4]
706 complex[3].real = halfcomplex[3]
707 complex[3].imag = -halfcomplex[4]
708 complex[4].real = halfcomplex[1]
709 complex[4].imag = -halfcomplex[2]
713 The upper elements of the @var{complex} array, @code{complex[3]} and
714 @code{complex[4]} are filled in using the symmetry condition. The
715 imaginary part of the zero-frequency term @code{complex[0].imag} is
716 known to be zero by the symmetry.
718 The next table shows the output for an even-length sequence, @math{n=6}
719 In the even case there are two values which are purely real,
722 complex[0].real = halfcomplex[0]
724 complex[1].real = halfcomplex[1]
725 complex[1].imag = halfcomplex[2]
726 complex[2].real = halfcomplex[3]
727 complex[2].imag = halfcomplex[4]
728 complex[3].real = halfcomplex[5]
730 complex[4].real = halfcomplex[3]
731 complex[4].imag = -halfcomplex[4]
732 complex[5].real = halfcomplex[1]
733 complex[5].imag = -halfcomplex[2]
737 The upper elements of the @var{complex} array, @code{complex[4]} and
738 @code{complex[5]} are filled in using the symmetry condition. Both
739 @code{complex[0].imag} and @code{complex[3].imag} are known to be zero.
741 All these functions are declared in the header files
742 @file{gsl_fft_real.h} and @file{gsl_fft_halfcomplex.h}.
744 @deftypefun {gsl_fft_real_wavetable *} gsl_fft_real_wavetable_alloc (size_t @var{n})
745 @deftypefunx {gsl_fft_halfcomplex_wavetable *} gsl_fft_halfcomplex_wavetable_alloc (size_t @var{n})
746 These functions prepare trigonometric lookup tables for an FFT of size
747 @math{n} real elements. The functions return a pointer to the newly
748 allocated struct if no errors were detected, and a null pointer in the
749 case of error. The length @var{n} is factorized into a product of
750 subtransforms, and the factors and their trigonometric coefficients are
751 stored in the wavetable. The trigonometric coefficients are computed
752 using direct calls to @code{sin} and @code{cos}, for accuracy.
753 Recursion relations could be used to compute the lookup table faster,
754 but if an application performs many FFTs of the same length then
755 computing the wavetable is a one-off overhead which does not affect the
758 The wavetable structure can be used repeatedly for any transform of the
759 same length. The table is not modified by calls to any of the other FFT
760 functions. The appropriate type of wavetable must be used for forward
761 real or inverse half-complex transforms.
764 @deftypefun void gsl_fft_real_wavetable_free (gsl_fft_real_wavetable * @var{wavetable})
765 @deftypefunx void gsl_fft_halfcomplex_wavetable_free (gsl_fft_halfcomplex_wavetable * @var{wavetable})
766 These functions free the memory associated with the wavetable
767 @var{wavetable}. The wavetable can be freed if no further FFTs of the
768 same length will be needed.
772 The mixed radix algorithms require additional working space to hold
773 the intermediate steps of the transform,
775 @deftypefun {gsl_fft_real_workspace *} gsl_fft_real_workspace_alloc (size_t @var{n})
776 This function allocates a workspace for a real transform of length
777 @var{n}. The same workspace can be used for both forward real and inverse
778 halfcomplex transforms.
781 @deftypefun void gsl_fft_real_workspace_free (gsl_fft_real_workspace * @var{workspace})
782 This function frees the memory associated with the workspace
783 @var{workspace}. The workspace can be freed if no further FFTs of the
784 same length will be needed.
788 The following functions compute the transforms of real and half-complex
791 @deftypefun int gsl_fft_real_transform (double @var{data}[], size_t @var{stride}, size_t @var{n}, const gsl_fft_real_wavetable * @var{wavetable}, gsl_fft_real_workspace * @var{work})
792 @deftypefunx int gsl_fft_halfcomplex_transform (double @var{data}[], size_t @var{stride}, size_t @var{n}, const gsl_fft_halfcomplex_wavetable * @var{wavetable}, gsl_fft_real_workspace * @var{work})
793 These functions compute the FFT of @var{data}, a real or half-complex
794 array of length @var{n}, using a mixed radix decimation-in-frequency
795 algorithm. For @code{gsl_fft_real_transform} @var{data} is an array of
796 time-ordered real data. For @code{gsl_fft_halfcomplex_transform}
797 @var{data} contains fourier coefficients in the half-complex ordering
798 described above. There is no restriction on the length @var{n}.
799 Efficient modules are provided for subtransforms of length 2, 3, 4 and
800 5. Any remaining factors are computed with a slow, @math{O(n^2)},
801 general-n module. The caller must supply a @var{wavetable} containing
802 trigonometric lookup tables and a workspace @var{work}.
805 @deftypefun int gsl_fft_real_unpack (const double @var{real_coefficient}[], gsl_complex_packed_array @var{complex_coefficient}, size_t @var{stride}, size_t @var{n})
807 This function converts a single real array, @var{real_coefficient} into
808 an equivalent complex array, @var{complex_coefficient}, (with imaginary
809 part set to zero), suitable for @code{gsl_fft_complex} routines. The
810 algorithm for the conversion is simply,
813 for (i = 0; i < n; i++)
815 complex_coefficient[i].real
816 = real_coefficient[i];
817 complex_coefficient[i].imag
823 @deftypefun int gsl_fft_halfcomplex_unpack (const double @var{halfcomplex_coefficient}[], gsl_complex_packed_array @var{complex_coefficient}, size_t @var{stride}, size_t @var{n})
825 This function converts @var{halfcomplex_coefficient}, an array of
826 half-complex coefficients as returned by @code{gsl_fft_real_transform}, into an
827 ordinary complex array, @var{complex_coefficient}. It fills in the
828 complex array using the symmetry
829 @c{$z_k = z_{N-k}^*$}
830 @math{z_k = z_@{N-k@}^*}
831 to reconstruct the redundant elements. The algorithm for the conversion
835 complex_coefficient[0].real
836 = halfcomplex_coefficient[0];
837 complex_coefficient[0].imag
840 for (i = 1; i < n - i; i++)
843 = halfcomplex_coefficient[2 * i - 1];
845 = halfcomplex_coefficient[2 * i];
846 complex_coefficient[i].real = hc_real;
847 complex_coefficient[i].imag = hc_imag;
848 complex_coefficient[n - i].real = hc_real;
849 complex_coefficient[n - i].imag = -hc_imag;
854 complex_coefficient[i].real
855 = halfcomplex_coefficient[n - 1];
856 complex_coefficient[i].imag
862 @comment @node Example of using mixed-radix FFT routines for real data
863 @comment @subsection Example of using mixed-radix FFT routines for real data
865 Here is an example program using @code{gsl_fft_real_transform} and
866 @code{gsl_fft_halfcomplex_inverse}. It generates a real signal in the
867 shape of a square pulse. The pulse is fourier transformed to frequency
868 space, and all but the lowest ten frequency components are removed from
869 the array of fourier coefficients returned by
870 @code{gsl_fft_real_transform}.
872 The remaining fourier coefficients are transformed back to the
873 time-domain, to give a filtered version of the square pulse. Since
874 fourier coefficients are stored using the half-complex symmetry both
875 positive and negative frequencies are removed and the final filtered
879 @verbatiminclude examples/fftreal.c
884 @center @image{fft-real-mixedradix,3.4in}
886 @center Low-pass filtered version of a real pulse,
887 @center output from the example program.
890 @node FFT References and Further Reading
891 @section References and Further Reading
893 A good starting point for learning more about the FFT is the review
894 article @cite{Fast Fourier Transforms: A Tutorial Review and A State of
895 the Art} by Duhamel and Vetterli,
899 P. Duhamel and M. Vetterli.
900 Fast fourier transforms: A tutorial review and a state of the art.
901 @cite{Signal Processing}, 19:259--299, 1990.
905 To find out about the algorithms used in the GSL routines you may want
906 to consult the document @cite{GSL FFT Algorithms} (it is included
907 in GSL, as @file{doc/fftalgorithms.tex}). This has general information
908 on FFTs and explicit derivations of the implementation for each
909 routine. There are also references to the relevant literature. For
910 convenience some of the more important references are reproduced below.
913 There are several introductory books on the FFT with example programs,
914 such as @cite{The Fast Fourier Transform} by Brigham and @cite{DFT/FFT
915 and Convolution Algorithms} by Burrus and Parks,
920 @cite{The Fast Fourier Transform}.
924 C. S. Burrus and T. W. Parks.
925 @cite{DFT/FFT and Convolution Algorithms}.
930 Both these introductory books cover the radix-2 FFT in some detail.
931 The mixed-radix algorithm at the heart of the @sc{fftpack} routines is
932 reviewed in Clive Temperton's paper,
937 Self-sorting mixed-radix fast fourier transforms.
938 @cite{Journal of Computational Physics}, 52(1):1--23, 1983.
942 The derivation of FFTs for real-valued data is explained in the
943 following two articles,
947 Henrik V. Sorenson, Douglas L. Jones, Michael T. Heideman, and C. Sidney
949 Real-valued fast fourier transform algorithms.
950 @cite{IEEE Transactions on Acoustics, Speech, and Signal Processing},
951 ASSP-35(6):849--863, 1987.
955 Fast mixed-radix real fourier transforms.
956 @cite{Journal of Computational Physics}, 52:340--350, 1983.
960 In 1979 the IEEE published a compendium of carefully-reviewed Fortran
961 FFT programs in @cite{Programs for Digital Signal Processing}. It is a
962 useful reference for implementations of many different FFT
967 Digital Signal Processing Committee and IEEE Acoustics, Speech, and Signal
968 Processing Committee, editors.
969 @cite{Programs for Digital Signal Processing}.
973 @comment There is also an annotated bibliography of papers on the FFT and related
974 @comment topics by Burrus,
976 @comment @itemize @asis
977 @comment @item C. S. Burrus. Notes on the FFT.
978 @comment @end itemize
980 @comment The notes are available from @url{http://www-dsp.rice.edu/res/fft/fftnote.asc}.
983 For large-scale FFT work we recommend the use of the dedicated FFTW library
984 by Frigo and Johnson. The FFTW library is self-optimizing---it
985 automatically tunes itself for each hardware platform in order to
986 achieve maximum performance. It is available under the GNU GPL.
990 FFTW Website, @uref{http://www.fftw.org/}
994 The source code for @sc{fftpack} is available from Netlib,
998 FFTPACK, @uref{http://www.netlib.org/fftpack/}