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 |
|
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include <config.h> |
|
26 #endif |
|
27 |
|
28 #include "lo-mappers.h" |
|
29 |
|
30 #include "defun-dld.h" |
|
31 #include "error.h" |
|
32 #include "gripes.h" |
|
33 #include "oct-obj.h" |
|
34 #include "utils.h" |
|
35 |
4773
|
36 #if defined (HAVE_FFTW3) |
|
37 #define FFTSRC "@sc{Fftw}" |
|
38 #else |
|
39 #define FFTSRC "@sc{Fftpack}" |
|
40 #endif |
2928
|
41 |
4773
|
42 static octave_value |
|
43 do_fft (const octave_value_list &args, const char *fcn, int type) |
2928
|
44 { |
4233
|
45 octave_value retval; |
2928
|
46 |
|
47 int nargin = args.length (); |
|
48 |
4773
|
49 if (nargin < 1 || nargin > 3) |
2928
|
50 { |
4773
|
51 print_usage (fcn); |
2928
|
52 return retval; |
|
53 } |
|
54 |
|
55 octave_value arg = args(0); |
4773
|
56 dim_vector dims = arg.dims (); |
|
57 int n_points = -1; |
|
58 int dim = -1; |
|
59 |
|
60 if (nargin > 1) |
|
61 { |
|
62 if (! args(1).is_empty ()) |
|
63 { |
|
64 double dval = args(1).double_value (); |
|
65 if (xisnan (dval)) |
|
66 error ("%s: NaN is invalid as the N_POINTS", fcn); |
|
67 else |
|
68 { |
|
69 n_points = NINT (dval); |
|
70 if (n_points < 0) |
|
71 error ("%s: number of points must be greater than zero", fcn); |
|
72 } |
|
73 } |
|
74 } |
2928
|
75 |
4773
|
76 if (error_state) |
|
77 return retval; |
|
78 |
|
79 if (nargin > 2) |
2928
|
80 { |
4773
|
81 double dval = args(2).double_value (); |
2928
|
82 if (xisnan (dval)) |
4773
|
83 error ("%s: NaN is invalid as the N_POINTS", fcn); |
|
84 else if (dval < 1 || dval > dims.length ()) |
|
85 error ("%s: invalid dimension along which to perform fft", fcn); |
2928
|
86 else |
4773
|
87 dim = NINT (dval) - 1; |
2928
|
88 } |
|
89 |
|
90 if (error_state) |
|
91 return retval; |
|
92 |
4773
|
93 for (int i = 0; i < dims.length (); i++) |
|
94 if (dims(i) < 0) |
2928
|
95 return retval; |
|
96 |
4773
|
97 if (dim < 0) |
|
98 for (int i = 0; i < dims.length (); i++) |
|
99 if ( dims(i) > 1) |
|
100 { |
|
101 dim = i; |
|
102 break; |
|
103 } |
2928
|
104 |
4773
|
105 if (n_points < 0) |
|
106 n_points = dims (dim); |
|
107 else |
|
108 dims (dim) = n_points; |
|
109 |
|
110 if (dims.all_zero () || n_points == 0) |
4233
|
111 return octave_value (Matrix ()); |
2928
|
112 |
|
113 if (arg.is_real_type ()) |
|
114 { |
4773
|
115 NDArray nda = arg.array_value (); |
2928
|
116 |
|
117 if (! error_state) |
|
118 { |
4773
|
119 nda.resize (dims, 0.0); |
|
120 retval = (type != 0 ? nda.ifourier (dim) : nda.fourier (dim)); |
2928
|
121 } |
|
122 } |
|
123 else if (arg.is_complex_type ()) |
|
124 { |
4773
|
125 ComplexNDArray cnda = arg.complex_array_value (); |
2928
|
126 |
|
127 if (! error_state) |
|
128 { |
4773
|
129 cnda.resize (dims, 0.0); |
|
130 retval = (type != 0 ? cnda.ifourier (dim) : cnda.fourier (dim)); |
2928
|
131 } |
|
132 } |
|
133 else |
|
134 { |
4773
|
135 gripe_wrong_type_arg (fcn, arg); |
2928
|
136 } |
|
137 |
|
138 return retval; |
|
139 } |
|
140 |
4773
|
141 |
|
142 DEFUN_DLD (fft, args, , |
|
143 "-*- texinfo -*-\n\ |
|
144 @deftypefn {Loadable Function} {} fft (@var{a}, @var{n}, @var{dim})\n\ |
|
145 Compute the FFT of @var{a} using subroutines from\n" |
|
146 FFTSRC |
|
147 ". The FFT is calculated along the first non-singleton dimension of the\n\ |
|
148 array. Thus if @var{a} is a matrix, @code{fft (@var{a})} computes the\n\ |
|
149 FFT for each column of @var{a}.\n\ |
|
150 \n\ |
|
151 If called with two arguments, @var{n} is expected to be an integer\n\ |
|
152 specifying the number of elements of @var{a} to use, or an empty\n\ |
|
153 matrix to specify that its value should be ignored. If @var{n} is\n\ |
|
154 larger than the dimension along which the FFT is calculated, then\n\ |
|
155 @var{a} is resized and padded with zeros. Otherwise, if@var{n} is\n\ |
|
156 smaller than the dimension along which the FFT is calculated, then\n\ |
|
157 @var{a} is truncated.\n\ |
|
158 \n\ |
|
159 If called with three agruments, @var{dim} is an integer specifying the\n\ |
|
160 dimension of the matrix along which the FFT is performed\n\ |
|
161 @end deftypefn\n\ |
4782
|
162 @seealso {ifft, fft2, fftn, fftw_wisdom}") |
4773
|
163 { |
4782
|
164 return do_fft (args, "fft", 0); |
4773
|
165 } |
|
166 |
|
167 |
|
168 DEFUN_DLD (ifft, args, , |
|
169 "-*- texinfo -*-\n\ |
|
170 @deftypefn {Loadable Function} {} ifft (@var{a}, @var{n}, @var{dim})\n\ |
|
171 Compute the inverse FFT of @var{a} using subroutines from\n" |
|
172 FFTSRC |
|
173 ". The inverse FFT is calculated along the first non-singleton dimension\n\ |
|
174 of the array. Thus if @var{a} is a matrix, @code{fft (@var{a})} computes\n\ |
|
175 the inverse FFT for each column of @var{a}.\n\ |
|
176 \n\ |
|
177 If called with two arguments, @var{n} is expected to be an integer\n\ |
|
178 specifying the number of elements of @var{a} to use, or an empty\n\ |
|
179 matrix to specify that its value should be ignored. If @var{n} is\n\ |
|
180 larger than the dimension along which the inverse FFT is calculated, then\n\ |
|
181 @var{a} is resized and padded with zeros. Otherwise, if@var{n} is\n\ |
|
182 smaller than the dimension along which the inverse FFT is calculated,\n\ |
|
183 then @var{a} is truncated.\n\ |
|
184 \n\ |
|
185 If called with three agruments, @var{dim} is an integer specifying the\n\ |
|
186 dimension of the matrix along which the inverse FFT is performed\n\ |
|
187 @end deftypefn\n\ |
4782
|
188 @seealso {fft, ifft2, ifftn, fftw_wisdom}") |
4773
|
189 { |
4782
|
190 return do_fft (args, "ifft", 1); |
4773
|
191 } |
|
192 |
2928
|
193 /* |
|
194 ;;; Local Variables: *** |
|
195 ;;; mode: C++ *** |
|
196 ;;; End: *** |
|
197 */ |