GNU Octave  4.2.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
fft2.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 1997-2017 David Bateman
4 Copyright (C) 1996-1997 John W. Eaton
5 
6 This file is part of Octave.
7 
8 Octave is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 3 of the License, or (at your
11 option) any later version.
12 
13 Octave is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17 
18 You should have received a copy of the GNU General Public License
19 along with Octave; see the file COPYING. If not, see
20 <http://www.gnu.org/licenses/>.
21 
22 */
23 
24 #if defined (HAVE_CONFIG_H)
25 # include "config.h"
26 #endif
27 
28 #include "lo-mappers.h"
29 
30 #include "defun.h"
31 #include "error.h"
32 #include "errwarn.h"
33 #include "ovl.h"
34 #include "utils.h"
35 
36 // This function should be merged with Fifft.
37 
38 #if defined (HAVE_FFTW)
39 # define FFTSRC "@sc{fftw}"
40 #else
41 # define FFTSRC "@sc{fftpack}"
42 #endif
43 
44 static octave_value
45 do_fft2 (const octave_value_list &args, const char *fcn, int type)
46 {
47  int nargin = args.length ();
48 
49  if (nargin < 1 || nargin > 3)
50  print_usage ();
51 
53  octave_value arg = args(0);
54  dim_vector dims = arg.dims ();
55  octave_idx_type n_rows = -1;
56 
57  if (nargin > 1)
58  {
59  double dval = args(1).double_value ();
60  if (octave::math::isnan (dval))
61  error ("%s: number of rows (N) cannot be NaN", fcn);
62 
63  n_rows = octave::math::nint_big (dval);
64  if (n_rows < 0)
65  error ("%s: number of rows (N) must be greater than zero", fcn);
66  }
67 
68  octave_idx_type n_cols = -1;
69  if (nargin > 2)
70  {
71  double dval = args(2).double_value ();
72  if (octave::math::isnan (dval))
73  error ("%s: number of columns (M) cannot be NaN", fcn);
74 
75  n_cols = octave::math::nint_big (dval);
76  if (n_cols < 0)
77  error ("%s: number of columns (M) must be greater than zero", fcn);
78  }
79 
80  for (int i = 0; i < dims.ndims (); i++)
81  if (dims(i) < 0)
82  return retval;
83 
84  if (n_rows < 0)
85  n_rows = dims(0);
86  else
87  dims(0) = n_rows;
88 
89  if (n_cols < 0)
90  n_cols = dims(1);
91  else
92  dims(1) = n_cols;
93 
94  if (dims.all_zero () || n_rows == 0 || n_cols == 0)
95  {
96  if (arg.is_single_type ())
97  return octave_value (FloatMatrix ());
98  else
99  return octave_value (Matrix ());
100  }
101 
102  if (arg.is_single_type ())
103  {
104  if (arg.is_real_type ())
105  {
106  FloatNDArray nda = arg.float_array_value ();
107 
108  nda.resize (dims, 0.0);
109  retval = (type != 0 ? nda.ifourier2d () : nda.fourier2d ());
110  }
111  else
112  {
114 
115  cnda.resize (dims, 0.0);
116  retval = (type != 0 ? cnda.ifourier2d () : cnda.fourier2d ());
117  }
118  }
119  else
120  {
121  if (arg.is_real_type ())
122  {
123  NDArray nda = arg.array_value ();
124 
125  nda.resize (dims, 0.0);
126  retval = (type != 0 ? nda.ifourier2d () : nda.fourier2d ());
127  }
128  else if (arg.is_complex_type ())
129  {
130  ComplexNDArray cnda = arg.complex_array_value ();
131 
132  cnda.resize (dims, 0.0);
133  retval = (type != 0 ? cnda.ifourier2d () : cnda.fourier2d ());
134  }
135  else
136  err_wrong_type_arg (fcn, arg);
137  }
138 
139  return retval;
140 }
141 
142 DEFUN (fft2, args, ,
143  doc: /* -*- texinfo -*-
144 @deftypefn {} {} fft2 (@var{A})
145 @deftypefnx {} {} fft2 (@var{A}, @var{m}, @var{n})
146 Compute the two-dimensional discrete Fourier transform of @var{A} using
147 a Fast Fourier Transform (FFT) algorithm.
148 
149 The optional arguments @var{m} and @var{n} may be used specify the number of
150 rows and columns of @var{A} to use. If either of these is larger than the
151 size of @var{A}, @var{A} is resized and padded with zeros.
152 
153 If @var{A} is a multi-dimensional matrix, each two-dimensional sub-matrix
154 of @var{A} is treated separately.
155 @seealso{ifft2, fft, fftn, fftw}
156 @end deftypefn */)
157 {
158  return do_fft2 (args, "fft2", 0);
159 }
160 
161 
162 DEFUN (ifft2, args, ,
163  doc: /* -*- texinfo -*-
164 @deftypefn {} {} ifft2 (@var{A})
165 @deftypefnx {} {} ifft2 (@var{A}, @var{m}, @var{n})
166 Compute the inverse two-dimensional discrete Fourier transform of @var{A}
167 using a Fast Fourier Transform (FFT) algorithm.
168 
169 The optional arguments @var{m} and @var{n} may be used specify the number of
170 rows and columns of @var{A} to use. If either of these is larger than the
171 size of @var{A}, @var{A} is resized and padded with zeros.
172 
173 If @var{A} is a multi-dimensional matrix, each two-dimensional sub-matrix
174 of @var{A} is treated separately
175 @seealso{fft2, ifft, ifftn, fftw}
176 @end deftypefn */)
177 {
178  return do_fft2 (args, "ifft2", 1);
179 }
180 
181 /*
182 %% Author: David Billinghurst (David.Billinghurst@riotinto.com.au)
183 %% Comalco Research and Technology
184 %% 02 May 2000
185 %!test
186 %! M = 16;
187 %! N = 8;
188 %!
189 %! m = 5;
190 %! n = 3;
191 %!
192 %! x = 2*pi*(0:1:M-1)/M;
193 %! y = 2*pi*(0:1:N-1)/N;
194 %! sx = cos (m*x);
195 %! sy = sin (n*y);
196 %! s = kron (sx',sy);
197 %! S = fft2 (s);
198 %! answer = kron (fft (sx)', fft (sy));
199 %! assert (S, answer, 4*M*N*eps);
200 
201 %% Author: David Billinghurst (David.Billinghurst@riotinto.com.au)
202 %% Comalco Research and Technology
203 %% 02 May 2000
204 %!test
205 %! M = 12;
206 %! N = 7;
207 %!
208 %! m = 3;
209 %! n = 2;
210 %!
211 %! x = 2*pi*(0:1:M-1)/M;
212 %! y = 2*pi*(0:1:N-1)/N;
213 %!
214 %! sx = cos (m*x);
215 %! sy = cos (n*y);
216 %!
217 %! S = kron (fft (sx)', fft (sy));
218 %! answer = kron (sx', sy);
219 %! s = ifft2 (S);
220 %!
221 %! assert (s, answer, 30*eps);
222 
223 %% Author: David Billinghurst (David.Billinghurst@riotinto.com.au)
224 %% Comalco Research and Technology
225 %% 02 May 2000
226 %!test
227 %! M = 16;
228 %! N = 8;
229 %!
230 %! m = 5;
231 %! n = 3;
232 %!
233 %! x = 2*pi*(0:1:M-1)/M;
234 %! y = 2*pi*(0:1:N-1)/N;
235 %! sx = single (cos (m*x));
236 %! sy = single (sin (n*y));
237 %! s = kron (sx', sy);
238 %! S = fft2 (s);
239 %! answer = kron (fft (sx)', fft (sy));
240 %! assert (S, answer, 4*M*N*eps ("single"));
241 
242 %% Author: David Billinghurst (David.Billinghurst@riotinto.com.au)
243 %% Comalco Research and Technology
244 %% 02 May 2000
245 %!test
246 %! M = 12;
247 %! N = 7;
248 %!
249 %! m = 3;
250 %! n = 2;
251 %!
252 %! x = single (2*pi*(0:1:M-1)/M);
253 %! y = single (2*pi*(0:1:N-1)/N);
254 %!
255 %! sx = cos (m*x);
256 %! sy = cos (n*y);
257 %!
258 %! S = kron (fft (sx)', fft (sy));
259 %! answer = kron (sx', sy);
260 %! s = ifft2 (S);
261 %!
262 %! assert (s, answer, 30*eps ("single"));
263 */
ComplexNDArray complex_array_value(bool frc_str_conv=false) const
Definition: ov.h:812
bool is_real_type(void) const
Definition: ov.h:667
ComplexNDArray ifourier2d(void) const
Definition: CNDArray.cc:140
OCTINTERP_API void print_usage(void)
Definition: defun.cc:52
octave_idx_type length(void) const
Definition: ovl.h:96
bool isnan(double x)
Definition: lo-mappers.cc:347
FloatComplexNDArray fourier2d(void) const
Definition: fCNDArray.cc:120
#define DEFUN(name, args_name, nargout_name, doc)
Definition: defun.h:46
void error(const char *fmt,...)
Definition: error.cc:570
static octave_value do_fft2(const octave_value_list &args, const char *fcn, int type)
Definition: fft2.cc:45
octave_value arg
Definition: pr-output.cc:3440
octave_function * fcn
Definition: ov-class.cc:1743
ComplexNDArray fourier2d(void) const
Definition: CNDArray.cc:120
FloatComplexNDArray fourier2d(void) const
Definition: fNDArray.cc:119
JNIEnv void * args
Definition: ov-java.cc:67
FloatComplexNDArray ifourier2d(void) const
Definition: fNDArray.cc:139
FloatNDArray float_array_value(bool frc_str_conv=false) const
Definition: ov.h:796
int nargin
Definition: graphics.cc:10115
FloatComplexNDArray float_complex_array_value(bool frc_str_conv=false) const
Definition: ov.h:816
ComplexNDArray ifourier2d(void) const
Definition: dNDArray.cc:181
bool is_complex_type(void) const
Definition: ov.h:670
void resize(const dim_vector &dv, const T &rfv)
Definition: Array.cc:1028
octave_value retval
Definition: data.cc:6294
idx type
Definition: ov.cc:3129
Definition: dMatrix.h:37
the exceeded dimensions are set to if fewer subscripts than dimensions are the exceeding dimensions are merged into the final requested dimension For consider the following dims
Definition: sub2ind.cc:255
dim_vector dims(void) const
Definition: ov.h:486
bool all_zero(void) const
Definition: dim-vector.h:333
void err_wrong_type_arg(const char *name, const char *s)
Definition: errwarn.cc:156
NDArray array_value(bool frc_str_conv=false) const
Definition: ov.h:793
=val(i)}if ode{val(i)}occurs in table i
Definition: lookup.cc:239
octave_idx_type nint_big(double x)
Definition: lo-mappers.cc:409
octave_idx_type ndims(void) const
Number of dimensions.
Definition: dim-vector.h:301
ComplexNDArray fourier2d(void) const
Definition: dNDArray.cc:161
FloatComplexNDArray ifourier2d(void) const
Definition: fCNDArray.cc:140
bool is_single_type(void) const
Definition: ov.h:627
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:87
return octave_value(v1.char_array_value().concat(v2.char_array_value(), ra_idx),((a1.is_sq_string()||a2.is_sq_string())? '\'': '"'))