Mercurial > hg > js-dsp-test
comparison fft/fftw/fftw-3.3.4/reodft/reodft11e-r2hc.c @ 19:26056e866c29
Add FFTW to comparison table
author | Chris Cannam |
---|---|
date | Tue, 06 Oct 2015 13:08:39 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
18:8db794ca3e0b | 19:26056e866c29 |
---|---|
1 /* | |
2 * Copyright (c) 2003, 2007-14 Matteo Frigo | |
3 * Copyright (c) 2003, 2007-14 Massachusetts Institute of Technology | |
4 * | |
5 * This program is free software; you can redistribute it and/or modify | |
6 * it under the terms of the GNU General Public License as published by | |
7 * the Free Software Foundation; either version 2 of the License, or | |
8 * (at your option) any later version. | |
9 * | |
10 * This program is distributed in the hope that it will be useful, | |
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 * GNU General Public License for more details. | |
14 * | |
15 * You should have received a copy of the GNU General Public License | |
16 * along with this program; if not, write to the Free Software | |
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
18 * | |
19 */ | |
20 | |
21 | |
22 /* Do an R{E,O}DFT11 problem via an R2HC problem, with some | |
23 pre/post-processing ala FFTPACK. Use a trick from: | |
24 | |
25 S. C. Chan and K. L. Ho, "Direct methods for computing discrete | |
26 sinusoidal transforms," IEE Proceedings F 137 (6), 433--442 (1990). | |
27 | |
28 to re-express as an REDFT01 (DCT-III) problem. | |
29 | |
30 NOTE: We no longer use this algorithm, because it turns out to suffer | |
31 a catastrophic loss of accuracy for certain inputs, apparently because | |
32 its post-processing multiplies the output by a cosine. Near the zero | |
33 of the cosine, the REDFT01 must produce a near-singular output. | |
34 */ | |
35 | |
36 #include "reodft.h" | |
37 | |
38 typedef struct { | |
39 solver super; | |
40 } S; | |
41 | |
42 typedef struct { | |
43 plan_rdft super; | |
44 plan *cld; | |
45 twid *td, *td2; | |
46 INT is, os; | |
47 INT n; | |
48 INT vl; | |
49 INT ivs, ovs; | |
50 rdft_kind kind; | |
51 } P; | |
52 | |
53 static void apply_re11(const plan *ego_, R *I, R *O) | |
54 { | |
55 const P *ego = (const P *) ego_; | |
56 INT is = ego->is, os = ego->os; | |
57 INT i, n = ego->n; | |
58 INT iv, vl = ego->vl; | |
59 INT ivs = ego->ivs, ovs = ego->ovs; | |
60 R *W; | |
61 R *buf; | |
62 E cur; | |
63 | |
64 buf = (R *) MALLOC(sizeof(R) * n, BUFFERS); | |
65 | |
66 for (iv = 0; iv < vl; ++iv, I += ivs, O += ovs) { | |
67 /* I wish that this didn't require an extra pass. */ | |
68 /* FIXME: use recursive/cascade summation for better stability? */ | |
69 buf[n - 1] = cur = K(2.0) * I[is * (n - 1)]; | |
70 for (i = n - 1; i > 0; --i) { | |
71 E curnew; | |
72 buf[(i - 1)] = curnew = K(2.0) * I[is * (i - 1)] - cur; | |
73 cur = curnew; | |
74 } | |
75 | |
76 W = ego->td->W; | |
77 for (i = 1; i < n - i; ++i) { | |
78 E a, b, apb, amb, wa, wb; | |
79 a = buf[i]; | |
80 b = buf[n - i]; | |
81 apb = a + b; | |
82 amb = a - b; | |
83 wa = W[2*i]; | |
84 wb = W[2*i + 1]; | |
85 buf[i] = wa * amb + wb * apb; | |
86 buf[n - i] = wa * apb - wb * amb; | |
87 } | |
88 if (i == n - i) { | |
89 buf[i] = K(2.0) * buf[i] * W[2*i]; | |
90 } | |
91 | |
92 { | |
93 plan_rdft *cld = (plan_rdft *) ego->cld; | |
94 cld->apply((plan *) cld, buf, buf); | |
95 } | |
96 | |
97 W = ego->td2->W; | |
98 O[0] = W[0] * buf[0]; | |
99 for (i = 1; i < n - i; ++i) { | |
100 E a, b; | |
101 INT k; | |
102 a = buf[i]; | |
103 b = buf[n - i]; | |
104 k = i + i; | |
105 O[os * (k - 1)] = W[k - 1] * (a - b); | |
106 O[os * k] = W[k] * (a + b); | |
107 } | |
108 if (i == n - i) { | |
109 O[os * (n - 1)] = W[n - 1] * buf[i]; | |
110 } | |
111 } | |
112 | |
113 X(ifree)(buf); | |
114 } | |
115 | |
116 /* like for rodft01, rodft11 is obtained from redft11 by | |
117 reversing the input and flipping the sign of every other output. */ | |
118 static void apply_ro11(const plan *ego_, R *I, R *O) | |
119 { | |
120 const P *ego = (const P *) ego_; | |
121 INT is = ego->is, os = ego->os; | |
122 INT i, n = ego->n; | |
123 INT iv, vl = ego->vl; | |
124 INT ivs = ego->ivs, ovs = ego->ovs; | |
125 R *W; | |
126 R *buf; | |
127 E cur; | |
128 | |
129 buf = (R *) MALLOC(sizeof(R) * n, BUFFERS); | |
130 | |
131 for (iv = 0; iv < vl; ++iv, I += ivs, O += ovs) { | |
132 /* I wish that this didn't require an extra pass. */ | |
133 /* FIXME: use recursive/cascade summation for better stability? */ | |
134 buf[n - 1] = cur = K(2.0) * I[0]; | |
135 for (i = n - 1; i > 0; --i) { | |
136 E curnew; | |
137 buf[(i - 1)] = curnew = K(2.0) * I[is * (n - i)] - cur; | |
138 cur = curnew; | |
139 } | |
140 | |
141 W = ego->td->W; | |
142 for (i = 1; i < n - i; ++i) { | |
143 E a, b, apb, amb, wa, wb; | |
144 a = buf[i]; | |
145 b = buf[n - i]; | |
146 apb = a + b; | |
147 amb = a - b; | |
148 wa = W[2*i]; | |
149 wb = W[2*i + 1]; | |
150 buf[i] = wa * amb + wb * apb; | |
151 buf[n - i] = wa * apb - wb * amb; | |
152 } | |
153 if (i == n - i) { | |
154 buf[i] = K(2.0) * buf[i] * W[2*i]; | |
155 } | |
156 | |
157 { | |
158 plan_rdft *cld = (plan_rdft *) ego->cld; | |
159 cld->apply((plan *) cld, buf, buf); | |
160 } | |
161 | |
162 W = ego->td2->W; | |
163 O[0] = W[0] * buf[0]; | |
164 for (i = 1; i < n - i; ++i) { | |
165 E a, b; | |
166 INT k; | |
167 a = buf[i]; | |
168 b = buf[n - i]; | |
169 k = i + i; | |
170 O[os * (k - 1)] = W[k - 1] * (b - a); | |
171 O[os * k] = W[k] * (a + b); | |
172 } | |
173 if (i == n - i) { | |
174 O[os * (n - 1)] = -W[n - 1] * buf[i]; | |
175 } | |
176 } | |
177 | |
178 X(ifree)(buf); | |
179 } | |
180 | |
181 static void awake(plan *ego_, enum wakefulness wakefulness) | |
182 { | |
183 P *ego = (P *) ego_; | |
184 static const tw_instr reodft010e_tw[] = { | |
185 { TW_COS, 0, 1 }, | |
186 { TW_SIN, 0, 1 }, | |
187 { TW_NEXT, 1, 0 } | |
188 }; | |
189 static const tw_instr reodft11e_tw[] = { | |
190 { TW_COS, 1, 1 }, | |
191 { TW_NEXT, 2, 0 } | |
192 }; | |
193 | |
194 X(plan_awake)(ego->cld, wakefulness); | |
195 | |
196 X(twiddle_awake)(wakefulness, | |
197 &ego->td, reodft010e_tw, 4*ego->n, 1, ego->n/2+1); | |
198 X(twiddle_awake)(wakefulness, | |
199 &ego->td2, reodft11e_tw, 8*ego->n, 1, ego->n * 2); | |
200 } | |
201 | |
202 static void destroy(plan *ego_) | |
203 { | |
204 P *ego = (P *) ego_; | |
205 X(plan_destroy_internal)(ego->cld); | |
206 } | |
207 | |
208 static void print(const plan *ego_, printer *p) | |
209 { | |
210 const P *ego = (const P *) ego_; | |
211 p->print(p, "(%se-r2hc-%D%v%(%p%))", | |
212 X(rdft_kind_str)(ego->kind), ego->n, ego->vl, ego->cld); | |
213 } | |
214 | |
215 static int applicable0(const solver *ego_, const problem *p_) | |
216 { | |
217 const problem_rdft *p = (const problem_rdft *) p_; | |
218 | |
219 UNUSED(ego_); | |
220 | |
221 return (1 | |
222 && p->sz->rnk == 1 | |
223 && p->vecsz->rnk <= 1 | |
224 && (p->kind[0] == REDFT11 || p->kind[0] == RODFT11) | |
225 ); | |
226 } | |
227 | |
228 static int applicable(const solver *ego, const problem *p, const planner *plnr) | |
229 { | |
230 return (!NO_SLOWP(plnr) && applicable0(ego, p)); | |
231 } | |
232 | |
233 static plan *mkplan(const solver *ego_, const problem *p_, planner *plnr) | |
234 { | |
235 P *pln; | |
236 const problem_rdft *p; | |
237 plan *cld; | |
238 R *buf; | |
239 INT n; | |
240 opcnt ops; | |
241 | |
242 static const plan_adt padt = { | |
243 X(rdft_solve), awake, print, destroy | |
244 }; | |
245 | |
246 if (!applicable(ego_, p_, plnr)) | |
247 return (plan *)0; | |
248 | |
249 p = (const problem_rdft *) p_; | |
250 | |
251 n = p->sz->dims[0].n; | |
252 buf = (R *) MALLOC(sizeof(R) * n, BUFFERS); | |
253 | |
254 cld = X(mkplan_d)(plnr, X(mkproblem_rdft_1_d)(X(mktensor_1d)(n, 1, 1), | |
255 X(mktensor_0d)(), | |
256 buf, buf, R2HC)); | |
257 X(ifree)(buf); | |
258 if (!cld) | |
259 return (plan *)0; | |
260 | |
261 pln = MKPLAN_RDFT(P, &padt, p->kind[0]==REDFT11 ? apply_re11:apply_ro11); | |
262 pln->n = n; | |
263 pln->is = p->sz->dims[0].is; | |
264 pln->os = p->sz->dims[0].os; | |
265 pln->cld = cld; | |
266 pln->td = pln->td2 = 0; | |
267 pln->kind = p->kind[0]; | |
268 | |
269 X(tensor_tornk1)(p->vecsz, &pln->vl, &pln->ivs, &pln->ovs); | |
270 | |
271 X(ops_zero)(&ops); | |
272 ops.other = 5 + (n-1) * 2 + (n-1)/2 * 12 + (1 - n % 2) * 6; | |
273 ops.add = (n - 1) * 1 + (n-1)/2 * 6; | |
274 ops.mul = 2 + (n-1) * 1 + (n-1)/2 * 6 + (1 - n % 2) * 3; | |
275 | |
276 X(ops_zero)(&pln->super.super.ops); | |
277 X(ops_madd2)(pln->vl, &ops, &pln->super.super.ops); | |
278 X(ops_madd2)(pln->vl, &cld->ops, &pln->super.super.ops); | |
279 | |
280 return &(pln->super.super); | |
281 } | |
282 | |
283 /* constructor */ | |
284 static solver *mksolver(void) | |
285 { | |
286 static const solver_adt sadt = { PROBLEM_RDFT, mkplan, 0 }; | |
287 S *slv = MKSOLVER(S, &sadt); | |
288 return &(slv->super); | |
289 } | |
290 | |
291 void X(reodft11e_r2hc_register)(planner *p) | |
292 { | |
293 REGISTER_SOLVER(p, mksolver()); | |
294 } |