Mercurial > octave-nkf
annotate src/DLD-FUNCTIONS/fftw.cc @ 8920:eb63fbe60fab
update copyright notices
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Sat, 07 Mar 2009 10:41:27 -0500 |
parents | 37c1bdb9805c |
children | 7c02ec148a3c |
rev | line source |
---|---|
6228 | 1 /* |
2 | |
8920 | 3 Copyright (C) 2006, 2007, 2008 David Bateman |
6228 | 4 |
5 This file is part of Octave. | |
6 | |
7 Octave is free software; you can redistribute it and/or modify it | |
8 under the terms of the GNU General Public License as published by the | |
7016 | 9 Free Software Foundation; either version 3 of the License, or (at your |
10 option) any later version. | |
6228 | 11 |
12 Octave is distributed in the hope that it will be useful, but WITHOUT | |
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
15 for more details. | |
16 | |
17 You should have received a copy of the GNU General Public License | |
7016 | 18 along with Octave; see the file COPYING. If not, see |
19 <http://www.gnu.org/licenses/>. | |
6228 | 20 |
21 */ | |
22 | |
23 #ifdef HAVE_CONFIG_H | |
24 #include <config.h> | |
25 #endif | |
26 | |
27 #include <algorithm> | |
28 #include "ov.h" | |
29 #include "defun-dld.h" | |
30 #include "error.h" | |
31 | |
32 #if defined (HAVE_FFTW3) | |
33 #include "oct-fftw.h" | |
34 #endif | |
35 | |
36 DEFUN_DLD (fftw, args, , | |
37 "-*- texinfo -*-\n\ | |
38 @deftypefn {Loadable Function} {@var{method} =} fftw ('planner')\n\ | |
39 @deftypefnx {Loadable Function} {} fftw ('planner', @var{method})\n\ | |
40 @deftypefnx {Loadable Function} {@var{wisdom} =} fftw ('dwisdom')\n\ | |
41 @deftypefnx {Loadable Function} {@var{wisdom} =} fftw ('dwisdom', @var{wisdom})\n\ | |
42 \n\ | |
43 Manage FFTW wisdom data. Wisdom data can be used to significantly\n\ | |
7001 | 44 accelerate the calculation of the FFTs but implies an initial cost\n\ |
8115
37c1bdb9805c
Clarify fftw documentation
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
45 in its calculation. When the FFTW libraries are initialized, they read\n\ |
37c1bdb9805c
Clarify fftw documentation
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
46 a system wide wisdom file (typically in @file{/etc/fftw/wisdom}), allowing wisdom\n\ |
37c1bdb9805c
Clarify fftw documentation
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
47 to be shared between applications other than Octave. Alternatively, the\n\ |
37c1bdb9805c
Clarify fftw documentation
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
48 @code{fftw} function can be used to import wisdom. For example\n\ |
6228 | 49 \n\ |
50 @example\n\ | |
51 @var{wisdom} = fftw ('dwisdom')\n\ | |
52 @end example\n\ | |
53 \n\ | |
54 will save the existing wisdom used by Octave to the string @var{wisdom}.\n\ | |
8115
37c1bdb9805c
Clarify fftw documentation
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
55 This string can then be saved to a file and restored using the @code{save}\n\ |
37c1bdb9805c
Clarify fftw documentation
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
56 and @code{load} commands respectively. This existing wisdom can be reimported\n\ |
37c1bdb9805c
Clarify fftw documentation
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
57 as follows\n\ |
6228 | 58 \n\ |
59 @example\n\ | |
60 fftw ('dwisdom', @var{wisdom})\n\ | |
61 @end example \n\ | |
62 \n\ | |
63 If @var{wisdom} is an empty matrix, then the wisdom used is cleared.\n\ | |
64 \n\ | |
65 During the calculation of fourier transforms further wisdom is generated.\n\ | |
66 The fashion in which this wisdom is generated is equally controlled by\n\ | |
8115
37c1bdb9805c
Clarify fftw documentation
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
67 the @code{fftw} function. There are five different manners in which the\n\ |
6228 | 68 wisdom can be treated, these being\n\ |
69 \n\ | |
70 @table @asis\n\ | |
71 @item 'estimate'\n\ | |
72 This specifies that no run-time measurement of the optimal means of\n\ | |
73 calculating a particular is performed, and a simple heuristic is used\n\ | |
74 to pick a (probably sub-optimal) plan. The advantage of this method is\n\ | |
75 that there is little or no overhead in the generation of the plan, which\n\ | |
76 is appropriate for a fourier transform that will be calculated once.\n\ | |
77 \n\ | |
78 @item 'measure'\n\ | |
79 In this case a range of algorithms to perform the transform is considered\n\ | |
80 and the best is selected based on their execution time.\n\ | |
81 \n\ | |
82 @item 'patient'\n\ | |
83 This is like 'measure', but a wider range of algorithms is considered.\n\ | |
84 \n\ | |
7001 | 85 @item 'exhaustive'\n\ |
86 This is like 'measure', but all possible algorithms that may be used to\n\ | |
6228 | 87 treat the transform are considered.\n\ |
88 \n\ | |
89 @item 'hybrid'\n\ | |
90 As run-time measurement of the algorithm can be expensive, this is a\n\ | |
7001 | 91 compromise where 'measure' is used for transforms up to the size of 8192\n\ |
6228 | 92 and beyond that the 'estimate' method is used.\n\ |
93 @end table\n\ | |
94 \n\ | |
95 The default method is 'estimate', and the method currently being used can\n\ | |
96 be probed with\n\ | |
97 \n\ | |
98 @example\n\ | |
99 @var{method} = fftw ('planner')\n\ | |
100 @end example\n\ | |
101 \n\ | |
102 and the method used can be set using\n\ | |
103 \n\ | |
104 @example\n\ | |
105 fftw ('planner', @var{method})\n\ | |
106 @end example\n\ | |
107 \n\ | |
108 Note that calculated wisdom will be lost when restarting Octave. However,\n\ | |
109 the wisdom data can be reloaded if it is saved to a file as described\n\ | |
8115
37c1bdb9805c
Clarify fftw documentation
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
110 above. Saved wisdom files should not be used on different platforms since\n\ |
37c1bdb9805c
Clarify fftw documentation
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
111 they will not be efficient and the point of calculating the wisdom is lost.\n\ |
6228 | 112 @seealso{fft, ifft, fft2, ifft2, fftn, ifftn}\n\ |
113 @end deftypefn") | |
114 { | |
115 octave_value retval; | |
116 | |
117 int nargin = args.length(); | |
118 | |
119 if (nargin < 1 || nargin > 2) | |
120 { | |
121 print_usage (); | |
122 return retval; | |
123 } | |
124 | |
125 #if defined (HAVE_FFTW3) | |
126 if (args(0).is_string ()) | |
127 { | |
128 std::string arg0 = args(0).string_value (); | |
129 | |
130 if (!error_state) | |
131 { | |
132 // Use STL function to convert to lower case | |
133 std::transform (arg0.begin (), arg0.end (), arg0.begin (), tolower); | |
134 | |
135 if (nargin == 2) | |
136 { | |
137 std::string arg1 = args(1).string_value (); | |
138 if (!error_state) | |
139 { | |
140 if (arg0 == "planner") | |
141 { | |
142 std::transform (arg1.begin (), arg1.end (), | |
143 arg1.begin (), tolower); | |
6484 | 144 octave_fftw_planner::FftwMethod meth |
145 = octave_fftw_planner::UNKNOWN; | |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
146 octave_float_fftw_planner::FftwMethod methf |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
147 = octave_float_fftw_planner::UNKNOWN; |
6228 | 148 |
149 if (arg1 == "estimate") | |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
150 { |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
151 meth = octave_fftw_planner::ESTIMATE; |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
152 methf = octave_float_fftw_planner::ESTIMATE; |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
153 } |
6228 | 154 else if (arg1 == "measure") |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
155 { |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
156 meth = octave_fftw_planner::MEASURE; |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
157 methf = octave_float_fftw_planner::MEASURE; |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
158 } |
6228 | 159 else if (arg1 == "patient") |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
160 { |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
161 meth = octave_fftw_planner::PATIENT; |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
162 methf = octave_float_fftw_planner::PATIENT; |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
163 } |
6228 | 164 else if (arg1 == "exhaustive") |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
165 { |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
166 meth = octave_fftw_planner::EXHAUSTIVE; |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
167 methf = octave_float_fftw_planner::EXHAUSTIVE; |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
168 } |
6228 | 169 else if (arg1 == "hybrid") |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
170 { |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
171 meth = octave_fftw_planner::HYBRID; |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
172 methf = octave_float_fftw_planner::HYBRID; |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
173 } |
6228 | 174 else |
175 error ("unrecognized planner method"); | |
176 | |
177 if (!error_state) | |
178 { | |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
179 meth = fftw_planner.method (meth); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
180 float_fftw_planner.method (methf); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
181 |
6228 | 182 if (meth == octave_fftw_planner::MEASURE) |
183 retval = octave_value ("measure"); | |
184 else if (meth == octave_fftw_planner::PATIENT) | |
185 retval = octave_value ("patient"); | |
186 else if (meth == octave_fftw_planner::EXHAUSTIVE) | |
187 retval = octave_value ("exhaustive"); | |
188 else if (meth == octave_fftw_planner::HYBRID) | |
189 retval = octave_value ("hybrid"); | |
190 else | |
191 retval = octave_value ("estimate"); | |
192 } | |
193 } | |
194 else if (arg0 == "dwisdom") | |
195 { | |
196 char *str = fftw_export_wisdom_to_string (); | |
197 | |
198 if (arg1.length() < 1) | |
199 fftw_forget_wisdom (); | |
200 else if (! fftw_import_wisdom_from_string (arg1.c_str())) | |
201 error ("could not import supplied wisdom"); | |
202 | |
203 if (!error_state) | |
204 retval = octave_value (std::string (str)); | |
205 | |
206 free (str); | |
207 } | |
208 else if (arg0 == "swisdom") | |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
209 { |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
210 char *str = fftwf_export_wisdom_to_string (); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
211 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
212 if (arg1.length() < 1) |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
213 fftwf_forget_wisdom (); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
214 else if (! fftwf_import_wisdom_from_string (arg1.c_str())) |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
215 error ("could not import supplied wisdom"); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
216 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
217 if (!error_state) |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
218 retval = octave_value (std::string (str)); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
219 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
220 free (str); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
221 } |
6228 | 222 else |
223 error ("unrecognized argument"); | |
224 } | |
225 } | |
226 else | |
227 { | |
228 if (arg0 == "planner") | |
229 { | |
230 octave_fftw_planner::FftwMethod meth = | |
231 fftw_planner.method (); | |
232 | |
233 if (meth == octave_fftw_planner::MEASURE) | |
234 retval = octave_value ("measure"); | |
235 else if (meth == octave_fftw_planner::PATIENT) | |
236 retval = octave_value ("patient"); | |
237 else if (meth == octave_fftw_planner::EXHAUSTIVE) | |
238 retval = octave_value ("exhaustive"); | |
239 else if (meth == octave_fftw_planner::HYBRID) | |
240 retval = octave_value ("hybrid"); | |
241 else | |
242 retval = octave_value ("estimate"); | |
243 } | |
244 else if (arg0 == "dwisdom") | |
245 { | |
246 char *str = fftw_export_wisdom_to_string (); | |
247 retval = octave_value (std::string (str)); | |
248 free (str); | |
249 } | |
250 else if (arg0 == "swisdom") | |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
251 { |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
252 char *str = fftwf_export_wisdom_to_string (); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
253 retval = octave_value (std::string (str)); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
254 free (str); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
255 } |
6228 | 256 else |
257 error ("unrecognized argument"); | |
258 } | |
259 } | |
260 } | |
261 #else | |
262 | |
263 warning ("fftw: this copy of Octave was not configured to use FFTW3"); | |
264 | |
265 #endif | |
266 | |
267 return retval; | |
268 } | |
269 |