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