GNU Octave  4.4.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
fftn.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2004-2018 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
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License 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 <https://www.gnu.org/licenses/>.
20 
21 */
22 
23 #if defined (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 "errwarn.h"
32 #include "ovl.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  int nargin = args.length ();
47 
49  print_usage ();
50 
52  octave_value arg = args(0);
53  dim_vector dims = arg.dims ();
54 
55  for (int i = 0; i < dims.ndims (); i++)
56  if (dims(i) < 0)
57  return retval;
58 
59  if (nargin > 1)
60  {
61  Matrix val = args(1).xmatrix_value ("%s: SIZE must be a vector of length dim", fcn);
62 
63  if (val.rows () > val.columns ())
64  val = val.transpose ();
65 
66  if (val.columns () != dims.ndims () || val.rows () != 1)
67  error ("%s: SIZE must be a vector of length dim", fcn);
68 
69  for (int i = 0; i < dims.ndims (); i++)
70  {
71  if (octave::math::isnan (val(i,0)))
72  error ("%s: SIZE has invalid NaN entries", fcn);
73  else if (octave::math::nint_big (val(i,0)) < 0)
74  error ("%s: all dimensions in SIZE must be greater than zero", fcn);
75  else
77  }
78  }
79 
80  if (dims.all_zero ())
81  {
82  if (arg.is_single_type ())
83  return octave_value (FloatMatrix ());
84  else
85  return octave_value (Matrix ());
86  }
87 
88  if (arg.is_single_type ())
89  {
90  if (arg.isreal ())
91  {
93 
94  nda.resize (dims, 0.0);
95  retval = (type != 0 ? nda.ifourierNd () : nda.fourierNd ());
96  }
97  else
98  {
100 
101  cnda.resize (dims, 0.0);
102  retval = (type != 0 ? cnda.ifourierNd () : cnda.fourierNd ());
103  }
104  }
105  else
106  {
107  if (arg.isreal ())
108  {
109  NDArray nda = arg.array_value ();
110 
111  nda.resize (dims, 0.0);
112  retval = (type != 0 ? nda.ifourierNd () : nda.fourierNd ());
113  }
114  else if (arg.iscomplex ())
115  {
117 
118  cnda.resize (dims, 0.0);
119  retval = (type != 0 ? cnda.ifourierNd () : cnda.fourierNd ());
120  }
121  else
123  }
124 
125  return retval;
126 }
127 
128 DEFUN (fftn, args, ,
129  doc: /* -*- texinfo -*-
130 @deftypefn {} {} fftn (@var{A})
131 @deftypefnx {} {} fftn (@var{A}, @var{size})
132 Compute the N-dimensional discrete Fourier transform of @var{A} using
133 a Fast Fourier Transform (FFT) algorithm.
134 
135 The optional vector argument @var{size} may be used specify the dimensions
136 of the array to be used. If an element of @var{size} is smaller than the
137 corresponding dimension of @var{A}, then the dimension of @var{A} is
138 truncated prior to performing the FFT@. Otherwise, if an element of
139 @var{size} is larger than the corresponding dimension then @var{A} is
140 resized and padded with zeros.
141 @seealso{ifftn, fft, fft2, fftw}
142 @end deftypefn */)
143 {
144  return do_fftn (args, "fftn", 0);
145 }
146 
147 DEFUN (ifftn, args, ,
148  doc: /* -*- texinfo -*-
149 @deftypefn {} {} ifftn (@var{A})
150 @deftypefnx {} {} ifftn (@var{A}, @var{size})
151 Compute the inverse N-dimensional discrete Fourier transform of @var{A}
152 using a Fast Fourier Transform (FFT) algorithm.
153 
154 The optional vector argument @var{size} may be used specify the dimensions
155 of the array to be used. If an element of @var{size} is smaller than the
156 corresponding dimension of @var{A}, then the dimension of @var{A} is
157 truncated prior to performing the inverse FFT@. Otherwise, if an element of
158 @var{size} is larger than the corresponding dimension then @var{A} is
159 resized and padded with zeros.
160 @seealso{fftn, ifft, ifft2, fftw}
161 @end deftypefn */)
162 {
163  return do_fftn (args, "ifftn", 1);
164 }
ComplexNDArray fourierNd(void) const
Definition: CNDArray.cc:158
ComplexNDArray ifourierNd(void) const
Definition: CNDArray.cc:173
OCTINTERP_API void print_usage(void)
Definition: defun.cc:54
identity matrix If supplied two scalar respectively For allows like xample val
Definition: data.cc:4986
FloatComplexNDArray ifourierNd(void) const
Definition: fNDArray.cc:171
bool isnan(bool)
Definition: lo-mappers.h:187
#define DEFUN(name, args_name, nargout_name, doc)
Macro to define a builtin function.
Definition: defun.h:53
void error(const char *fmt,...)
Definition: error.cc:578
FloatNDArray float_array_value(bool frc_str_conv=false) const
Definition: ov.h:843
octave_value arg
Definition: pr-output.cc:3244
octave_function * fcn
Definition: ov-class.cc:1754
static octave_value do_fftn(const octave_value_list &args, const char *fcn, int type)
Definition: fftn.cc:44
FloatComplexNDArray fourierNd(void) const
Definition: fCNDArray.cc:158
bool is_single_type(void) const
Definition: ov.h:651
dim_vector dims(void) const
Definition: ov.h:469
void resize(const dim_vector &dv, const T &rfv)
Resizing (with fill).
Definition: Array.cc:1010
ComplexNDArray ifourierNd(void) const
Definition: dNDArray.cc:213
octave_value retval
Definition: data.cc:6246
FloatComplexNDArray ifourierNd(void) const
Definition: fCNDArray.cc:173
idx type
Definition: ov.cc:3114
Definition: dMatrix.h:36
the exceeded dimensions are set to if fewer subscripts than dimensions are the exceeding dimensions are merged into the final requested dimension For consider the following dims
Definition: sub2ind.cc:255
FloatComplexNDArray float_complex_array_value(bool frc_str_conv=false) const
Definition: ov.h:863
void err_wrong_type_arg(const char *name, const char *s)
Definition: errwarn.cc:162
return octave_value(v1.char_array_value() . concat(v2.char_array_value(), ra_idx),((a1.is_sq_string()||a2.is_sq_string()) ? '\'' :'"'))
bool isreal(void) const
Definition: ov.h:703
octave_idx_type nint_big(double x)
Definition: lo-mappers.cc:182
octave_idx_type length(void) const
Definition: ovl.h:96
args.length() nargin
Definition: file-io.cc:589
bool iscomplex(void) const
Definition: ov.h:710
for i
Definition: data.cc:5264
ComplexNDArray complex_array_value(bool frc_str_conv=false) const
Definition: ov.h:859
ComplexNDArray fourierNd(void) const
Definition: dNDArray.cc:198
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:87
FloatComplexNDArray fourierNd(void) const
Definition: fNDArray.cc:156
NDArray array_value(bool frc_str_conv=false) const
Definition: ov.h:840