GNU Octave  4.0.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-2015 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 dimensions\n\
161 of the array to be used. If an element of @var{size} is smaller than the\n\
162 corresponding dimension of @var{A}, then the dimension of @var{A} is\n\
163 truncated prior to performing the FFT@. Otherwise, if an element of\n\
164 @var{size} is larger than the corresponding dimension then @var{A} is\n\
165 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 dimensions\n\
180 of the array to be used. If an element of @var{size} is smaller than the\n\
181 corresponding dimension of @var{A}, then the dimension of @var{A} is\n\
182 truncated prior to performing the inverse FFT@. Otherwise, if an element of\n\
183 @var{size} is larger than the corresponding dimension then @var{A} is\n\
184 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 }
ComplexNDArray complex_array_value(bool frc_str_conv=false) const
Definition: ov.h:798
bool is_real_type(void) const
Definition: ov.h:651
void gripe_wrong_type_arg(const char *name, const char *s, bool is_error)
Definition: gripes.cc:135
bool xisnan(double x)
Definition: lo-mappers.cc:144
OCTINTERP_API void print_usage(void)
Definition: defun.cc:51
octave_idx_type length(void) const
Definition: oct-obj.h:89
#define DEFUN(name, args_name, nargout_name, doc)
Definition: defun.h:44
void error(const char *fmt,...)
Definition: error.cc:476
ComplexNDArray ifourierNd(void) const
Definition: CNDArray.cc:176
octave_idx_type rows(void) const
Definition: Array.h:313
FloatComplexNDArray ifourierNd(void) const
Definition: fCNDArray.cc:176
FloatComplexNDArray ifourierNd(void) const
Definition: fNDArray.cc:173
static octave_value do_fftn(const octave_value_list &args, const char *fcn, int type)
Definition: fftn.cc:44
FloatNDArray float_array_value(bool frc_str_conv=false) const
Definition: ov.h:782
ComplexNDArray fourierNd(void) const
Definition: dNDArray.cc:200
FloatComplexNDArray float_complex_array_value(bool frc_str_conv=false) const
Definition: ov.h:802
int error_state
Definition: error.cc:101
FloatComplexNDArray fourierNd(void) const
Definition: fCNDArray.cc:161
bool is_complex_type(void) const
Definition: ov.h:654
void resize(const dim_vector &dv, const T &rfv)
Definition: Array.cc:1033
Matrix transpose(void) const
Definition: dMatrix.h:114
FloatComplexNDArray fourierNd(void) const
Definition: fNDArray.cc:158
Definition: dMatrix.h:35
dim_vector dims(void) const
Definition: ov.h:470
bool all_zero(void) const
Definition: dim-vector.h:304
double arg(double x)
Definition: lo-mappers.h:37
ComplexNDArray fourierNd(void) const
Definition: CNDArray.cc:161
NDArray array_value(bool frc_str_conv=false) const
Definition: ov.h:779
ComplexNDArray ifourierNd(void) const
Definition: dNDArray.cc:215
bool is_single_type(void) const
Definition: ov.h:611
octave_idx_type NINTbig(double x)
Definition: lo-mappers.cc:635
int length(void) const
Definition: dim-vector.h:281
octave_idx_type columns(void) const
Definition: Array.h:322
return octave_value(v1.char_array_value().concat(v2.char_array_value(), ra_idx),((a1.is_sq_string()||a2.is_sq_string())? '\'': '"'))