GNU Octave  9.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
fftn.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 2004-2024 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 
39 
40 // This function should be merged with Fifft.
41 
42 static octave_value
43 do_fftn (const octave_value_list& args, const char *fcn, int type)
44 {
45  int nargin = args.length ();
46 
47  if (nargin < 1 || nargin > 2)
48  print_usage ();
49 
50  octave_value retval;
51  octave_value arg = args(0);
52  dim_vector dims = arg.dims ();
53 
54  for (int i = 0; i < dims.ndims (); i++)
55  if (dims(i) < 0)
56  return retval;
57 
58  if (nargin > 1)
59  {
60  Matrix val = args(1).xmatrix_value ("%s: SIZE must be a vector of length dim", fcn);
61 
62  if (val.rows () > val.columns ())
63  val = val.transpose ();
64 
65  if (val.columns () != dims.ndims () || val.rows () != 1)
66  error ("%s: SIZE must be a vector of length dim", fcn);
67 
68  for (int i = 0; i < dims.ndims (); i++)
69  {
70  if (math::isnan (val(i, 0)))
71  error ("%s: SIZE has invalid NaN entries", fcn);
72  else if (math::nint_big (val(i, 0)) < 0)
73  error ("%s: all dimensions in SIZE must be greater than zero", fcn);
74  else
75  dims(i) = math::nint_big(val(i, 0));
76  }
77  }
78 
79  if (dims.all_zero ())
80  {
81  if (arg.is_single_type ())
82  return octave_value (FloatMatrix ());
83  else
84  return octave_value (Matrix ());
85  }
86 
87  if (arg.is_single_type ())
88  {
89  if (arg.isreal ())
90  {
91  FloatNDArray nda = arg.float_array_value ();
92 
93  nda.resize (dims, 0.0);
94  retval = (type != 0 ? nda.ifourierNd () : nda.fourierNd ());
95  }
96  else
97  {
99 
100  cnda.resize (dims, 0.0);
101  retval = (type != 0 ? cnda.ifourierNd () : cnda.fourierNd ());
102  }
103  }
104  else
105  {
106  if (arg.isreal ())
107  {
108  NDArray nda = arg.array_value ();
109 
110  nda.resize (dims, 0.0);
111  retval = (type != 0 ? nda.ifourierNd () : nda.fourierNd ());
112  }
113  else if (arg.iscomplex ())
114  {
115  ComplexNDArray cnda = arg.complex_array_value ();
116 
117  cnda.resize (dims, 0.0);
118  retval = (type != 0 ? cnda.ifourierNd () : cnda.fourierNd ());
119  }
120  else
121  err_wrong_type_arg (fcn, arg);
122  }
123 
124  return retval;
125 }
126 
127 DEFUN (fftn, args, ,
128  doc: /* -*- texinfo -*-
129 @deftypefn {} {@var{B} =} fftn (@var{A})
130 @deftypefnx {} {@var{B} =} fftn (@var{A}, @var{size})
131 Compute the N-dimensional discrete Fourier transform of @var{A} using
132 a Fast Fourier Transform (FFT) algorithm.
133 
134 The optional vector argument @var{size} may be used specify the dimensions
135 of the array to be used. If an element of @var{size} is smaller than the
136 corresponding dimension of @var{A}, then the dimension of @var{A} is
137 truncated prior to performing the FFT@. Otherwise, if an element of
138 @var{size} is larger than the corresponding dimension then @var{A} is
139 resized and padded with zeros.
140 @seealso{ifftn, fft, fft2, fftw}
141 @end deftypefn */)
142 {
143  return do_fftn (args, "fftn", 0);
144 }
145 
146 DEFUN (ifftn, args, ,
147  doc: /* -*- texinfo -*-
148 @deftypefn {} {@var{A} =} ifftn (@var{B})
149 @deftypefnx {} {@var{A} =} ifftn (@var{B}, @var{size})
150 Compute the inverse N-dimensional discrete Fourier transform of @var{B}
151 using a Fast Fourier Transform (FFT) algorithm.
152 
153 The optional vector argument @var{size} may be used specify the dimensions
154 of the array to be used. If an element of @var{size} is smaller than the
155 corresponding dimension of @var{B}, then the dimension of @var{B} is
156 truncated prior to performing the inverse FFT@. Otherwise, if an element of
157 @var{size} is larger than the corresponding dimension then @var{B} is
158 resized and padded with zeros.
159 @seealso{fftn, ifft, ifft2, fftw}
160 @end deftypefn */)
161 {
162  return do_fftn (args, "ifftn", 1);
163 }
164 
165 OCTAVE_END_NAMESPACE(octave)
octave_idx_type rows() const
Definition: Array.h:459
void resize(const dim_vector &dv, const T &rfv)
Size of the specified dimension.
Definition: Array-base.cc:1023
octave_idx_type columns() const
Definition: Array.h:471
ComplexNDArray ifourierNd() const
Definition: CNDArray.cc:175
ComplexNDArray fourierNd() const
Definition: CNDArray.cc:160
FloatComplexNDArray ifourierNd() const
Definition: fCNDArray.cc:175
FloatComplexNDArray fourierNd() const
Definition: fCNDArray.cc:160
FloatComplexNDArray fourierNd() const
Definition: fNDArray.cc:158
FloatComplexNDArray ifourierNd() const
Definition: fNDArray.cc:173
Definition: dMatrix.h:42
Matrix transpose() const
Definition: dMatrix.h:140
ComplexNDArray fourierNd() const
Definition: dNDArray.cc:200
ComplexNDArray ifourierNd() const
Definition: dNDArray.cc:215
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:94
octave_idx_type ndims() const
Number of dimensions.
Definition: dim-vector.h:257
bool all_zero() const
Definition: dim-vector.h:300
octave_idx_type length() const
Definition: ovl.h:113
bool isreal() const
Definition: ov.h:738
ComplexNDArray complex_array_value(bool frc_str_conv=false) const
Definition: ov.h:878
bool is_single_type() const
Definition: ov.h:698
bool iscomplex() const
Definition: ov.h:741
NDArray array_value(bool frc_str_conv=false) const
Definition: ov.h:859
FloatComplexNDArray float_complex_array_value(bool frc_str_conv=false) const
Definition: ov.h:882
FloatNDArray float_array_value(bool frc_str_conv=false) const
Definition: ov.h:862
dim_vector dims() const
Definition: ov.h:541
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
void print_usage(void)
Definition: defun-int.h:72
#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:988
void err_wrong_type_arg(const char *name, const char *s)
Definition: errwarn.cc:166
octave_idx_type nint_big(double x)
Definition: lo-mappers.cc:188
bool isnan(bool)
Definition: lo-mappers.h:178
return octave_value(v1.char_array_value() . concat(v2.char_array_value(), ra_idx),((a1.is_sq_string()||a2.is_sq_string()) ? '\'' :'"'))