GNU Octave 10.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 
Loading...
Searching...
No Matches
fftn.cc
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////
2//
3// Copyright (C) 2004-2025 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
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.any_zero ())
80 {
81 if (arg.is_single_type ())
82 return octave_value (FloatNDArray (dims));
83 else
84 return octave_value (NDArray (dims));
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 {} {@var{B} =} fftn (@var{A})
130@deftypefnx {} {@var{B} =} 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
146/*
147%!testif HAVE_FFTW <*65414>
148%! sz = size (fftn (ones (2, 0, 3)));
149%! assert (sz, [2, 0, 3]);
150
151%!testif HAVE_FFTW <*65414>
152%! sz = size (fftn (ones (5, 4, 3), [2, 0, 3]));
153%! assert (sz, [2, 0, 3]);
154*/
155
156DEFUN (ifftn, args, ,
157 doc: /* -*- texinfo -*-
158@deftypefn {} {@var{A} =} ifftn (@var{B})
159@deftypefnx {} {@var{A} =} ifftn (@var{B}, @var{size})
160Compute the inverse N-dimensional discrete Fourier transform of @var{B}
161using a Fast Fourier Transform (FFT) algorithm.
162
163The optional vector argument @var{size} may be used specify the dimensions
164of the array to be used. If an element of @var{size} is smaller than the
165corresponding dimension of @var{B}, then the dimension of @var{B} is
166truncated prior to performing the inverse FFT@. Otherwise, if an element of
167@var{size} is larger than the corresponding dimension then @var{B} is
168resized and padded with zeros.
169@seealso{fftn, ifft, ifft2, fftw}
170@end deftypefn */)
171{
172 return do_fftn (args, "ifftn", 1);
173}
174
175/*
176%!testif HAVE_FFTW <*65414>
177%! sz = size (ifftn (ones (2, 0, 3)));
178%! assert (sz, [2, 0, 3]);
179
180%!testif HAVE_FFTW <*65414>
181%! sz = size (ifftn (ones (5, 4, 3), [2, 0, 3]));
182%! assert (sz, [2, 0, 3]);
183*/
184
185OCTAVE_END_NAMESPACE(octave)
octave_idx_type rows() const
Definition Array.h:463
void resize(const dim_vector &dv, const T &rfv)
Size of the specified dimension.
octave_idx_type columns() const
Definition Array.h:475
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
Matrix transpose() const
Definition dMatrix.h:138
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:90
octave_idx_type ndims() const
Number of dimensions.
Definition dim-vector.h:253
bool any_zero() const
Definition dim-vector.h:312
octave_idx_type length() const
Definition ovl.h:111
bool isreal() const
Definition ov.h:738
ComplexNDArray complex_array_value(bool frc_str_conv=false) const
Definition ov.h:884
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:865
FloatComplexNDArray float_complex_array_value(bool frc_str_conv=false) const
Definition ov.h:888
FloatNDArray float_array_value(bool frc_str_conv=false) const
Definition ov.h:868
dim_vector dims() const
Definition ov.h:541
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
void print_usage()
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:1003
void err_wrong_type_arg(const char *name, const char *s)
Definition errwarn.cc:166