Chris@42:
Chris@42: FFTW 3 has semantics incompatible with earlier versions: its plans can
Chris@42: only be used for a given stride, multiplicity, and other
Chris@42: characteristics of the input and output arrays; these stronger
Chris@42: semantics are necessary for performance reasons. Thus, it is
Chris@42: impossible to efficiently emulate the older interface (whose plans can
Chris@42: be used for any transform of the same size). We believe that it
Chris@42: should be possible to upgrade most programs without any difficulty,
Chris@42: however.
Chris@42:
Chris@42:
Chris@42: There are several reasons:
Chris@42:
Chris@42:
It was important for performance reasons that the plan be specific to
Chris@42: array characteristics like the stride (and alignment, for SIMD), and
Chris@42: requiring that the user maintain these invariants is error prone.
Chris@42:
Chris@42:
In most high-performance applications, as far as we can tell, you are
Chris@42: usually transforming the same array over and over, so FFTW's semantics
Chris@42: should not be a burden.
Chris@42:
If you need to transform another array of the same size, creating a
Chris@42: new plan once the first exists is a cheap operation.
Chris@42:
Chris@42:
If you need to transform many arrays of the same size at once, you
Chris@42: should really use the plan_many routines in FFTW's "advanced"
Chris@42: interface.
Chris@42:
If the abovementioned array characteristics are the same, you are
Chris@42: willing to pay close attention to the documentation, and you really
Chris@42: need to, we provide a "new-array execution" interface to
Chris@42: apply a plan to a new array.
Chris@42:
Chris@42:
Chris@42: You are probably recreating the plan before every transform, rather
Chris@42: than creating it once and reusing it for all transforms of the same
Chris@42: size. FFTW is designed to be used in the following way:
Chris@42:
Chris@42:
Chris@42:
First, you create a plan. This will take several seconds.
Chris@42:
Chris@42:
Then, you reuse the plan many times to perform FFTs. These are fast.
Chris@42:
Chris@42:
Chris@42: If you don't need to compute many transforms and the time for the
Chris@42: planner is significant, you have two options. First, you can use the
Chris@42: FFTW_ESTIMATE option in the planner, which uses heuristics
Chris@42: instead of runtime measurements and produces a good plan in a short
Chris@42: time. Second, you can use the wisdom feature to precompute the plan;
Chris@42: see Q3.9 `Can I save FFTW's plans?'
Chris@42:
Chris@42:
Chris@42: Probably, NaNs or similar are creeping into your data, and the
Chris@42: slowdown is due to the resulting floating-point exceptions. For
Chris@42: example, be aware that repeatedly FFTing the same array is a diverging
Chris@42: process (because FFTW computes the unnormalized transform).
Chris@42:
Chris@42:
Chris@42:
Chris@42: Did the FFTW test programs pass (make check, or cd tests; make bigcheck if you want to be paranoid)? If so, you almost
Chris@42: certainly have a bug in your own code. For example, you could be
Chris@42: passing invalid arguments (such as wrongly-sized arrays) to FFTW, or
Chris@42: you could simply have memory corruption elsewhere in your program that
Chris@42: causes random crashes later on. Please don't complain to us unless
Chris@42: you can come up with a minimal self-contained program (preferably
Chris@42: under 30 lines) that illustrates the problem.
Chris@42:
Chris@42:
Chris@42:
Chris@42: As described in the manual, on 64-bit machines you must store the
Chris@42: plans in variables large enough to hold a pointer, for example
Chris@42: integer*8. We recommend using integer*8 on 32-bit machines as well, to simplify porting.
Chris@42:
Chris@42:
Chris@42:
Chris@42: People follow many different conventions for the DFT, and you should
Chris@42: be sure to know the ones that we use (described in the FFTW manual).
Chris@42: In particular, you should be aware that the
Chris@42: FFTW_FORWARD/FFTW_BACKWARD directions correspond to signs of -1/+1 in the exponent of the DFT definition.
Chris@42: (Numerical Recipes uses the opposite convention.)
Chris@42:
Chris@42: Finally, note that floating-point arithmetic is not exact, so
Chris@42: different FFT algorithms will give slightly different results (on the
Chris@42: order of the numerical accuracy; typically a fractional difference of
Chris@42: 1e-15 or so in double precision).
Chris@42:
Chris@42:
Chris@42: If you use FFTW_MEASURE or FFTW_PATIENT mode, then the algorithm FFTW employs is not deterministic: it depends on
Chris@42: runtime performance measurements. This will cause the results to vary
Chris@42: slightly from run to run. However, the differences should be slight,
Chris@42: on the order of the floating-point precision, and therefore should
Chris@42: have no practical impact on most applications.
Chris@42:
Chris@42:
Chris@42: If you use saved plans (wisdom) or FFTW_ESTIMATE mode, however, then the algorithm is deterministic and the results should be
Chris@42: identical between runs.
Chris@42:
Chris@42:
Chris@42: Yes. Starting with version 1.2, FFTW provides the
Chris@42: wisdom mechanism for saving plans; see the FFTW manual.
Chris@42:
Chris@42:
Chris@42:
Chris@42: Computing the forward transform followed by the backward transform (or
Chris@42: vice versa) yields the original array scaled by the size of the array.
Chris@42: (For multi-dimensional transforms, the size of the array is the
Chris@42: product of the dimensions.) We could, instead, have chosen a
Chris@42: normalization that would have returned the unscaled array. Or, to
Chris@42: accomodate the many conventions in this matter, the transform routines
Chris@42: could have accepted a "scale factor" parameter. We did not
Chris@42: do this, however, for two reasons. First, we didn't want to sacrifice
Chris@42: performance in the common case where the scale factor is 1. Second, in
Chris@42: real applications the FFT is followed or preceded by some computation
Chris@42: on the data, into which the scale factor can typically be absorbed at
Chris@42: little or no cost.
Chris@42:
Chris@42:
Chris@42: For human viewing of a spectrum, it is often convenient to put the
Chris@42: origin in frequency space at the center of the output array, rather
Chris@42: than in the zero-th element (the default in FFTW). If all of the
Chris@42: dimensions of your array are even, you can accomplish this by simply
Chris@42: multiplying each element of the input array by (-1)^(i + j + ...),
Chris@42: where i, j, etcetera are the indices of the element. (This trick is a
Chris@42: general property of the DFT, and is not specific to FFTW.)
Chris@42:
Chris@42:
Chris@42:
Chris@42: FFTW performs an FFT on an array of floating-point values. You can
Chris@42: certainly use it to compute the transform of an image or audio stream,
Chris@42: but you are responsible for figuring out your data format and
Chris@42: converting it to the form FFTW requires.
Chris@42:
Chris@42:
Chris@42:
Chris@42: The libraries must be listed in the correct order
Chris@42: (-lfftw3 -lm for FFTW 3.x) and after your program sources/objects. (The general rule is that if A uses B, then A must be listed before B in the link command.).
Chris@42:
Chris@42:
Chris@42: You're a C++ programmer, aren't you? You have to compile the FFTW
Chris@42: library and link it into your program, not just
Chris@42: #include <fftw3.h>. (Yes, this is really a FAQ.)
Chris@42:
Chris@42:
Chris@42: You cannot declare large arrays with automatic storage (e.g. via
Chris@42: fftw_complex array[N]); you should use fftw_malloc (or equivalent) to allocate the arrays you want
Chris@42: to transform if they are larger than a few hundred elements.
Chris@42:
Chris@42:
Chris@42:
Chris@42: After you create a plan, FFTW caches the information required to
Chris@42: quickly recreate the plan. (See Q3.9 `Can I save FFTW's plans?') It also maintains a small amount of other persistent memory. You can deallocate all of
Chris@42: FFTW's internally allocated memory, if you wish, by calling
Chris@42: fftw_cleanup(), as documented in the manual.
Chris@42:
Chris@42:
Chris@42: You should initialize your input array after creating the plan, unless you use FFTW_ESTIMATE: planning with FFTW_MEASURE or FFTW_PATIENT overwrites the input/output arrays, as described in the manual.
Chris@42:
Chris@42:
Chris@42:
Chris@42: Please do not ask us Windows-specific questions. We do not
Chris@42: use Windows. We know nothing about Visual Basic, Visual C++, or .NET.
Chris@42: Please find the appropriate Usenet discussion group and ask your
Chris@42: question there. See also Q2.2 `Does FFTW run on Windows?'.
Chris@42:
Chris@42:
Chris@42: In general, no, an FFT intrinsically computes all outputs from all
Chris@42: inputs. In principle, there is something called a
Chris@42: pruned FFT that can do what you want, but to compute K outputs out of N the
Chris@42: complexity is in general O(N log K) instead of O(N log N), thus saving
Chris@42: only a small additive factor in the log. (The same argument holds if
Chris@42: you instead have only K nonzero inputs.)
Chris@42:
Chris@42:
Chris@42: There are some specific cases in which you can get the O(N log K)
Chris@42: performance benefits easily, however, by combining a few ordinary
Chris@42: FFTs. In particular, the case where you want the first K outputs,
Chris@42: where K divides N, can be handled by performing N/K transforms of size
Chris@42: K and then summing the outputs multiplied by appropriate phase
Chris@42: factors. For more details, see pruned FFTs with FFTW.
Chris@42:
Chris@42: There are also some algorithms that compute pruned transforms
Chris@42: approximately, but they are beyond the scope of this FAQ.
Chris@42:
Chris@42:
Chris@42:
Chris@42: You can use the FFTW guru interface to create a rank-0 transform of
Chris@42: vector rank 2 where the vector strides are transposed. (A rank-0
Chris@42: transform is equivalent to a 1D transform of size 1, which. just
Chris@42: copies the input into the output.) Specifying the same location for
Chris@42: the input and output makes the transpose in-place.
Chris@42:
Chris@42:
Chris@42: For double-valued data stored in row-major format, plan creation looks
Chris@42: like this: