2928
|
1 /* |
|
2 |
4773
|
3 Copyright (C) 2004 David Bateman |
2928
|
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 2, or (at your option) any |
|
11 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, write to the Free |
5307
|
20 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
21 02110-1301, USA. |
2928
|
22 |
|
23 */ |
|
24 |
|
25 #ifdef HAVE_CONFIG_H |
|
26 #include <config.h> |
|
27 #endif |
|
28 |
|
29 #include "lo-mappers.h" |
|
30 |
|
31 #include "defun-dld.h" |
|
32 #include "error.h" |
|
33 #include "gripes.h" |
|
34 #include "oct-obj.h" |
|
35 #include "utils.h" |
|
36 |
4773
|
37 // This function should be merged with Fifft. |
2928
|
38 |
4773
|
39 #if defined (HAVE_FFTW3) |
|
40 #define FFTSRC "@sc{Fftw}" |
|
41 #else |
|
42 #define FFTSRC "@sc{Fftpack}" |
|
43 #endif |
|
44 |
|
45 static octave_value |
|
46 do_fft2 (const octave_value_list &args, const char *fcn, int type) |
2928
|
47 { |
4233
|
48 octave_value retval; |
2928
|
49 |
|
50 int nargin = args.length (); |
|
51 |
|
52 if (nargin < 1 || nargin > 3) |
|
53 { |
5823
|
54 print_usage (); |
2928
|
55 return retval; |
|
56 } |
|
57 |
|
58 octave_value arg = args(0); |
4773
|
59 dim_vector dims = arg.dims (); |
5275
|
60 octave_idx_type n_rows = -1; |
4773
|
61 |
2928
|
62 if (nargin > 1) |
|
63 { |
|
64 double dval = args(1).double_value (); |
|
65 if (xisnan (dval)) |
4773
|
66 error ("%s: NaN is invalid as the N_ROWS", fcn); |
2928
|
67 else |
4773
|
68 { |
5275
|
69 n_rows = NINTbig (dval); |
4773
|
70 if (n_rows < 0) |
|
71 error ("%s: number of rows must be greater than zero", fcn); |
|
72 } |
2928
|
73 } |
|
74 |
|
75 if (error_state) |
|
76 return retval; |
|
77 |
5275
|
78 octave_idx_type n_cols = -1; |
2928
|
79 if (nargin > 2) |
|
80 { |
|
81 double dval = args(2).double_value (); |
|
82 if (xisnan (dval)) |
4773
|
83 error ("%s: NaN is invalid as the N_COLS", fcn); |
2928
|
84 else |
4773
|
85 { |
5275
|
86 n_cols = NINTbig (dval); |
4773
|
87 if (n_cols < 0) |
|
88 error ("%s: number of columns must be greater than zero", fcn); |
|
89 } |
2928
|
90 } |
|
91 |
|
92 if (error_state) |
|
93 return retval; |
|
94 |
4773
|
95 for (int i = 0; i < dims.length (); i++) |
|
96 if (dims(i) < 0) |
2928
|
97 return retval; |
|
98 |
4773
|
99 if (n_rows < 0) |
|
100 n_rows = dims (0); |
|
101 else |
|
102 dims (0) = n_rows; |
2928
|
103 |
4773
|
104 if (n_cols < 0) |
|
105 n_cols = dims (1); |
|
106 else |
|
107 dims (1) = n_cols; |
|
108 |
|
109 if (dims.all_zero () || n_rows == 0 || n_cols == 0) |
4233
|
110 return octave_value (Matrix ()); |
2928
|
111 |
|
112 if (arg.is_real_type ()) |
|
113 { |
4773
|
114 NDArray nda = arg.array_value (); |
2928
|
115 |
|
116 if (! error_state) |
|
117 { |
4773
|
118 nda.resize (dims, 0.0); |
|
119 retval = (type != 0 ? nda.ifourier2d () : nda.fourier2d ()); |
2928
|
120 } |
|
121 } |
|
122 else if (arg.is_complex_type ()) |
|
123 { |
4773
|
124 ComplexNDArray cnda = arg.complex_array_value (); |
2928
|
125 |
|
126 if (! error_state) |
|
127 { |
4773
|
128 cnda.resize (dims, 0.0); |
|
129 retval = (type != 0 ? cnda.ifourier2d () : cnda.fourier2d ()); |
2928
|
130 } |
|
131 } |
|
132 else |
|
133 { |
4773
|
134 gripe_wrong_type_arg (fcn, arg); |
2928
|
135 } |
|
136 |
|
137 return retval; |
|
138 } |
|
139 |
4773
|
140 DEFUN_DLD (fft2, args, , |
|
141 "-*- texinfo -*-\n\ |
|
142 @deftypefn {Loadable Function} {} fft2 (@var{a}, @var{n}, @var{m})\n\ |
|
143 Compute the two dimensional FFT of @var{a} using subroutines from\n" |
|
144 FFTSRC |
|
145 ". The optional arguments @var{n} and @var{m} may be used specify the\n\ |
|
146 number of rows and columns of @var{a} to use. If either of these is\n\ |
|
147 larger than the size of @var{a}, @var{a} is resized and padded with\n\ |
|
148 zeros.\n\ |
|
149 \n\ |
|
150 If @var{a} is a multi-dimensional matrix, each two-dimensional sub-matrix\n\ |
7001
|
151 of @var{a} is treated separately\n\ |
6971
|
152 @seealso {ifft2, fft, fftn, fftw}\n\ |
5642
|
153 @end deftypefn") |
4773
|
154 { |
4782
|
155 return do_fft2 (args, "fft2", 0); |
4773
|
156 } |
|
157 |
|
158 |
|
159 DEFUN_DLD (ifft2, args, , |
|
160 "-*- texinfo -*-\n\ |
|
161 @deftypefn {Loadable Function} {} fft2 (@var{a}, @var{n}, @var{m})\n\ |
|
162 Compute the inverse two dimensional FFT of @var{a} using subroutines from\n" |
|
163 FFTSRC |
|
164 ". The optional arguments @var{n} and @var{m} may be used specify the\n\ |
|
165 number of rows and columns of @var{a} to use. If either of these is\n\ |
|
166 larger than the size of @var{a}, @var{a} is resized and padded with\n\ |
|
167 zeros.\n\ |
|
168 \n\ |
|
169 If @var{a} is a multi-dimensional matrix, each two-dimensional sub-matrix\n\ |
7001
|
170 of @var{a} is treated separately\n\ |
6971
|
171 @seealso {fft2, ifft, ifftn, fftw}\n\ |
5642
|
172 @end deftypefn") |
4773
|
173 { |
4782
|
174 return do_fft2 (args, "ifft2", 1); |
4773
|
175 } |
|
176 |
2928
|
177 /* |
|
178 ;;; Local Variables: *** |
|
179 ;;; mode: C++ *** |
|
180 ;;; End: *** |
|
181 */ |