Chris@10
|
1 /*
|
Chris@10
|
2 * Copyright (c) 2003, 2007-11 Matteo Frigo
|
Chris@10
|
3 * Copyright (c) 2003, 2007-11 Massachusetts Institute of Technology
|
Chris@10
|
4 *
|
Chris@10
|
5 * This program is free software; you can redistribute it and/or modify
|
Chris@10
|
6 * it under the terms of the GNU General Public License as published by
|
Chris@10
|
7 * the Free Software Foundation; either version 2 of the License, or
|
Chris@10
|
8 * (at your option) any later version.
|
Chris@10
|
9 *
|
Chris@10
|
10 * This program is distributed in the hope that it will be useful,
|
Chris@10
|
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
Chris@10
|
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
Chris@10
|
13 * GNU General Public License for more details.
|
Chris@10
|
14 *
|
Chris@10
|
15 * You should have received a copy of the GNU General Public License
|
Chris@10
|
16 * along with this program; if not, write to the Free Software
|
Chris@10
|
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
Chris@10
|
18 *
|
Chris@10
|
19 */
|
Chris@10
|
20
|
Chris@10
|
21
|
Chris@10
|
22 /* Do an R{E,O}DFT{01,10} problem via an R2HC problem, with some
|
Chris@10
|
23 pre/post-processing ala FFTPACK. */
|
Chris@10
|
24
|
Chris@10
|
25 #include "reodft.h"
|
Chris@10
|
26
|
Chris@10
|
27 typedef struct {
|
Chris@10
|
28 solver super;
|
Chris@10
|
29 } S;
|
Chris@10
|
30
|
Chris@10
|
31 typedef struct {
|
Chris@10
|
32 plan_rdft super;
|
Chris@10
|
33 plan *cld;
|
Chris@10
|
34 twid *td;
|
Chris@10
|
35 INT is, os;
|
Chris@10
|
36 INT n;
|
Chris@10
|
37 INT vl;
|
Chris@10
|
38 INT ivs, ovs;
|
Chris@10
|
39 rdft_kind kind;
|
Chris@10
|
40 } P;
|
Chris@10
|
41
|
Chris@10
|
42 /* A real-even-01 DFT operates logically on a size-4N array:
|
Chris@10
|
43 I 0 -r(I*) -I 0 r(I*),
|
Chris@10
|
44 where r denotes reversal and * denotes deletion of the 0th element.
|
Chris@10
|
45 To compute the transform of this, we imagine performing a radix-4
|
Chris@10
|
46 (real-input) DIF step, which turns the size-4N DFT into 4 size-N
|
Chris@10
|
47 (contiguous) DFTs, two of which are zero and two of which are
|
Chris@10
|
48 conjugates. The non-redundant size-N DFT has halfcomplex input, so
|
Chris@10
|
49 we can do it with a size-N hc2r transform. (In order to share
|
Chris@10
|
50 plans with the re10 (inverse) transform, however, we use the DHT
|
Chris@10
|
51 trick to re-express the hc2r problem as r2hc. This has little cost
|
Chris@10
|
52 since we are already pre- and post-processing the data in {i,n-i}
|
Chris@10
|
53 order.) Finally, we have to write out the data in the correct
|
Chris@10
|
54 order...the two size-N redundant (conjugate) hc2r DFTs correspond
|
Chris@10
|
55 to the even and odd outputs in O (i.e. the usual interleaved output
|
Chris@10
|
56 of DIF transforms); since this data has even symmetry, we only
|
Chris@10
|
57 write the first half of it.
|
Chris@10
|
58
|
Chris@10
|
59 The real-even-10 DFT is just the reverse of these steps, i.e. a
|
Chris@10
|
60 radix-4 DIT transform. There, however, we just use the r2hc
|
Chris@10
|
61 transform naturally without resorting to the DHT trick.
|
Chris@10
|
62
|
Chris@10
|
63 A real-odd-01 DFT is very similar, except that the input is
|
Chris@10
|
64 0 I (rI)* 0 -I -(rI)*. This format, however, can be transformed
|
Chris@10
|
65 into precisely the real-even-01 format above by sending I -> rI
|
Chris@10
|
66 and shifting the array by N. The former swap is just another
|
Chris@10
|
67 transformation on the input during preprocessing; the latter
|
Chris@10
|
68 multiplies the even/odd outputs by i/-i, which combines with
|
Chris@10
|
69 the factor of -i (to take the imaginary part) to simply flip
|
Chris@10
|
70 the sign of the odd outputs. Vice-versa for real-odd-10.
|
Chris@10
|
71
|
Chris@10
|
72 The FFTPACK source code was very helpful in working this out.
|
Chris@10
|
73 (They do unnecessary passes over the array, though.) The same
|
Chris@10
|
74 algorithm is also described in:
|
Chris@10
|
75
|
Chris@10
|
76 John Makhoul, "A fast cosine transform in one and two dimensions,"
|
Chris@10
|
77 IEEE Trans. on Acoust. Speech and Sig. Proc., ASSP-28 (1), 27--34 (1980).
|
Chris@10
|
78
|
Chris@10
|
79 Note that Numerical Recipes suggests a different algorithm that
|
Chris@10
|
80 requires more operations and uses trig. functions for both the pre-
|
Chris@10
|
81 and post-processing passes.
|
Chris@10
|
82 */
|
Chris@10
|
83
|
Chris@10
|
84 static void apply_re01(const plan *ego_, R *I, R *O)
|
Chris@10
|
85 {
|
Chris@10
|
86 const P *ego = (const P *) ego_;
|
Chris@10
|
87 INT is = ego->is, os = ego->os;
|
Chris@10
|
88 INT i, n = ego->n;
|
Chris@10
|
89 INT iv, vl = ego->vl;
|
Chris@10
|
90 INT ivs = ego->ivs, ovs = ego->ovs;
|
Chris@10
|
91 R *W = ego->td->W;
|
Chris@10
|
92 R *buf;
|
Chris@10
|
93
|
Chris@10
|
94 buf = (R *) MALLOC(sizeof(R) * n, BUFFERS);
|
Chris@10
|
95
|
Chris@10
|
96 for (iv = 0; iv < vl; ++iv, I += ivs, O += ovs) {
|
Chris@10
|
97 buf[0] = I[0];
|
Chris@10
|
98 for (i = 1; i < n - i; ++i) {
|
Chris@10
|
99 E a, b, apb, amb, wa, wb;
|
Chris@10
|
100 a = I[is * i];
|
Chris@10
|
101 b = I[is * (n - i)];
|
Chris@10
|
102 apb = a + b;
|
Chris@10
|
103 amb = a - b;
|
Chris@10
|
104 wa = W[2*i];
|
Chris@10
|
105 wb = W[2*i + 1];
|
Chris@10
|
106 buf[i] = wa * amb + wb * apb;
|
Chris@10
|
107 buf[n - i] = wa * apb - wb * amb;
|
Chris@10
|
108 }
|
Chris@10
|
109 if (i == n - i) {
|
Chris@10
|
110 buf[i] = K(2.0) * I[is * i] * W[2*i];
|
Chris@10
|
111 }
|
Chris@10
|
112
|
Chris@10
|
113 {
|
Chris@10
|
114 plan_rdft *cld = (plan_rdft *) ego->cld;
|
Chris@10
|
115 cld->apply((plan *) cld, buf, buf);
|
Chris@10
|
116 }
|
Chris@10
|
117
|
Chris@10
|
118 O[0] = buf[0];
|
Chris@10
|
119 for (i = 1; i < n - i; ++i) {
|
Chris@10
|
120 E a, b;
|
Chris@10
|
121 INT k;
|
Chris@10
|
122 a = buf[i];
|
Chris@10
|
123 b = buf[n - i];
|
Chris@10
|
124 k = i + i;
|
Chris@10
|
125 O[os * (k - 1)] = a - b;
|
Chris@10
|
126 O[os * k] = a + b;
|
Chris@10
|
127 }
|
Chris@10
|
128 if (i == n - i) {
|
Chris@10
|
129 O[os * (n - 1)] = buf[i];
|
Chris@10
|
130 }
|
Chris@10
|
131 }
|
Chris@10
|
132
|
Chris@10
|
133 X(ifree)(buf);
|
Chris@10
|
134 }
|
Chris@10
|
135
|
Chris@10
|
136 /* ro01 is same as re01, but with i <-> n - 1 - i in the input and
|
Chris@10
|
137 the sign of the odd output elements flipped. */
|
Chris@10
|
138 static void apply_ro01(const plan *ego_, R *I, R *O)
|
Chris@10
|
139 {
|
Chris@10
|
140 const P *ego = (const P *) ego_;
|
Chris@10
|
141 INT is = ego->is, os = ego->os;
|
Chris@10
|
142 INT i, n = ego->n;
|
Chris@10
|
143 INT iv, vl = ego->vl;
|
Chris@10
|
144 INT ivs = ego->ivs, ovs = ego->ovs;
|
Chris@10
|
145 R *W = ego->td->W;
|
Chris@10
|
146 R *buf;
|
Chris@10
|
147
|
Chris@10
|
148 buf = (R *) MALLOC(sizeof(R) * n, BUFFERS);
|
Chris@10
|
149
|
Chris@10
|
150 for (iv = 0; iv < vl; ++iv, I += ivs, O += ovs) {
|
Chris@10
|
151 buf[0] = I[is * (n - 1)];
|
Chris@10
|
152 for (i = 1; i < n - i; ++i) {
|
Chris@10
|
153 E a, b, apb, amb, wa, wb;
|
Chris@10
|
154 a = I[is * (n - 1 - i)];
|
Chris@10
|
155 b = I[is * (i - 1)];
|
Chris@10
|
156 apb = a + b;
|
Chris@10
|
157 amb = a - b;
|
Chris@10
|
158 wa = W[2*i];
|
Chris@10
|
159 wb = W[2*i+1];
|
Chris@10
|
160 buf[i] = wa * amb + wb * apb;
|
Chris@10
|
161 buf[n - i] = wa * apb - wb * amb;
|
Chris@10
|
162 }
|
Chris@10
|
163 if (i == n - i) {
|
Chris@10
|
164 buf[i] = K(2.0) * I[is * (i - 1)] * W[2*i];
|
Chris@10
|
165 }
|
Chris@10
|
166
|
Chris@10
|
167 {
|
Chris@10
|
168 plan_rdft *cld = (plan_rdft *) ego->cld;
|
Chris@10
|
169 cld->apply((plan *) cld, buf, buf);
|
Chris@10
|
170 }
|
Chris@10
|
171
|
Chris@10
|
172 O[0] = buf[0];
|
Chris@10
|
173 for (i = 1; i < n - i; ++i) {
|
Chris@10
|
174 E a, b;
|
Chris@10
|
175 INT k;
|
Chris@10
|
176 a = buf[i];
|
Chris@10
|
177 b = buf[n - i];
|
Chris@10
|
178 k = i + i;
|
Chris@10
|
179 O[os * (k - 1)] = b - a;
|
Chris@10
|
180 O[os * k] = a + b;
|
Chris@10
|
181 }
|
Chris@10
|
182 if (i == n - i) {
|
Chris@10
|
183 O[os * (n - 1)] = -buf[i];
|
Chris@10
|
184 }
|
Chris@10
|
185 }
|
Chris@10
|
186
|
Chris@10
|
187 X(ifree)(buf);
|
Chris@10
|
188 }
|
Chris@10
|
189
|
Chris@10
|
190 static void apply_re10(const plan *ego_, R *I, R *O)
|
Chris@10
|
191 {
|
Chris@10
|
192 const P *ego = (const P *) ego_;
|
Chris@10
|
193 INT is = ego->is, os = ego->os;
|
Chris@10
|
194 INT i, n = ego->n;
|
Chris@10
|
195 INT iv, vl = ego->vl;
|
Chris@10
|
196 INT ivs = ego->ivs, ovs = ego->ovs;
|
Chris@10
|
197 R *W = ego->td->W;
|
Chris@10
|
198 R *buf;
|
Chris@10
|
199
|
Chris@10
|
200 buf = (R *) MALLOC(sizeof(R) * n, BUFFERS);
|
Chris@10
|
201
|
Chris@10
|
202 for (iv = 0; iv < vl; ++iv, I += ivs, O += ovs) {
|
Chris@10
|
203 buf[0] = I[0];
|
Chris@10
|
204 for (i = 1; i < n - i; ++i) {
|
Chris@10
|
205 E u, v;
|
Chris@10
|
206 INT k = i + i;
|
Chris@10
|
207 u = I[is * (k - 1)];
|
Chris@10
|
208 v = I[is * k];
|
Chris@10
|
209 buf[n - i] = u;
|
Chris@10
|
210 buf[i] = v;
|
Chris@10
|
211 }
|
Chris@10
|
212 if (i == n - i) {
|
Chris@10
|
213 buf[i] = I[is * (n - 1)];
|
Chris@10
|
214 }
|
Chris@10
|
215
|
Chris@10
|
216 {
|
Chris@10
|
217 plan_rdft *cld = (plan_rdft *) ego->cld;
|
Chris@10
|
218 cld->apply((plan *) cld, buf, buf);
|
Chris@10
|
219 }
|
Chris@10
|
220
|
Chris@10
|
221 O[0] = K(2.0) * buf[0];
|
Chris@10
|
222 for (i = 1; i < n - i; ++i) {
|
Chris@10
|
223 E a, b, wa, wb;
|
Chris@10
|
224 a = K(2.0) * buf[i];
|
Chris@10
|
225 b = K(2.0) * buf[n - i];
|
Chris@10
|
226 wa = W[2*i];
|
Chris@10
|
227 wb = W[2*i + 1];
|
Chris@10
|
228 O[os * i] = wa * a + wb * b;
|
Chris@10
|
229 O[os * (n - i)] = wb * a - wa * b;
|
Chris@10
|
230 }
|
Chris@10
|
231 if (i == n - i) {
|
Chris@10
|
232 O[os * i] = K(2.0) * buf[i] * W[2*i];
|
Chris@10
|
233 }
|
Chris@10
|
234 }
|
Chris@10
|
235
|
Chris@10
|
236 X(ifree)(buf);
|
Chris@10
|
237 }
|
Chris@10
|
238
|
Chris@10
|
239 /* ro10 is same as re10, but with i <-> n - 1 - i in the output and
|
Chris@10
|
240 the sign of the odd input elements flipped. */
|
Chris@10
|
241 static void apply_ro10(const plan *ego_, R *I, R *O)
|
Chris@10
|
242 {
|
Chris@10
|
243 const P *ego = (const P *) ego_;
|
Chris@10
|
244 INT is = ego->is, os = ego->os;
|
Chris@10
|
245 INT i, n = ego->n;
|
Chris@10
|
246 INT iv, vl = ego->vl;
|
Chris@10
|
247 INT ivs = ego->ivs, ovs = ego->ovs;
|
Chris@10
|
248 R *W = ego->td->W;
|
Chris@10
|
249 R *buf;
|
Chris@10
|
250
|
Chris@10
|
251 buf = (R *) MALLOC(sizeof(R) * n, BUFFERS);
|
Chris@10
|
252
|
Chris@10
|
253 for (iv = 0; iv < vl; ++iv, I += ivs, O += ovs) {
|
Chris@10
|
254 buf[0] = I[0];
|
Chris@10
|
255 for (i = 1; i < n - i; ++i) {
|
Chris@10
|
256 E u, v;
|
Chris@10
|
257 INT k = i + i;
|
Chris@10
|
258 u = -I[is * (k - 1)];
|
Chris@10
|
259 v = I[is * k];
|
Chris@10
|
260 buf[n - i] = u;
|
Chris@10
|
261 buf[i] = v;
|
Chris@10
|
262 }
|
Chris@10
|
263 if (i == n - i) {
|
Chris@10
|
264 buf[i] = -I[is * (n - 1)];
|
Chris@10
|
265 }
|
Chris@10
|
266
|
Chris@10
|
267 {
|
Chris@10
|
268 plan_rdft *cld = (plan_rdft *) ego->cld;
|
Chris@10
|
269 cld->apply((plan *) cld, buf, buf);
|
Chris@10
|
270 }
|
Chris@10
|
271
|
Chris@10
|
272 O[os * (n - 1)] = K(2.0) * buf[0];
|
Chris@10
|
273 for (i = 1; i < n - i; ++i) {
|
Chris@10
|
274 E a, b, wa, wb;
|
Chris@10
|
275 a = K(2.0) * buf[i];
|
Chris@10
|
276 b = K(2.0) * buf[n - i];
|
Chris@10
|
277 wa = W[2*i];
|
Chris@10
|
278 wb = W[2*i + 1];
|
Chris@10
|
279 O[os * (n - 1 - i)] = wa * a + wb * b;
|
Chris@10
|
280 O[os * (i - 1)] = wb * a - wa * b;
|
Chris@10
|
281 }
|
Chris@10
|
282 if (i == n - i) {
|
Chris@10
|
283 O[os * (i - 1)] = K(2.0) * buf[i] * W[2*i];
|
Chris@10
|
284 }
|
Chris@10
|
285 }
|
Chris@10
|
286
|
Chris@10
|
287 X(ifree)(buf);
|
Chris@10
|
288 }
|
Chris@10
|
289
|
Chris@10
|
290 static void awake(plan *ego_, enum wakefulness wakefulness)
|
Chris@10
|
291 {
|
Chris@10
|
292 P *ego = (P *) ego_;
|
Chris@10
|
293 static const tw_instr reodft010e_tw[] = {
|
Chris@10
|
294 { TW_COS, 0, 1 },
|
Chris@10
|
295 { TW_SIN, 0, 1 },
|
Chris@10
|
296 { TW_NEXT, 1, 0 }
|
Chris@10
|
297 };
|
Chris@10
|
298
|
Chris@10
|
299 X(plan_awake)(ego->cld, wakefulness);
|
Chris@10
|
300
|
Chris@10
|
301 X(twiddle_awake)(wakefulness, &ego->td, reodft010e_tw,
|
Chris@10
|
302 4*ego->n, 1, ego->n/2+1);
|
Chris@10
|
303 }
|
Chris@10
|
304
|
Chris@10
|
305 static void destroy(plan *ego_)
|
Chris@10
|
306 {
|
Chris@10
|
307 P *ego = (P *) ego_;
|
Chris@10
|
308 X(plan_destroy_internal)(ego->cld);
|
Chris@10
|
309 }
|
Chris@10
|
310
|
Chris@10
|
311 static void print(const plan *ego_, printer *p)
|
Chris@10
|
312 {
|
Chris@10
|
313 const P *ego = (const P *) ego_;
|
Chris@10
|
314 p->print(p, "(%se-r2hc-%D%v%(%p%))",
|
Chris@10
|
315 X(rdft_kind_str)(ego->kind), ego->n, ego->vl, ego->cld);
|
Chris@10
|
316 }
|
Chris@10
|
317
|
Chris@10
|
318 static int applicable0(const solver *ego_, const problem *p_)
|
Chris@10
|
319 {
|
Chris@10
|
320 const problem_rdft *p = (const problem_rdft *) p_;
|
Chris@10
|
321 UNUSED(ego_);
|
Chris@10
|
322
|
Chris@10
|
323 return (1
|
Chris@10
|
324 && p->sz->rnk == 1
|
Chris@10
|
325 && p->vecsz->rnk <= 1
|
Chris@10
|
326 && (p->kind[0] == REDFT01 || p->kind[0] == REDFT10
|
Chris@10
|
327 || p->kind[0] == RODFT01 || p->kind[0] == RODFT10)
|
Chris@10
|
328 );
|
Chris@10
|
329 }
|
Chris@10
|
330
|
Chris@10
|
331 static int applicable(const solver *ego, const problem *p, const planner *plnr)
|
Chris@10
|
332 {
|
Chris@10
|
333 return (!NO_SLOWP(plnr) && applicable0(ego, p));
|
Chris@10
|
334 }
|
Chris@10
|
335
|
Chris@10
|
336 static plan *mkplan(const solver *ego_, const problem *p_, planner *plnr)
|
Chris@10
|
337 {
|
Chris@10
|
338 P *pln;
|
Chris@10
|
339 const problem_rdft *p;
|
Chris@10
|
340 plan *cld;
|
Chris@10
|
341 R *buf;
|
Chris@10
|
342 INT n;
|
Chris@10
|
343 opcnt ops;
|
Chris@10
|
344
|
Chris@10
|
345 static const plan_adt padt = {
|
Chris@10
|
346 X(rdft_solve), awake, print, destroy
|
Chris@10
|
347 };
|
Chris@10
|
348
|
Chris@10
|
349 if (!applicable(ego_, p_, plnr))
|
Chris@10
|
350 return (plan *)0;
|
Chris@10
|
351
|
Chris@10
|
352 p = (const problem_rdft *) p_;
|
Chris@10
|
353
|
Chris@10
|
354 n = p->sz->dims[0].n;
|
Chris@10
|
355 buf = (R *) MALLOC(sizeof(R) * n, BUFFERS);
|
Chris@10
|
356
|
Chris@10
|
357 cld = X(mkplan_d)(plnr, X(mkproblem_rdft_1_d)(X(mktensor_1d)(n, 1, 1),
|
Chris@10
|
358 X(mktensor_0d)(),
|
Chris@10
|
359 buf, buf, R2HC));
|
Chris@10
|
360 X(ifree)(buf);
|
Chris@10
|
361 if (!cld)
|
Chris@10
|
362 return (plan *)0;
|
Chris@10
|
363
|
Chris@10
|
364 switch (p->kind[0]) {
|
Chris@10
|
365 case REDFT01: pln = MKPLAN_RDFT(P, &padt, apply_re01); break;
|
Chris@10
|
366 case REDFT10: pln = MKPLAN_RDFT(P, &padt, apply_re10); break;
|
Chris@10
|
367 case RODFT01: pln = MKPLAN_RDFT(P, &padt, apply_ro01); break;
|
Chris@10
|
368 case RODFT10: pln = MKPLAN_RDFT(P, &padt, apply_ro10); break;
|
Chris@10
|
369 default: A(0); return (plan*)0;
|
Chris@10
|
370 }
|
Chris@10
|
371
|
Chris@10
|
372 pln->n = n;
|
Chris@10
|
373 pln->is = p->sz->dims[0].is;
|
Chris@10
|
374 pln->os = p->sz->dims[0].os;
|
Chris@10
|
375 pln->cld = cld;
|
Chris@10
|
376 pln->td = 0;
|
Chris@10
|
377 pln->kind = p->kind[0];
|
Chris@10
|
378
|
Chris@10
|
379 X(tensor_tornk1)(p->vecsz, &pln->vl, &pln->ivs, &pln->ovs);
|
Chris@10
|
380
|
Chris@10
|
381 X(ops_zero)(&ops);
|
Chris@10
|
382 ops.other = 4 + (n-1)/2 * 10 + (1 - n % 2) * 5;
|
Chris@10
|
383 if (p->kind[0] == REDFT01 || p->kind[0] == RODFT01) {
|
Chris@10
|
384 ops.add = (n-1)/2 * 6;
|
Chris@10
|
385 ops.mul = (n-1)/2 * 4 + (1 - n % 2) * 2;
|
Chris@10
|
386 }
|
Chris@10
|
387 else { /* 10 transforms */
|
Chris@10
|
388 ops.add = (n-1)/2 * 2;
|
Chris@10
|
389 ops.mul = 1 + (n-1)/2 * 6 + (1 - n % 2) * 2;
|
Chris@10
|
390 }
|
Chris@10
|
391
|
Chris@10
|
392 X(ops_zero)(&pln->super.super.ops);
|
Chris@10
|
393 X(ops_madd2)(pln->vl, &ops, &pln->super.super.ops);
|
Chris@10
|
394 X(ops_madd2)(pln->vl, &cld->ops, &pln->super.super.ops);
|
Chris@10
|
395
|
Chris@10
|
396 return &(pln->super.super);
|
Chris@10
|
397 }
|
Chris@10
|
398
|
Chris@10
|
399 /* constructor */
|
Chris@10
|
400 static solver *mksolver(void)
|
Chris@10
|
401 {
|
Chris@10
|
402 static const solver_adt sadt = { PROBLEM_RDFT, mkplan, 0 };
|
Chris@10
|
403 S *slv = MKSOLVER(S, &sadt);
|
Chris@10
|
404 return &(slv->super);
|
Chris@10
|
405 }
|
Chris@10
|
406
|
Chris@10
|
407 void X(reodft010e_r2hc_register)(planner *p)
|
Chris@10
|
408 {
|
Chris@10
|
409 REGISTER_SOLVER(p, mksolver());
|
Chris@10
|
410 }
|