annotate src/fftw-3.3.3/doc/html/Complex-One_002dDimensional-DFTs.html @ 10:37bf6b4a2645

Add FFTW3
author Chris Cannam
date Wed, 20 Mar 2013 15:35:50 +0000
parents
children
rev   line source
Chris@10 1 <html lang="en">
Chris@10 2 <head>
Chris@10 3 <title>Complex One-Dimensional DFTs - FFTW 3.3.3</title>
Chris@10 4 <meta http-equiv="Content-Type" content="text/html">
Chris@10 5 <meta name="description" content="FFTW 3.3.3">
Chris@10 6 <meta name="generator" content="makeinfo 4.13">
Chris@10 7 <link title="Top" rel="start" href="index.html#Top">
Chris@10 8 <link rel="up" href="Tutorial.html#Tutorial" title="Tutorial">
Chris@10 9 <link rel="prev" href="Tutorial.html#Tutorial" title="Tutorial">
Chris@10 10 <link rel="next" href="Complex-Multi_002dDimensional-DFTs.html#Complex-Multi_002dDimensional-DFTs" title="Complex Multi-Dimensional DFTs">
Chris@10 11 <link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
Chris@10 12 <!--
Chris@10 13 This manual is for FFTW
Chris@10 14 (version 3.3.3, 25 November 2012).
Chris@10 15
Chris@10 16 Copyright (C) 2003 Matteo Frigo.
Chris@10 17
Chris@10 18 Copyright (C) 2003 Massachusetts Institute of Technology.
Chris@10 19
Chris@10 20 Permission is granted to make and distribute verbatim copies of
Chris@10 21 this manual provided the copyright notice and this permission
Chris@10 22 notice are preserved on all copies.
Chris@10 23
Chris@10 24 Permission is granted to copy and distribute modified versions of
Chris@10 25 this manual under the conditions for verbatim copying, provided
Chris@10 26 that the entire resulting derived work is distributed under the
Chris@10 27 terms of a permission notice identical to this one.
Chris@10 28
Chris@10 29 Permission is granted to copy and distribute translations of this
Chris@10 30 manual into another language, under the above conditions for
Chris@10 31 modified versions, except that this permission notice may be
Chris@10 32 stated in a translation approved by the Free Software Foundation.
Chris@10 33 -->
Chris@10 34 <meta http-equiv="Content-Style-Type" content="text/css">
Chris@10 35 <style type="text/css"><!--
Chris@10 36 pre.display { font-family:inherit }
Chris@10 37 pre.format { font-family:inherit }
Chris@10 38 pre.smalldisplay { font-family:inherit; font-size:smaller }
Chris@10 39 pre.smallformat { font-family:inherit; font-size:smaller }
Chris@10 40 pre.smallexample { font-size:smaller }
Chris@10 41 pre.smalllisp { font-size:smaller }
Chris@10 42 span.sc { font-variant:small-caps }
Chris@10 43 span.roman { font-family:serif; font-weight:normal; }
Chris@10 44 span.sansserif { font-family:sans-serif; font-weight:normal; }
Chris@10 45 --></style>
Chris@10 46 </head>
Chris@10 47 <body>
Chris@10 48 <div class="node">
Chris@10 49 <a name="Complex-One-Dimensional-DFTs"></a>
Chris@10 50 <a name="Complex-One_002dDimensional-DFTs"></a>
Chris@10 51 <p>
Chris@10 52 Next:&nbsp;<a rel="next" accesskey="n" href="Complex-Multi_002dDimensional-DFTs.html#Complex-Multi_002dDimensional-DFTs">Complex Multi-Dimensional DFTs</a>,
Chris@10 53 Previous:&nbsp;<a rel="previous" accesskey="p" href="Tutorial.html#Tutorial">Tutorial</a>,
Chris@10 54 Up:&nbsp;<a rel="up" accesskey="u" href="Tutorial.html#Tutorial">Tutorial</a>
Chris@10 55 <hr>
Chris@10 56 </div>
Chris@10 57
Chris@10 58 <h3 class="section">2.1 Complex One-Dimensional DFTs</h3>
Chris@10 59
Chris@10 60 <blockquote>
Chris@10 61 Plan: To bother about the best method of accomplishing an accidental result.
Chris@10 62 [Ambrose Bierce, <cite>The Enlarged Devil's Dictionary</cite>.]
Chris@10 63 <a name="index-Devil-15"></a></blockquote>
Chris@10 64
Chris@10 65 <p>The basic usage of FFTW to compute a one-dimensional DFT of size
Chris@10 66 <code>N</code> is simple, and it typically looks something like this code:
Chris@10 67
Chris@10 68 <pre class="example"> #include &lt;fftw3.h&gt;
Chris@10 69 ...
Chris@10 70 {
Chris@10 71 fftw_complex *in, *out;
Chris@10 72 fftw_plan p;
Chris@10 73 ...
Chris@10 74 in = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * N);
Chris@10 75 out = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * N);
Chris@10 76 p = fftw_plan_dft_1d(N, in, out, FFTW_FORWARD, FFTW_ESTIMATE);
Chris@10 77 ...
Chris@10 78 fftw_execute(p); /* <span class="roman">repeat as needed</span> */
Chris@10 79 ...
Chris@10 80 fftw_destroy_plan(p);
Chris@10 81 fftw_free(in); fftw_free(out);
Chris@10 82 }
Chris@10 83 </pre>
Chris@10 84 <p>You must link this code with the <code>fftw3</code> library. On Unix systems,
Chris@10 85 link with <code>-lfftw3 -lm</code>.
Chris@10 86
Chris@10 87 <p>The example code first allocates the input and output arrays. You can
Chris@10 88 allocate them in any way that you like, but we recommend using
Chris@10 89 <code>fftw_malloc</code>, which behaves like
Chris@10 90 <a name="index-fftw_005fmalloc-16"></a><code>malloc</code> except that it properly aligns the array when SIMD
Chris@10 91 instructions (such as SSE and Altivec) are available (see <a href="SIMD-alignment-and-fftw_005fmalloc.html#SIMD-alignment-and-fftw_005fmalloc">SIMD alignment and fftw_malloc</a>). [Alternatively, we provide a convenient wrapper function <code>fftw_alloc_complex(N)</code> which has the same effect.]
Chris@10 92 <a name="index-fftw_005falloc_005fcomplex-17"></a><a name="index-SIMD-18"></a>
Chris@10 93
Chris@10 94 <p>The data is an array of type <code>fftw_complex</code>, which is by default a
Chris@10 95 <code>double[2]</code> composed of the real (<code>in[i][0]</code>) and imaginary
Chris@10 96 (<code>in[i][1]</code>) parts of a complex number.
Chris@10 97 <a name="index-fftw_005fcomplex-19"></a>
Chris@10 98 The next step is to create a <dfn>plan</dfn>, which is an object
Chris@10 99 <a name="index-plan-20"></a>that contains all the data that FFTW needs to compute the FFT.
Chris@10 100 This function creates the plan:
Chris@10 101
Chris@10 102 <pre class="example"> fftw_plan fftw_plan_dft_1d(int n, fftw_complex *in, fftw_complex *out,
Chris@10 103 int sign, unsigned flags);
Chris@10 104 </pre>
Chris@10 105 <p><a name="index-fftw_005fplan_005fdft_005f1d-21"></a><a name="index-fftw_005fplan-22"></a>
Chris@10 106 The first argument, <code>n</code>, is the size of the transform you are
Chris@10 107 trying to compute. The size <code>n</code> can be any positive integer, but
Chris@10 108 sizes that are products of small factors are transformed most
Chris@10 109 efficiently (although prime sizes still use an <i>O</i>(<i>n</i>&nbsp;log&nbsp;<i>n</i>) algorithm).
Chris@10 110
Chris@10 111 <p>The next two arguments are pointers to the input and output arrays of
Chris@10 112 the transform. These pointers can be equal, indicating an
Chris@10 113 <dfn>in-place</dfn> transform.
Chris@10 114 <a name="index-in_002dplace-23"></a>
Chris@10 115
Chris@10 116 <p>The fourth argument, <code>sign</code>, can be either <code>FFTW_FORWARD</code>
Chris@10 117 (<code>-1</code>) or <code>FFTW_BACKWARD</code> (<code>+1</code>),
Chris@10 118 <a name="index-FFTW_005fFORWARD-24"></a><a name="index-FFTW_005fBACKWARD-25"></a>and indicates the direction of the transform you are interested in;
Chris@10 119 technically, it is the sign of the exponent in the transform.
Chris@10 120
Chris@10 121 <p>The <code>flags</code> argument is usually either <code>FFTW_MEASURE</code> or
Chris@10 122 <a name="index-flags-26"></a><code>FFTW_ESTIMATE</code>. <code>FFTW_MEASURE</code> instructs FFTW to run
Chris@10 123 <a name="index-FFTW_005fMEASURE-27"></a>and measure the execution time of several FFTs in order to find the
Chris@10 124 best way to compute the transform of size <code>n</code>. This process takes
Chris@10 125 some time (usually a few seconds), depending on your machine and on
Chris@10 126 the size of the transform. <code>FFTW_ESTIMATE</code>, on the contrary,
Chris@10 127 does not run any computation and just builds a
Chris@10 128 <a name="index-FFTW_005fESTIMATE-28"></a>reasonable plan that is probably sub-optimal. In short, if your
Chris@10 129 program performs many transforms of the same size and initialization
Chris@10 130 time is not important, use <code>FFTW_MEASURE</code>; otherwise use the
Chris@10 131 estimate.
Chris@10 132
Chris@10 133 <p><em>You must create the plan before initializing the input</em>, because
Chris@10 134 <code>FFTW_MEASURE</code> overwrites the <code>in</code>/<code>out</code> arrays.
Chris@10 135 (Technically, <code>FFTW_ESTIMATE</code> does not touch your arrays, but you
Chris@10 136 should always create plans first just to be sure.)
Chris@10 137
Chris@10 138 <p>Once the plan has been created, you can use it as many times as you
Chris@10 139 like for transforms on the specified <code>in</code>/<code>out</code> arrays,
Chris@10 140 computing the actual transforms via <code>fftw_execute(plan)</code>:
Chris@10 141 <pre class="example"> void fftw_execute(const fftw_plan plan);
Chris@10 142 </pre>
Chris@10 143 <p><a name="index-fftw_005fexecute-29"></a>
Chris@10 144 The DFT results are stored in-order in the array <code>out</code>, with the
Chris@10 145 zero-frequency (DC) component in <code>out[0]</code>.
Chris@10 146 <a name="index-frequency-30"></a>If <code>in != out</code>, the transform is <dfn>out-of-place</dfn> and the input
Chris@10 147 array <code>in</code> is not modified. Otherwise, the input array is
Chris@10 148 overwritten with the transform.
Chris@10 149
Chris@10 150 <p><a name="index-execute-31"></a>If you want to transform a <em>different</em> array of the same size, you
Chris@10 151 can create a new plan with <code>fftw_plan_dft_1d</code> and FFTW
Chris@10 152 automatically reuses the information from the previous plan, if
Chris@10 153 possible. Alternatively, with the &ldquo;guru&rdquo; interface you can apply a
Chris@10 154 given plan to a different array, if you are careful.
Chris@10 155 See <a href="FFTW-Reference.html#FFTW-Reference">FFTW Reference</a>.
Chris@10 156
Chris@10 157 <p>When you are done with the plan, you deallocate it by calling
Chris@10 158 <code>fftw_destroy_plan(plan)</code>:
Chris@10 159 <pre class="example"> void fftw_destroy_plan(fftw_plan plan);
Chris@10 160 </pre>
Chris@10 161 <p><a name="index-fftw_005fdestroy_005fplan-32"></a>If you allocate an array with <code>fftw_malloc()</code> you must deallocate
Chris@10 162 it with <code>fftw_free()</code>. Do not use <code>free()</code> or, heaven
Chris@10 163 forbid, <code>delete</code>.
Chris@10 164 <a name="index-fftw_005ffree-33"></a>
Chris@10 165 FFTW computes an <em>unnormalized</em> DFT. Thus, computing a forward
Chris@10 166 followed by a backward transform (or vice versa) results in the original
Chris@10 167 array scaled by <code>n</code>. For the definition of the DFT, see <a href="What-FFTW-Really-Computes.html#What-FFTW-Really-Computes">What FFTW Really Computes</a>.
Chris@10 168 <a name="index-DFT-34"></a><a name="index-normalization-35"></a>
Chris@10 169
Chris@10 170 <p>If you have a C compiler, such as <code>gcc</code>, that supports the
Chris@10 171 C99 standard, and you <code>#include &lt;complex.h&gt;</code> <em>before</em>
Chris@10 172 <code>&lt;fftw3.h&gt;</code>, then <code>fftw_complex</code> is the native
Chris@10 173 double-precision complex type and you can manipulate it with ordinary
Chris@10 174 arithmetic. Otherwise, FFTW defines its own complex type, which is
Chris@10 175 bit-compatible with the C99 complex type. See <a href="Complex-numbers.html#Complex-numbers">Complex numbers</a>.
Chris@10 176 (The C++ <code>&lt;complex&gt;</code> template class may also be usable via a
Chris@10 177 typecast.)
Chris@10 178 <a name="index-C_002b_002b-36"></a>
Chris@10 179 To use single or long-double precision versions of FFTW, replace the
Chris@10 180 <code>fftw_</code> prefix by <code>fftwf_</code> or <code>fftwl_</code> and link with
Chris@10 181 <code>-lfftw3f</code> or <code>-lfftw3l</code>, but use the <em>same</em>
Chris@10 182 <code>&lt;fftw3.h&gt;</code> header file.
Chris@10 183 <a name="index-precision-37"></a>
Chris@10 184
Chris@10 185 <p>Many more flags exist besides <code>FFTW_MEASURE</code> and
Chris@10 186 <code>FFTW_ESTIMATE</code>. For example, use <code>FFTW_PATIENT</code> if you're
Chris@10 187 willing to wait even longer for a possibly even faster plan (see <a href="FFTW-Reference.html#FFTW-Reference">FFTW Reference</a>).
Chris@10 188 <a name="index-FFTW_005fPATIENT-38"></a>You can also save plans for future use, as described by <a href="Words-of-Wisdom_002dSaving-Plans.html#Words-of-Wisdom_002dSaving-Plans">Words of Wisdom-Saving Plans</a>.
Chris@10 189
Chris@10 190 <!-- -->
Chris@10 191 </body></html>
Chris@10 192