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