GNU Octave 10.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 
Loading...
Searching...
No Matches
fft2.cc
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////
2//
3// Copyright (C) 1996-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_fft2 (const octave_value_list& args, const char *fcn, int type)
44{
45 int nargin = args.length ();
46
47 if (nargin < 1 || nargin > 3)
48 print_usage ();
49
50 octave_value retval;
51 octave_value arg = args(0);
52 dim_vector dims = arg.dims ();
53 octave_idx_type n_rows = -1;
54
55 if (nargin > 1)
56 {
57 double dval = args(1).double_value ();
58 if (math::isnan (dval))
59 error ("%s: number of rows (M) cannot be NaN", fcn);
60
61 n_rows = math::nint_big (dval);
62 if (n_rows < 0)
63 error ("%s: number of rows (M) must be greater than zero", fcn);
64 }
65
66 octave_idx_type n_cols = -1;
67 if (nargin > 2)
68 {
69 double dval = args(2).double_value ();
70 if (math::isnan (dval))
71 error ("%s: number of columns (N) cannot be NaN", fcn);
72
73 n_cols = math::nint_big (dval);
74 if (n_cols < 0)
75 error ("%s: number of columns (N) must be greater than zero", fcn);
76 }
77
78 for (int i = 0; i < dims.ndims (); i++)
79 if (dims(i) < 0)
80 return retval;
81
82 if (n_rows < 0)
83 n_rows = dims(0);
84 else
85 dims(0) = n_rows;
86
87 if (n_cols < 0)
88 n_cols = dims(1);
89 else
90 dims(1) = n_cols;
91
92 if (dims.any_zero ())
93 {
94 if (arg.is_single_type ())
95 return octave_value (FloatNDArray (dims));
96 else
97 return octave_value (NDArray (dims));
98 }
99
100 if (arg.is_single_type ())
101 {
102 if (arg.isreal ())
103 {
104 FloatNDArray nda = arg.float_array_value ();
105
106 nda.resize (dims, 0.0);
107 retval = (type != 0 ? nda.ifourier2d () : nda.fourier2d ());
108 }
109 else
110 {
112
113 cnda.resize (dims, 0.0);
114 retval = (type != 0 ? cnda.ifourier2d () : cnda.fourier2d ());
115 }
116 }
117 else
118 {
119 if (arg.isreal ())
120 {
121 NDArray nda = arg.array_value ();
122
123 nda.resize (dims, 0.0);
124 retval = (type != 0 ? nda.ifourier2d () : nda.fourier2d ());
125 }
126 else if (arg.iscomplex ())
127 {
129
130 cnda.resize (dims, 0.0);
131 retval = (type != 0 ? cnda.ifourier2d () : cnda.fourier2d ());
132 }
133 else
134 err_wrong_type_arg (fcn, arg);
135 }
136
137 return retval;
138}
139
140DEFUN (fft2, args, ,
141 doc: /* -*- texinfo -*-
142@deftypefn {} {@var{B} =} fft2 (@var{A})
143@deftypefnx {} {@var{B} =} fft2 (@var{A}, @var{m}, @var{n})
144Compute the two-dimensional discrete Fourier transform of @var{A} using
145a Fast Fourier Transform (FFT) algorithm.
146
147The optional arguments @var{m} and @var{n} may be used specify the number of
148rows and columns of @var{A} to use. If either of these is larger than the
149size of @var{A}, @var{A} is resized and padded with zeros.
150
151If @var{A} is a multi-dimensional matrix, each two-dimensional sub-matrix
152of @var{A} is treated separately.
153@seealso{ifft2, fft, fftn, fftw}
154@end deftypefn */)
155{
156 return do_fft2 (args, "fft2", 0);
157}
158
159/*
160%!testif HAVE_FFTW <*65414>
161%! sz = size (fft2 (ones (2, 0, 3)));
162%! assert (sz, [2, 0, 3]);
163
164%!testif HAVE_FFTW <*65414>
165%! sz = size (fft2 (ones (5, 4, 3), 2, 0));
166%! assert (sz, [2, 0, 3]);
167
168%!error <number of rows \‍(M\‍) cannot be NaN> fft2 (ones (5,4,3), NaN, 2)
169%!error <number of rows \‍(M\‍) .* greater than zero> fft2 (ones (5,4,3), -1, 2)
170%!error <number of columns \‍(N\‍) cannot be NaN> fft2 (ones (5,4,3), 2, NaN)
171%!error <number of columns \‍(N\‍) .* greater than zero> fft2 (ones (5,4,3), 2, -1)
172*/
173
174DEFUN (ifft2, args, ,
175 doc: /* -*- texinfo -*-
176@deftypefn {} {@var{A} =} ifft2 (@var{B})
177@deftypefnx {} {@var{A} =} ifft2 (@var{B}, @var{m}, @var{n})
178Compute the inverse two-dimensional discrete Fourier transform of @var{B}
179using a Fast Fourier Transform (FFT) algorithm.
180
181The optional arguments @var{m} and @var{n} may be used specify the number of
182rows and columns of @var{B} to use. If either of these is larger than the
183size of @var{B}, @var{B} is resized and padded with zeros.
184
185If @var{B} is a multi-dimensional matrix, each two-dimensional sub-matrix
186of @var{B} is treated separately.
187@seealso{fft2, ifft, ifftn, fftw}
188@end deftypefn */)
189{
190 return do_fft2 (args, "ifft2", 1);
191}
192
193/*
194%!testif HAVE_FFTW <*65414>
195%! sz = size (ifft2 (ones (2, 0, 3)));
196%! assert (sz, [2, 0, 3]);
197
198%!testif HAVE_FFTW <*65414>
199%! sz = size (ifft2 (ones (5, 4, 3), 2, 0));
200%! assert (sz, [2, 0, 3]);
201
202## Author: David Billinghurst (David.Billinghurst@riotinto.com.au)
203## Comalco Research and Technology
204## 02 May 2000
205%!testif HAVE_FFTW
206%! M = 16;
207%! N = 8;
208%!
209%! m = 5;
210%! n = 3;
211%!
212%! x = 2*pi*(0:1:M-1)/M;
213%! y = 2*pi*(0:1:N-1)/N;
214%! sx = cos (m*x);
215%! sy = sin (n*y);
216%! s = kron (sx',sy);
217%! S = fft2 (s);
218%! answer = kron (fft (sx)', fft (sy));
219%! assert (S, answer, 4*M*N*eps);
220
221## Author: David Billinghurst (David.Billinghurst@riotinto.com.au)
222## Comalco Research and Technology
223## 02 May 2000
224%!testif HAVE_FFTW
225%! M = 12;
226%! N = 7;
227%!
228%! m = 3;
229%! n = 2;
230%!
231%! x = 2*pi*(0:1:M-1)/M;
232%! y = 2*pi*(0:1:N-1)/N;
233%!
234%! sx = cos (m*x);
235%! sy = cos (n*y);
236%!
237%! S = kron (fft (sx)', fft (sy));
238%! answer = kron (sx', sy);
239%! s = ifft2 (S);
240%!
241%! assert (s, answer, 30*eps);
242
243## Author: David Billinghurst (David.Billinghurst@riotinto.com.au)
244## Comalco Research and Technology
245## 02 May 2000
246%!testif HAVE_FFTW
247%! M = 16;
248%! N = 8;
249%!
250%! m = 5;
251%! n = 3;
252%!
253%! x = 2*pi*(0:1:M-1)/M;
254%! y = 2*pi*(0:1:N-1)/N;
255%! sx = single (cos (m*x));
256%! sy = single (sin (n*y));
257%! s = kron (sx', sy);
258%! S = fft2 (s);
259%! answer = kron (fft (sx)', fft (sy));
260%! assert (S, answer, 4*M*N* eps ("single"));
261
262## Author: David Billinghurst (David.Billinghurst@riotinto.com.au)
263## Comalco Research and Technology
264## 02 May 2000
265%!testif HAVE_FFTW
266%! M = 12;
267%! N = 7;
268%!
269%! m = 3;
270%! n = 2;
271%!
272%! x = single (2*pi*(0:1:M-1)/M);
273%! y = single (2*pi*(0:1:N-1)/N);
274%!
275%! sx = cos (m*x);
276%! sy = cos (n*y);
277%!
278%! S = kron (fft (sx)', fft (sy));
279%! answer = kron (sx', sy);
280%! s = ifft2 (S);
281%!
282%! assert (s, answer, 30* eps ("single"));
283
284%!error <number of rows \‍(M\‍) cannot be NaN> ifft2 (ones (5,4,3), NaN, 2)
285%!error <number of rows \‍(M\‍) .* greater than zero> ifft2 (ones (5,4,3), -1, 2)
286%!error <number of columns \‍(N\‍) cannot be NaN> ifft2 (ones (5,4,3), 2, NaN)
287%!error <number of columns \‍(N\‍) .* greater than zero> ifft2 (ones (5,4,3), 2, -1)
288*/
289
290OCTAVE_END_NAMESPACE(octave)
void resize(const dim_vector &dv, const T &rfv)
Size of the specified dimension.
ComplexNDArray ifourier2d() const
Definition CNDArray.cc:140
ComplexNDArray fourier2d() const
Definition CNDArray.cc:120
FloatComplexNDArray fourier2d() const
Definition fCNDArray.cc:120
FloatComplexNDArray ifourier2d() const
Definition fCNDArray.cc:140
FloatComplexNDArray ifourier2d() const
Definition fNDArray.cc:139
FloatComplexNDArray fourier2d() const
Definition fNDArray.cc:119
ComplexNDArray fourier2d() const
Definition dNDArray.cc:161
ComplexNDArray ifourier2d() const
Definition dNDArray.cc:181
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