GNU Octave 7.1.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-2022 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
38OCTAVE_NAMESPACE_BEGIN
39
40// This function should be merged with Fifft.
41
42static octave_value
43do_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 {
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 {
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
127DEFUN (fftn, args, ,
128 doc: /* -*- texinfo -*-
129@deftypefn {} {} fftn (@var{A})
130@deftypefnx {} {} fftn (@var{A}, @var{size})
131Compute the N-dimensional discrete Fourier transform of @var{A} using
132a Fast Fourier Transform (FFT) algorithm.
133
134The optional vector argument @var{size} may be used specify the dimensions
135of the array to be used. If an element of @var{size} is smaller than the
136corresponding dimension of @var{A}, then the dimension of @var{A} is
137truncated prior to performing the FFT@. Otherwise, if an element of
138@var{size} is larger than the corresponding dimension then @var{A} is
139resized and padded with zeros.
140@seealso{ifftn, fft, fft2, fftw}
141@end deftypefn */)
142{
143 return do_fftn (args, "fftn", 0);
144}
145
146DEFUN (ifftn, args, ,
147 doc: /* -*- texinfo -*-
148@deftypefn {} {} ifftn (@var{A})
149@deftypefnx {} {} ifftn (@var{A}, @var{size})
150Compute the inverse N-dimensional discrete Fourier transform of @var{A}
151using a Fast Fourier Transform (FFT) algorithm.
152
153The optional vector argument @var{size} may be used specify the dimensions
154of the array to be used. If an element of @var{size} is smaller than the
155corresponding dimension of @var{A}, then the dimension of @var{A} is
156truncated prior to performing the inverse FFT@. Otherwise, if an element of
157@var{size} is larger than the corresponding dimension then @var{A} is
158resized and padded with zeros.
159@seealso{fftn, ifft, ifft2, fftw}
160@end deftypefn */)
161{
162 return do_fftn (args, "ifftn", 1);
163}
164
165OCTAVE_NAMESPACE_END
octave_idx_type rows(void) const
Definition: Array.h:449
OCTARRAY_API void resize(const dim_vector &dv, const T &rfv)
Size of the specified dimension.
Definition: Array.cc:1010
octave_idx_type columns(void) const
Definition: Array.h:458
OCTAVE_API ComplexNDArray fourierNd(void) const
Definition: CNDArray.cc:160
OCTAVE_API ComplexNDArray ifourierNd(void) const
Definition: CNDArray.cc:175
OCTAVE_API FloatComplexNDArray ifourierNd(void) const
Definition: fCNDArray.cc:175
OCTAVE_API FloatComplexNDArray fourierNd(void) const
Definition: fCNDArray.cc:160
OCTAVE_API FloatComplexNDArray fourierNd(void) const
Definition: fNDArray.cc:158
OCTAVE_API FloatComplexNDArray ifourierNd(void) const
Definition: fNDArray.cc:173
Definition: dMatrix.h:42
Matrix transpose(void) const
Definition: dMatrix.h:140
OCTAVE_API ComplexNDArray fourierNd(void) const
Definition: dNDArray.cc:200
OCTAVE_API ComplexNDArray ifourierNd(void) const
Definition: dNDArray.cc:215
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:94
bool all_zero(void) const
Definition: dim-vector.h:300
octave_idx_type ndims(void) const
Number of dimensions.
Definition: dim-vector.h:257
octave_idx_type length(void) const
Definition: ovl.h:113
bool isreal(void) const
Definition: ov.h:783
ComplexNDArray complex_array_value(bool frc_str_conv=false) const
Definition: ov.h:923
NDArray array_value(bool frc_str_conv=false) const
Definition: ov.h:904
bool is_single_type(void) const
Definition: ov.h:743
FloatComplexNDArray float_complex_array_value(bool frc_str_conv=false) const
Definition: ov.h:927
FloatNDArray float_array_value(bool frc_str_conv=false) const
Definition: ov.h:907
bool iscomplex(void) const
Definition: ov.h:786
dim_vector dims(void) const
Definition: ov.h:586
OCTINTERP_API 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:980
void err_wrong_type_arg(const char *name, const char *s)
Definition: errwarn.cc:166
static OCTAVE_NAMESPACE_BEGIN octave_value do_fftn(const octave_value_list &args, const char *fcn, int type)
Definition: fftn.cc:43
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()) ? '\'' :'"'))