changelog shortlog tags changeset files revisions annotate raw

src/DLD-FUNCTIONS/fft.cc

changeset 10289: 4b124317dc38
parent:40dfc0c99116
author: John W. Eaton <jwe@octave.org>
date: Tue Feb 09 20:58:55 2010 -0500 (47 minutes ago)
permissions: -rw-r--r--
description: base_properties::set_children: account for hidden children
1/*
2
3Copyright (C) 1997, 1999, 2002, 2004, 2005, 2006, 2007, 2008 David Bateman
4Copyright (C) 1996, 1997 John W. Eaton
5
6This file is part of Octave.
7
8Octave is free software; you can redistribute it and/or modify it
9under the terms of the GNU General Public License as published by the
10Free Software Foundation; either version 3 of the License, or (at your
11option) any later version.
12
13Octave is distributed in the hope that it will be useful, but WITHOUT
14ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16for more details.
17
18You should have received a copy of the GNU General Public License
19along with Octave; see the file COPYING. If not, see
20<http://www.gnu.org/licenses/>.
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
36#if defined (HAVE_FFTW)
37#define FFTSRC "@sc{fftw}"
38#else
39#define FFTSRC "@sc{fftpack}"
40#endif
41
42static octave_value
43do_fft (const octave_value_list &args, const char *fcn, int type)
44{
45 octave_value retval;
46
47 int nargin = args.length ();
48
49 if (nargin < 1 || nargin > 3)
50 {
51 print_usage ();
52 return retval;
53 }
54
55 octave_value arg = args(0);
56 dim_vector dims = arg.dims ();
57 octave_idx_type 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 = NINTbig (dval);
70 if (n_points < 0)
71 error ("%s: number of points must be greater than zero", fcn);
72 }
73 }
74 }
75
76 if (error_state)
77 return retval;
78
79 if (nargin > 2)
80 {
81 double dval = args(2).double_value ();
82 if (xisnan (dval))
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);
86 else
87 // to be safe, cast it back to int since dim is an int
88 dim = NINT (dval) - 1;
89 }
90
91 if (error_state)
92 return retval;
93
94 for (octave_idx_type i = 0; i < dims.length (); i++)
95 if (dims(i) < 0)
96 return retval;
97
98 if (dim < 0)
99 {
100 for (octave_idx_type i = 0; i < dims.length (); i++)
101 if (dims(i) > 1)
102 {
103 dim = i;
104 break;
105 }
106
107 // And if the first argument is scalar?
108 if (dim < 0)
109 dim = 1;
110 }
111
112 if (n_points < 0)
113 n_points = dims (dim);
114 else
115 dims (dim) = n_points;
116
117 if (dims.any_zero () || n_points == 0)
118 {
119 if (arg.is_single_type ())
120 return octave_value (FloatNDArray (dims));
121 else
122 return octave_value (NDArray (dims));
123 }
124
125 if (arg.is_single_type ())
126 {
127 if (arg.is_real_type ())
128 {
129 FloatNDArray nda = arg.float_array_value ();
130
131 if (! error_state)
132 {
133 nda.resize (dims, 0.0);
134 retval = (type != 0 ? nda.ifourier (dim) : nda.fourier (dim));
135 }
136 }
137 else
138 {
139 FloatComplexNDArray cnda = arg.float_complex_array_value ();
140
141 if (! error_state)
142 {
143 cnda.resize (dims, 0.0);
144 retval = (type != 0 ? cnda.ifourier (dim) : cnda.fourier (dim));
145 }
146 }
147 }
148 else
149 {
150 if (arg.is_real_type ())
151 {
152 NDArray nda = arg.array_value ();
153
154 if (! error_state)
155 {
156 nda.resize (dims, 0.0);
157 retval = (type != 0 ? nda.ifourier (dim) : nda.fourier (dim));
158 }
159 }
160 else if (arg.is_complex_type ())
161 {
162 ComplexNDArray cnda = arg.complex_array_value ();
163
164 if (! error_state)
165 {
166 cnda.resize (dims, 0.0);
167 retval = (type != 0 ? cnda.ifourier (dim) : cnda.fourier (dim));
168 }
169 }
170 else
171 {
172 gripe_wrong_type_arg (fcn, arg);
173 }
174 }
175
176 return retval;
177}
178
179/*
180
181%!error(fft())
182%!assert(fft([]), [])
183%!assert(fft(zeros(10,0)), zeros(10,0))
184%!assert(fft(zeros(0,10)), zeros(0,10))
185%!assert(fft(0), 0)
186%!assert(fft(1), 1)
187%!assert(fft(ones(2,2)), [2,2; 0,0])
188%!assert(fft(eye(2,2)), [1,1; 1,-1])
189
190%!assert(fft(single([])), single([]))
191%!assert(fft(zeros(10,0,'single')), zeros(10,0,'single'))
192%!assert(fft(zeros(0,10,'single')), zeros(0,10,'single'))
193%!assert(fft(single(0)), single(0))
194%!assert(fft(single(1)), single(1))
195%!assert(fft(ones(2,2,'single')), single([2,2; 0,0]))
196%!assert(fft(eye(2,2,'single')), single([1,1; 1,-1]))
197
198*/
199
200
201DEFUN_DLD (fft, args, ,
202 "-*- texinfo -*-\n\
203@deftypefn {Loadable Function} {} fft (@var{a}, @var{n}, @var{dim})\n\
204Compute the FFT of @var{a} using subroutines from\n"
205FFTSRC
206". The FFT is calculated along the first non-singleton dimension of the\n\
207array. Thus if @var{a} is a matrix, @code{fft (@var{a})} computes the\n\
208FFT for each column of @var{a}.\n\
209\n\
210If called with two arguments, @var{n} is expected to be an integer\n\
211specifying the number of elements of @var{a} to use, or an empty\n\
212matrix to specify that its value should be ignored. If @var{n} is\n\
213larger than the dimension along which the FFT is calculated, then\n\
214@var{a} is resized and padded with zeros. Otherwise, if @var{n} is\n\
215smaller than the dimension along which the FFT is calculated, then\n\
216@var{a} is truncated.\n\
217\n\
218If called with three arguments, @var{dim} is an integer specifying the\n\
219dimension of the matrix along which the FFT is performed\n\
220@seealso{ifft, fft2, fftn, fftw}\n\
221@end deftypefn")
222{
223 return do_fft (args, "fft", 0);
224}
225
226
227DEFUN_DLD (ifft, args, ,
228 "-*- texinfo -*-\n\
229@deftypefn {Loadable Function} {} ifft (@var{a}, @var{n}, @var{dim})\n\
230Compute the inverse FFT of @var{a} using subroutines from\n"
231FFTSRC
232". The inverse FFT is calculated along the first non-singleton dimension\n\
233of the array. Thus if @var{a} is a matrix, @code{fft (@var{a})} computes\n\
234the inverse FFT for each column of @var{a}.\n\
235\n\
236If called with two arguments, @var{n} is expected to be an integer\n\
237specifying the number of elements of @var{a} to use, or an empty\n\
238matrix to specify that its value should be ignored. If @var{n} is\n\
239larger than the dimension along which the inverse FFT is calculated, then\n\
240@var{a} is resized and padded with zeros. Otherwise, if@var{n} is\n\
241smaller than the dimension along which the inverse FFT is calculated,\n\
242then @var{a} is truncated.\n\
243\n\
244If called with three arguments, @var{dim} is an integer specifying the\n\
245dimension of the matrix along which the inverse FFT is performed\n\
246@seealso{fft, ifft2, ifftn, fftw}\n\
247@end deftypefn")
248{
249 return do_fft (args, "ifft", 1);
250}
251
252/*
253
254%% Author: David Billinghurst (David.Billinghurst@riotinto.com.au)
255%% Comalco Research and Technology
256%% 02 May 2000
257%!test
258%! N=64;
259%! n=4;
260%! t = 2*pi*(0:1:N-1)/N;
261%! s = cos(n*t);
262%! S = fft(s);
263%!
264%! answer = zeros (size(t));
265%! answer(n+1) = N/2;
266%! answer(N-n+1) = N/2;
267%!
268%! assert(S, answer, 4*N*eps);
269
270%% Author: David Billinghurst (David.Billinghurst@riotinto.com.au)
271%% Comalco Research and Technology
272%% 02 May 2000
273%!test
274%! N=64;
275%! n=7;
276%! t = 2*pi*(0:1:N-1)/N;
277%! s = cos(n*t);
278%!
279%! S = zeros (size(t));
280%! S(n+1) = N/2;
281%! S(N-n+1) = N/2;
282%!
283%! assert(ifft(S), s, 4*N*eps);
284
285%% Author: David Billinghurst (David.Billinghurst@riotinto.com.au)
286%% Comalco Research and Technology
287%% 02 May 2000
288%!test
289%! N=64;
290%! n=4;
291%! t = single (2*pi*(0:1:N-1)/N);
292%! s = cos(n*t);
293%! S = fft(s);
294%!
295%! answer = zeros (size(t),'single');
296%! answer(n+1) = N/2;
297%! answer(N-n+1) = N/2;
298%!
299%! assert(S, answer, 4*N*eps('single'));
300
301%% Author: David Billinghurst (David.Billinghurst@riotinto.com.au)
302%% Comalco Research and Technology
303%% 02 May 2000
304%!test
305%! N=64;
306%! n=7;
307%! t = 2*pi*(0:1:N-1)/N;
308%! s = cos(n*t);
309%!
310%! S = zeros (size(t),'single');
311%! S(n+1) = N/2;
312%! S(N-n+1) = N/2;
313%!
314%! assert(ifft(S), s, 4*N*eps('single'));
315
316*/