fftw user’s manualfftw/fftw2.pdfthe fast fourier transform by e. o. brigham (prentice-hall,...

74
FFTW User’s Manual For version 2.1.5, 16 March 2003 Matteo Frigo Steven G. Johnson

Upload: others

Post on 08-Feb-2021

0 views

Category:

Documents


0 download

TRANSCRIPT

  • FFTW User’s ManualFor version 2.1.5, 16 March 2003

    Matteo FrigoSteven G. Johnson

  • Copyright c© 1997–1999 Massachusetts Institute of Technology.Permission is granted to make and distribute verbatim copies of this manual provided thecopyright notice and this permission notice are preserved on all copies.Permission is granted to copy and distribute modified versions of this manual under the con-ditions for verbatim copying, provided that the entire resulting derived work is distributedunder the terms of a permission notice identical to this one.Permission is granted to copy and distribute translations of this manual into another lan-guage, under the above conditions for modified versions, except that this permission noticemay be stated in a translation approved by the Free Software Foundation.

  • i

    Table of Contents

    1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1

    2 Tutorial . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32.1 Complex One-dimensional Transforms Tutorial . . . . . . . . . . . . 32.2 Complex Multi-dimensional Transforms Tutorial . . . . . . . . . . . 42.3 Real One-dimensional Transforms Tutorial . . . . . . . . . . . . . . . . 62.4 Real Multi-dimensional Transforms Tutorial . . . . . . . . . . . . . . . 72.5 Multi-dimensional Array Format . . . . . . . . . . . . . . . . . . . . . . . . 11

    2.5.1 Row-major Format . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112.5.2 Column-major Format . . . . . . . . . . . . . . . . . . . . . . . . . 112.5.3 Static Arrays in C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112.5.4 Dynamic Arrays in C . . . . . . . . . . . . . . . . . . . . . . . . . . 122.5.5 Dynamic Arrays in C—The Wrong Way . . . . . . . . . 12

    2.6 Words of Wisdom . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132.6.1 Caveats in Using Wisdom . . . . . . . . . . . . . . . . . . . . . . 142.6.2 Importing and Exporting Wisdom . . . . . . . . . . . . . . 14

    3 FFTW Reference . . . . . . . . . . . . . . . . . . . . . . . . . 173.1 Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173.2 One-dimensional Transforms Reference . . . . . . . . . . . . . . . . . . . 18

    3.2.1 Plan Creation for One-dimensional Transforms . . . 183.2.2 Discussion on Specific Plans . . . . . . . . . . . . . . . . . . . . 203.2.3 Computing the One-dimensional Transform . . . . . . 203.2.4 Destroying a One-dimensional Plan . . . . . . . . . . . . . 213.2.5 What FFTW Really Computes . . . . . . . . . . . . . . . . . 21

    3.3 Multi-dimensional Transforms Reference . . . . . . . . . . . . . . . . . 223.3.1 Plan Creation for Multi-dimensional Transforms . . 223.3.2 Computing the Multi-dimensional Transform . . . . 243.3.3 Destroying a Multi-dimensional Plan . . . . . . . . . . . . 253.3.4 What FFTWND Really Computes . . . . . . . . . . . . . . 25

    3.4 Real One-dimensional Transforms Reference . . . . . . . . . . . . . . 263.4.1 Plan Creation for Real One-dimensional Transforms

    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263.4.2 Computing the Real One-dimensional Transform

    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273.4.3 Destroying a Real One-dimensional Plan . . . . . . . . 283.4.4 What RFFTW Really Computes . . . . . . . . . . . . . . . . 28

    3.5 Real Multi-dimensional Transforms Reference . . . . . . . . . . . . 293.5.1 Plan Creation for Real Multi-dimensional Transforms

    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293.5.2 Computing the Real Multi-dimensional Transform

    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30

  • ii FFTW

    3.5.3 Array Dimensions for Real Multi-dimensionalTransforms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32

    3.5.4 Strides in In-place RFFTWND . . . . . . . . . . . . . . . . . 323.5.5 Destroying a Multi-dimensional Plan . . . . . . . . . . . . 333.5.6 What RFFTWND Really Computes . . . . . . . . . . . . 33

    3.6 Wisdom Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343.6.1 Exporting Wisdom . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343.6.2 Importing Wisdom . . . . . . . . . . . . . . . . . . . . . . . . . . . . 353.6.3 Forgetting Wisdom . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35

    3.7 Memory Allocator Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . 353.8 Thread safety . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36

    4 Parallel FFTW . . . . . . . . . . . . . . . . . . . . . . . . . . . 374.1 Multi-threaded FFTW . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37

    4.1.1 Installation and Supported Hardware/Software. . . 374.1.2 Usage of Multi-threaded FFTW . . . . . . . . . . . . . . . . 384.1.3 How Many Threads to Use? . . . . . . . . . . . . . . . . . . . . 394.1.4 Using Multi-threaded FFTW in a Multi-threaded

    Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 404.1.5 Tips for Optimal Threading . . . . . . . . . . . . . . . . . . . . 404.1.6 Parallelization deficiencies in one-dimensional

    transforms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 404.2 MPI FFTW . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40

    4.2.1 MPI FFTW Installation . . . . . . . . . . . . . . . . . . . . . . . . 404.2.2 Usage of MPI FFTW for Complex Multi-dimensional

    Transforms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 414.2.3 MPI Data Layout. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 434.2.4 Usage of MPI FFTW for Real Multi-dimensional

    Transforms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 444.2.5 Usage of MPI FFTW for Complex One-dimensional

    Transforms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 474.2.6 MPI Tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48

    5 Calling FFTW from Fortran . . . . . . . . . . . . . . . 515.1 Wrapper Routines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 515.2 FFTW Constants in Fortran . . . . . . . . . . . . . . . . . . . . . . . . . . . . 525.3 Fortran Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52

    6 Installation and Customization . . . . . . . . . . . . 556.1 Installation on Unix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 556.2 Installation on non-Unix Systems . . . . . . . . . . . . . . . . . . . . . . . . 566.3 Installing FFTW in both single and double precision . . . . . . 576.4 gcc and Pentium hacks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 576.5 Customizing the timer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 586.6 Generating your own code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59

    7 Acknowledgments. . . . . . . . . . . . . . . . . . . . . . . . . 61

  • iii

    8 License and Copyright . . . . . . . . . . . . . . . . . . . . 63

    9 Concept Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65

    10 Library Index . . . . . . . . . . . . . . . . . . . . . . . . . . . 67

  • iv FFTW

  • Chapter 1: Introduction 1

    1 Introduction

    This manual documents version 2.1.5 of FFTW, the Fastest Fourier Transform in theWest. FFTW is a comprehensive collection of fast C routines for computing the discreteFourier transform (DFT) in one or more dimensions, of both real and complex data, andof arbitrary input size. FFTW also includes parallel transforms for both shared- anddistributed-memory systems. We assume herein that the reader is already familiar withthe properties and uses of the DFT that are relevant to her application. Otherwise, see e.g.The Fast Fourier Transform by E. O. Brigham (Prentice-Hall, Englewood Cliffs, NJ, 1974).Our web page also has links to FFT-related information online.

    FFTW is usually faster (and sometimes much faster) than all other freely-availableFourier transform programs found on the Net. For transforms whose size is a power oftwo, it compares favorably with the FFT codes in Sun’s Performance Library and IBM’sESSL library, which are targeted at specific machines. Moreover, FFTW’s performance isportable. Indeed, FFTW is unique in that it automatically adapts itself to your machine,your cache, the size of your memory, the number of registers, and all the other factorsthat normally make it impossible to optimize a program for more than one machine. Anextensive comparison of FFTW’s performance with that of other Fourier transform codeshas been made. The results are available on the Web at the benchFFT home page.

    In order to use FFTW effectively, you need to understand one basic concept of FFTW’sinternal structure. FFTW does not used a fixed algorithm for computing the transform,but it can adapt the DFT algorithm to details of the underlying hardware in order toachieve best performance. Hence, the computation of the transform is split into two phases.First, FFTW’s planner is called, which “learns” the fastest way to compute the transformon your machine. The planner produces a data structure called a plan that contains thisinformation. Subsequently, the plan is passed to FFTW’s executor, along with an arrayof input data. The executor computes the actual transform, as dictated by the plan. Theplan can be reused as many times as needed. In typical high-performance applications,many transforms of the same size are computed, and consequently a relatively-expensiveinitialization of this sort is acceptable. On the other hand, if you need a single transformof a given size, the one-time cost of the planner becomes significant. For this case, FFTWprovides fast planners based on heuristics or on previously computed plans.

    The pattern of planning/execution applies to all four operation modes of FFTW, that is,I) one-dimensional complex transforms (FFTW), II) multi-dimensional complex transforms(FFTWND), III) one-dimensional transforms of real data (RFFTW), IV) multi-dimensionaltransforms of real data (RFFTWND). Each mode comes with its own planner and executor.

    Besides the automatic performance adaptation performed by the planner, it is also pos-sible for advanced users to customize FFTW for their special needs. As distributed, FFTWworks most efficiently for arrays whose size can be factored into small primes (2, 3, 5, and7), and uses a slower general-purpose routine for other factors. FFTW, however, comeswith a code generator that can produce fast C programs for any particular array size youmay care about. For example, if you need transforms of size 513 = 19 ·33, you can customizeFFTW to support the factor 19 efficiently.

    FFTW can exploit multiple processors if you have them. FFTW comes with a shared-memory implementation on top of POSIX (and similar) threads, as well as a distributed-memory implementation based on MPI. We also provide an experimental parallel implemen-

    http://www.fftw.orghttp://theory.lcs.mit.edu/~benchfft

  • 2 FFTW

    tation written in Cilk, the superior programming tool of choice for discriminating hackers(Olin Shivers). (See the Cilk home page.)

    For more information regarding FFTW, see the paper, “The Fastest Fourier Transformin the West,” by M. Frigo and S. G. Johnson, which is the technical report MIT-LCS-TR-728 (Sep. ’97). See also, “FFTW: An Adaptive Software Architecture for the FFT,” by M.Frigo and S. G. Johnson, which appeared in the 23rd International Conference on Acoustics,Speech, and Signal Processing (Proc. ICASSP 1998 3, p. 1381). The code generator isdescribed in the paper “A Fast Fourier Transform Compiler”, by M. Frigo, to appear in theProceedings of the 1999 ACM SIGPLAN Conference on Programming Language Design andImplementation (PLDI), Atlanta, Georgia, May 1999. These papers, along with the latestversion of FFTW, the FAQ, benchmarks, and other links, are available at the FFTW homepage. The current version of FFTW incorporates many good ideas from the past thirtyyears of FFT literature. In one way or another, FFTW uses the Cooley-Tukey algorithm,the Prime Factor algorithm, Rader’s algorithm for prime sizes, and the split-radix algorithm(with a variation due to Dan Bernstein). Our code generator also produces new algorithmsthat we do not yet completely understand. The reader is referred to the cited papers forthe appropriate references.

    The rest of this manual is organized as follows. We first discuss the sequential (one-processor) implementation. We start by describing the basic features of FFTW in Chap-ter 2 [Tutorial], page 3. This discussion includes the storage scheme of multi-dimensionalarrays (Section 2.5 [Multi-dimensional Array Format], page 11) and FFTW’s mechanismsfor storing plans on disk (Section 2.6 [Words of Wisdom], page 13). Next, Chapter 3 [FFTWReference], page 17 provides comprehensive documentation of all FFTW’s features. Paralleltransforms are discussed in their own chapter Chapter 4 [Parallel FFTW], page 37. Fortranprogrammers can also use FFTW, as described in Chapter 5 [Calling FFTW from Fortran],page 51. Chapter 6 [Installation and Customization], page 55 explains how to install FFTWin your computer system and how to adapt FFTW to your needs. License and copyrightinformation is given in Chapter 8 [License and Copyright], page 63. Finally, we thank allthe people who helped us in Chapter 7 [Acknowledgments], page 61.

    http://supertech.lcs.mit.edu/cilkhttp://www.fftw.orghttp://www.fftw.org

  • Chapter 2: Tutorial 3

    2 Tutorial

    This chapter describes the basic usage of FFTW, i.e., how to compute the Fourier trans-form of a single array. This chapter tells the truth, but not the whole truth. Specifically,FFTW implements additional routines and flags, providing extra functionality, that are notdocumented here. See Chapter 3 [FFTW Reference], page 17, for more complete informa-tion. (Note that you need to compile and install FFTW before you can use it in a program.See Chapter 6 [Installation and Customization], page 55, for the details of the installation.)

    Here, we assume a default installation of FFTW. In some installations (particulary frombinary packages), the FFTW header files and libraries are prefixed with ‘d’ or ‘s’ to indicateversions in double or single precision, respectively. The usage of FFTW in that case is thesame, except that #include directives and link commands must use the appropriate prefix.See Section 6.3 [Installing FFTW in both single and double precision], page 57, for moreinformation.

    This tutorial chapter is structured as follows. Section 2.1 [Complex One-dimensionalTransforms Tutorial], page 3 describes the basic usage of the one-dimensional transform ofcomplex data. Section 2.2 [Complex Multi-dimensional Transforms Tutorial], page 4 de-scribes the basic usage of the multi-dimensional transform of complex data. Section 2.3[Real One-dimensional Transforms Tutorial], page 6 describes the one-dimensional trans-form of real data and its inverse. Finally, Section 2.4 [Real Multi-dimensional TransformsTutorial], page 7 describes the multi-dimensional transform of real data and its inverse. Werecommend that you read these sections in the order that they are presented. We thendiscuss two topics in detail. In Section 2.5 [Multi-dimensional Array Format], page 11, wediscuss the various alternatives for storing multi-dimensional arrays in memory. Section 2.6[Words of Wisdom], page 13 shows how you can save FFTW’s plans for future use.

    2.1 Complex One-dimensional Transforms Tutorial

    The basic usage of FFTW is simple. A typical call to FFTW looks like:#include ...{

    fftw_complex in[N], out[N];fftw_plan p;...p = fftw_create_plan(N, FFTW_FORWARD, FFTW_ESTIMATE);...fftw_one(p, in, out);...fftw_destroy_plan(p);

    }

    The first thing we do is to create a plan, which is an object that contains all the datathat FFTW needs to compute the FFT, using the following function:

    fftw_plan fftw_create_plan(int n, fftw_direction dir, int flags);

    The first argument, n, is the size of the transform you are trying to compute. The size ncan be any positive integer, but sizes that are products of small factors are transformed most

  • 4 FFTW

    efficiently. The second argument, dir, can be either FFTW_FORWARD or FFTW_BACKWARD, andindicates the direction of the transform you are interested in. Alternatively, you can usethe sign of the exponent in the transform, −1 or +1, which corresponds to FFTW_FORWARDor FFTW_BACKWARD respectively. The flags argument is either FFTW_MEASURE or FFTW_ESTIMATE. FFTW_MEASURE means that FFTW actually runs and measures the executiontime of several FFTs in order to find the best way to compute the transform of size n.This may take some time, depending on your installation and on the precision of the timerin your machine. FFTW_ESTIMATE, on the contrary, does not run any computation, andjust builds a reasonable plan, which may be sub-optimal. In other words, if your programperforms many transforms of the same size and initialization time is not important, useFFTW_MEASURE; otherwise use the estimate. (A compromise between these two extremesexists. See Section 2.6 [Words of Wisdom], page 13.)

    Once the plan has been created, you can use it as many times as you like for transformson arrays of the same size. When you are done with the plan, you deallocate it by callingfftw_destroy_plan(plan).

    The transform itself is computed by passing the plan along with the input and outputarrays to fftw_one:

    void fftw_one(fftw_plan plan, fftw_complex *in, fftw_complex *out);

    Note that the transform is out of place: in and out must point to distinct arrays.It operates on data of type fftw_complex, a data structure with real (in[i].re) andimaginary (in[i].im) floating-point components. The in and out arrays should have thelength specified when the plan was created. An alternative function, fftw, allows you toefficiently perform multiple and/or strided transforms (see Chapter 3 [FFTW Reference],page 17).

    The DFT results are stored in-order in the array out, with the zero-frequency (DC)component in out[0]. The array in is not modified. Users should note that FFTWcomputes an unnormalized DFT, the sign of whose exponent is given by the dir parameterof fftw_create_plan. Thus, computing a forward followed by a backward transform (orvice versa) results in the original array scaled by n. See Section 3.2.5 [What FFTW ReallyComputes], page 21, for the definition of DFT.

    A program using FFTW should be linked with -lfftw -lm on Unix systems, or withthe FFTW and standard math libraries in general.

    2.2 Complex Multi-dimensional Transforms Tutorial

    FFTW can also compute transforms of any number of dimensions (rank). The syntaxis similar to that for the one-dimensional transforms, with ‘fftw_’ replaced by ‘fftwnd_’(which stands for “fftw in N dimensions”).

    As before, we #include and create a plan for the transforms, this time of typefftwnd_plan:

    fftwnd_plan fftwnd_create_plan(int rank, const int *n,fftw_direction dir, int flags);

    rank is the dimensionality of the array, and can be any non-negative integer. The nextargument, n, is a pointer to an integer array of length rank containing the (positive) sizesof each dimension of the array. (Note that the array will be stored in row-major order. See

  • Chapter 2: Tutorial 5

    Section 2.5 [Multi-dimensional Array Format], page 11, for information on row-major order.)The last two parameters are the same as in fftw_create_plan. We now, however, have anadditional possible flag, FFTW_IN_PLACE, since fftwnd supports true in-place transforms.Multiple flags are combined using a bitwise or (‘|’). (An in-place transform is one in whichthe output data overwrite the input data. It thus requires half as much memory as—and isoften faster than—its opposite, an out-of-place transform.)

    For two- and three-dimensional transforms, FFTWND provides alternative routines thataccept the sizes of each dimension directly, rather than indirectly through a rank and anarray of sizes. These are otherwise identical to fftwnd_create_plan, and are sometimesmore convenient:

    fftwnd_plan fftw2d_create_plan(int nx, int ny,fftw_direction dir, int flags);

    fftwnd_plan fftw3d_create_plan(int nx, int ny, int nz,fftw_direction dir, int flags);

    Once the plan has been created, you can use it any number of times for transforms of thesame size. When you do not need a plan anymore, you can deallocate the plan by callingfftwnd_destroy_plan(plan).

    Given a plan, you can compute the transform of an array of data by calling:

    void fftwnd_one(fftwnd_plan plan, fftw_complex *in, fftw_complex *out);

    Here, in and out point to multi-dimensional arrays in row-major order, of the sizespecified when the plan was created. In the case of an in-place transform, the out parameteris ignored and the output data are stored in the in array. The results are stored in-order,unnormalized, with the zero-frequency component in out[0]. A forward followed by abackward transform (or vice-versa) yields the original data multiplied by the size of thearray (i.e. the product of the dimensions). See Section 3.3.4 [What FFTWND ReallyComputes], page 25, for a discussion of what FFTWND computes.

    For example, code to perform an in-place FFT of a three-dimensional array might looklike:

    #include ...{

    fftw_complex in[L][M][N];fftwnd_plan p;...p = fftw3d_create_plan(L, M, N, FFTW_FORWARD,

    FFTW_MEASURE | FFTW_IN_PLACE);...fftwnd_one(p, &in[0][0][0], NULL);...fftwnd_destroy_plan(p);

    }

    Note that in is a statically-declared array, which is automatically in row-major order,but we must take the address of the first element in order to fit the type expected byfftwnd_one. (See Section 2.5 [Multi-dimensional Array Format], page 11.)

  • 6 FFTW

    2.3 Real One-dimensional Transforms Tutorial

    If the input data are purely real, you can save roughly a factor of two in both time andstorage by using the rfftw transforms, which are FFTs specialized for real data. The outputof a such a transform is a halfcomplex array, which consists of only half of the complex DFTamplitudes (since the negative-frequency amplitudes for real data are the complex conjugateof the positive-frequency amplitudes).

    In exchange for these speed and space advantages, the user sacrifices some of the sim-plicity of FFTW’s complex transforms. First of all, to allow maximum performance, theoutput format of the one-dimensional real transforms is different from that used by themulti-dimensional transforms. Second, the inverse transform (halfcomplex to real) has theside-effect of destroying its input array. Neither of these inconveniences should pose a seriousproblem for users, but it is important to be aware of them. (Both the inconvenient outputformat and the side-effect of the inverse transform can be ameliorated for one-dimensionaltransforms, at the expense of some performance, by using instead the multi-dimensionaltransform routines with a rank of one.)

    The computation of the plan is similar to that for the complex transforms. First, you#include . Then, you create a plan (of type rfftw_plan) by calling:

    rfftw_plan rfftw_create_plan(int n, fftw_direction dir, int flags);

    n is the length of the real array in the transform (even for halfcomplex-to-real trans-forms), and can be any positive integer (although sizes with small factors are transformedmore efficiently). dir is either FFTW_REAL_TO_COMPLEX or FFTW_COMPLEX_TO_REAL. Theflags parameter is the same as in fftw_create_plan.

    Once created, a plan can be used for any number of transforms, and is deallocated whenyou are done with it by calling rfftw_destroy_plan(plan).

    Given a plan, a real-to-complex or complex-to-real transform is computed by calling:void rfftw_one(rfftw_plan plan, fftw_real *in, fftw_real *out);

    (Note that fftw_real is an alias for the floating-point type for which FFTW was com-piled.) Depending upon the direction of the plan, either the input or the output array ishalfcomplex, and is stored in the following format:

    r0, r1, r2, . . . , rn/2, i(n+1)/2−1, . . . , i2, i1

    Here, rk is the real part of the kth output, and ik is the imaginary part. (We follow herethe C convention that integer division is rounded down, e.g. 7/2 = 3.) For a halfcomplexarray hc[], the kth component has its real part in hc[k] and its imaginary part in hc[n-k], with the exception of k == 0 or n/2 (the latter only if n is even)—in these two cases,the imaginary part is zero due to symmetries of the real-complex transform, and is notstored. Thus, the transform of n real values is a halfcomplex array of length n, and viceversa.1 This is actually only half of the DFT spectrum of the data. Although the otherhalf can be obtained by complex conjugation, it is not required by many applications suchas convolution and filtering.

    1 The output for the multi-dimensional rfftw is a more-conventional array of fftw_complex values, but theformat here permitted us greater efficiency in one dimension.

  • Chapter 2: Tutorial 7

    Like the complex transforms, the RFFTW transforms are unnormalized, so a forwardfollowed by a backward transform (or vice-versa) yields the original data scaled by thelength of the array, n.

    Let us reiterate here our warning that an FFTW_COMPLEX_TO_REAL transform has theside-effect of destroying its (halfcomplex) input. The FFTW_REAL_TO_COMPLEX transform,however, leaves its (real) input untouched, just as you would hope.

    As an example, here is an outline of how you might use RFFTW to compute the powerspectrum of a real array (i.e. the squares of the absolute values of the DFT amplitudes):

    #include ...{

    fftw_real in[N], out[N], power_spectrum[N/2+1];rfftw_plan p;int k;...p = rfftw_create_plan(N, FFTW_REAL_TO_COMPLEX, FFTW_ESTIMATE);...rfftw_one(p, in, out);power_spectrum[0] = out[0]*out[0]; /* DC component */for (k = 1; k < (N+1)/2; ++k) /* (k < N/2 rounded up) */

    power_spectrum[k] = out[k]*out[k] + out[N-k]*out[N-k];if (N % 2 == 0) /* N is even */

    power_spectrum[N/2] = out[N/2]*out[N/2]; /* Nyquist freq. */...rfftw_destroy_plan(p);

    }

    Programs using RFFTW should link with -lrfftw -lfftw -lm on Unix, or with theFFTW, RFFTW, and math libraries in general.

    2.4 Real Multi-dimensional Transforms Tutorial

    FFTW includes multi-dimensional transforms for real data of any rank. As with theone-dimensional real transforms, they save roughly a factor of two in time and storageover complex transforms of the same size. Also as in one dimension, these gains come atthe expense of some increase in complexity—the output format is different from the one-dimensional RFFTW (and is more similar to that of the complex FFTW) and the inverse(complex to real) transforms have the side-effect of overwriting their input data.

    To use the real multi-dimensional transforms, you first #include and thencreate a plan for the size and direction of transform that you are interested in:

    rfftwnd_plan rfftwnd_create_plan(int rank, const int *n,fftw_direction dir, int flags);

    The first two parameters describe the size of the real data (not the halfcomplex data,which will have different dimensions). The last two parameters are the same as those forrfftw_create_plan. Just as for fftwnd, there are two alternate versions of this routine,rfftw2d_create_plan and rfftw3d_create_plan, that are sometimes more convenient fortwo- and three-dimensional transforms. Also as in fftwnd, rfftwnd supports true in-placetransforms, specified by including FFTW_IN_PLACE in the flags.

  • 8 FFTW

    Once created, a plan can be used for any number of transforms, and is deallocated bycalling rfftwnd_destroy_plan(plan).

    Given a plan, the transform is computed by calling one of the following two routines:

    void rfftwnd_one_real_to_complex(rfftwnd_plan plan,fftw_real *in, fftw_complex *out);

    void rfftwnd_one_complex_to_real(rfftwnd_plan plan,fftw_complex *in, fftw_real *out);

    As is clear from their names and parameter types, the former function is for FFTW_REAL_TO_COMPLEX transforms and the latter is for FFTW_COMPLEX_TO_REAL transforms. (We couldhave used only a single routine, since the direction of the transform is encoded in the plan,but we wanted to correctly express the datatypes of the parameters.) The latter routine,as we discuss elsewhere, has the side-effect of overwriting its input (except when the rankof the array is one). In both cases, the out parameter is ignored for in-place transforms.

    The format of the complex arrays deserves careful attention. Suppose that the real datahas dimensions n1 × n2 × · · · × nd (in row-major order). Then, after a real-to-complextransform, the output is an n1×n2× · · ·× (nd/2 + 1) array of fftw_complex values in row-major order, corresponding to slightly over half of the output of the corresponding complextransform. (Note that the division is rounded down.) The ordering of the data is otherwiseexactly the same as in the complex case. (In principle, the output could be exactly halfthe size of the complex transform output, but in more than one dimension this requires toocomplicated a format to be practical.) Note that, unlike the one-dimensional RFFTW, thereal and imaginary parts of the DFT amplitudes are here stored together in the naturalway.

    Since the complex data is slightly larger than the real data, some complications arise forin-place transforms. In this case, the final dimension of the real data must be padded withextra values to accommodate the size of the complex data—two extra if the last dimension iseven and one if it is odd. That is, the last dimension of the real data must physically contain2(nd/2 + 1) fftw_real values (exactly enough to hold the complex data). This physicalarray size does not, however, change the logical array size—only nd values are actuallystored in the last dimension, and nd is the last dimension passed to rfftwnd_create_plan.

    For example, consider the transform of a two-dimensional real array of size nx by ny.The output of the rfftwnd transform is a two-dimensional complex array of size nx byny/2+1, where the y dimension has been cut nearly in half because of redundancies in theoutput. Because fftw_complex is twice the size of fftw_real, the output array is slightlybigger than the input array. Thus, if we want to compute the transform in place, we mustpad the input array so that it is of size nx by 2*(ny/2+1). If ny is even, then there are twopadding elements at the end of each row (which need not be initialized, as they are onlyused for output).

    The following illustration depicts the input and output arrays just described, for boththe out-of-place and in-place transforms (with the arrows indicating consecutive memorylocations):

  • Chapter 2: Tutorial 9

    ...

    ny + 2−ny%2 = 2*(ny/2+1)

    nx

    0

    ny+1

    0

    nx−1

    (pa

    ddin

    g)

    inp

    ut,

    in

    −p

    lac

    e

    0

    1

    2

    3

    ny−4

    ny−2

    ny−1

    ny−3

    ny+2

    ny+3

    ny

    ny+1

    ...

    ny/2+1

    nx

    0

    ny/2

    0

    nx−1

    = fftw_real

    = fftw_complex

    ou

    tpu

    t

    0

    1

    ny/2−2

    ny/2−1

    ny/2+1

    ny/2

    ...

    ny

    nx

    0

    ny−1

    0

    nx−1

    inp

    ut,

    ou

    t−o

    f−p

    lac

    e

    0

    1

    2

    3

    ny−4

    ny−2

    ny−1

    ny−3

    ny

    ny+1

    The RFFTWND transforms are unnormalized, so a forward followed by a backwardtransform will result in the original data scaled by the number of real data elements—thatis, the product of the (logical) dimensions of the real data.

    Below, we illustrate the use of RFFTWND by showing how you might use it to computethe (cyclic) convolution of two-dimensional real arrays a and b (using the identity that aconvolution corresponds to a pointwise product of the Fourier transforms). For variety,in-place transforms are used for the forward FFTs and an out-of-place transform is used forthe inverse transform.

    #include ...{

    fftw_real a[M][2*(N/2+1)], b[M][2*(N/2+1)], c[M][N];fftw_complex *A, *B, C[M][N/2+1];rfftwnd_plan p, pinv;fftw_real scale = 1.0 / (M * N);int i, j;...p = rfftw2d_create_plan(M, N, FFTW_REAL_TO_COMPLEX,

  • 10 FFTW

    FFTW_ESTIMATE | FFTW_IN_PLACE);pinv = rfftw2d_create_plan(M, N, FFTW_COMPLEX_TO_REAL,

    FFTW_ESTIMATE);

    /* aliases for accessing complex transform outputs: */A = (fftw_complex*) &a[0][0];B = (fftw_complex*) &b[0][0];...for (i = 0; i < M; ++i)

    for (j = 0; j < N; ++j) {a[i][j] = ... ;b[i][j] = ... ;

    }...rfftwnd_one_real_to_complex(p, &a[0][0], NULL);rfftwnd_one_real_to_complex(p, &b[0][0], NULL);

    for (i = 0; i < M; ++i)for (j = 0; j < N/2+1; ++j) {

    int ij = i*(N/2+1) + j;C[i][j].re = (A[ij].re * B[ij].re

    - A[ij].im * B[ij].im) * scale;C[i][j].im = (A[ij].re * B[ij].im

    + A[ij].im * B[ij].re) * scale;}

    /* inverse transform to get c, the convolution of a and b;this has the side effect of overwriting C */

    rfftwnd_one_complex_to_real(pinv, &C[0][0], &c[0][0]);...rfftwnd_destroy_plan(p);rfftwnd_destroy_plan(pinv);

    }

    We access the complex outputs of the in-place transforms by casting each real array to afftw_complex pointer. Because this is a “flat” pointer, we have to compute the row-majorindex ij explicitly in the convolution product loop. In order to normalize the convolution,we must multiply by a scale factor—we can do so either before or after the inverse transform,and choose the former because it obviates the necessity of an additional loop. Notice thelimits of the loops and the dimensions of the various arrays.

    As with the one-dimensional RFFTW, an out-of-place FFTW_COMPLEX_TO_REAL trans-form has the side-effect of overwriting its input array. (The real-to-complex transform, onthe other hand, leaves its input array untouched.) If you use RFFTWND for a rank-onetransform, however, this side-effect does not occur. Because of this fact (and the simpleroutput format), users may find the RFFTWND interface more convenient than RFFTWfor one-dimensional transforms. However, RFFTWND in one dimension is slightly slowerthan RFFTW because RFFTWND uses an extra buffer array internally.

  • Chapter 2: Tutorial 11

    2.5 Multi-dimensional Array Format

    This section describes the format in which multi-dimensional arrays are stored. We feltthat a detailed discussion of this topic was necessary, since it is often a source of confusionamong users and several different formats are common. Although the comments below referto fftwnd, they are also applicable to the rfftwnd routines.

    2.5.1 Row-major Format

    The multi-dimensional arrays passed to fftwnd are expected to be stored as a singlecontiguous block in row-major order (sometimes called “C order”). Basically, this meansthat as you step through adjacent memory locations, the first dimension’s index varies mostslowly and the last dimension’s index varies most quickly.

    To be more explicit, let us consider an array of rank d whose dimensions are n1 × n2 ×n3× · · ·×nd. Now, we specify a location in the array by a sequence of (zero-based) indices,one for each dimension: (i1, i2, i3, . . . , id). If the array is stored in row-major order, thenthis element is located at the position id + nd(id−1 + nd−1(. . . + n2i1)).

    Note that each element of the array must be of type fftw_complex; i.e. a (real, imagi-nary) pair of (double-precision) numbers. Note also that, in fftwnd, the expression aboveis multiplied by the stride to get the actual array index—this is useful in situations whereeach element of the multi-dimensional array is actually a data structure or another array,and you just want to transform a single field. In most cases, however, you use a stride of 1.

    2.5.2 Column-major Format

    Readers from the Fortran world are used to arrays stored in column-major order (some-times called “Fortran order”). This is essentially the exact opposite of row-major order inthat, here, the first dimension’s index varies most quickly.

    If you have an array stored in column-major order and wish to transform it using fftwnd,it is quite easy to do. When creating the plan, simply pass the dimensions of the array tofftwnd_create_plan in reverse order. For example, if your array is a rank three N x M xL matrix in column-major order, you should pass the dimensions of the array as if it werean L x M x N matrix (which it is, from the perspective of fftwnd). This is done for youautomatically by the FFTW Fortran wrapper routines (see Chapter 5 [Calling FFTW fromFortran], page 51).

    2.5.3 Static Arrays in C

    Multi-dimensional arrays declared statically (that is, at compile time, not necessarilywith the static keyword) in C are already in row-major order. You don’t have to do any-thing special to transform them. (See Section 2.2 [Complex Multi-dimensional TransformsTutorial], page 4, for an example of this sort of code.)

  • 12 FFTW

    2.5.4 Dynamic Arrays in C

    Often, especially for large arrays, it is desirable to allocate the arrays dynamically,at runtime. This isn’t too hard to do, although it is not as straightforward for multi-dimensional arrays as it is for one-dimensional arrays.

    Creating the array is simple: using a dynamic-allocation routine like malloc, allocatean array big enough to store N fftw_complex values, where N is the product of the sizes ofthe array dimensions (i.e. the total number of complex values in the array). For example,here is code to allocate a 5x12x27 rank 3 array:

    fftw_complex *an_array;

    an_array = (fftw_complex *) malloc(5 * 12 * 27 * sizeof(fftw_complex));

    Accessing the array elements, however, is more tricky—you can’t simply use multipleapplications of the ‘[]’ operator like you could for static arrays. Instead, you have toexplicitly compute the offset into the array using the formula given earlier for row-majorarrays. For example, to reference the (i, j, k)-th element of the array allocated above, youwould use the expression an_array[k + 27 * (j + 12 * i)].

    This pain can be alleviated somewhat by defining appropriate macros, or, in C++, cre-ating a class and overloading the ‘()’ operator.

    2.5.5 Dynamic Arrays in C—The Wrong Way

    A different method for allocating multi-dimensional arrays in C is often suggested thatis incompatible with fftwnd: using it will cause FFTW to die a painful death. We discussthe technique here, however, because it is so commonly known and used. This method isto create arrays of pointers of arrays of pointers of . . . etcetera. For example, the analoguein this method to the example above is:

    int i,j;fftw_complex ***a_bad_array; /* another way to make a 5x12x27 array */

    a_bad_array = (fftw_complex ***) malloc(5 * sizeof(fftw_complex **));for (i = 0; i < 5; ++i) {

    a_bad_array[i] =(fftw_complex **) malloc(12 * sizeof(fftw_complex *));

    for (j = 0; j < 12; ++j)a_bad_array[i][j] =

    (fftw_complex *) malloc(27 * sizeof(fftw_complex));}

    As you can see, this sort of array is inconvenient to allocate (and deallocate). On theother hand, it has the advantage that the (i, j, k)-th element can be referenced simply bya_bad_array[i][j][k].

    If you like this technique and want to maximize convenience in accessing the array, butstill want to pass the array to FFTW, you can use a hybrid method. Allocate the array asone contiguous block, but also declare an array of arrays of pointers that point to appropriateplaces in the block. That sort of trick is beyond the scope of this documentation; for moreinformation on multi-dimensional arrays in C, see the comp.lang.c FAQ.

    http://www.eskimo.com/~scs/C-faq/s6.html

  • Chapter 2: Tutorial 13

    2.6 Words of Wisdom

    FFTW implements a method for saving plans to disk and restoring them. In fact, whatFFTW does is more general than just saving and loading plans. The mechanism is calledwisdom. Here, we describe this feature at a high level. See Chapter 3 [FFTW Reference],page 17, for a less casual (but more complete) discussion of how to use wisdom in FFTW.

    Plans created with the FFTW_MEASURE option produce near-optimal FFT performance,but it can take a long time to compute a plan because FFTW must actually measure theruntime of many possible plans and select the best one. This is designed for the situationswhere so many transforms of the same size must be computed that the start-up time isirrelevant. For short initialization times but slightly slower transforms, we have providedFFTW_ESTIMATE. The wisdom mechanism is a way to get the best of both worlds. There are,however, certain caveats that the user must be aware of in using wisdom. For this reason,wisdom is an optional feature which is not enabled by default.

    At its simplest, wisdom provides a way of saving plans to disk so that they can be reusedin other program runs. You create a plan with the flags FFTW_MEASURE and FFTW_USE_WISDOM, and then save the wisdom using fftw_export_wisdom:

    plan = fftw_create_plan(..., ... | FFTW_MEASURE | FFTW_USE_WISDOM);fftw_export_wisdom(...);

    The next time you run the program, you can restore the wisdom with fftw_import_wisdom, and then recreate the plan using the same flags as before. This time, however, thesame optimal plan will be created very quickly without measurements. (FFTW still needssome time to compute trigonometric tables, however.) The basic outline is:

    fftw_import_wisdom(...);plan = fftw_create_plan(..., ... | FFTW_USE_WISDOM);

    Wisdom is more than mere rote memorization, however. FFTW’s wisdom encompassesall of the knowledge and measurements that were used to create the plan for a given size.Therefore, existing wisdom is also applied to the creation of other plans of different sizes.

    Whenever a plan is created with the FFTW_MEASURE and FFTW_USE_WISDOM flags, wisdomis generated. Thereafter, plans for any transform with a similar factorization will be com-puted more quickly, so long as they use the FFTW_USE_WISDOM flag. In fact, for transformswith the same factors and of equal or lesser size, no measurements at all need to be madeand an optimal plan can be created with negligible delay!

    For example, suppose that you create a plan for N = 216. Then, for any equal or smallerpower of two, FFTW can create a plan (with the same direction and flags) quickly, using theprecomputed wisdom. Even for larger powers of two, or sizes that are a power of two timessome other prime factors, plans will be computed more quickly than they would otherwise(although some measurements still have to be made).

    The wisdom is cumulative, and is stored in a global, private data structure managedinternally by FFTW. The storage space required is minimal, proportional to the logarithmof the sizes the wisdom was generated from. The wisdom can be forgotten (and its associatedmemory freed) by a call to fftw_forget_wisdom(); otherwise, it is remembered until theprogram terminates. It can also be exported to a file, a string, or any other medium usingfftw_export_wisdom and restored during a subsequent execution of the program (or adifferent program) using fftw_import_wisdom (these functions are described below).

  • 14 FFTW

    Because wisdom is incorporated into FFTW at a very low level, the same wisdom can beused for one-dimensional transforms, multi-dimensional transforms, and even the parallelextensions to FFTW. Just include FFTW_USE_WISDOM in the flags for whatever plans youcreate (i.e., always plan wisely).

    Plans created with the FFTW_ESTIMATE plan can use wisdom, but cannot generate it; onlyFFTW_MEASURE plans actually produce wisdom. Also, plans can only use wisdom generatedfrom plans created with the same direction and flags. For example, a size 42 FFTW_BACKWARDtransform will not use wisdom produced by a size 42 FFTW_FORWARD transform. The onlyexception to this rule is that FFTW_ESTIMATE plans can use wisdom from FFTW_MEASUREplans.

    2.6.1 Caveats in Using Wisdom

    For in much wisdom is much grief, and he that increaseth knowledge increasethsorrow. [Ecclesiastes 1:18]

    There are pitfalls to using wisdom, in that it can negate FFTW’s ability to adapt tochanging hardware and other conditions. For example, it would be perfectly possible toexport wisdom from a program running on one processor and import it into a programrunning on another processor. Doing so, however, would mean that the second programwould use plans optimized for the first processor, instead of the one it is running on.

    It should be safe to reuse wisdom as long as the hardware and program binaries remainunchanged. (Actually, the optimal plan may change even between runs of the same binaryon identical hardware, due to differences in the virtual memory environment, etcetera. Usersseriously interested in performance should worry about this problem, too.) It is likely that,if the same wisdom is used for two different program binaries, even running on the samemachine, the plans may be sub-optimal because of differing code alignments. It is thereforewise to recreate wisdom every time an application is recompiled. The more the underlyinghardware and software changes between the creation of wisdom and its use, the greatergrows the risk of sub-optimal plans.

    2.6.2 Importing and Exporting Wisdom

    void fftw_export_wisdom_to_file(FILE *output_file);fftw_status fftw_import_wisdom_from_file(FILE *input_file);

    fftw_export_wisdom_to_file writes the wisdom to output_file, which must be a fileopen for writing. fftw_import_wisdom_from_file reads the wisdom from input_file,which must be a file open for reading, and returns FFTW_SUCCESS if successful and FFTW_FAILURE otherwise. In both cases, the file is left open and must be closed by the caller. Itis perfectly fine if other data lie before or after the wisdom in the file, as long as the file ispositioned at the beginning of the wisdom data before import.

    char *fftw_export_wisdom_to_string(void);fftw_status fftw_import_wisdom_from_string(const char *input_string)

    fftw_export_wisdom_to_string allocates a string, exports the wisdom to it in NULL-terminated format, and returns a pointer to the string. If there is an error in allocatingor writing the data, it returns NULL. The caller is responsible for deallocating the string(with fftw_free) when she is done with it. fftw_import_wisdom_from_string imports

  • Chapter 2: Tutorial 15

    the wisdom from input_string, returning FFTW_SUCCESS if successful and FFTW_FAILUREotherwise.

    Exporting wisdom does not affect the store of wisdom. Imported wisdom supplementsthe current store rather than replacing it (except when there is conflicting wisdom, in whichcase the older wisdom is discarded). The format of the exported wisdom is “nerd-readable”LISP-like ASCII text; we will not document it here except to note that it is insensitive towhite space (interested users can contact us for more details).

    See Chapter 3 [FFTW Reference], page 17, for more information, and for a descriptionof how you can implement wisdom import/export for other media besides files and strings.

    The following is a brief example in which the wisdom is read from a file, a plan is created(possibly generating more wisdom), and then the wisdom is exported to a string and printedto stdout.

    {fftw_plan plan;char *wisdom_string;FILE *input_file;

    /* open file to read wisdom from */input_file = fopen("sample.wisdom", "r");if (FFTW_FAILURE == fftw_import_wisdom_from_file(input_file))

    printf("Error reading wisdom!\n");fclose(input_file); /* be sure to close the file! */

    /* create a plan for N=64, possibly creating and/or using wisdom */plan = fftw_create_plan(64,FFTW_FORWARD,

    FFTW_MEASURE | FFTW_USE_WISDOM);

    /* ... do some computations with the plan ... */

    /* always destroy plans when you are done */fftw_destroy_plan(plan);

    /* write the wisdom to a string */wisdom_string = fftw_export_wisdom_to_string();if (wisdom_string != NULL) {

    printf("Accumulated wisdom: %s\n",wisdom_string);

    /* Just for fun, destroy and restore the wisdom */fftw_forget_wisdom(); /* all gone! */fftw_import_wisdom_from_string(wisdom_string);/* wisdom is back! */

    fftw_free(wisdom_string); /* deallocate it since we’re done */}

    }

  • 16 FFTW

  • Chapter 3: FFTW Reference 17

    3 FFTW Reference

    This chapter provides a complete reference for all sequential (i.e., one-processor) FFTWfunctions. We first define the data types upon which FFTW operates, that is, real, com-plex, and “halfcomplex” numbers (see Section 3.1 [Data Types], page 17). Then, in four sec-tions, we explain the FFTW program interface for complex one-dimensional transforms (seeSection 3.2 [One-dimensional Transforms Reference], page 18), complex multi-dimensionaltransforms (see Section 3.3 [Multi-dimensional Transforms Reference], page 22), and realone-dimensional transforms (see Section 3.4 [Real One-dimensional Transforms Reference],page 26), real multi-dimensional transforms (see Section 3.5 [Real Multi-dimensional Trans-forms Reference], page 29). Section 3.6 [Wisdom Reference], page 34 describes the wisdommechanism for exporting and importing plans. Finally, Section 3.7 [Memory Allocator Ref-erence], page 35 describes how to change FFTW’s default memory allocator. For paralleltransforms, See Chapter 4 [Parallel FFTW], page 37.

    3.1 Data Types

    The routines in the FFTW package use three main kinds of data types. Real andcomplex numbers should be already known to the reader. We also use the term halfcomplexto describe complex arrays in a special packed format used by the one-dimensional realtransforms (taking advantage of the hermitian symmetry that arises in those cases).

    By including or , you will have access to the following definitions:typedef double fftw_real;

    typedef struct {fftw_real re, im;

    } fftw_complex;

    #define c_re(c) ((c).re)#define c_im(c) ((c).im)

    All FFTW operations are performed on the fftw_real and fftw_complex data types.For fftw_complex numbers, the two macros c_re and c_im retrieve, respectively, the realand imaginary parts of the number.

    A real array is an array of real numbers. A complex array is an array of complexnumbers. A one-dimensional array X of n complex numbers is hermitian if the followingproperty holds: for all 0 ≤ i < n, we have Xi = X∗n−i, where x∗ denotes the complexconjugate of x. Hermitian arrays are relevant to FFTW because the Fourier transform of areal array is hermitian.

    Because of its symmetry, a hermitian array can be stored in half the space of a complexarray of the same size. FFTW’s one-dimensional real transforms store hermitian arrays ashalfcomplex arrays. A halfcomplex array of size n is a one-dimensional array of n fftw_real numbers. A hermitian array X in stored into a halfcomplex array Y as follows. Forall integers i such that 0 ≤ i ≤ n/2, we have Yi := Re(Xi). For all integers i such that0 < i < n/2, we have Yn−i := Im(Xi).

    We now illustrate halfcomplex storage for n = 4 and n = 5, since the scheme depends onthe parity of n. Let n = 4. In this case, we have Y0 := Re(X0), Y1 := Re(X1), Y2 := Re(X2),

  • 18 FFTW

    and Y3 := Im(X1). Let now n = 5. In this case, we have Y0 := Re(X0), Y1 := Re(X1),Y2 := Re(X2), Y3 := Im(X2), and Y4 := Im(X1).

    By default, the type fftw_real equals the C type double. To work in single precisionrather than double precision, #define the symbol FFTW_ENABLE_FLOAT in fftw.h and thenrecompile the library. On Unix systems, you can instead use configure --enable-floatat installation time (see Chapter 6 [Installation and Customization], page 55).

    In version 1 of FFTW, the data types were called FFTW_REAL and FFTW_COMPLEX. Wechanged the capitalization for consistency with the rest of FFTW’s conventions. The oldnames are still supported, but their use is deprecated.

    3.2 One-dimensional Transforms Reference

    The one-dimensional complex routines are generally prefixed with fftw_. Programsusing FFTW should be linked with -lfftw -lm on Unix systems, or with the FFTW andstandard math libraries in general.

    3.2.1 Plan Creation for One-dimensional Transforms

    #include

    fftw_plan fftw_create_plan(int n, fftw_direction dir,int flags);

    fftw_plan fftw_create_plan_specific(int n, fftw_direction dir,int flags,fftw_complex *in, int istride,fftw_complex *out, int ostride);

    The function fftw_create_plan creates a plan, which is a data structure containing allthe information that fftw needs in order to compute the 1D Fourier transform. You cancreate as many plans as you need, but only one plan for a given array size is required (aplan can be reused many times).

    fftw_create_plan returns a valid plan, or NULL if, for some reason, the plan can’t becreated. In the default installation, this cannot happen, but it is possible to configureFFTW in such a way that some input sizes are forbidden, and FFTW cannot create a plan.

    The fftw_create_plan_specific variant takes as additional arguments specific in-put/output arrays and their strides. For the last four arguments, you should pass thearrays and strides that you will eventually be passing to fftw. The resulting plans will beoptimized for those arrays and strides, although they may be used on other arrays as well.Note: the contents of the in and out arrays are destroyed by the specific planner (the initialcontents are ignored, so the arrays need not have been initialized).

    Arguments

    • n is the size of the transform. It can be any positive integer.− FFTW is best at handling sizes of the form 2a3b5c7d11e13f , where e+f is either 0 or

    1, and the other exponents are arbitrary. Other sizes are computed by means of aslow, general-purpose routine (which nevertheless retains O(n log n) performance,

  • Chapter 3: FFTW Reference 19

    even for prime sizes). (It is possible to customize FFTW for different array sizes.See Chapter 6 [Installation and Customization], page 55, for more information.)Transforms whose sizes are powers of 2 are especially fast.

    • dir is the sign of the exponent in the formula that defines the Fourier transform. Itcan be −1 or +1. The aliases FFTW_FORWARD and FFTW_BACKWARD are provided, whereFFTW_FORWARD stands for −1.

    • flags is a boolean OR (‘|’) of zero or more of the following:− FFTW_MEASURE: this flag tells FFTW to find the optimal plan by actually computing

    several FFTs and measuring their execution time. Depending on the installation,this can take some time.1

    − FFTW_ESTIMATE: do not run any FFT and provide a “reasonable” plan (for a RISCprocessor with many registers). If neither FFTW_ESTIMATE nor FFTW_MEASURE isprovided, the default is FFTW_ESTIMATE.

    − FFTW_OUT_OF_PLACE: produce a plan assuming that the input and output arrayswill be distinct (this is the default).

    − FFTW_IN_PLACE: produce a plan assuming that you want the output in the inputarray. The algorithm used is not necessarily in place: FFTW is able to computetrue in-place transforms only for small values of n. If FFTW is not able to computethe transform in-place, it will allocate a temporary array (unless you provide oneyourself), compute the transform out of place, and copy the result back. Warning:This option changes the meaning of some parameters of fftw (see Section 3.2.3[Computing the One-dimensional Transform], page 20).The in-place option is mainly provided for people who want to write their own in-place multi-dimensional Fourier transform, using FFTW as a base. For example,consider a three-dimensional n * n * n transform. An out-of-place algorithm willneed another array (which may be huge). However, FFTW can compute thein-place transform along each dimension using only a temporary array of size n.Moreover, if FFTW happens to be able to compute the transform truly in-place,no temporary array and no copying are needed. As distributed, FFTW ‘knows’how to compute in-place transforms of size 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,14, 15, 16, 32 and 64.The default mode of operation is FFTW_OUT_OF_PLACE.

    − FFTW_USE_WISDOM: use any wisdom that is available to help in the creation of theplan. (See Section 2.6 [Words of Wisdom], page 13.) This can greatly speed thecreation of plans, especially with the FFTW_MEASURE option. FFTW_ESTIMATE planscan also take advantage of wisdom to produce a more optimal plan (based on pastmeasurements) than the estimation heuristic would normally generate. When theFFTW_MEASURE option is used, new wisdom will also be generated if the currenttransform size is not completely understood by existing wisdom.

    • in, out, istride, ostride (only for fftw_create_plan_specific): see correspond-ing arguments in the description of fftw. (See Section 3.2.3 [Computing the One-dimensional Transform], page 20.) In particular, the out and ostride parametershave the same special meaning for FFTW_IN_PLACE transforms as they have for fftw.

    1 The basic problem is the resolution of the clock: FFTW needs to run for a certain time for the clock tobe reliable.

  • 20 FFTW

    3.2.2 Discussion on Specific Plans

    We recommend the use of the specific planners, even in cases where you will be transform-ing arrays different from those passed to the specific planners, as they confer the followingadvantages:

    • The resulting plans will be optimized for your specific arrays and strides. This mayor may not make a significant difference, but it certainly doesn’t hurt. (The ordinaryplanner does its planning based upon a stride-one temporary array that it allocates.)

    • Less intermediate storage is required during the planning process. (The ordinary plan-ner uses O(N) temporary storage, where N is the maximum dimension, while it is cre-ating the plan.)

    • For multi-dimensional transforms, new parameters become accessible for optimizationby the planner. (Since multi-dimensional arrays can be very large, we don’t dare toallocate one in the ordinary planner for experimentation. This prevents us from doingcertain optimizations that can yield dramatic improvements in some cases.)

    On the other hand, note that the specific planner destroys the contents of the in andout arrays.

    3.2.3 Computing the One-dimensional Transform

    #include

    void fftw(fftw_plan plan, int howmany,fftw_complex *in, int istride, int idist,fftw_complex *out, int ostride, int odist);

    void fftw_one(fftw_plan plan, fftw_complex *in,fftw_complex *out);

    The function fftw computes the one-dimensional Fourier transform, using a plan createdby fftw_create_plan (See Section 3.2.1 [Plan Creation for One-dimensional Transforms],page 18.) The function fftw_one provides a simplified interface for the common case ofsingle input array of stride 1.

    Arguments

    • plan is the plan created by fftw_create_plan (see Section 3.2.1 [Plan Creation forOne-dimensional Transforms], page 18).

    • howmany is the number of transforms fftw will compute. It is faster to tell FFTW tocompute many transforms, instead of simply calling fftw many times.

    • in, istride and idist describe the input array(s). There are howmany input arrays;the first one is pointed to by in, the second one is pointed to by in + idist, and so on,up to in + (howmany - 1) * idist. Each input array consists of complex numbers (seeSection 3.1 [Data Types], page 17), which are not necessarily contiguous in memory.Specifically, in[0] is the first element of the first array, in[istride] is the secondelement of the first array, and so on. In general, the i-th element of the j-th inputarray will be in position in[i * istride + j * idist].

  • Chapter 3: FFTW Reference 21

    • out, ostride and odist describe the output array(s). The format is the same as forthe input array.− In-place transforms: If the plan specifies an in-place transform, ostride and

    odist are always ignored. If out is NULL, out is ignored, too. Otherwise, out isinterpreted as a pointer to an array of n complex numbers, that FFTW will useas temporary space to perform the in-place computation. out is used as scratchspace and its contents destroyed. In this case, out must be an ordinary arraywhose elements are contiguous in memory (no striding).

    The function fftw_one transforms a single, contiguous input array to a contiguousoutput array. By definition, the call

    fftw_one(plan, in, out)

    is equivalent tofftw(plan, 1, in, 1, 0, out, 1, 0)

    3.2.4 Destroying a One-dimensional Plan

    #include

    void fftw_destroy_plan(fftw_plan plan);

    The function fftw_destroy_plan frees the plan plan and releases all the memory asso-ciated with it. After destruction, a plan is no longer valid.

    3.2.5 What FFTW Really Computes

    In this section, we define precisely what FFTW computes. Please be warned that differ-ent authors and software packages might employ different conventions than FFTW does.

    The forward transform of a complex array X of size n computes an array Y , where

    Yi =n−1∑j=0

    Xje−2πij

    √−1/n .

    The backward transform computes

    Yi =n−1∑j=0

    Xje2πij√−1/n .

    FFTW computes an unnormalized transform, that is, the equation IFFT (FFT (X)) =nX holds. In other words, applying the forward and then the backward transform willmultiply the input by n.

    An FFTW_FORWARD transform corresponds to a sign of −1 in the exponent of the DFT.Note also that we use the standard “in-order” output ordering—the k-th output correspondsto the frequency k/n (or k/T , where T is your total sampling period). For those who like tothink in terms of positive and negative frequencies, this means that the positive frequenciesare stored in the first half of the output and the negative frequencies are stored in backwardsorder in the second half of the output. (The frequency −k/n is the same as the frequency(n− k)/n.)

  • 22 FFTW

    3.3 Multi-dimensional Transforms Reference

    The multi-dimensional complex routines are generally prefixed with fftwnd_. Programsusing FFTWND should be linked with -lfftw -lm on Unix systems, or with the FFTWand standard math libraries in general.

    3.3.1 Plan Creation for Multi-dimensional Transforms

    #include

    fftwnd_plan fftwnd_create_plan(int rank, const int *n,fftw_direction dir, int flags);

    fftwnd_plan fftw2d_create_plan(int nx, int ny,fftw_direction dir, int flags);

    fftwnd_plan fftw3d_create_plan(int nx, int ny, int nz,fftw_direction dir, int flags);

    fftwnd_plan fftwnd_create_plan_specific(int rank, const int *n,fftw_direction dir,int flags,fftw_complex *in, int istride,fftw_complex *out, int ostride);

    fftwnd_plan fftw2d_create_plan_specific(int nx, int ny,fftw_direction dir,int flags,fftw_complex *in, int istride,fftw_complex *out, int ostride);

    fftwnd_plan fftw3d_create_plan_specific(int nx, int ny, int nz,fftw_direction dir, int flags,fftw_complex *in, int istride,fftw_complex *out, int ostride);

    The function fftwnd_create_plan creates a plan, which is a data structure containingall the information that fftwnd needs in order to compute a multi-dimensional Fouriertransform. You can create as many plans as you need, but only one plan for a given arraysize is required (a plan can be reused many times). The functions fftw2d_create_planand fftw3d_create_plan are optional, alternative interfaces to fftwnd_create_plan fortwo and three dimensions, respectively.

    fftwnd_create_plan returns a valid plan, or NULL if, for some reason, the plan can’t becreated. This can happen if memory runs out or if the arguments are invalid in some way(e.g. if rank < 0).

    The create_plan_specific variants take as additional arguments specific input/outputarrays and their strides. For the last four arguments, you should pass the arrays and stridesthat you will eventually be passing to fftwnd. The resulting plans will be optimized forthose arrays and strides, although they may be used on other arrays as well. Note: the

  • Chapter 3: FFTW Reference 23

    contents of the in and out arrays are destroyed by the specific planner (the initial contentsare ignored, so the arrays need not have been initialized). See Section 3.2.2 [Discussion onSpecific Plans], page 20, for a discussion on specific plans.

    Arguments

    • rank is the dimensionality of the arrays to be transformed. It can be any non-negativeinteger.

    • n is a pointer to an array of rank integers, giving the size of each dimension of thearrays to be transformed. These sizes, which must be positive integers, correspondto the dimensions of row-major arrays—i.e. n[0] is the size of the dimension whoseindices vary most slowly, and so on. (See Section 2.5 [Multi-dimensional Array Format],page 11, for more information on row-major storage.) See Section 3.2.1 [Plan Creationfor One-dimensional Transforms], page 18, for more information regarding optimalarray sizes.

    • nx and ny in fftw2d_create_plan are positive integers specifying the dimensions ofthe rank 2 array to be transformed. i.e. they specify that the transform will operateon nx x ny arrays in row-major order, where nx is the number of rows and ny is thenumber of columns.

    • nx, ny and nz in fftw3d_create_plan are positive integers specifying the dimensionsof the rank 3 array to be transformed. i.e. they specify that the transform will operateon nx x ny x nz arrays in row-major order.

    • dir is the sign of the exponent in the formula that defines the Fourier transform. Itcan be −1 or +1. The aliases FFTW_FORWARD and FFTW_BACKWARD are provided, whereFFTW_FORWARD stands for −1.

    • flags is a boolean OR (‘|’) of zero or more of the following:− FFTW_MEASURE: this flag tells FFTW to find the optimal plan by actually computing

    several FFTs and measuring their execution time.− FFTW_ESTIMATE: do not run any FFT and provide a “reasonable” plan (for a RISC

    processor with many registers). If neither FFTW_ESTIMATE nor FFTW_MEASURE isprovided, the default is FFTW_ESTIMATE.

    − FFTW_OUT_OF_PLACE: produce a plan assuming that the input and output arrayswill be distinct (this is the default).

    − FFTW_IN_PLACE: produce a plan assuming that you want to perform the trans-form in-place. (Unlike the one-dimensional transform, this “really”2 performs thetransform in-place.) Note that, if you want to perform in-place transforms, youmust use a plan created with this option.The default mode of operation is FFTW_OUT_OF_PLACE.

    − FFTW_USE_WISDOM: use any wisdom that is available to help in the creation ofthe plan. (See Section 2.6 [Words of Wisdom], page 13.) This can greatly speedthe creation of plans, especially with the FFTW_MEASURE option. FFTW_ESTIMATE

    2 fftwnd actually may use some temporary storage (hidden in the plan), but this storage space is onlythe size of the largest dimension of the array, rather than being as big as the entire array. (Unless youuse fftwnd to perform one-dimensional transforms, in which case the temporary storage required forin-place transforms is as big as the entire array.)

  • 24 FFTW

    plans can also take advantage of wisdom to produce a more optimal plan (basedon past measurements) than the estimation heuristic would normally generate.When the FFTW_MEASURE option is used, new wisdom will also be generated if thecurrent transform size is not completely understood by existing wisdom. Notethat the same wisdom is shared between one-dimensional and multi-dimensionaltransforms.

    • in, out, istride, ostride (only for the _create_plan_specific variants): see cor-responding arguments in the description of fftwnd. (See Section 3.3.2 [Computing theMulti-dimensional Transform], page 24.)

    3.3.2 Computing the Multi-dimensional Transform

    #include

    void fftwnd(fftwnd_plan plan, int howmany,fftw_complex *in, int istride, int idist,fftw_complex *out, int ostride, int odist);

    void fftwnd_one(fftwnd_plan p, fftw_complex *in,fftw_complex *out);

    The function fftwnd computes one or more multi-dimensional Fourier Transforms, us-ing a plan created by fftwnd_create_plan (see Section 3.3.1 [Plan Creation for Multi-dimensional Transforms], page 22). (Note that the plan determines the rank and dimensionsof the array to be transformed.) The function fftwnd_one provides a simplified interfacefor the common case of single input array of stride 1.

    Arguments

    • plan is the plan created by fftwnd_create_plan. (see Section 3.3.1 [PlanCreation for Multi-dimensional Transforms], page 22). In the case of two andthree-dimensional transforms, it could also have been created by fftw2d_create_planor fftw3d_create_plan, respectively.

    • howmany is the number of multi-dimensional transforms fftwnd will compute.• in, istride and idist describe the input array(s). There are howmany multi-

    dimensional input arrays; the first one is pointed to by in, the second one is pointed toby in + idist, and so on, up to in + (howmany - 1) * idist. Each multi-dimensionalinput array consists of complex numbers (see Section 3.1 [Data Types], page 17),stored in row-major format (see Section 2.5 [Multi-dimensional Array Format],page 11), which are not necessarily contiguous in memory. Specifically, in[0] isthe first element of the first array, in[istride] is the second element of the firstarray, and so on. In general, the i-th element of the j-th input array will be inposition in[i * istride + j * idist]. Note that, here, i refers to an index intothe row-major format for the multi-dimensional array, rather than an index in anyparticular dimension.− In-place transforms: For plans created with the FFTW_IN_PLACE option, the trans-

    form is computed in-place—the output is returned in the in array, using the samestrides, etcetera, as were used in the input.

  • Chapter 3: FFTW Reference 25

    • out, ostride and odist describe the output array(s). The format is the same as forthe input array.

    − In-place transforms: These parameters are ignored for plans created with theFFTW_IN_PLACE option.

    The function fftwnd_one transforms a single, contiguous input array to a contiguousoutput array. By definition, the call

    fftwnd_one(plan, in, out)

    is equivalent to

    fftwnd(plan, 1, in, 1, 0, out, 1, 0)

    3.3.3 Destroying a Multi-dimensional Plan

    #include

    void fftwnd_destroy_plan(fftwnd_plan plan);

    The function fftwnd_destroy_plan frees the plan plan and releases all the memoryassociated with it. After destruction, a plan is no longer valid.

    3.3.4 What FFTWND Really Computes

    The conventions that we follow for the multi-dimensional transform are analogous tothose for the one-dimensional transform. In particular, the forward transform has a negativesign in the exponent and neither the forward nor the backward transforms will perform anynormalization. Computing the backward transform of the forward transform will multiplythe array by the product of its dimensions. The output is in-order, and the zeroth elementof the output is the amplitude of the zero frequency component.

    The exact mathematical definition of our multi-dimensional transform follows. Let Xbe a d-dimensional complex array whose elements are X[j1, j2, . . . , jd], where 0 ≤ js < nsfor all s ∈ {1, 2, . . . , d}. Let also ωs = e2π

    √−1/ns , for all s ∈ {1, 2, . . . , d}.

    The forward transform computes a complex array Y , whose structure is the same as thatof X, defined by

    Y [i1, i2, . . . , id] =n1−1∑j1=0

    n2−1∑j2=0

    · · ·nd−1∑jd=0

    X[j1, j2, . . . , jd]ω−i1j11 ω

    −i2j22 · · ·ω−idjdd .

    The backward transform computes

    Y [i1, i2, . . . , id] =n1−1∑j1=0

    n2−1∑j2=0

    · · ·nd−1∑jd=0

    X[j1, j2, . . . , jd]ωi1j11 ω

    i2j22 · · ·ωidjdd .

    Computing the forward transform followed by the backward transform will multiply thearray by

    ∏ds=1 nd.

  • 26 FFTW

    3.4 Real One-dimensional Transforms Reference

    The one-dimensional real routines are generally prefixed with rfftw_.3 Programs usingRFFTW should be linked with -lrfftw -lfftw -lm on Unix systems, or with the RFFTW,the FFTW, and the standard math libraries in general.

    3.4.1 Plan Creation for Real One-dimensional Transforms

    #include

    rfftw_plan rfftw_create_plan(int n, fftw_direction dir, int flags);

    rfftw_plan rfftw_create_plan_specific(int n, fftw_direction dir,int flags, fftw_real *in, int istride,fftw_real *out, int ostride);

    The function rfftw_create_plan creates a plan, which is a data structure containing allthe information that rfftw needs in order to compute the 1D real Fourier transform. Youcan create as many plans as you need, but only one plan for a given array size is required(a plan can be reused many times).

    rfftw_create_plan returns a valid plan, or NULL if, for some reason, the plan can’tbe created. In the default installation, this cannot happen, but it is possible to configureRFFTW in such a way that some input sizes are forbidden, and RFFTW cannot create aplan.

    The rfftw_create_plan_specific variant takes as additional arguments specific in-put/output arrays and their strides. For the last four arguments, you should pass thearrays and strides that you will eventually be passing to rfftw. The resulting plans willbe optimized for those arrays and strides, although they may be used on other arrays aswell. Note: the contents of the in and out arrays are destroyed by the specific planner (theinitial contents are ignored, so the arrays need not have been initialized). See Section 3.2.2[Discussion on Specific Plans], page 20, for a discussion on specific plans.

    Arguments

    • n is the size of the transform. It can be any positive integer.− RFFTW is best at handling sizes of the form 2a3b5c7d11e13f , where e+f is either

    0 or 1, and the other exponents are arbitrary. Other sizes are computed by meansof a slow, general-purpose routine (reducing to O(n2) performance for prime sizes).(It is possible to customize RFFTW for different array sizes. See Chapter 6 [In-stallation and Customization], page 55, for more information.) Transforms whosesizes are powers of 2 are especially fast. If you have large prime factors, it maybe faster to switch over to the complex FFTW routines, which have O(n log n)performance even for prime sizes (we don’t know of a similar algorithm specializedfor real data, unfortunately).

    • dir is the direction of the desired transform, either FFTW_REAL_TO_COMPLEX or FFTW_COMPLEX_TO_REAL, corresponding to FFTW_FORWARD or FFTW_BACKWARD, respectively.

    3 The etymologically-correct spelling would be frftw_, but it is hard to remember.

  • Chapter 3: FFTW Reference 27

    • flags is a boolean OR (‘|’) of zero or more of the following:− FFTW_MEASURE: this flag tells RFFTW to find the optimal plan by actually com-

    puting several FFTs and measuring their execution time. Depending on the in-stallation, this can take some time.

    − FFTW_ESTIMATE: do not run any FFT and provide a “reasonable” plan (for a RISCprocessor with many registers). If neither FFTW_ESTIMATE nor FFTW_MEASURE isprovided, the default is FFTW_ESTIMATE.

    − FFTW_OUT_OF_PLACE: produce a plan assuming that the input and output arrayswill be distinct (this is the default).

    − FFTW_IN_PLACE: produce a plan assuming that you want the output in the inputarray. The algorithm used is not necessarily in place: RFFTW is able to computetrue in-place transforms only for small values of n. If RFFTW is not able tocompute the transform in-place, it will allocate a temporary array (unless youprovide one yourself), compute the transform out of place, and copy the resultback. Warning: This option changes the meaning of some parameters of rfftw(see Section 3.4.2 [Computing the Real One-dimensional Transform], page 27).

    The default mode of operation is FFTW_OUT_OF_PLACE.

    − FFTW_USE_WISDOM: use any wisdom that is available to help in the creation of theplan. (See Section 2.6 [Words of Wisdom], page 13.) This can greatly speed thecreation of plans, especially with the FFTW_MEASURE option. FFTW_ESTIMATE planscan also take advantage of wisdom to produce a more optimal plan (based on pastmeasurements) than the estimation heuristic would normally generate. When theFFTW_MEASURE option is used, new wisdom will also be generated if the currenttransform size is not completely understood by existing wisdom.

    • in, out, istride, ostride (only for rfftw_create_plan_specific): see correspond-ing arguments in the description of rfftw. (See Section 3.4.2 [Computing the RealOne-dimensional Transform], page 27.) In particular, the out and ostride parametershave the same special meaning for FFTW_IN_PLACE transforms as they have for rfftw.

    3.4.2 Computing the Real One-dimensional Transform

    #include

    void rfftw(rfftw_plan plan, int howmany,fftw_real *in, int istride, int idist,fftw_real *out, int ostride, int odist);

    void rfftw_one(rfftw_plan plan, fftw_real *in, fftw_real *out);

    The function rfftw computes the Real One-dimensional Fourier Transform, using a plancreated by rfftw_create_plan (see Section 3.4.1 [Plan Creation for Real One-dimensionalTransforms], page 26). The function rfftw_one provides a simplified interface for thecommon case of single input array of stride 1.

    Important: When invoked for an out-of-place, FFTW_COMPLEX_TO_REAL transform, theinput array is overwritten with scratch values by these routines. The input array is notmodified for FFTW_REAL_TO_COMPLEX transforms.

  • 28 FFTW

    Arguments

    • plan is the plan created by rfftw_create_plan (see Section 3.4.1 [Plan Creation forReal One-dimensional Transforms], page 26).

    • howmany is the number of transforms rfftw will compute. It is faster to tell RFFTWto compute many transforms, instead of simply calling rfftw many times.

    • in, istride and idist describe the input array(s). There are two cases. If theplan defines a FFTW_REAL_TO_COMPLEX transform, in is a real array. Otherwise, forFFTW_COMPLEX_TO_REAL transforms, in is a halfcomplex array whose contents will bedestroyed.

    • out, ostride and odist describe the output array(s), and have the same meaning asthe corresponding parameters for the input array.− In-place transforms: If the plan specifies an in-place transform, ostride and

    odist are always ignored. If out is NULL, out is ignored, too. Otherwise, out isinterpreted as a pointer to an array of n complex numbers, that FFTW will useas temporary space to perform the in-place computation. out is used as scratchspace and its contents destroyed. In this case, out must be an ordinary arraywhose elements are contiguous in memory (no striding).

    The function rfftw_one transforms a single, contiguous input array to a contiguousoutput array. By definition, the call

    rfftw_one(plan, in, out)

    is equivalent torfftw(plan, 1, in, 1, 0, out, 1, 0)

    3.4.3 Destroying a Real One-dimensional Plan

    #include

    void rfftw_destroy_plan(rfftw_plan plan);

    The function rfftw_destroy_plan frees the plan plan and releases all the memoryassociated with it. After destruction, a plan is no longer valid.

    3.4.4 What RFFTW Really Computes

    In this section, we define precisely what RFFTW computes.

    The real to complex (FFTW_REAL_TO_COMPLEX) transform of a real array X of size ncomputes an hermitian array Y , where

    Yi =n−1∑j=0

    Xje−2πij

    √−1/n

    (That Y is a hermitian array is not intended to be obvious, although the proof is easy.) Thehermitian array Y is stored in halfcomplex order (see Section 3.1 [Data Types], page 17).Currently, RFFTW provides no way to compute a real to complex transform with a positivesign in the exponent.

  • Chapter 3: FFTW Reference 29

    The complex to real (FFTW_COMPLEX_TO_REAL) transform of a hermitian array X of sizen computes a real array Y , where

    Yi =n−1∑j=0

    Xje2πij√−1/n

    (That Y is a real array is not intended to be obvious, although the proof is easy.) Thehermitian input array X is stored in halfcomplex order (see Section 3.1 [Data Types],page 17). Currently, RFFTW provides no way to compute a complex to real transformwith a negative sign in the exponent.

    Like FFTW, RFFTW computes an unnormalized transform. In other words, applyingthe real to complex (forward) and then the complex to real (backward) transform willmultiply the input by n.

    3.5 Real Multi-dimensional Transforms Reference

    The multi-dimensional real routines are generally prefixed with rfftwnd_. Programsusing RFFTWND should be linked with -lrfftw -lfftw -lm on Unix systems, or with theFFTW, RFFTW, and standard math libraries in general.

    3.5.1 Plan Creation for Real Multi-dimensional Transforms

    #include

    rfftwnd_plan rfftwnd_create_plan(int rank, const int *n,fftw_direction dir, int flags);

    rfftwnd_plan rfftw2d_create_plan(int nx, int ny,fftw_direction dir, int flags);

    rfftwnd_plan rfftw3d_create_plan(int nx, int ny, int nz,fftw_direction dir, int flags);

    The function rfftwnd_create_plan creates a plan, which is a data structure containingall the information that rfftwnd needs in order to compute a multi-dimensional real Fouriertransform. You can create as many plans as you need, but only one plan for a given arraysize is required (a plan can be reused many times). The functions rfftw2d_create_planand rfftw3d_create_plan are optional, alternative interfaces to rfftwnd_create_planfor two and three dimensions, respectively.

    rfftwnd_create_plan returns a valid plan, or NULL if, for some reason, the plan can’tbe created. This can happen if the arguments are invalid in some way (e.g. if rank < 0).

    Arguments

    • rank is the dimensionality of the arrays to be transformed. It can be any non-negativeinteger.

    • n is a pointer to an array of rank integers, giving the size of each dimension of the arraysto be transformed. Note that these are always the dimensions of the real arrays; the

  • 30 FFTW

    complex arrays have different dimensions (see Section 3.5.3 [Array Dimensions for RealMulti-dimensional Transforms], page 32). These sizes, which must be positive integers,correspond to the dimensions of row-major arrays—i.e. n[0] is the size of the dimensionwhose indices vary most slowly, and so on. (See Section 2.5 [Multi-dimensional ArrayFormat], page 11, for more information.)− See Section 3.4.1 [Plan Creation for Real One-dimensional Transforms], page 26,

    for more information regarding optimal array sizes.• nx and ny in rfftw2d_create_plan are positive integers specifying the dimensions of

    the rank 2 array to be transformed. i.e. they specify that the transform will operateon nx x ny arrays in row-major order, where nx is the number of rows and ny is thenumber of columns.

    • nx, ny and nz in rfftw3d_create_plan are positive integers specifying the dimensionsof the rank 3 array to be transformed. i.e. they specify that the transform will operateon nx x ny x nz arrays in row-major order.

    • dir is the direction of the desired transform, either FFTW_REAL_TO_COMPLEX or FFTW_COMPLEX_TO_REAL, corresponding to FFTW_FORWARD or FFTW_BACKWARD, respectively.

    • flags is a boolean OR (‘|’) of zero or more of the following:− FFTW_MEASURE: this flag tells FFTW to find the optimal plan by actually computing

    several FFTs and measuring their execution time.− FFTW_ESTIMATE: do not run any FFT and provide a “reasonable” plan (for a RISC

    processor with many registers). If neither FFTW_ESTIMATE nor FFTW_MEASURE isprovided, the default is FFTW_ESTIMATE.

    − FFTW_OUT_OF_PLACE: produce a plan assuming that the input and output arrayswill be distinct (this is the default).

    − FFTW_IN_PLACE: produce a plan assuming that you want to perform the trans-form in-place. (Unlike the one-dimensional transform, this “really” performs thetransform in-place.) Note that, if you want to perform in-place transforms, youmust use a plan created with this option. The use of this option has importantimplications for the size of the input/output array (see Section 3.5.2 [Computingthe Real Multi-dimensional Transform], page 30).The default mode of operation is FFTW_OUT_OF_PLACE.

    − FFTW_USE_WISDOM: use any wisdom that is available to help in the creation ofthe plan. (See Section 2.6 [Words of Wisdom], page 13.) This can greatly speedthe creation of plans, especially with the FFTW_MEASURE option. FFTW_ESTIMATEplans can also take advantage of wisdom to produce a more optimal plan (basedon past measurements) than the estimation heuristic would normally generate.When the FFTW_MEASURE option is used, new wisdom will also be generated if thecurrent transform size is not completely understood by existing wisdom. Notethat the same wisdom is shared between one-dimensional and multi-dimensionaltransforms.

    3.5.2 Computing the Real Multi-dimensional Transform

    #include

  • Chapter 3: FFTW Reference 31

    void rfftwnd_real_to_complex(rfftwnd_plan plan, int howmany,fftw_real *in, int istride, int idist,fftw_complex *out, int ostride, int odist);

    void rfftwnd_complex_to_real(rfftwnd_plan plan, int howmany,fftw_complex *in, int istride, int idist,fftw_real *out, int ostride, int odist);

    void rfftwnd_one_real_to_complex(rfftwnd_plan p, fftw_real *in,fftw_complex *out);

    void rfftwnd_one_complex_to_real(rfftwnd_plan p, fftw_complex *in,fftw_real *out);

    These functions compute the real multi-dimensional Fourier Transform, using a plan cre-ated by rfftwnd_create_plan (see Section 3.5.1 [Plan Creation for Real Multi-dimensionalTransforms], page 29). (Note that the plan determines the rank and dimensions of the ar-ray to be transformed.) The ‘rfftwnd_one_’ functions provide a simplified interface for thecommon case of single input array of stride 1. Unlike other transform routines in FFTW,we here use separate functions for the two directions of the transform in order to correctlyexpress the datatypes of the parameters.

    Important: When invoked for an out-of-place, FFTW_COMPLEX_TO_REAL transform withrank > 1, the input array is overwritten with scratch values by these routines. The inputarray is not modified for FFTW_REAL_TO_COMPLEX transforms or for FFTW_COMPLEX_TO_REALwith rank == 1.

    Arguments

    • plan is the plan created by rfftwnd_create_plan. (see Section 3.5.1 [Plan Creation forReal Multi-dimensional Transforms], page 29). In the case of two and three-dimensionaltransforms, it could also have been created by rfftw2d_create_plan or rfftw3d_create_plan, respectively.FFTW_REAL_TO_COMPLEX plans must be used with the ‘real_to_complex’ functions, andFFTW_COMPLEX_TO_REAL plans must be used with the ‘complex_to_real’ functions. Itis an error to mismatch the plan direction and the transform function.

    • howmany is the number of transforms to be computed.• in, istride and idist describe the input array(s). There are howmany input arrays;

    the first one is pointed to by in, the second one is pointed to by in + idist, andso on, up to in + (howmany - 1) * idist. Each input array is stored in row-majorformat (see Section 2.5 [Multi-dimensional Array Format], page 11), and is not neces-sarily contiguous in memory. Specifically, in[0] is the first element of the first array,in[istride] is the second element of the first array, and so on. In general, the i-thelement of the j-th input array will b