4773
|
1 /* |
|
2 |
|
3 Copyright (C) 2004 David Bateman |
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
5307
|
19 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
20 02110-1301, USA. |
4773
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include <config.h> |
|
26 #endif |
|
27 |
|
28 #include "lo-mappers.h" |
|
29 |
|
30 #include "defun-dld.h" |
|
31 #include "error.h" |
|
32 #include "gripes.h" |
|
33 #include "oct-obj.h" |
|
34 #include "utils.h" |
|
35 |
|
36 // This function should be merged with Fifft. |
|
37 |
|
38 #if defined (HAVE_FFTW3) |
|
39 #define FFTSRC "@sc{Fftw}" |
|
40 #else |
|
41 #define FFTSRC "@sc{Fftpack}" |
|
42 #endif |
|
43 |
|
44 static octave_value |
|
45 do_fftn (const octave_value_list &args, const char *fcn, int type) |
|
46 { |
|
47 octave_value retval; |
|
48 |
|
49 int nargin = args.length (); |
|
50 |
|
51 if (nargin < 1 || nargin > 2) |
|
52 { |
5823
|
53 print_usage (); |
4773
|
54 return retval; |
|
55 } |
|
56 |
|
57 octave_value arg = args(0); |
|
58 dim_vector dims = arg.dims (); |
|
59 |
|
60 for (int i = 0; i < dims.length (); i++) |
|
61 if (dims(i) < 0) |
|
62 return retval; |
|
63 |
|
64 if (nargin > 1) |
|
65 { |
|
66 Matrix val = args(1).matrix_value (); |
|
67 if (val.rows () > val.columns ()) |
4849
|
68 val = val.transpose (); |
4773
|
69 |
4849
|
70 if (error_state || val.columns () != dims.length () || val.rows () != 1) |
4773
|
71 error ("%s: second argument must be a vector of length dim", fcn); |
|
72 else |
|
73 { |
|
74 for (int i = 0; i < dims.length (); i++) |
|
75 { |
|
76 if (xisnan (val(i,0))) |
|
77 error ("%s: NaN is invalid as a dimension", fcn); |
5275
|
78 else if (NINTbig (val(i,0)) < 0) |
4773
|
79 error ("%s: all dimension must be greater than zero", fcn); |
|
80 else |
|
81 { |
5275
|
82 dims(i) = NINTbig(val(i,0)); |
4773
|
83 } |
|
84 } |
|
85 } |
|
86 } |
|
87 |
|
88 if (error_state) |
|
89 return retval; |
|
90 |
|
91 if (dims.all_zero ()) |
|
92 return octave_value (Matrix ()); |
|
93 |
|
94 if (arg.is_real_type ()) |
|
95 { |
|
96 NDArray nda = arg.array_value (); |
|
97 |
|
98 if (! error_state) |
|
99 { |
|
100 nda.resize (dims, 0.0); |
|
101 retval = (type != 0 ? nda.ifourierNd () : nda.fourierNd ()); |
|
102 } |
|
103 } |
|
104 else if (arg.is_complex_type ()) |
|
105 { |
|
106 ComplexNDArray cnda = arg.complex_array_value (); |
|
107 |
|
108 if (! error_state) |
|
109 { |
|
110 cnda.resize (dims, 0.0); |
|
111 retval = (type != 0 ? cnda.ifourierNd () : cnda.fourierNd ()); |
|
112 } |
|
113 } |
|
114 else |
|
115 { |
|
116 gripe_wrong_type_arg (fcn, arg); |
|
117 } |
|
118 |
|
119 return retval; |
|
120 } |
|
121 |
|
122 DEFUN_DLD (fftn, args, , |
|
123 "-*- texinfo -*-\n\ |
4787
|
124 @deftypefn {Loadable Function} {} fftn (@var{a}, @var{size})\n\ |
4773
|
125 Compute the N dimensional FFT of @var{a} using subroutines from\n" |
|
126 FFTSRC |
4787
|
127 ". The optional vector argument @var{size} may be used specify the\n\ |
|
128 dimensions of the array to be used. If an element of @var{size} is\n\ |
4773
|
129 smaller than the corresponding dimension, then the dimension is\n\ |
|
130 truncated prior to performing the FFT. Otherwise if an element\n\ |
4787
|
131 of @var{size} is larger than the corresponding dimension @var{a}\n\ |
4773
|
132 is resized and padded with zeros.\n\ |
6971
|
133 @seealso {ifftn, fft, fft2, fftw}\n\ |
5642
|
134 @end deftypefn") |
4773
|
135 { |
4782
|
136 return do_fftn (args, "fftn", 0); |
4773
|
137 } |
|
138 |
|
139 DEFUN_DLD (ifftn, args, , |
|
140 "-*- texinfo -*-\n\ |
4787
|
141 @deftypefn {Loadable Function} {} ifftn (@var{a}, @var{size})\n\ |
7007
|
142 Compute the inverse N dimensional FFT of @var{a} using subroutines from\n" |
4773
|
143 FFTSRC |
4787
|
144 ". The optional vector argument @var{size} may be used specify the\n\ |
|
145 dimensions of the array to be used. If an element of @var{size} is\n\ |
4773
|
146 smaller than the corresponding dimension, then the dimension is\n\ |
|
147 truncated prior to performing the inverse FFT. Otherwise if an element\n\ |
4787
|
148 of @var{size} is larger than the corresponding dimension @var{a}\n\ |
4773
|
149 is resized and padded with zeros.\n\ |
6971
|
150 @seealso {fftn, ifft, ifft2, fftw}\n\ |
5642
|
151 @end deftypefn") |
4773
|
152 { |
4782
|
153 return do_fftn (args, "ifftn", 1); |
4773
|
154 } |
|
155 |
|
156 /* |
|
157 ;;; Local Variables: *** |
|
158 ;;; mode: C++ *** |
|
159 ;;; End: *** |
|
160 */ |