GNU Octave  6.2.0
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-2021 The Octave Project Developers
4 //
5 // See the file COPYRIGHT.md in the top-level directory of this
6 // distribution or <https://octave.org/copyright/>.
7 //
8 // This file is part of Octave.
9 //
10 // Octave is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Octave is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Octave; see the file COPYING. If not, see
22 // <https://www.gnu.org/licenses/>.
23 //
24 ////////////////////////////////////////////////////////////////////////
25 
26 #if defined (HAVE_CONFIG_H)
27 # include "config.h"
28 #endif
29 
30 #include "lo-mappers.h"
31 
32 #include "defun.h"
33 #include "error.h"
34 #include "errwarn.h"
35 #include "ovl.h"
36 #include "utils.h"
37 
38 // This function should be merged with Fifft.
39 
40 static octave_value
41 do_fftn (const octave_value_list& args, const char *fcn, int type)
42 {
43  int nargin = args.length ();
44 
45  if (nargin < 1 || nargin > 2)
46  print_usage ();
47 
49  octave_value arg = args(0);
50  dim_vector dims = arg.dims ();
51 
52  for (int i = 0; i < dims.ndims (); i++)
53  if (dims(i) < 0)
54  return retval;
55 
56  if (nargin > 1)
57  {
58  Matrix val = args(1).xmatrix_value ("%s: SIZE must be a vector of length dim", fcn);
59 
60  if (val.rows () > val.columns ())
61  val = val.transpose ();
62 
63  if (val.columns () != dims.ndims () || val.rows () != 1)
64  error ("%s: SIZE must be a vector of length dim", fcn);
65 
66  for (int i = 0; i < dims.ndims (); i++)
67  {
68  if (octave::math::isnan (val(i,0)))
69  error ("%s: SIZE has invalid NaN entries", fcn);
70  else if (octave::math::nint_big (val(i,0)) < 0)
71  error ("%s: all dimensions in SIZE must be greater than zero", fcn);
72  else
73  dims(i) = octave::math::nint_big(val(i,0));
74  }
75  }
76 
77  if (dims.all_zero ())
78  {
79  if (arg.is_single_type ())
80  return octave_value (FloatMatrix ());
81  else
82  return octave_value (Matrix ());
83  }
84 
85  if (arg.is_single_type ())
86  {
87  if (arg.isreal ())
88  {
89  FloatNDArray nda = arg.float_array_value ();
90 
91  nda.resize (dims, 0.0);
92  retval = (type != 0 ? nda.ifourierNd () : nda.fourierNd ());
93  }
94  else
95  {
97 
98  cnda.resize (dims, 0.0);
99  retval = (type != 0 ? cnda.ifourierNd () : cnda.fourierNd ());
100  }
101  }
102  else
103  {
104  if (arg.isreal ())
105  {
106  NDArray nda = arg.array_value ();
107 
108  nda.resize (dims, 0.0);
109  retval = (type != 0 ? nda.ifourierNd () : nda.fourierNd ());
110  }
111  else if (arg.iscomplex ())
112  {
113  ComplexNDArray cnda = arg.complex_array_value ();
114 
115  cnda.resize (dims, 0.0);
116  retval = (type != 0 ? cnda.ifourierNd () : cnda.fourierNd ());
117  }
118  else
119  err_wrong_type_arg (fcn, arg);
120  }
121 
122  return retval;
123 }
124 
125 DEFUN (fftn, args, ,
126  doc: /* -*- texinfo -*-
127 @deftypefn {} {} fftn (@var{A})
128 @deftypefnx {} {} fftn (@var{A}, @var{size})
129 Compute the N-dimensional discrete Fourier transform of @var{A} using
130 a Fast Fourier Transform (FFT) algorithm.
131 
132 The optional vector argument @var{size} may be used specify the dimensions
133 of the array to be used. If an element of @var{size} is smaller than the
134 corresponding dimension of @var{A}, then the dimension of @var{A} is
135 truncated prior to performing the FFT@. Otherwise, if an element of
136 @var{size} is larger than the corresponding dimension then @var{A} is
137 resized and padded with zeros.
138 @seealso{ifftn, fft, fft2, fftw}
139 @end deftypefn */)
140 {
141  return do_fftn (args, "fftn", 0);
142 }
143 
144 DEFUN (ifftn, args, ,
145  doc: /* -*- texinfo -*-
146 @deftypefn {} {} ifftn (@var{A})
147 @deftypefnx {} {} ifftn (@var{A}, @var{size})
148 Compute the inverse N-dimensional discrete Fourier transform of @var{A}
149 using a Fast Fourier Transform (FFT) algorithm.
150 
151 The optional vector argument @var{size} may be used specify the dimensions
152 of the array to be used. If an element of @var{size} is smaller than the
153 corresponding dimension of @var{A}, then the dimension of @var{A} is
154 truncated prior to performing the inverse FFT@. Otherwise, if an element of
155 @var{size} is larger than the corresponding dimension then @var{A} is
156 resized and padded with zeros.
157 @seealso{fftn, ifft, ifft2, fftw}
158 @end deftypefn */)
159 {
160  return do_fftn (args, "ifftn", 1);
161 }
void resize(const dim_vector &dv, const T &rfv)
Size of the specified dimension.
Definition: Array.cc:1011
octave_idx_type columns(void) const
Definition: Array.h:424
octave_idx_type rows(void) const
Definition: Array.h:415
ComplexNDArray fourierNd(void) const
Definition: CNDArray.cc:160
ComplexNDArray ifourierNd(void) const
Definition: CNDArray.cc:175
FloatComplexNDArray ifourierNd(void) const
Definition: fCNDArray.cc:175
FloatComplexNDArray fourierNd(void) const
Definition: fCNDArray.cc:160
FloatComplexNDArray fourierNd(void) const
Definition: fNDArray.cc:158
FloatComplexNDArray ifourierNd(void) const
Definition: fNDArray.cc:173
Definition: dMatrix.h:42
Matrix transpose(void) const
Definition: dMatrix.h:135
ComplexNDArray fourierNd(void) const
Definition: dNDArray.cc:200
ComplexNDArray ifourierNd(void) const
Definition: dNDArray.cc:215
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:95
bool all_zero(void) const
Definition: dim-vector.h:366
octave_idx_type ndims(void) const
Number of dimensions.
Definition: dim-vector.h:334
octave_idx_type length(void) const
Definition: ovl.h:113
bool isreal(void) const
Definition: ov.h:691
ComplexNDArray complex_array_value(bool frc_str_conv=false) const
Definition: ov.h:831
NDArray array_value(bool frc_str_conv=false) const
Definition: ov.h:812
bool is_single_type(void) const
Definition: ov.h:651
FloatComplexNDArray float_complex_array_value(bool frc_str_conv=false) const
Definition: ov.h:835
FloatNDArray float_array_value(bool frc_str_conv=false) const
Definition: ov.h:815
bool iscomplex(void) const
Definition: ov.h:694
dim_vector dims(void) const
Definition: ov.h:500
OCTINTERP_API void print_usage(void)
Definition: defun.cc:53
#define DEFUN(name, args_name, nargout_name, doc)
Macro to define a builtin function.
Definition: defun.h:56
void error(const char *fmt,...)
Definition: error.cc:968
void err_wrong_type_arg(const char *name, const char *s)
Definition: errwarn.cc:166
static octave_value do_fftn(const octave_value_list &args, const char *fcn, int type)
Definition: fftn.cc:41
bool isnan(bool)
Definition: lo-mappers.h:178
octave_idx_type nint_big(double x)
Definition: lo-mappers.cc:184
return octave_value(v1.char_array_value() . concat(v2.char_array_value(), ra_idx),((a1.is_sq_string()||a2.is_sq_string()) ? '\'' :'"'))
octave_value::octave_value(const Array< char > &chm, char type) return retval
Definition: ov.cc:811