Jamoma API  0.6.0.a19
fft4g.cpp
1 /*
2 Fast Fourier/Cosine/Sine Transform
3  dimension :one
4  data length :power of 2
5  decimation :frequency
7  data :inplace
8  table :use
9 functions
10  cdft: Complex Discrete Fourier Transform
11  rdft: Real Discrete Fourier Transform
12  ddct: Discrete Cosine Transform
13  ddst: Discrete Sine Transform
14  dfct: Cosine Transform of RDFT (Real Symmetric DFT)
15  dfst: Sine Transform of RDFT (Real Anti-symmetric DFT)
16 function prototypes
17  void cdft(int, int, double *, int *, double *);
18  void rdft(int, int, double *, int *, double *);
19  void ddct(int, int, double *, int *, double *);
20  void ddst(int, int, double *, int *, double *);
21  void dfct(int, double *, double *, int *, double *);
22  void dfst(int, double *, double *, int *, double *);
23
24
25 -------- Complex DFT (Discrete Fourier Transform) --------
26  [definition]
27  <case1>
28  X[k] = sum_j=0^n-1 x[j]*exp(2*pi*i*j*k/n), 0<=k<n
29  <case2>
30  X[k] = sum_j=0^n-1 x[j]*exp(-2*pi*i*j*k/n), 0<=k<n
31  (notes: sum_j=0^n-1 is a summation from j=0 to n-1)
32  [usage]
33  <case1>
34  ip[0] = 0; // first time only
35  cdft(2*n, 1, a, ip, w);
36  <case2>
37  ip[0] = 0; // first time only
38  cdft(2*n, -1, a, ip, w);
39  [parameters]
40  2*n :data length (int)
41  n >= 1, n = power of 2
42  a[0...2*n-1] :input/output data (double *)
43  input data
44  a[2*j] = Re(x[j]),
45  a[2*j+1] = Im(x[j]), 0<=j<n
46  output data
47  a[2*k] = Re(X[k]),
48  a[2*k+1] = Im(X[k]), 0<=k<n
49  ip[0...*] :work area for bit reversal (int *)
50  length of ip >= 2+sqrt(n)
51  strictly,
52  length of ip >=
53  2+(1<<(int)(log(n+0.5)/log(2))/2).
54  ip[0],ip[1] are pointers of the cos/sin table.
55  w[0...n/2-1] :cos/sin table (double *)
56  w[],ip[] are initialized if ip[0] == 0.
57  [remark]
58  Inverse of
59  cdft(2*n, -1, a, ip, w);
60  is
61  cdft(2*n, 1, a, ip, w);
62  for (j = 0; j <= 2 * n - 1; j++) {
63  a[j] *= 1.0 / n;
64  }
65  .
66
67
68 -------- Real DFT / Inverse of Real DFT --------
69  [definition]
70  <case1> RDFT
71  R[k] = sum_j=0^n-1 a[j]*cos(2*pi*j*k/n), 0<=k<=n/2
72  I[k] = sum_j=0^n-1 a[j]*sin(2*pi*j*k/n), 0<k<n/2
73  <case2> IRDFT (excluding scale)
74  a[k] = (R[0] + R[n/2]*cos(pi*k))/2 +
75  sum_j=1^n/2-1 R[j]*cos(2*pi*j*k/n) +
76  sum_j=1^n/2-1 I[j]*sin(2*pi*j*k/n), 0<=k<n
77  [usage]
78  <case1>
79  ip[0] = 0; // first time only
80  rdft(n, 1, a, ip, w);
81  <case2>
82  ip[0] = 0; // first time only
83  rdft(n, -1, a, ip, w);
84  [parameters]
85  n :data length (int)
86  n >= 2, n = power of 2
87  a[0...n-1] :input/output data (double *)
88  <case1>
89  output data
90  a[2*k] = R[k], 0<=k<n/2
91  a[2*k+1] = I[k], 0<k<n/2
92  a[1] = R[n/2]
93  <case2>
94  input data
95  a[2*j] = R[j], 0<=j<n/2
96  a[2*j+1] = I[j], 0<j<n/2
97  a[1] = R[n/2]
98  ip[0...*] :work area for bit reversal (int *)
99  length of ip >= 2+sqrt(n/2)
100  strictly,
101  length of ip >=
102  2+(1<<(int)(log(n/2+0.5)/log(2))/2).
103  ip[0],ip[1] are pointers of the cos/sin table.
104  w[0...n/2-1] :cos/sin table (double *)
105  w[],ip[] are initialized if ip[0] == 0.
106  [remark]
107  Inverse of
108  rdft(n, 1, a, ip, w);
109  is
110  rdft(n, -1, a, ip, w);
111  for (j = 0; j <= n - 1; j++) {
112  a[j] *= 2.0 / n;
113  }
114  .
115
116
117 -------- DCT (Discrete Cosine Transform) / Inverse of DCT --------
118  [definition]
119  <case1> IDCT (excluding scale)
120  C[k] = sum_j=0^n-1 a[j]*cos(pi*j*(k+1/2)/n), 0<=k<n
121  <case2> DCT
122  C[k] = sum_j=0^n-1 a[j]*cos(pi*(j+1/2)*k/n), 0<=k<n
123  [usage]
124  <case1>
125  ip[0] = 0; // first time only
126  ddct(n, 1, a, ip, w);
127  <case2>
128  ip[0] = 0; // first time only
129  ddct(n, -1, a, ip, w);
130  [parameters]
131  n :data length (int)
132  n >= 2, n = power of 2
133  a[0...n-1] :input/output data (double *)
134  output data
135  a[k] = C[k], 0<=k<n
136  ip[0...*] :work area for bit reversal (int *)
137  length of ip >= 2+sqrt(n/2)
138  strictly,
139  length of ip >=
140  2+(1<<(int)(log(n/2+0.5)/log(2))/2).
141  ip[0],ip[1] are pointers of the cos/sin table.
142  w[0...n*5/4-1] :cos/sin table (double *)
143  w[],ip[] are initialized if ip[0] == 0.
144  [remark]
145  Inverse of
146  ddct(n, -1, a, ip, w);
147  is
148  a[0] *= 0.5;
149  ddct(n, 1, a, ip, w);
150  for (j = 0; j <= n - 1; j++) {
151  a[j] *= 2.0 / n;
152  }
153  .
154
155
156 -------- DST (Discrete Sine Transform) / Inverse of DST --------
157  [definition]
158  <case1> IDST (excluding scale)
159  S[k] = sum_j=1^n A[j]*sin(pi*j*(k+1/2)/n), 0<=k<n
160  <case2> DST
161  S[k] = sum_j=0^n-1 a[j]*sin(pi*(j+1/2)*k/n), 0<k<=n
162  [usage]
163  <case1>
164  ip[0] = 0; // first time only
165  ddst(n, 1, a, ip, w);
166  <case2>
167  ip[0] = 0; // first time only
168  ddst(n, -1, a, ip, w);
169  [parameters]
170  n :data length (int)
171  n >= 2, n = power of 2
172  a[0...n-1] :input/output data (double *)
173  <case1>
174  input data
175  a[j] = A[j], 0<j<n
176  a[0] = A[n]
177  output data
178  a[k] = S[k], 0<=k<n
179  <case2>
180  output data
181  a[k] = S[k], 0<k<n
182  a[0] = S[n]
183  ip[0...*] :work area for bit reversal (int *)
184  length of ip >= 2+sqrt(n/2)
185  strictly,
186  length of ip >=
187  2+(1<<(int)(log(n/2+0.5)/log(2))/2).
188  ip[0],ip[1] are pointers of the cos/sin table.
189  w[0...n*5/4-1] :cos/sin table (double *)
190  w[],ip[] are initialized if ip[0] == 0.
191  [remark]
192  Inverse of
193  ddst(n, -1, a, ip, w);
194  is
195  a[0] *= 0.5;
196  ddst(n, 1, a, ip, w);
197  for (j = 0; j <= n - 1; j++) {
198  a[j] *= 2.0 / n;
199  }
200  .
201
202
203 -------- Cosine Transform of RDFT (Real Symmetric DFT) --------
204  [definition]
205  C[k] = sum_j=0^n a[j]*cos(pi*j*k/n), 0<=k<=n
206  [usage]
207  ip[0] = 0; // first time only
208  dfct(n, a, t, ip, w);
209  [parameters]
210  n :data length - 1 (int)
211  n >= 2, n = power of 2
212  a[0...n] :input/output data (double *)
213  output data
214  a[k] = C[k], 0<=k<=n
215  t[0...n/2] :work area (double *)
216  ip[0...*] :work area for bit reversal (int *)
217  length of ip >= 2+sqrt(n/4)
218  strictly,
219  length of ip >=
220  2+(1<<(int)(log(n/4+0.5)/log(2))/2).
221  ip[0],ip[1] are pointers of the cos/sin table.
222  w[0...n*5/8-1] :cos/sin table (double *)
223  w[],ip[] are initialized if ip[0] == 0.
224  [remark]
225  Inverse of
226  a[0] *= 0.5;
227  a[n] *= 0.5;
228  dfct(n, a, t, ip, w);
229  is
230  a[0] *= 0.5;
231  a[n] *= 0.5;
232  dfct(n, a, t, ip, w);
233  for (j = 0; j <= n; j++) {
234  a[j] *= 2.0 / n;
235  }
236  .
237
238
239 -------- Sine Transform of RDFT (Real Anti-symmetric DFT) --------
240  [definition]
241  S[k] = sum_j=1^n-1 a[j]*sin(pi*j*k/n), 0<k<n
242  [usage]
243  ip[0] = 0; // first time only
244  dfst(n, a, t, ip, w);
245  [parameters]
246  n :data length + 1 (int)
247  n >= 2, n = power of 2
248  a[0...n-1] :input/output data (double *)
249  output data
250  a[k] = S[k], 0<k<n
251  (a[0] is used for work area)
252  t[0...n/2-1] :work area (double *)
253  ip[0...*] :work area for bit reversal (int *)
254  length of ip >= 2+sqrt(n/4)
255  strictly,
256  length of ip >=
257  2+(1<<(int)(log(n/4+0.5)/log(2))/2).
258  ip[0],ip[1] are pointers of the cos/sin table.
259  w[0...n*5/8-1] :cos/sin table (double *)
260  w[],ip[] are initialized if ip[0] == 0.
261  [remark]
262  Inverse of
263  dfst(n, a, t, ip, w);
264  is
265  dfst(n, a, t, ip, w);
266  for (j = 1; j <= n - 1; j++) {
267  a[j] *= 2.0 / n;
268  }
269  .
270
271
272 Appendix :
273  The cos/sin table is recalculated when the larger table required.
274  w[] and ip[] are compatible with all routines.
275 */
276
277
278 void cdft(int n, int isgn, double *a, int *ip, double *w)
279 {
280  void makewt(int nw, int *ip, double *w);
281  void bitrv2(int n, int *ip, double *a);
282  void bitrv2conj(int n, int *ip, double *a);
283  void cftfsub(int n, double *a, double *w);
284  void cftbsub(int n, double *a, double *w);
285
286  if (n > (ip[0] << 2)) {
287  makewt(n >> 2, ip, w);
288  }
289  if (n > 4) {
290  if (isgn >= 0) {
291  bitrv2(n, ip + 2, a);
292  cftfsub(n, a, w);
293  } else {
294  bitrv2conj(n, ip + 2, a);
295  cftbsub(n, a, w);
296  }
297  } else if (n == 4) {
298  cftfsub(n, a, w);
299  }
300 }
301
302
303 void rdft(int n, int isgn, double *a, int *ip, double *w)
304 {
305  void makewt(int nw, int *ip, double *w);
306  void makect(int nc, int *ip, double *c);
307  void bitrv2(int n, int *ip, double *a);
308  void cftfsub(int n, double *a, double *w);
309  void cftbsub(int n, double *a, double *w);
310  void rftfsub(int n, double *a, int nc, double *c);
311  void rftbsub(int n, double *a, int nc, double *c);
312  int nw, nc;
313  double xi;
314
315  nw = ip[0];
316  if (n > (nw << 2)) {
317  nw = n >> 2;
318  makewt(nw, ip, w);
319  }
320  nc = ip[1];
321  if (n > (nc << 2)) {
322  nc = n >> 2;
323  makect(nc, ip, w + nw);
324  }
325  if (isgn >= 0) {
326  if (n > 4) {
327  bitrv2(n, ip + 2, a);
328  cftfsub(n, a, w);
329  rftfsub(n, a, nc, w + nw);
330  } else if (n == 4) {
331  cftfsub(n, a, w);
332  }
333  xi = a[0] - a[1];
334  a[0] += a[1];
335  a[1] = xi;
336  } else {
337  a[1] = 0.5 * (a[0] - a[1]);
338  a[0] -= a[1];
339  if (n > 4) {
340  rftbsub(n, a, nc, w + nw);
341  bitrv2(n, ip + 2, a);
342  cftbsub(n, a, w);
343  } else if (n == 4) {
344  cftfsub(n, a, w);
345  }
346  }
347 }
348
349
350 void ddct(int n, int isgn, double *a, int *ip, double *w)
351 {
352  void makewt(int nw, int *ip, double *w);
353  void makect(int nc, int *ip, double *c);
354  void bitrv2(int n, int *ip, double *a);
355  void cftfsub(int n, double *a, double *w);
356  void cftbsub(int n, double *a, double *w);
357  void rftfsub(int n, double *a, int nc, double *c);
358  void rftbsub(int n, double *a, int nc, double *c);
359  void dctsub(int n, double *a, int nc, double *c);
360  int j, nw, nc;
361  double xr;
362
363  nw = ip[0];
364  if (n > (nw << 2)) {
365  nw = n >> 2;
366  makewt(nw, ip, w);
367  }
368  nc = ip[1];
369  if (n > nc) {
370  nc = n;
371  makect(nc, ip, w + nw);
372  }
373  if (isgn < 0) {
374  xr = a[n - 1];
375  for (j = n - 2; j >= 2; j -= 2) {
376  a[j + 1] = a[j] - a[j - 1];
377  a[j] += a[j - 1];
378  }
379  a[1] = a[0] - xr;
380  a[0] += xr;
381  if (n > 4) {
382  rftbsub(n, a, nc, w + nw);
383  bitrv2(n, ip + 2, a);
384  cftbsub(n, a, w);
385  } else if (n == 4) {
386  cftfsub(n, a, w);
387  }
388  }
389  dctsub(n, a, nc, w + nw);
390  if (isgn >= 0) {
391  if (n > 4) {
392  bitrv2(n, ip + 2, a);
393  cftfsub(n, a, w);
394  rftfsub(n, a, nc, w + nw);
395  } else if (n == 4) {
396  cftfsub(n, a, w);
397  }
398  xr = a[0] - a[1];
399  a[0] += a[1];
400  for (j = 2; j < n; j += 2) {
401  a[j - 1] = a[j] - a[j + 1];
402  a[j] += a[j + 1];
403  }
404  a[n - 1] = xr;
405  }
406 }
407
408
409 void ddst(int n, int isgn, double *a, int *ip, double *w)
410 {
411  void makewt(int nw, int *ip, double *w);
412  void makect(int nc, int *ip, double *c);
413  void bitrv2(int n, int *ip, double *a);
414  void cftfsub(int n, double *a, double *w);
415  void cftbsub(int n, double *a, double *w);
416  void rftfsub(int n, double *a, int nc, double *c);
417  void rftbsub(int n, double *a, int nc, double *c);
418  void dstsub(int n, double *a, int nc, double *c);
419  int j, nw, nc;
420  double xr;
421
422  nw = ip[0];
423  if (n > (nw << 2)) {
424  nw = n >> 2;
425  makewt(nw, ip, w);
426  }
427  nc = ip[1];
428  if (n > nc) {
429  nc = n;
430  makect(nc, ip, w + nw);
431  }
432  if (isgn < 0) {
433  xr = a[n - 1];
434  for (j = n - 2; j >= 2; j -= 2) {
435  a[j + 1] = -a[j] - a[j - 1];
436  a[j] -= a[j - 1];
437  }
438  a[1] = a[0] + xr;
439  a[0] -= xr;
440  if (n > 4) {
441  rftbsub(n, a, nc, w + nw);
442  bitrv2(n, ip + 2, a);
443  cftbsub(n, a, w);
444  } else if (n == 4) {
445  cftfsub(n, a, w);
446  }
447  }
448  dstsub(n, a, nc, w + nw);
449  if (isgn >= 0) {
450  if (n > 4) {
451  bitrv2(n, ip + 2, a);
452  cftfsub(n, a, w);
453  rftfsub(n, a, nc, w + nw);
454  } else if (n == 4) {
455  cftfsub(n, a, w);
456  }
457  xr = a[0] - a[1];
458  a[0] += a[1];
459  for (j = 2; j < n; j += 2) {
460  a[j - 1] = -a[j] - a[j + 1];
461  a[j] -= a[j + 1];
462  }
463  a[n - 1] = -xr;
464  }
465 }
466
467
468 void dfct(int n, double *a, double *t, int *ip, double *w)
469 {
470  void makewt(int nw, int *ip, double *w);
471  void makect(int nc, int *ip, double *c);
472  void bitrv2(int n, int *ip, double *a);
473  void cftfsub(int n, double *a, double *w);
474  void rftfsub(int n, double *a, int nc, double *c);
475  void dctsub(int n, double *a, int nc, double *c);
476  int j, k, l, m, mh, nw, nc;
477  double xr, xi, yr, yi;
478
479  nw = ip[0];
480  if (n > (nw << 3)) {
481  nw = n >> 3;
482  makewt(nw, ip, w);
483  }
484  nc = ip[1];
485  if (n > (nc << 1)) {
486  nc = n >> 1;
487  makect(nc, ip, w + nw);
488  }
489  m = n >> 1;
490  yi = a[m];
491  xi = a[0] + a[n];
492  a[0] -= a[n];
493  t[0] = xi - yi;
494  t[m] = xi + yi;
495  if (n > 2) {
496  mh = m >> 1;
497  for (j = 1; j < mh; j++) {
498  k = m - j;
499  xr = a[j] - a[n - j];
500  xi = a[j] + a[n - j];
501  yr = a[k] - a[n - k];
502  yi = a[k] + a[n - k];
503  a[j] = xr;
504  a[k] = yr;
505  t[j] = xi - yi;
506  t[k] = xi + yi;
507  }
508  t[mh] = a[mh] + a[n - mh];
509  a[mh] -= a[n - mh];
510  dctsub(m, a, nc, w + nw);
511  if (m > 4) {
512  bitrv2(m, ip + 2, a);
513  cftfsub(m, a, w);
514  rftfsub(m, a, nc, w + nw);
515  } else if (m == 4) {
516  cftfsub(m, a, w);
517  }
518  a[n - 1] = a[0] - a[1];
519  a[1] = a[0] + a[1];
520  for (j = m - 2; j >= 2; j -= 2) {
521  a[2 * j + 1] = a[j] + a[j + 1];
522  a[2 * j - 1] = a[j] - a[j + 1];
523  }
524  l = 2;
525  m = mh;
526  while (m >= 2) {
527  dctsub(m, t, nc, w + nw);
528  if (m > 4) {
529  bitrv2(m, ip + 2, t);
530  cftfsub(m, t, w);
531  rftfsub(m, t, nc, w + nw);
532  } else if (m == 4) {
533  cftfsub(m, t, w);
534  }
535  a[n - l] = t[0] - t[1];
536  a[l] = t[0] + t[1];
537  k = 0;
538  for (j = 2; j < m; j += 2) {
539  k += l << 2;
540  a[k - l] = t[j] - t[j + 1];
541  a[k + l] = t[j] + t[j + 1];
542  }
543  l <<= 1;
544  mh = m >> 1;
545  for (j = 0; j < mh; j++) {
546  k = m - j;
547  t[j] = t[m + k] - t[m + j];
548  t[k] = t[m + k] + t[m + j];
549  }
550  t[mh] = t[m + mh];
551  m = mh;
552  }
553  a[l] = t[0];
554  a[n] = t[2] - t[1];
555  a[0] = t[2] + t[1];
556  } else {
557  a[1] = a[0];
558  a[2] = t[0];
559  a[0] = t[1];
560  }
561 }
562
563
564 void dfst(int n, double *a, double *t, int *ip, double *w)
565 {
566  void makewt(int nw, int *ip, double *w);
567  void makect(int nc, int *ip, double *c);
568  void bitrv2(int n, int *ip, double *a);
569  void cftfsub(int n, double *a, double *w);
570  void rftfsub(int n, double *a, int nc, double *c);
571  void dstsub(int n, double *a, int nc, double *c);
572  int j, k, l, m, mh, nw, nc;
573  double xr, xi, yr, yi;
574
575  nw = ip[0];
576  if (n > (nw << 3)) {
577  nw = n >> 3;
578  makewt(nw, ip, w);
579  }
580  nc = ip[1];
581  if (n > (nc << 1)) {
582  nc = n >> 1;
583  makect(nc, ip, w + nw);
584  }
585  if (n > 2) {
586  m = n >> 1;
587  mh = m >> 1;
588  for (j = 1; j < mh; j++) {
589  k = m - j;
590  xr = a[j] + a[n - j];
591  xi = a[j] - a[n - j];
592  yr = a[k] + a[n - k];
593  yi = a[k] - a[n - k];
594  a[j] = xr;
595  a[k] = yr;
596  t[j] = xi + yi;
597  t[k] = xi - yi;
598  }
599  t[0] = a[mh] - a[n - mh];
600  a[mh] += a[n - mh];
601  a[0] = a[m];
602  dstsub(m, a, nc, w + nw);
603  if (m > 4) {
604  bitrv2(m, ip + 2, a);
605  cftfsub(m, a, w);
606  rftfsub(m, a, nc, w + nw);
607  } else if (m == 4) {
608  cftfsub(m, a, w);
609  }
610  a[n - 1] = a[1] - a[0];
611  a[1] = a[0] + a[1];
612  for (j = m - 2; j >= 2; j -= 2) {
613  a[2 * j + 1] = a[j] - a[j + 1];
614  a[2 * j - 1] = -a[j] - a[j + 1];
615  }
616  l = 2;
617  m = mh;
618  while (m >= 2) {
619  dstsub(m, t, nc, w + nw);
620  if (m > 4) {
621  bitrv2(m, ip + 2, t);
622  cftfsub(m, t, w);
623  rftfsub(m, t, nc, w + nw);
624  } else if (m == 4) {
625  cftfsub(m, t, w);
626  }
627  a[n - l] = t[1] - t[0];
628  a[l] = t[0] + t[1];
629  k = 0;
630  for (j = 2; j < m; j += 2) {
631  k += l << 2;
632  a[k - l] = -t[j] - t[j + 1];
633  a[k + l] = t[j] - t[j + 1];
634  }
635  l <<= 1;
636  mh = m >> 1;
637  for (j = 1; j < mh; j++) {
638  k = m - j;
639  t[j] = t[m + k] + t[m + j];
640  t[k] = t[m + k] - t[m + j];
641  }
642  t[0] = t[m + mh];
643  m = mh;
644  }
645  a[l] = t[0];
646  }
647  a[0] = 0;
648 }
649
650
651 /* -------- initializing routines -------- */
652
653
654 #include <math.h>
655
656 void makewt(int nw, int *ip, double *w)
657 {
658  void bitrv2(int n, int *ip, double *a);
659  int j, nwh;
660  double delta, x, y;
661
662  ip[0] = nw;
663  ip[1] = 1;
664  if (nw > 2) {
665  nwh = nw >> 1;
666  delta = atan(1.0) / nwh;
667  w[0] = 1;
668  w[1] = 0;
669  w[nwh] = cos(delta * nwh);
670  w[nwh + 1] = w[nwh];
671  if (nwh > 2) {
672  for (j = 2; j < nwh; j += 2) {
673  x = cos(delta * j);
674  y = sin(delta * j);
675  w[j] = x;
676  w[j + 1] = y;
677  w[nw - j] = y;
678  w[nw - j + 1] = x;
679  }
680  bitrv2(nw, ip + 2, w);
681  }
682  }
683 }
684
685
686 void makect(int nc, int *ip, double *c)
687 {
688  int j, nch;
689  double delta;
690
691  ip[1] = nc;
692  if (nc > 1) {
693  nch = nc >> 1;
694  delta = atan(1.0) / nch;
695  c[0] = cos(delta * nch);
696  c[nch] = 0.5 * c[0];
697  for (j = 1; j < nch; j++) {
698  c[j] = 0.5 * cos(delta * j);
699  c[nc - j] = 0.5 * sin(delta * j);
700  }
701  }
702 }
703
704
705 /* -------- child routines -------- */
706
707
708 void bitrv2(int n, int *ip, double *a)
709 {
710  int j, j1, k, k1, l, m, m2;
711  double xr, xi, yr, yi;
712
713  ip[0] = 0;
714  l = n;
715  m = 1;
716  while ((m << 3) < l) {
717  l >>= 1;
718  for (j = 0; j < m; j++) {
719  ip[m + j] = ip[j] + l;
720  }
721  m <<= 1;
722  }
723  m2 = 2 * m;
724  if ((m << 3) == l) {
725  for (k = 0; k < m; k++) {
726  for (j = 0; j < k; j++) {
727  j1 = 2 * j + ip[k];
728  k1 = 2 * k + ip[j];
729  xr = a[j1];
730  xi = a[j1 + 1];
731  yr = a[k1];
732  yi = a[k1 + 1];
733  a[j1] = yr;
734  a[j1 + 1] = yi;
735  a[k1] = xr;
736  a[k1 + 1] = xi;
737  j1 += m2;
738  k1 += 2 * m2;
739  xr = a[j1];
740  xi = a[j1 + 1];
741  yr = a[k1];
742  yi = a[k1 + 1];
743  a[j1] = yr;
744  a[j1 + 1] = yi;
745  a[k1] = xr;
746  a[k1 + 1] = xi;
747  j1 += m2;
748  k1 -= m2;
749  xr = a[j1];
750  xi = a[j1 + 1];
751  yr = a[k1];
752  yi = a[k1 + 1];
753  a[j1] = yr;
754  a[j1 + 1] = yi;
755  a[k1] = xr;
756  a[k1 + 1] = xi;
757  j1 += m2;
758  k1 += 2 * m2;
759  xr = a[j1];
760  xi = a[j1 + 1];
761  yr = a[k1];
762  yi = a[k1 + 1];
763  a[j1] = yr;
764  a[j1 + 1] = yi;
765  a[k1] = xr;
766  a[k1 + 1] = xi;
767  }
768  j1 = 2 * k + m2 + ip[k];
769  k1 = j1 + m2;
770  xr = a[j1];
771  xi = a[j1 + 1];
772  yr = a[k1];
773  yi = a[k1 + 1];
774  a[j1] = yr;
775  a[j1 + 1] = yi;
776  a[k1] = xr;
777  a[k1 + 1] = xi;
778  }
779  } else {
780  for (k = 1; k < m; k++) {
781  for (j = 0; j < k; j++) {
782  j1 = 2 * j + ip[k];
783  k1 = 2 * k + ip[j];
784  xr = a[j1];
785  xi = a[j1 + 1];
786  yr = a[k1];
787  yi = a[k1 + 1];
788  a[j1] = yr;
789  a[j1 + 1] = yi;
790  a[k1] = xr;
791  a[k1 + 1] = xi;
792  j1 += m2;
793  k1 += m2;
794  xr = a[j1];
795  xi = a[j1 + 1];
796  yr = a[k1];
797  yi = a[k1 + 1];
798  a[j1] = yr;
799  a[j1 + 1] = yi;
800  a[k1] = xr;
801  a[k1 + 1] = xi;
802  }
803  }
804  }
805 }
806
807
808 void bitrv2conj(int n, int *ip, double *a)
809 {
810  int j, j1, k, k1, l, m, m2;
811  double xr, xi, yr, yi;
812
813  ip[0] = 0;
814  l = n;
815  m = 1;
816  while ((m << 3) < l) {
817  l >>= 1;
818  for (j = 0; j < m; j++) {
819  ip[m + j] = ip[j] + l;
820  }
821  m <<= 1;
822  }
823  m2 = 2 * m;
824  if ((m << 3) == l) {
825  for (k = 0; k < m; k++) {
826  for (j = 0; j < k; j++) {
827  j1 = 2 * j + ip[k];
828  k1 = 2 * k + ip[j];
829  xr = a[j1];
830  xi = -a[j1 + 1];
831  yr = a[k1];
832  yi = -a[k1 + 1];
833  a[j1] = yr;
834  a[j1 + 1] = yi;
835  a[k1] = xr;
836  a[k1 + 1] = xi;
837  j1 += m2;
838  k1 += 2 * m2;
839  xr = a[j1];
840  xi = -a[j1 + 1];
841  yr = a[k1];
842  yi = -a[k1 + 1];
843  a[j1] = yr;
844  a[j1 + 1] = yi;
845  a[k1] = xr;
846  a[k1 + 1] = xi;
847  j1 += m2;
848  k1 -= m2;
849  xr = a[j1];
850  xi = -a[j1 + 1];
851  yr = a[k1];
852  yi = -a[k1 + 1];
853  a[j1] = yr;
854  a[j1 + 1] = yi;
855  a[k1] = xr;
856  a[k1 + 1] = xi;
857  j1 += m2;
858  k1 += 2 * m2;
859  xr = a[j1];
860  xi = -a[j1 + 1];
861  yr = a[k1];
862  yi = -a[k1 + 1];
863  a[j1] = yr;
864  a[j1 + 1] = yi;
865  a[k1] = xr;
866  a[k1 + 1] = xi;
867  }
868  k1 = 2 * k + ip[k];
869  a[k1 + 1] = -a[k1 + 1];
870  j1 = k1 + m2;
871  k1 = j1 + m2;
872  xr = a[j1];
873  xi = -a[j1 + 1];
874  yr = a[k1];
875  yi = -a[k1 + 1];
876  a[j1] = yr;
877  a[j1 + 1] = yi;
878  a[k1] = xr;
879  a[k1 + 1] = xi;
880  k1 += m2;
881  a[k1 + 1] = -a[k1 + 1];
882  }
883  } else {
884  a[1] = -a[1];
885  a[m2 + 1] = -a[m2 + 1];
886  for (k = 1; k < m; k++) {
887  for (j = 0; j < k; j++) {
888  j1 = 2 * j + ip[k];
889  k1 = 2 * k + ip[j];
890  xr = a[j1];
891  xi = -a[j1 + 1];
892  yr = a[k1];
893  yi = -a[k1 + 1];
894  a[j1] = yr;
895  a[j1 + 1] = yi;
896  a[k1] = xr;
897  a[k1 + 1] = xi;
898  j1 += m2;
899  k1 += m2;
900  xr = a[j1];
901  xi = -a[j1 + 1];
902  yr = a[k1];
903  yi = -a[k1 + 1];
904  a[j1] = yr;
905  a[j1 + 1] = yi;
906  a[k1] = xr;
907  a[k1 + 1] = xi;
908  }
909  k1 = 2 * k + ip[k];
910  a[k1 + 1] = -a[k1 + 1];
911  a[k1 + m2 + 1] = -a[k1 + m2 + 1];
912  }
913  }
914 }
915
916
917 void cftfsub(int n, double *a, double *w)
918 {
919  void cft1st(int n, double *a, double *w);
920  void cftmdl(int n, int l, double *a, double *w);
921  int j, j1, j2, j3, l;
922  double x0r, x0i, x1r, x1i, x2r, x2i, x3r, x3i;
923
924  l = 2;
925  if (n > 8) {
926  cft1st(n, a, w);
927  l = 8;
928  while ((l << 2) < n) {
929  cftmdl(n, l, a, w);
930  l <<= 2;
931  }
932  }
933  if ((l << 2) == n) {
934  for (j = 0; j < l; j += 2) {
935  j1 = j + l;
936  j2 = j1 + l;
937  j3 = j2 + l;
938  x0r = a[j] + a[j1];
939  x0i = a[j + 1] + a[j1 + 1];
940  x1r = a[j] - a[j1];
941  x1i = a[j + 1] - a[j1 + 1];
942  x2r = a[j2] + a[j3];
943  x2i = a[j2 + 1] + a[j3 + 1];
944  x3r = a[j2] - a[j3];
945  x3i = a[j2 + 1] - a[j3 + 1];
946  a[j] = x0r + x2r;
947  a[j + 1] = x0i + x2i;
948  a[j2] = x0r - x2r;
949  a[j2 + 1] = x0i - x2i;
950  a[j1] = x1r - x3i;
951  a[j1 + 1] = x1i + x3r;
952  a[j3] = x1r + x3i;
953  a[j3 + 1] = x1i - x3r;
954  }
955  } else {
956  for (j = 0; j < l; j += 2) {
957  j1 = j + l;
958  x0r = a[j] - a[j1];
959  x0i = a[j + 1] - a[j1 + 1];
960  a[j] += a[j1];
961  a[j + 1] += a[j1 + 1];
962  a[j1] = x0r;
963  a[j1 + 1] = x0i;
964  }
965  }
966 }
967
968
969 void cftbsub(int n, double *a, double *w)
970 {
971  void cft1st(int n, double *a, double *w);
972  void cftmdl(int n, int l, double *a, double *w);
973  int j, j1, j2, j3, l;
974  double x0r, x0i, x1r, x1i, x2r, x2i, x3r, x3i;
975
976  l = 2;
977  if (n > 8) {
978  cft1st(n, a, w);
979  l = 8;
980  while ((l << 2) < n) {
981  cftmdl(n, l, a, w);
982  l <<= 2;
983  }
984  }
985  if ((l << 2) == n) {
986  for (j = 0; j < l; j += 2) {
987  j1 = j + l;
988  j2 = j1 + l;
989  j3 = j2 + l;
990  x0r = a[j] + a[j1];
991  x0i = -a[j + 1] - a[j1 + 1];
992  x1r = a[j] - a[j1];
993  x1i = -a[j + 1] + a[j1 + 1];
994  x2r = a[j2] + a[j3];
995  x2i = a[j2 + 1] + a[j3 + 1];
996  x3r = a[j2] - a[j3];
997  x3i = a[j2 + 1] - a[j3 + 1];
998  a[j] = x0r + x2r;
999  a[j + 1] = x0i - x2i;
1000  a[j2] = x0r - x2r;
1001  a[j2 + 1] = x0i + x2i;
1002  a[j1] = x1r - x3i;
1003  a[j1 + 1] = x1i - x3r;
1004  a[j3] = x1r + x3i;
1005  a[j3 + 1] = x1i + x3r;
1006  }
1007  } else {
1008  for (j = 0; j < l; j += 2) {
1009  j1 = j + l;
1010  x0r = a[j] - a[j1];
1011  x0i = -a[j + 1] + a[j1 + 1];
1012  a[j] += a[j1];
1013  a[j + 1] = -a[j + 1] - a[j1 + 1];
1014  a[j1] = x0r;
1015  a[j1 + 1] = x0i;
1016  }
1017  }
1018 }
1019
1020
1021 void cft1st(int n, double *a, double *w)
1022 {
1023  int j, k1, k2;
1024  double wk1r, wk1i, wk2r, wk2i, wk3r, wk3i;
1025  double x0r, x0i, x1r, x1i, x2r, x2i, x3r, x3i;
1026
1027  x0r = a[0] + a[2];
1028  x0i = a[1] + a[3];
1029  x1r = a[0] - a[2];
1030  x1i = a[1] - a[3];
1031  x2r = a[4] + a[6];
1032  x2i = a[5] + a[7];
1033  x3r = a[4] - a[6];
1034  x3i = a[5] - a[7];
1035  a[0] = x0r + x2r;
1036  a[1] = x0i + x2i;
1037  a[4] = x0r - x2r;
1038  a[5] = x0i - x2i;
1039  a[2] = x1r - x3i;
1040  a[3] = x1i + x3r;
1041  a[6] = x1r + x3i;
1042  a[7] = x1i - x3r;
1043  wk1r = w[2];
1044  x0r = a[8] + a[10];
1045  x0i = a[9] + a[11];
1046  x1r = a[8] - a[10];
1047  x1i = a[9] - a[11];
1048  x2r = a[12] + a[14];
1049  x2i = a[13] + a[15];
1050  x3r = a[12] - a[14];
1051  x3i = a[13] - a[15];
1052  a[8] = x0r + x2r;
1053  a[9] = x0i + x2i;
1054  a[12] = x2i - x0i;
1055  a[13] = x0r - x2r;
1056  x0r = x1r - x3i;
1057  x0i = x1i + x3r;
1058  a[10] = wk1r * (x0r - x0i);
1059  a[11] = wk1r * (x0r + x0i);
1060  x0r = x3i + x1r;
1061  x0i = x3r - x1i;
1062  a[14] = wk1r * (x0i - x0r);
1063  a[15] = wk1r * (x0i + x0r);
1064  k1 = 0;
1065  for (j = 16; j < n; j += 16) {
1066  k1 += 2;
1067  k2 = 2 * k1;
1068  wk2r = w[k1];
1069  wk2i = w[k1 + 1];
1070  wk1r = w[k2];
1071  wk1i = w[k2 + 1];
1072  wk3r = wk1r - 2 * wk2i * wk1i;
1073  wk3i = 2 * wk2i * wk1r - wk1i;
1074  x0r = a[j] + a[j + 2];
1075  x0i = a[j + 1] + a[j + 3];
1076  x1r = a[j] - a[j + 2];
1077  x1i = a[j + 1] - a[j + 3];
1078  x2r = a[j + 4] + a[j + 6];
1079  x2i = a[j + 5] + a[j + 7];
1080  x3r = a[j + 4] - a[j + 6];
1081  x3i = a[j + 5] - a[j + 7];
1082  a[j] = x0r + x2r;
1083  a[j + 1] = x0i + x2i;
1084  x0r -= x2r;
1085  x0i -= x2i;
1086  a[j + 4] = wk2r * x0r - wk2i * x0i;
1087  a[j + 5] = wk2r * x0i + wk2i * x0r;
1088  x0r = x1r - x3i;
1089  x0i = x1i + x3r;
1090  a[j + 2] = wk1r * x0r - wk1i * x0i;
1091  a[j + 3] = wk1r * x0i + wk1i * x0r;
1092  x0r = x1r + x3i;
1093  x0i = x1i - x3r;
1094  a[j + 6] = wk3r * x0r - wk3i * x0i;
1095  a[j + 7] = wk3r * x0i + wk3i * x0r;
1096  wk1r = w[k2 + 2];
1097  wk1i = w[k2 + 3];
1098  wk3r = wk1r - 2 * wk2r * wk1i;
1099  wk3i = 2 * wk2r * wk1r - wk1i;
1100  x0r = a[j + 8] + a[j + 10];
1101  x0i = a[j + 9] + a[j + 11];
1102  x1r = a[j + 8] - a[j + 10];
1103  x1i = a[j + 9] - a[j + 11];
1104  x2r = a[j + 12] + a[j + 14];
1105  x2i = a[j + 13] + a[j + 15];
1106  x3r = a[j + 12] - a[j + 14];
1107  x3i = a[j + 13] - a[j + 15];
1108  a[j + 8] = x0r + x2r;
1109  a[j + 9] = x0i + x2i;
1110  x0r -= x2r;
1111  x0i -= x2i;
1112  a[j + 12] = -wk2i * x0r - wk2r * x0i;
1113  a[j + 13] = -wk2i * x0i + wk2r * x0r;
1114  x0r = x1r - x3i;
1115  x0i = x1i + x3r;
1116  a[j + 10] = wk1r * x0r - wk1i * x0i;
1117  a[j + 11] = wk1r * x0i + wk1i * x0r;
1118  x0r = x1r + x3i;
1119  x0i = x1i - x3r;
1120  a[j + 14] = wk3r * x0r - wk3i * x0i;
1121  a[j + 15] = wk3r * x0i + wk3i * x0r;
1122  }
1123 }
1124
1125
1126 void cftmdl(int n, int l, double *a, double *w)
1127 {
1128  int j, j1, j2, j3, k, k1, k2, m, m2;
1129  double wk1r, wk1i, wk2r, wk2i, wk3r, wk3i;
1130  double x0r, x0i, x1r, x1i, x2r, x2i, x3r, x3i;
1131
1132  m = l << 2;
1133  for (j = 0; j < l; j += 2) {
1134  j1 = j + l;
1135  j2 = j1 + l;
1136  j3 = j2 + l;
1137  x0r = a[j] + a[j1];
1138  x0i = a[j + 1] + a[j1 + 1];
1139  x1r = a[j] - a[j1];
1140  x1i = a[j + 1] - a[j1 + 1];
1141  x2r = a[j2] + a[j3];
1142  x2i = a[j2 + 1] + a[j3 + 1];
1143  x3r = a[j2] - a[j3];
1144  x3i = a[j2 + 1] - a[j3 + 1];
1145  a[j] = x0r + x2r;
1146  a[j + 1] = x0i + x2i;
1147  a[j2] = x0r - x2r;
1148  a[j2 + 1] = x0i - x2i;
1149  a[j1] = x1r - x3i;
1150  a[j1 + 1] = x1i + x3r;
1151  a[j3] = x1r + x3i;
1152  a[j3 + 1] = x1i - x3r;
1153  }
1154  wk1r = w[2];
1155  for (j = m; j < l + m; j += 2) {
1156  j1 = j + l;
1157  j2 = j1 + l;
1158  j3 = j2 + l;
1159  x0r = a[j] + a[j1];
1160  x0i = a[j + 1] + a[j1 + 1];
1161  x1r = a[j] - a[j1];
1162  x1i = a[j + 1] - a[j1 + 1];
1163  x2r = a[j2] + a[j3];
1164  x2i = a[j2 + 1] + a[j3 + 1];
1165  x3r = a[j2] - a[j3];
1166  x3i = a[j2 + 1] - a[j3 + 1];
1167  a[j] = x0r + x2r;
1168  a[j + 1] = x0i + x2i;
1169  a[j2] = x2i - x0i;
1170  a[j2 + 1] = x0r - x2r;
1171  x0r = x1r - x3i;
1172  x0i = x1i + x3r;
1173  a[j1] = wk1r * (x0r - x0i);
1174  a[j1 + 1] = wk1r * (x0r + x0i);
1175  x0r = x3i + x1r;
1176  x0i = x3r - x1i;
1177  a[j3] = wk1r * (x0i - x0r);
1178  a[j3 + 1] = wk1r * (x0i + x0r);
1179  }
1180  k1 = 0;
1181  m2 = 2 * m;
1182  for (k = m2; k < n; k += m2) {
1183  k1 += 2;
1184  k2 = 2 * k1;
1185  wk2r = w[k1];
1186  wk2i = w[k1 + 1];
1187  wk1r = w[k2];
1188  wk1i = w[k2 + 1];
1189  wk3r = wk1r - 2 * wk2i * wk1i;
1190  wk3i = 2 * wk2i * wk1r - wk1i;
1191  for (j = k; j < l + k; j += 2) {
1192  j1 = j + l;
1193  j2 = j1 + l;
1194  j3 = j2 + l;
1195  x0r = a[j] + a[j1];
1196  x0i = a[j + 1] + a[j1 + 1];
1197  x1r = a[j] - a[j1];
1198  x1i = a[j + 1] - a[j1 + 1];
1199  x2r = a[j2] + a[j3];
1200  x2i = a[j2 + 1] + a[j3 + 1];
1201  x3r = a[j2] - a[j3];
1202  x3i = a[j2 + 1] - a[j3 + 1];
1203  a[j] = x0r + x2r;
1204  a[j + 1] = x0i + x2i;
1205  x0r -= x2r;
1206  x0i -= x2i;
1207  a[j2] = wk2r * x0r - wk2i * x0i;
1208  a[j2 + 1] = wk2r * x0i + wk2i * x0r;
1209  x0r = x1r - x3i;
1210  x0i = x1i + x3r;
1211  a[j1] = wk1r * x0r - wk1i * x0i;
1212  a[j1 + 1] = wk1r * x0i + wk1i * x0r;
1213  x0r = x1r + x3i;
1214  x0i = x1i - x3r;
1215  a[j3] = wk3r * x0r - wk3i * x0i;
1216  a[j3 + 1] = wk3r * x0i + wk3i * x0r;
1217  }
1218  wk1r = w[k2 + 2];
1219  wk1i = w[k2 + 3];
1220  wk3r = wk1r - 2 * wk2r * wk1i;
1221  wk3i = 2 * wk2r * wk1r - wk1i;
1222  for (j = k + m; j < l + (k + m); j += 2) {
1223  j1 = j + l;
1224  j2 = j1 + l;
1225  j3 = j2 + l;
1226  x0r = a[j] + a[j1];
1227  x0i = a[j + 1] + a[j1 + 1];
1228  x1r = a[j] - a[j1];
1229  x1i = a[j + 1] - a[j1 + 1];
1230  x2r = a[j2] + a[j3];
1231  x2i = a[j2 + 1] + a[j3 + 1];
1232  x3r = a[j2] - a[j3];
1233  x3i = a[j2 + 1] - a[j3 + 1];
1234  a[j] = x0r + x2r;
1235  a[j + 1] = x0i + x2i;
1236  x0r -= x2r;
1237  x0i -= x2i;
1238  a[j2] = -wk2i * x0r - wk2r * x0i;
1239  a[j2 + 1] = -wk2i * x0i + wk2r * x0r;
1240  x0r = x1r - x3i;
1241  x0i = x1i + x3r;
1242  a[j1] = wk1r * x0r - wk1i * x0i;
1243  a[j1 + 1] = wk1r * x0i + wk1i * x0r;
1244  x0r = x1r + x3i;
1245  x0i = x1i - x3r;
1246  a[j3] = wk3r * x0r - wk3i * x0i;
1247  a[j3 + 1] = wk3r * x0i + wk3i * x0r;
1248  }
1249  }
1250 }
1251
1252
1253 void rftfsub(int n, double *a, int nc, double *c)
1254 {
1255  int j, k, kk, ks, m;
1256  double wkr, wki, xr, xi, yr, yi;
1257
1258  m = n >> 1;
1259  ks = 2 * nc / m;
1260  kk = 0;
1261  for (j = 2; j < m; j += 2) {
1262  k = n - j;
1263  kk += ks;
1264  wkr = 0.5 - c[nc - kk];
1265  wki = c[kk];
1266  xr = a[j] - a[k];
1267  xi = a[j + 1] + a[k + 1];
1268  yr = wkr * xr - wki * xi;
1269  yi = wkr * xi + wki * xr;
1270  a[j] -= yr;
1271  a[j + 1] -= yi;
1272  a[k] += yr;
1273  a[k + 1] -= yi;
1274  }
1275 }
1276
1277
1278 void rftbsub(int n, double *a, int nc, double *c)
1279 {
1280  int j, k, kk, ks, m;
1281  double wkr, wki, xr, xi, yr, yi;
1282
1283  a[1] = -a[1];
1284  m = n >> 1;
1285  ks = 2 * nc / m;
1286  kk = 0;
1287  for (j = 2; j < m; j += 2) {
1288  k = n - j;
1289  kk += ks;
1290  wkr = 0.5 - c[nc - kk];
1291  wki = c[kk];
1292  xr = a[j] - a[k];
1293  xi = a[j + 1] + a[k + 1];
1294  yr = wkr * xr + wki * xi;
1295  yi = wkr * xi - wki * xr;
1296  a[j] -= yr;
1297  a[j + 1] = yi - a[j + 1];
1298  a[k] += yr;
1299  a[k + 1] = yi - a[k + 1];
1300  }
1301  a[m + 1] = -a[m + 1];
1302 }
1303
1304
1305 void dctsub(int n, double *a, int nc, double *c)
1306 {
1307  int j, k, kk, ks, m;
1308  double wkr, wki, xr;
1309
1310  m = n >> 1;
1311  ks = nc / n;
1312  kk = 0;
1313  for (j = 1; j < m; j++) {
1314  k = n - j;
1315  kk += ks;
1316  wkr = c[kk] - c[nc - kk];
1317  wki = c[kk] + c[nc - kk];
1318  xr = wki * a[j] - wkr * a[k];
1319  a[j] = wkr * a[j] + wki * a[k];
1320  a[k] = xr;
1321  }
1322  a[m] *= c[0];
1323 }
1324
1325
1326 void dstsub(int n, double *a, int nc, double *c)
1327 {
1328  int j, k, kk, ks, m;
1329  double wkr, wki, xr;
1330
1331  m = n >> 1;
1332  ks = nc / n;
1333  kk = 0;
1334  for (j = 1; j < m; j++) {
1335  k = n - j;
1336  kk += ks;
1337  wkr = c[kk] - c[nc - kk];
1338  wki = c[kk] + c[nc - kk];
1339  xr = wki * a[k] - wkr * a[j];
1340  a[k] = wkr * a[k] + wki * a[j];
1341  a[j] = xr;
1342  }
1343  a[m] *= c[0];
1344 }
1345