6228
|
1 /* |
|
2 |
|
3 Copyright (C) 2006 David Bateman |
|
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\ |
6228
|
45 in its calculation. The wisdom used by Octave can be imported directly,\n\ |
|
46 usually from a file /etc/fftw/wisdom, or @dfn{fftw} can be used\n\ |
|
47 to import wisdom. For example\n\ |
|
48 \n\ |
|
49 @example\n\ |
|
50 @var{wisdom} = fftw ('dwisdom')\n\ |
|
51 @end example\n\ |
|
52 \n\ |
|
53 will save the existing wisdom used by Octave to the string @var{wisdom}.\n\ |
|
54 This string can then be saved in the usual manner. This existing wisdom\n\ |
|
55 can be reimported as follows\n\ |
|
56 \n\ |
|
57 @example\n\ |
|
58 fftw ('dwisdom', @var{wisdom})\n\ |
|
59 @end example \n\ |
|
60 \n\ |
|
61 If @var{wisdom} is an empty matrix, then the wisdom used is cleared.\n\ |
|
62 \n\ |
|
63 During the calculation of fourier transforms further wisdom is generated.\n\ |
|
64 The fashion in which this wisdom is generated is equally controlled by\n\ |
|
65 the @dfn{fftw} function. There are five different manners in which the\n\ |
|
66 wisdom can be treated, these being\n\ |
|
67 \n\ |
|
68 @table @asis\n\ |
|
69 @item 'estimate'\n\ |
|
70 This specifies that no run-time measurement of the optimal means of\n\ |
|
71 calculating a particular is performed, and a simple heuristic is used\n\ |
|
72 to pick a (probably sub-optimal) plan. The advantage of this method is\n\ |
|
73 that there is little or no overhead in the generation of the plan, which\n\ |
|
74 is appropriate for a fourier transform that will be calculated once.\n\ |
|
75 \n\ |
|
76 @item 'measure'\n\ |
|
77 In this case a range of algorithms to perform the transform is considered\n\ |
|
78 and the best is selected based on their execution time.\n\ |
|
79 \n\ |
|
80 @item 'patient'\n\ |
|
81 This is like 'measure', but a wider range of algorithms is considered.\n\ |
|
82 \n\ |
7001
|
83 @item 'exhaustive'\n\ |
|
84 This is like 'measure', but all possible algorithms that may be used to\n\ |
6228
|
85 treat the transform are considered.\n\ |
|
86 \n\ |
|
87 @item 'hybrid'\n\ |
|
88 As run-time measurement of the algorithm can be expensive, this is a\n\ |
7001
|
89 compromise where 'measure' is used for transforms up to the size of 8192\n\ |
6228
|
90 and beyond that the 'estimate' method is used.\n\ |
|
91 @end table\n\ |
|
92 \n\ |
|
93 The default method is 'estimate', and the method currently being used can\n\ |
|
94 be probed with\n\ |
|
95 \n\ |
|
96 @example\n\ |
|
97 @var{method} = fftw ('planner')\n\ |
|
98 @end example\n\ |
|
99 \n\ |
|
100 and the method used can be set using\n\ |
|
101 \n\ |
|
102 @example\n\ |
|
103 fftw ('planner', @var{method})\n\ |
|
104 @end example\n\ |
|
105 \n\ |
|
106 Note that calculated wisdom will be lost when restarting Octave. However,\n\ |
|
107 the wisdom data can be reloaded if it is saved to a file as described\n\ |
|
108 above. Also, any system-wide wisdom file that has been found will\n\ |
|
109 also be used. Saved wisdom files should not be used on different\n\ |
|
110 platforms since they will not be efficient and the point of calculating\n\ |
|
111 the wisdom is lost.\n\ |
|
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; |
6228
|
146 |
|
147 if (arg1 == "estimate") |
|
148 meth = fftw_planner.method |
|
149 (octave_fftw_planner::ESTIMATE); |
|
150 else if (arg1 == "measure") |
|
151 meth = fftw_planner.method |
|
152 (octave_fftw_planner::MEASURE); |
|
153 else if (arg1 == "patient") |
|
154 meth = fftw_planner.method |
|
155 (octave_fftw_planner::PATIENT); |
|
156 else if (arg1 == "exhaustive") |
|
157 meth = fftw_planner.method |
|
158 (octave_fftw_planner::EXHAUSTIVE); |
|
159 else if (arg1 == "hybrid") |
|
160 meth = fftw_planner.method |
|
161 (octave_fftw_planner::HYBRID); |
|
162 else |
|
163 error ("unrecognized planner method"); |
|
164 |
|
165 if (!error_state) |
|
166 { |
|
167 if (meth == octave_fftw_planner::MEASURE) |
|
168 retval = octave_value ("measure"); |
|
169 else if (meth == octave_fftw_planner::PATIENT) |
|
170 retval = octave_value ("patient"); |
|
171 else if (meth == octave_fftw_planner::EXHAUSTIVE) |
|
172 retval = octave_value ("exhaustive"); |
|
173 else if (meth == octave_fftw_planner::HYBRID) |
|
174 retval = octave_value ("hybrid"); |
|
175 else |
|
176 retval = octave_value ("estimate"); |
|
177 } |
|
178 } |
|
179 else if (arg0 == "dwisdom") |
|
180 { |
|
181 char *str = fftw_export_wisdom_to_string (); |
|
182 |
|
183 if (arg1.length() < 1) |
|
184 fftw_forget_wisdom (); |
|
185 else if (! fftw_import_wisdom_from_string (arg1.c_str())) |
|
186 error ("could not import supplied wisdom"); |
|
187 |
|
188 if (!error_state) |
|
189 retval = octave_value (std::string (str)); |
|
190 |
|
191 free (str); |
|
192 } |
|
193 else if (arg0 == "swisdom") |
|
194 error ("single precision wisdom is not supported"); |
|
195 else |
|
196 error ("unrecognized argument"); |
|
197 } |
|
198 } |
|
199 else |
|
200 { |
|
201 if (arg0 == "planner") |
|
202 { |
|
203 octave_fftw_planner::FftwMethod meth = |
|
204 fftw_planner.method (); |
|
205 |
|
206 if (meth == octave_fftw_planner::MEASURE) |
|
207 retval = octave_value ("measure"); |
|
208 else if (meth == octave_fftw_planner::PATIENT) |
|
209 retval = octave_value ("patient"); |
|
210 else if (meth == octave_fftw_planner::EXHAUSTIVE) |
|
211 retval = octave_value ("exhaustive"); |
|
212 else if (meth == octave_fftw_planner::HYBRID) |
|
213 retval = octave_value ("hybrid"); |
|
214 else |
|
215 retval = octave_value ("estimate"); |
|
216 } |
|
217 else if (arg0 == "dwisdom") |
|
218 { |
|
219 char *str = fftw_export_wisdom_to_string (); |
|
220 retval = octave_value (std::string (str)); |
|
221 free (str); |
|
222 } |
|
223 else if (arg0 == "swisdom") |
|
224 error ("single precision wisdom is not supported"); |
|
225 else |
|
226 error ("unrecognized argument"); |
|
227 } |
|
228 } |
|
229 } |
|
230 #else |
|
231 |
|
232 warning ("fftw: this copy of Octave was not configured to use FFTW3"); |
|
233 |
|
234 #endif |
|
235 |
|
236 return retval; |
|
237 } |
|
238 |