2928
|
1 /* |
|
2 |
4773
|
3 Copyright (C) 2004 David Bateman |
2928
|
4 Copyright (C) 1996, 1997 John W. Eaton |
|
5 |
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
|
10 Free Software Foundation; either version 2, or (at your option) any |
|
11 later version. |
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
|
19 along with Octave; see the file COPYING. If not, write to the Free |
5307
|
20 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
21 02110-1301, USA. |
2928
|
22 |
|
23 */ |
|
24 |
|
25 #ifdef HAVE_CONFIG_H |
|
26 #include <config.h> |
|
27 #endif |
|
28 |
|
29 #include "lo-mappers.h" |
|
30 |
|
31 #include "defun-dld.h" |
|
32 #include "error.h" |
|
33 #include "gripes.h" |
|
34 #include "oct-obj.h" |
|
35 #include "utils.h" |
|
36 |
4773
|
37 #if defined (HAVE_FFTW3) |
|
38 #define FFTSRC "@sc{Fftw}" |
|
39 #else |
|
40 #define FFTSRC "@sc{Fftpack}" |
|
41 #endif |
2928
|
42 |
4773
|
43 static octave_value |
|
44 do_fft (const octave_value_list &args, const char *fcn, int type) |
2928
|
45 { |
4233
|
46 octave_value retval; |
2928
|
47 |
|
48 int nargin = args.length (); |
|
49 |
4773
|
50 if (nargin < 1 || nargin > 3) |
2928
|
51 { |
5823
|
52 print_usage (); |
2928
|
53 return retval; |
|
54 } |
|
55 |
|
56 octave_value arg = args(0); |
4773
|
57 dim_vector dims = arg.dims (); |
5275
|
58 octave_idx_type n_points = -1; |
4773
|
59 int dim = -1; |
|
60 |
|
61 if (nargin > 1) |
|
62 { |
|
63 if (! args(1).is_empty ()) |
|
64 { |
|
65 double dval = args(1).double_value (); |
|
66 if (xisnan (dval)) |
|
67 error ("%s: NaN is invalid as the N_POINTS", fcn); |
|
68 else |
|
69 { |
5275
|
70 n_points = NINTbig (dval); |
4773
|
71 if (n_points < 0) |
|
72 error ("%s: number of points must be greater than zero", fcn); |
|
73 } |
|
74 } |
|
75 } |
2928
|
76 |
4773
|
77 if (error_state) |
|
78 return retval; |
|
79 |
|
80 if (nargin > 2) |
2928
|
81 { |
4773
|
82 double dval = args(2).double_value (); |
2928
|
83 if (xisnan (dval)) |
4773
|
84 error ("%s: NaN is invalid as the N_POINTS", fcn); |
|
85 else if (dval < 1 || dval > dims.length ()) |
|
86 error ("%s: invalid dimension along which to perform fft", fcn); |
2928
|
87 else |
5275
|
88 // to be safe, cast it back to int since dim is an int |
4773
|
89 dim = NINT (dval) - 1; |
2928
|
90 } |
|
91 |
|
92 if (error_state) |
|
93 return retval; |
|
94 |
5275
|
95 for (octave_idx_type i = 0; i < dims.length (); i++) |
4773
|
96 if (dims(i) < 0) |
2928
|
97 return retval; |
|
98 |
4773
|
99 if (dim < 0) |
4869
|
100 { |
5275
|
101 for (octave_idx_type i = 0; i < dims.length (); i++) |
6620
|
102 if (dims(i) > 1) |
4869
|
103 { |
|
104 dim = i; |
|
105 break; |
|
106 } |
|
107 |
|
108 // And if the first argument is scalar? |
|
109 if (dim < 0) |
|
110 dim = 1; |
|
111 } |
2928
|
112 |
4773
|
113 if (n_points < 0) |
|
114 n_points = dims (dim); |
|
115 else |
|
116 dims (dim) = n_points; |
|
117 |
6620
|
118 if (dims.any_zero () || n_points == 0) |
|
119 return octave_value (NDArray (dims)); |
2928
|
120 |
|
121 if (arg.is_real_type ()) |
|
122 { |
4773
|
123 NDArray nda = arg.array_value (); |
2928
|
124 |
|
125 if (! error_state) |
|
126 { |
4773
|
127 nda.resize (dims, 0.0); |
|
128 retval = (type != 0 ? nda.ifourier (dim) : nda.fourier (dim)); |
2928
|
129 } |
|
130 } |
|
131 else if (arg.is_complex_type ()) |
|
132 { |
4773
|
133 ComplexNDArray cnda = arg.complex_array_value (); |
2928
|
134 |
|
135 if (! error_state) |
|
136 { |
4773
|
137 cnda.resize (dims, 0.0); |
|
138 retval = (type != 0 ? cnda.ifourier (dim) : cnda.fourier (dim)); |
2928
|
139 } |
|
140 } |
|
141 else |
|
142 { |
4773
|
143 gripe_wrong_type_arg (fcn, arg); |
2928
|
144 } |
|
145 |
|
146 return retval; |
|
147 } |
|
148 |
6620
|
149 /* |
|
150 |
|
151 %!error(fft()) |
|
152 %!assert(fft([]), []) |
6621
|
153 %!assert(fft(zeros(10,0)), zeros(10,0)) |
|
154 %!assert(fft(zeros(0,10)), zeros(0,10)) |
6620
|
155 %!assert(fft(0), 0) |
|
156 %!assert(fft(1), 1) |
|
157 %!assert(fft(1), 1) |
|
158 %!assert(fft(ones(2,2)), [2,2; 0,0]) |
|
159 %!assert(fft(eye(2,2)), [1,1; 1,-1]) |
|
160 |
|
161 */ |
|
162 |
4773
|
163 |
|
164 DEFUN_DLD (fft, args, , |
|
165 "-*- texinfo -*-\n\ |
|
166 @deftypefn {Loadable Function} {} fft (@var{a}, @var{n}, @var{dim})\n\ |
|
167 Compute the FFT of @var{a} using subroutines from\n" |
|
168 FFTSRC |
|
169 ". The FFT is calculated along the first non-singleton dimension of the\n\ |
|
170 array. Thus if @var{a} is a matrix, @code{fft (@var{a})} computes the\n\ |
|
171 FFT for each column of @var{a}.\n\ |
|
172 \n\ |
|
173 If called with two arguments, @var{n} is expected to be an integer\n\ |
|
174 specifying the number of elements of @var{a} to use, or an empty\n\ |
|
175 matrix to specify that its value should be ignored. If @var{n} is\n\ |
|
176 larger than the dimension along which the FFT is calculated, then\n\ |
5448
|
177 @var{a} is resized and padded with zeros. Otherwise, if @var{n} is\n\ |
4773
|
178 smaller than the dimension along which the FFT is calculated, then\n\ |
|
179 @var{a} is truncated.\n\ |
|
180 \n\ |
5448
|
181 If called with three arguments, @var{dim} is an integer specifying the\n\ |
4773
|
182 dimension of the matrix along which the FFT is performed\n\ |
6971
|
183 @seealso{ifft, fft2, fftn, fftw}\n\ |
5642
|
184 @end deftypefn") |
4773
|
185 { |
4782
|
186 return do_fft (args, "fft", 0); |
4773
|
187 } |
|
188 |
|
189 |
|
190 DEFUN_DLD (ifft, args, , |
|
191 "-*- texinfo -*-\n\ |
|
192 @deftypefn {Loadable Function} {} ifft (@var{a}, @var{n}, @var{dim})\n\ |
|
193 Compute the inverse FFT of @var{a} using subroutines from\n" |
|
194 FFTSRC |
|
195 ". The inverse FFT is calculated along the first non-singleton dimension\n\ |
|
196 of the array. Thus if @var{a} is a matrix, @code{fft (@var{a})} computes\n\ |
|
197 the inverse FFT for each column of @var{a}.\n\ |
|
198 \n\ |
|
199 If called with two arguments, @var{n} is expected to be an integer\n\ |
|
200 specifying the number of elements of @var{a} to use, or an empty\n\ |
|
201 matrix to specify that its value should be ignored. If @var{n} is\n\ |
|
202 larger than the dimension along which the inverse FFT is calculated, then\n\ |
|
203 @var{a} is resized and padded with zeros. Otherwise, if@var{n} is\n\ |
|
204 smaller than the dimension along which the inverse FFT is calculated,\n\ |
|
205 then @var{a} is truncated.\n\ |
|
206 \n\ |
|
207 If called with three agruments, @var{dim} is an integer specifying the\n\ |
|
208 dimension of the matrix along which the inverse FFT is performed\n\ |
6971
|
209 @seealso{fft, ifft2, ifftn, fftw}\n\ |
5642
|
210 @end deftypefn") |
4773
|
211 { |
4782
|
212 return do_fft (args, "ifft", 1); |
4773
|
213 } |
|
214 |
2928
|
215 /* |
|
216 ;;; Local Variables: *** |
|
217 ;;; mode: C++ *** |
|
218 ;;; End: *** |
|
219 */ |