3Copyright (C) 1997, 1999, 2002, 2004, 2005, 2006, 2007, 2008 David Bateman
4Copyright (C) 1996, 1997 John W. Eaton
6This file is part of Octave.
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.
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
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/>.
28#include "lo-mappers.h"
36#if defined (HAVE_FFTW)
37#define FFTSRC "@sc{fftw}"
39#define FFTSRC "@sc{fftpack}"
43do_fft (const octave_value_list &args, const char *fcn, int type)
47 int nargin = args.length ();
49 if (nargin < 1 || nargin > 3)
55 octave_value arg = args(0);
56 dim_vector dims = arg.dims ();
57 octave_idx_type n_points = -1;
62 if (! args(1).is_empty ())
64 double dval = args(1).double_value ();
66 error ("%s: NaN is invalid as the N_POINTS", fcn);
69 n_points = NINTbig (dval);
71 error ("%s: number of points must be greater than zero", fcn);
81 double dval = args(2).double_value ();
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);
87 // to be safe, cast it back to int since dim is an int
88 dim = NINT (dval) - 1;
94 for (octave_idx_type i = 0; i < dims.length (); i++)
100 for (octave_idx_type i = 0; i < dims.length (); i++)
107 // And if the first argument is scalar?
113 n_points = dims (dim);
115 dims (dim) = n_points;
117 if (dims.any_zero () || n_points == 0)
119 if (arg.is_single_type ())
120 return octave_value (FloatNDArray (dims));
122 return octave_value (NDArray (dims));
125 if (arg.is_single_type ())
127 if (arg.is_real_type ())
129 FloatNDArray nda = arg.float_array_value ();
133 nda.resize (dims, 0.0);
134 retval = (type != 0 ? nda.ifourier (dim) : nda.fourier (dim));
139 FloatComplexNDArray cnda = arg.float_complex_array_value ();
143 cnda.resize (dims, 0.0);
144 retval = (type != 0 ? cnda.ifourier (dim) : cnda.fourier (dim));
150 if (arg.is_real_type ())
152 NDArray nda = arg.array_value ();
156 nda.resize (dims, 0.0);
157 retval = (type != 0 ? nda.ifourier (dim) : nda.fourier (dim));
160 else if (arg.is_complex_type ())
162 ComplexNDArray cnda = arg.complex_array_value ();
166 cnda.resize (dims, 0.0);
167 retval = (type != 0 ? cnda.ifourier (dim) : cnda.fourier (dim));
172 gripe_wrong_type_arg (fcn, arg);
183%!assert(fft(zeros(10,0)), zeros(10,0))
184%!assert(fft(zeros(0,10)), zeros(0,10))
187%!assert(fft(ones(2,2)), [2,2; 0,0])
188%!assert(fft(eye(2,2)), [1,1; 1,-1])
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]))
201DEFUN_DLD (fft, args, ,
203@deftypefn {Loadable Function} {} fft (@var{a}, @var{n}, @var{dim})\n\
204Compute the FFT of @var{a} using subroutines from\n"
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\
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\
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\
223 return do_fft (args, "fft", 0);
227DEFUN_DLD (ifft, args, ,
229@deftypefn {Loadable Function} {} ifft (@var{a}, @var{n}, @var{dim})\n\
230Compute the inverse FFT of @var{a} using subroutines from\n"
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\
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\
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\
249 return do_fft (args, "ifft", 1);
254%% Author: David Billinghurst (David.Billinghurst@riotinto.com.au)
255%% Comalco Research and Technology
260%! t = 2*pi*(0:1:N-1)/N;
264%! answer = zeros (size(t));
266%! answer(N-n+1) = N/2;
268%! assert(S, answer, 4*N*eps);
270%% Author: David Billinghurst (David.Billinghurst@riotinto.com.au)
271%% Comalco Research and Technology
276%! t = 2*pi*(0:1:N-1)/N;
279%! S = zeros (size(t));
283%! assert(ifft(S), s, 4*N*eps);
285%% Author: David Billinghurst (David.Billinghurst@riotinto.com.au)
286%% Comalco Research and Technology
291%! t = single (2*pi*(0:1:N-1)/N);
295%! answer = zeros (size(t),'single');
297%! answer(N-n+1) = N/2;
299%! assert(S, answer, 4*N*eps('single'));
301%% Author: David Billinghurst (David.Billinghurst@riotinto.com.au)
302%% Comalco Research and Technology
307%! t = 2*pi*(0:1:N-1)/N;
310%! S = zeros (size(t),'single');
314%! assert(ifft(S), s, 4*N*eps('single'));