changelog shortlog tags changeset files revisions annotate raw

src/DLD-FUNCTIONS/fft2.cc

changeset 10289: 4b124317dc38
parent:40dfc0c99116
author: John W. Eaton <jwe@octave.org>
date: Tue Feb 09 20:58:55 2010 -0500 (50 minutes ago)
permissions: -rw-r--r--
description: base_properties::set_children: account for hidden children
1/*
2
3Copyright (C) 1997, 1999, 2002, 2004, 2005, 2006, 2007, 2008,
4 2009 David Bateman
5Copyright (C) 1996, 1997 John W. Eaton
6
7This file is part of Octave.
8
9Octave is free software; you can redistribute it and/or modify it
10under the terms of the GNU General Public License as published by the
11Free Software Foundation; either version 3 of the License, or (at your
12option) any later version.
13
14Octave is distributed in the hope that it will be useful, but WITHOUT
15ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17for more details.
18
19You should have received a copy of the GNU General Public License
20along with Octave; see the file COPYING. If not, see
21<http://www.gnu.org/licenses/>.
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
37// This function should be merged with Fifft.
38
39#if defined (HAVE_FFTW)
40#define FFTSRC "@sc{fftw}"
41#else
42#define FFTSRC "@sc{fftpack}"
43#endif
44
45static octave_value
46do_fft2 (const octave_value_list &args, const char *fcn, int type)
47{
48 octave_value retval;
49
50 int nargin = args.length ();
51
52 if (nargin < 1 || nargin > 3)
53 {
54 print_usage ();
55 return retval;
56 }
57
58 octave_value arg = args(0);
59 dim_vector dims = arg.dims ();
60 octave_idx_type n_rows = -1;
61
62 if (nargin > 1)
63 {
64 double dval = args(1).double_value ();
65 if (xisnan (dval))
66 error ("%s: NaN is invalid as the N_ROWS", fcn);
67 else
68 {
69 n_rows = NINTbig (dval);
70 if (n_rows < 0)
71 error ("%s: number of rows must be greater than zero", fcn);
72 }
73 }
74
75 if (error_state)
76 return retval;
77
78 octave_idx_type n_cols = -1;
79 if (nargin > 2)
80 {
81 double dval = args(2).double_value ();
82 if (xisnan (dval))
83 error ("%s: NaN is invalid as the N_COLS", fcn);
84 else
85 {
86 n_cols = NINTbig (dval);
87 if (n_cols < 0)
88 error ("%s: number of columns must be greater than zero", fcn);
89 }
90 }
91
92 if (error_state)
93 return retval;
94
95 for (int i = 0; i < dims.length (); i++)
96 if (dims(i) < 0)
97 return retval;
98
99 if (n_rows < 0)
100 n_rows = dims (0);
101 else
102 dims (0) = n_rows;
103
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)
110 {
111 if (arg.is_single_type ())
112 return octave_value (FloatMatrix ());
113 else
114 return octave_value (Matrix ());
115 }
116
117 if (arg.is_single_type ())
118 {
119 if (arg.is_real_type ())
120 {
121 FloatNDArray nda = arg.float_array_value ();
122
123 if (! error_state)
124 {
125 nda.resize (dims, 0.0);
126 retval = (type != 0 ? nda.ifourier2d () : nda.fourier2d ());
127 }
128 }
129 else
130 {
131 FloatComplexNDArray cnda = arg.float_complex_array_value ();
132
133 if (! error_state)
134 {
135 cnda.resize (dims, 0.0);
136 retval = (type != 0 ? cnda.ifourier2d () : cnda.fourier2d ());
137 }
138 }
139 }
140 else
141 {
142 if (arg.is_real_type ())
143 {
144 NDArray nda = arg.array_value ();
145
146 if (! error_state)
147 {
148 nda.resize (dims, 0.0);
149 retval = (type != 0 ? nda.ifourier2d () : nda.fourier2d ());
150 }
151 }
152 else if (arg.is_complex_type ())
153 {
154 ComplexNDArray cnda = arg.complex_array_value ();
155
156 if (! error_state)
157 {
158 cnda.resize (dims, 0.0);
159 retval = (type != 0 ? cnda.ifourier2d () : cnda.fourier2d ());
160 }
161 }
162 else
163 {
164 gripe_wrong_type_arg (fcn, arg);
165 }
166 }
167
168 return retval;
169}
170
171DEFUN_DLD (fft2, args, ,
172 "-*- texinfo -*-\n\
173@deftypefn {Loadable Function} {} fft2 (@var{a}, @var{n}, @var{m})\n\
174Compute the two-dimensional FFT of @var{a} using subroutines from\n"
175FFTSRC
176". The optional arguments @var{n} and @var{m} may be used specify the\n\
177number of rows and columns of @var{a} to use. If either of these is\n\
178larger than the size of @var{a}, @var{a} is resized and padded with\n\
179zeros.\n\
180\n\
181If @var{a} is a multi-dimensional matrix, each two-dimensional sub-matrix\n\
182of @var{a} is treated separately\n\
183@seealso {ifft2, fft, fftn, fftw}\n\
184@end deftypefn")
185{
186 return do_fft2 (args, "fft2", 0);
187}
188
189
190DEFUN_DLD (ifft2, args, ,
191 "-*- texinfo -*-\n\
192@deftypefn {Loadable Function} {} ifft2 (@var{a}, @var{n}, @var{m})\n\
193Compute the inverse two-dimensional FFT of @var{a} using subroutines from\n"
194FFTSRC
195". The optional arguments @var{n} and @var{m} may be used specify the\n\
196number of rows and columns of @var{a} to use. If either of these is\n\
197larger than the size of @var{a}, @var{a} is resized and padded with\n\
198zeros.\n\
199\n\
200If @var{a} is a multi-dimensional matrix, each two-dimensional sub-matrix\n\
201of @var{a} is treated separately\n\
202@seealso {fft2, ifft, ifftn, fftw}\n\
203@end deftypefn")
204{
205 return do_fft2 (args, "ifft2", 1);
206}
207
208/*
209
210%% Author: David Billinghurst (David.Billinghurst@riotinto.com.au)
211%% Comalco Research and Technology
212%% 02 May 2000
213%!test
214%! M=16;
215%! N=8;
216%!
217%! m=5;
218%! n=3;
219%!
220%! x = 2*pi*(0:1:M-1)/M;
221%! y = 2*pi*(0:1:N-1)/N;
222%! sx = cos(m*x);
223%! sy = sin(n*y);
224%! s=kron(sx',sy);
225%! S = fft2(s);
226%! answer = kron(fft(sx)',fft(sy));
227%! assert(S, answer, 4*M*N*eps);
228
229%% Author: David Billinghurst (David.Billinghurst@riotinto.com.au)
230%% Comalco Research and Technology
231%% 02 May 2000
232%!test
233%! M=12;
234%! N=7;
235%!
236%! m=3;
237%! n=2;
238%!
239%! x = 2*pi*(0:1:M-1)/M;
240%! y = 2*pi*(0:1:N-1)/N;
241%!
242%! sx = cos(m*x);
243%! sy = cos(n*y);
244%!
245%! S = kron(fft(sx)',fft(sy));
246%! answer=kron(sx',sy);
247%! s = ifft2(S);
248%!
249%! assert(s, answer, 30*eps);
250
251
252%% Author: David Billinghurst (David.Billinghurst@riotinto.com.au)
253%% Comalco Research and Technology
254%% 02 May 2000
255%!test
256%! M=16;
257%! N=8;
258%!
259%! m=5;
260%! n=3;
261%!
262%! x = 2*pi*(0:1:M-1)/M;
263%! y = 2*pi*(0:1:N-1)/N;
264%! sx = single(cos(m*x));
265%! sy = single(sin(n*y));
266%! s=kron(sx',sy);
267%! S = fft2(s);
268%! answer = kron(fft(sx)',fft(sy));
269%! assert(S, answer, 4*M*N*eps('single'));
270
271%% Author: David Billinghurst (David.Billinghurst@riotinto.com.au)
272%% Comalco Research and Technology
273%% 02 May 2000
274%!test
275%! M=12;
276%! N=7;
277%!
278%! m=3;
279%! n=2;
280%!
281%! x = single(2*pi*(0:1:M-1)/M);
282%! y = single(2*pi*(0:1:N-1)/N);
283%!
284%! sx = cos(m*x);
285%! sy = cos(n*y);
286%!
287%! S = kron(fft(sx)',fft(sy));
288%! answer=kron(sx',sy);
289%! s = ifft2(S);
290%!
291%! assert(s, answer, 30*eps('single'));
292
293*/