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