GNU Octave  3.8.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
fftn.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2004-2013 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
9 Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
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
18 along with Octave; see the file COPYING. If not, see
19 <http://www.gnu.org/licenses/>.
20 
21 */
22 
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26 
27 #include "lo-mappers.h"
28 
29 #include "defun.h"
30 #include "error.h"
31 #include "gripes.h"
32 #include "oct-obj.h"
33 #include "utils.h"
34 
35 // This function should be merged with Fifft.
36 
37 #if defined (HAVE_FFTW)
38 #define FFTSRC "@sc{fftw}"
39 #else
40 #define FFTSRC "@sc{fftpack}"
41 #endif
42 
43 static octave_value
44 do_fftn (const octave_value_list &args, const char *fcn, int type)
45 {
46  octave_value retval;
47 
48  int nargin = args.length ();
49 
50  if (nargin < 1 || nargin > 2)
51  {
52  print_usage ();
53  return retval;
54  }
55 
56  octave_value arg = args(0);
57  dim_vector dims = arg.dims ();
58 
59  for (int i = 0; i < dims.length (); i++)
60  if (dims(i) < 0)
61  return retval;
62 
63  if (nargin > 1)
64  {
65  Matrix val = args(1).matrix_value ();
66  if (val.rows () > val.columns ())
67  val = val.transpose ();
68 
69  if (error_state || val.columns () != dims.length () || val.rows () != 1)
70  error ("%s: SIZE must be a vector of length dim", fcn);
71  else
72  {
73  for (int i = 0; i < dims.length (); i++)
74  {
75  if (xisnan (val(i,0)))
76  error ("%s: SIZE has invalid NaN entries", fcn);
77  else if (NINTbig (val(i,0)) < 0)
78  error ("%s: all dimensions in SIZE must be greater than zero",
79  fcn);
80  else
81  {
82  dims(i) = NINTbig(val(i,0));
83  }
84  }
85  }
86  }
87 
88  if (error_state)
89  return retval;
90 
91  if (dims.all_zero ())
92  {
93  if (arg.is_single_type ())
94  return octave_value (FloatMatrix ());
95  else
96  return octave_value (Matrix ());
97  }
98 
99  if (arg.is_single_type ())
100  {
101  if (arg.is_real_type ())
102  {
103  FloatNDArray nda = arg.float_array_value ();
104 
105  if (! error_state)
106  {
107  nda.resize (dims, 0.0);
108  retval = (type != 0 ? nda.ifourierNd () : nda.fourierNd ());
109  }
110  }
111  else
112  {
114 
115  if (! error_state)
116  {
117  cnda.resize (dims, 0.0);
118  retval = (type != 0 ? cnda.ifourierNd () : cnda.fourierNd ());
119  }
120  }
121  }
122  else
123  {
124  if (arg.is_real_type ())
125  {
126  NDArray nda = arg.array_value ();
127 
128  if (! error_state)
129  {
130  nda.resize (dims, 0.0);
131  retval = (type != 0 ? nda.ifourierNd () : nda.fourierNd ());
132  }
133  }
134  else if (arg.is_complex_type ())
135  {
136  ComplexNDArray cnda = arg.complex_array_value ();
137 
138  if (! error_state)
139  {
140  cnda.resize (dims, 0.0);
141  retval = (type != 0 ? cnda.ifourierNd () : cnda.fourierNd ());
142  }
143  }
144  else
145  {
146  gripe_wrong_type_arg (fcn, arg);
147  }
148  }
149 
150  return retval;
151 }
152 
153 DEFUN (fftn, args, ,
154  "-*- texinfo -*-\n\
155 @deftypefn {Built-in Function} {} fftn (@var{A})\n\
156 @deftypefnx {Built-in Function} {} fftn (@var{A}, @var{size})\n\
157 Compute the N-dimensional discrete Fourier transform of @var{A} using\n\
158 a Fast Fourier Transform (FFT) algorithm.\n\
159 \n\
160 The optional vector argument @var{size} may be used specify the\n\
161 dimensions of the array to be used. If an element of @var{size} is\n\
162 smaller than the corresponding dimension of @var{A}, then the dimension of\n\
163 @var{A} is truncated prior to performing the FFT@. Otherwise, if an element\n\
164 of @var{size} is larger than the corresponding dimension then @var{A}\n\
165 is resized and padded with zeros.\n\
166 @seealso{ifftn, fft, fft2, fftw}\n\
167 @end deftypefn")
168 {
169  return do_fftn (args, "fftn", 0);
170 }
171 
172 DEFUN (ifftn, args, ,
173  "-*- texinfo -*-\n\
174 @deftypefn {Built-in Function} {} ifftn (@var{A})\n\
175 @deftypefnx {Built-in Function} {} ifftn (@var{A}, @var{size})\n\
176 Compute the inverse N-dimensional discrete Fourier transform of @var{A}\n\
177 using a Fast Fourier Transform (FFT) algorithm.\n\
178 \n\
179 The optional vector argument @var{size} may be used specify the\n\
180 dimensions of the array to be used. If an element of @var{size} is\n\
181 smaller than the corresponding dimension of @var{A}, then the dimension of\n\
182 @var{A} is truncated prior to performing the inverse FFT@. Otherwise, if an\n\
183 element of @var{size} is larger than the corresponding dimension then @var{A}\n\
184 is resized and padded with zeros.\n\
185 @seealso{fftn, ifft, ifft2, fftw}\n\
186 @end deftypefn")
187 {
188  return do_fftn (args, "ifftn", 1);
189 }