1 /*
2 * http://www.kurims.kyoto-u.ac.jp/~ooura/fft.html
3 * Copyright Takuya OOURA, 1996-2001
4 *
5 * You may use, copy, modify and distribute this code for any purpose (include
6 * commercial use) and without fee. Please refer to this package when you modify
7 * this code.
8 *
9 * Changes:
10 * Trivial type modifications by the WebRTC authors.
11 */
12
13 /*
14 Fast Fourier/Cosine/Sine Transform
15 dimension :one
16 data length :power of 2
17 decimation :frequency
18 radix :4, 2
19 data :inplace
20 table :use
21 functions
22 cdft: Complex Discrete Fourier Transform
23 rdft: Real Discrete Fourier Transform
24 ddct: Discrete Cosine Transform
25 ddst: Discrete Sine Transform
26 dfct: Cosine Transform of RDFT (Real Symmetric DFT)
27 dfst: Sine Transform of RDFT (Real Anti-symmetric DFT)
28 function prototypes
29 void cdft(int, int, float *, int *, float *);
30 void rdft(size_t, int, float *, size_t *, float *);
31 void ddct(int, int, float *, int *, float *);
32 void ddst(int, int, float *, int *, float *);
33 void dfct(int, float *, float *, int *, float *);
34 void dfst(int, float *, float *, int *, float *);
35
36
37 -------- Complex DFT (Discrete Fourier Transform) --------
38 [definition]
39 <case1>
40 X[k] = sum_j=0^n-1 x[j]*exp(2*pi*i*j*k/n), 0<=k<n
41 <case2>
42 X[k] = sum_j=0^n-1 x[j]*exp(-2*pi*i*j*k/n), 0<=k<n
43 (notes: sum_j=0^n-1 is a summation from j=0 to n-1)
44 [usage]
45 <case1>
46 ip[0] = 0; // first time only
47 cdft(2*n, 1, a, ip, w);
48 <case2>
49 ip[0] = 0; // first time only
50 cdft(2*n, -1, a, ip, w);
51 [parameters]
52 2*n :data length (int)
53 n >= 1, n = power of 2
54 a[0...2*n-1] :input/output data (float *)
55 input data
56 a[2*j] = Re(x[j]),
57 a[2*j+1] = Im(x[j]), 0<=j<n
58 output data
59 a[2*k] = Re(X[k]),
60 a[2*k+1] = Im(X[k]), 0<=k<n
61 ip[0...*] :work area for bit reversal (int *)
62 length of ip >= 2+sqrt(n)
63 strictly,
64 length of ip >=
65 2+(1<<(int)(log(n+0.5)/log(2))/2).
66 ip[0],ip[1] are pointers of the cos/sin table.
67 w[0...n/2-1] :cos/sin table (float *)
68 w[],ip[] are initialized if ip[0] == 0.
69 [remark]
70 Inverse of
71 cdft(2*n, -1, a, ip, w);
72 is
73 cdft(2*n, 1, a, ip, w);
74 for (j = 0; j <= 2 * n - 1; j++) {
75 a[j] *= 1.0 / n;
76 }
77 .
78
79
80 -------- Real DFT / Inverse of Real DFT --------
81 [definition]
82 <case1> RDFT
83 R[k] = sum_j=0^n-1 a[j]*cos(2*pi*j*k/n), 0<=k<=n/2
84 I[k] = sum_j=0^n-1 a[j]*sin(2*pi*j*k/n), 0<k<n/2
85 <case2> IRDFT (excluding scale)
86 a[k] = (R[0] + R[n/2]*cos(pi*k))/2 +
87 sum_j=1^n/2-1 R[j]*cos(2*pi*j*k/n) +
88 sum_j=1^n/2-1 I[j]*sin(2*pi*j*k/n), 0<=k<n
89 [usage]
90 <case1>
91 ip[0] = 0; // first time only
92 rdft(n, 1, a, ip, w);
93 <case2>
94 ip[0] = 0; // first time only
95 rdft(n, -1, a, ip, w);
96 [parameters]
97 n :data length (size_t)
98 n >= 2, n = power of 2
99 a[0...n-1] :input/output data (float *)
100 <case1>
101 output data
102 a[2*k] = R[k], 0<=k<n/2
103 a[2*k+1] = I[k], 0<k<n/2
104 a[1] = R[n/2]
105 <case2>
106 input data
107 a[2*j] = R[j], 0<=j<n/2
108 a[2*j+1] = I[j], 0<j<n/2
109 a[1] = R[n/2]
110 ip[0...*] :work area for bit reversal (size_t *)
111 length of ip >= 2+sqrt(n/2)
112 strictly,
113 length of ip >=
114 2+(1<<(int)(log(n/2+0.5)/log(2))/2).
115 ip[0],ip[1] are pointers of the cos/sin table.
116 w[0...n/2-1] :cos/sin table (float *)
117 w[],ip[] are initialized if ip[0] == 0.
118 [remark]
119 Inverse of
120 rdft(n, 1, a, ip, w);
121 is
122 rdft(n, -1, a, ip, w);
123 for (j = 0; j <= n - 1; j++) {
124 a[j] *= 2.0 / n;
125 }
126 .
127
128
129 -------- DCT (Discrete Cosine Transform) / Inverse of DCT --------
130 [definition]
131 <case1> IDCT (excluding scale)
132 C[k] = sum_j=0^n-1 a[j]*cos(pi*j*(k+1/2)/n), 0<=k<n
133 <case2> DCT
134 C[k] = sum_j=0^n-1 a[j]*cos(pi*(j+1/2)*k/n), 0<=k<n
135 [usage]
136 <case1>
137 ip[0] = 0; // first time only
138 ddct(n, 1, a, ip, w);
139 <case2>
140 ip[0] = 0; // first time only
141 ddct(n, -1, a, ip, w);
142 [parameters]
143 n :data length (int)
144 n >= 2, n = power of 2
145 a[0...n-1] :input/output data (float *)
146 output data
147 a[k] = C[k], 0<=k<n
148 ip[0...*] :work area for bit reversal (int *)
149 length of ip >= 2+sqrt(n/2)
150 strictly,
151 length of ip >=
152 2+(1<<(int)(log(n/2+0.5)/log(2))/2).
153 ip[0],ip[1] are pointers of the cos/sin table.
154 w[0...n*5/4-1] :cos/sin table (float *)
155 w[],ip[] are initialized if ip[0] == 0.
156 [remark]
157 Inverse of
158 ddct(n, -1, a, ip, w);
159 is
160 a[0] *= 0.5;
161 ddct(n, 1, a, ip, w);
162 for (j = 0; j <= n - 1; j++) {
163 a[j] *= 2.0 / n;
164 }
165 .
166
167
168 -------- DST (Discrete Sine Transform) / Inverse of DST --------
169 [definition]
170 <case1> IDST (excluding scale)
171 S[k] = sum_j=1^n A[j]*sin(pi*j*(k+1/2)/n), 0<=k<n
172 <case2> DST
173 S[k] = sum_j=0^n-1 a[j]*sin(pi*(j+1/2)*k/n), 0<k<=n
174 [usage]
175 <case1>
176 ip[0] = 0; // first time only
177 ddst(n, 1, a, ip, w);
178 <case2>
179 ip[0] = 0; // first time only
180 ddst(n, -1, a, ip, w);
181 [parameters]
182 n :data length (int)
183 n >= 2, n = power of 2
184 a[0...n-1] :input/output data (float *)
185 <case1>
186 input data
187 a[j] = A[j], 0<j<n
188 a[0] = A[n]
189 output data
190 a[k] = S[k], 0<=k<n
191 <case2>
192 output data
193 a[k] = S[k], 0<k<n
194 a[0] = S[n]
195 ip[0...*] :work area for bit reversal (int *)
196 length of ip >= 2+sqrt(n/2)
197 strictly,
198 length of ip >=
199 2+(1<<(int)(log(n/2+0.5)/log(2))/2).
200 ip[0],ip[1] are pointers of the cos/sin table.
201 w[0...n*5/4-1] :cos/sin table (float *)
202 w[],ip[] are initialized if ip[0] == 0.
203 [remark]
204 Inverse of
205 ddst(n, -1, a, ip, w);
206 is
207 a[0] *= 0.5;
208 ddst(n, 1, a, ip, w);
209 for (j = 0; j <= n - 1; j++) {
210 a[j] *= 2.0 / n;
211 }
212 .
213
214
215 -------- Cosine Transform of RDFT (Real Symmetric DFT) --------
216 [definition]
217 C[k] = sum_j=0^n a[j]*cos(pi*j*k/n), 0<=k<=n
218 [usage]
219 ip[0] = 0; // first time only
220 dfct(n, a, t, ip, w);
221 [parameters]
222 n :data length - 1 (int)
223 n >= 2, n = power of 2
224 a[0...n] :input/output data (float *)
225 output data
226 a[k] = C[k], 0<=k<=n
227 t[0...n/2] :work area (float *)
228 ip[0...*] :work area for bit reversal (int *)
229 length of ip >= 2+sqrt(n/4)
230 strictly,
231 length of ip >=
232 2+(1<<(int)(log(n/4+0.5)/log(2))/2).
233 ip[0],ip[1] are pointers of the cos/sin table.
234 w[0...n*5/8-1] :cos/sin table (float *)
235 w[],ip[] are initialized if ip[0] == 0.
236 [remark]
237 Inverse of
238 a[0] *= 0.5;
239 a[n] *= 0.5;
240 dfct(n, a, t, ip, w);
241 is
242 a[0] *= 0.5;
243 a[n] *= 0.5;
244 dfct(n, a, t, ip, w);
245 for (j = 0; j <= n; j++) {
246 a[j] *= 2.0 / n;
247 }
248 .
249
250
251 -------- Sine Transform of RDFT (Real Anti-symmetric DFT) --------
252 [definition]
253 S[k] = sum_j=1^n-1 a[j]*sin(pi*j*k/n), 0<k<n
254 [usage]
255 ip[0] = 0; // first time only
256 dfst(n, a, t, ip, w);
257 [parameters]
258 n :data length + 1 (int)
259 n >= 2, n = power of 2
260 a[0...n-1] :input/output data (float *)
261 output data
262 a[k] = S[k], 0<k<n
263 (a[0] is used for work area)
264 t[0...n/2-1] :work area (float *)
265 ip[0...*] :work area for bit reversal (int *)
266 length of ip >= 2+sqrt(n/4)
267 strictly,
268 length of ip >=
269 2+(1<<(int)(log(n/4+0.5)/log(2))/2).
270 ip[0],ip[1] are pointers of the cos/sin table.
271 w[0...n*5/8-1] :cos/sin table (float *)
272 w[],ip[] are initialized if ip[0] == 0.
273 [remark]
274 Inverse of
275 dfst(n, a, t, ip, w);
276 is
277 dfst(n, a, t, ip, w);
278 for (j = 1; j <= n - 1; j++) {
279 a[j] *= 2.0 / n;
280 }
281 .
282
283
284 Appendix :
285 The cos/sin table is recalculated when the larger table required.
286 w[] and ip[] are compatible with all routines.
287 */
288
289 #include <math.h>
290 #include <stddef.h>
291
292 #include "common_audio/third_party/ooura/fft_size_256/fft4g.h"
293
294 namespace webrtc {
295
296 namespace {
297
298 void makewt(size_t nw, size_t* ip, float* w);
299 void makect(size_t nc, size_t* ip, float* c);
300 void bitrv2(size_t n, size_t* ip, float* a);
301 void cftfsub(size_t n, float* a, float* w);
302 void cftbsub(size_t n, float* a, float* w);
303 void cft1st(size_t n, float* a, float* w);
304 void cftmdl(size_t n, size_t l, float* a, float* w);
305 void rftfsub(size_t n, float* a, size_t nc, float* c);
306 void rftbsub(size_t n, float* a, size_t nc, float* c);
307
308 /* -------- initializing routines -------- */
309
makewt(size_t nw,size_t * ip,float * w)310 void makewt(size_t nw, size_t* ip, float* w) {
311 size_t j, nwh;
312 float delta, x, y;
313
314 ip[0] = nw;
315 ip[1] = 1;
316 if (nw > 2) {
317 nwh = nw >> 1;
318 delta = atanf(1.0f) / nwh;
319 w[0] = 1;
320 w[1] = 0;
321 w[nwh] = (float)cos(delta * nwh);
322 w[nwh + 1] = w[nwh];
323 if (nwh > 2) {
324 for (j = 2; j < nwh; j += 2) {
325 x = (float)cos(delta * j);
326 y = (float)sin(delta * j);
327 w[j] = x;
328 w[j + 1] = y;
329 w[nw - j] = y;
330 w[nw - j + 1] = x;
331 }
332 bitrv2(nw, ip + 2, w);
333 }
334 }
335 }
336
makect(size_t nc,size_t * ip,float * c)337 void makect(size_t nc, size_t* ip, float* c) {
338 size_t j, nch;
339 float delta;
340
341 ip[1] = nc;
342 if (nc > 1) {
343 nch = nc >> 1;
344 delta = atanf(1.0f) / nch;
345 c[0] = (float)cos(delta * nch);
346 c[nch] = 0.5f * c[0];
347 for (j = 1; j < nch; j++) {
348 c[j] = 0.5f * (float)cos(delta * j);
349 c[nc - j] = 0.5f * (float)sin(delta * j);
350 }
351 }
352 }
353
354 /* -------- child routines -------- */
355
bitrv2(size_t n,size_t * ip,float * a)356 void bitrv2(size_t n, size_t* ip, float* a) {
357 size_t j, j1, k, k1, l, m, m2;
358 float xr, xi, yr, yi;
359
360 ip[0] = 0;
361 l = n;
362 m = 1;
363 while ((m << 3) < l) {
364 l >>= 1;
365 for (j = 0; j < m; j++) {
366 ip[m + j] = ip[j] + l;
367 }
368 m <<= 1;
369 }
370 m2 = 2 * m;
371 if ((m << 3) == l) {
372 for (k = 0; k < m; k++) {
373 for (j = 0; j < k; j++) {
374 j1 = 2 * j + ip[k];
375 k1 = 2 * k + ip[j];
376 xr = a[j1];
377 xi = a[j1 + 1];
378 yr = a[k1];
379 yi = a[k1 + 1];
380 a[j1] = yr;
381 a[j1 + 1] = yi;
382 a[k1] = xr;
383 a[k1 + 1] = xi;
384 j1 += m2;
385 k1 += 2 * m2;
386 xr = a[j1];
387 xi = a[j1 + 1];
388 yr = a[k1];
389 yi = a[k1 + 1];
390 a[j1] = yr;
391 a[j1 + 1] = yi;
392 a[k1] = xr;
393 a[k1 + 1] = xi;
394 j1 += m2;
395 k1 -= m2;
396 xr = a[j1];
397 xi = a[j1 + 1];
398 yr = a[k1];
399 yi = a[k1 + 1];
400 a[j1] = yr;
401 a[j1 + 1] = yi;
402 a[k1] = xr;
403 a[k1 + 1] = xi;
404 j1 += m2;
405 k1 += 2 * m2;
406 xr = a[j1];
407 xi = a[j1 + 1];
408 yr = a[k1];
409 yi = a[k1 + 1];
410 a[j1] = yr;
411 a[j1 + 1] = yi;
412 a[k1] = xr;
413 a[k1 + 1] = xi;
414 }
415 j1 = 2 * k + m2 + ip[k];
416 k1 = j1 + m2;
417 xr = a[j1];
418 xi = a[j1 + 1];
419 yr = a[k1];
420 yi = a[k1 + 1];
421 a[j1] = yr;
422 a[j1 + 1] = yi;
423 a[k1] = xr;
424 a[k1 + 1] = xi;
425 }
426 } else {
427 for (k = 1; k < m; k++) {
428 for (j = 0; j < k; j++) {
429 j1 = 2 * j + ip[k];
430 k1 = 2 * k + ip[j];
431 xr = a[j1];
432 xi = a[j1 + 1];
433 yr = a[k1];
434 yi = a[k1 + 1];
435 a[j1] = yr;
436 a[j1 + 1] = yi;
437 a[k1] = xr;
438 a[k1 + 1] = xi;
439 j1 += m2;
440 k1 += m2;
441 xr = a[j1];
442 xi = a[j1 + 1];
443 yr = a[k1];
444 yi = a[k1 + 1];
445 a[j1] = yr;
446 a[j1 + 1] = yi;
447 a[k1] = xr;
448 a[k1 + 1] = xi;
449 }
450 }
451 }
452 }
453
cftfsub(size_t n,float * a,float * w)454 void cftfsub(size_t n, float* a, float* w) {
455 size_t j, j1, j2, j3, l;
456 float x0r, x0i, x1r, x1i, x2r, x2i, x3r, x3i;
457
458 l = 2;
459 if (n > 8) {
460 cft1st(n, a, w);
461 l = 8;
462 while ((l << 2) < n) {
463 cftmdl(n, l, a, w);
464 l <<= 2;
465 }
466 }
467 if ((l << 2) == n) {
468 for (j = 0; j < l; j += 2) {
469 j1 = j + l;
470 j2 = j1 + l;
471 j3 = j2 + l;
472 x0r = a[j] + a[j1];
473 x0i = a[j + 1] + a[j1 + 1];
474 x1r = a[j] - a[j1];
475 x1i = a[j + 1] - a[j1 + 1];
476 x2r = a[j2] + a[j3];
477 x2i = a[j2 + 1] + a[j3 + 1];
478 x3r = a[j2] - a[j3];
479 x3i = a[j2 + 1] - a[j3 + 1];
480 a[j] = x0r + x2r;
481 a[j + 1] = x0i + x2i;
482 a[j2] = x0r - x2r;
483 a[j2 + 1] = x0i - x2i;
484 a[j1] = x1r - x3i;
485 a[j1 + 1] = x1i + x3r;
486 a[j3] = x1r + x3i;
487 a[j3 + 1] = x1i - x3r;
488 }
489 } else {
490 for (j = 0; j < l; j += 2) {
491 j1 = j + l;
492 x0r = a[j] - a[j1];
493 x0i = a[j + 1] - a[j1 + 1];
494 a[j] += a[j1];
495 a[j + 1] += a[j1 + 1];
496 a[j1] = x0r;
497 a[j1 + 1] = x0i;
498 }
499 }
500 }
501
cftbsub(size_t n,float * a,float * w)502 void cftbsub(size_t n, float* a, float* w) {
503 size_t j, j1, j2, j3, l;
504 float x0r, x0i, x1r, x1i, x2r, x2i, x3r, x3i;
505
506 l = 2;
507 if (n > 8) {
508 cft1st(n, a, w);
509 l = 8;
510 while ((l << 2) < n) {
511 cftmdl(n, l, a, w);
512 l <<= 2;
513 }
514 }
515 if ((l << 2) == n) {
516 for (j = 0; j < l; j += 2) {
517 j1 = j + l;
518 j2 = j1 + l;
519 j3 = j2 + l;
520 x0r = a[j] + a[j1];
521 x0i = -a[j + 1] - a[j1 + 1];
522 x1r = a[j] - a[j1];
523 x1i = -a[j + 1] + a[j1 + 1];
524 x2r = a[j2] + a[j3];
525 x2i = a[j2 + 1] + a[j3 + 1];
526 x3r = a[j2] - a[j3];
527 x3i = a[j2 + 1] - a[j3 + 1];
528 a[j] = x0r + x2r;
529 a[j + 1] = x0i - x2i;
530 a[j2] = x0r - x2r;
531 a[j2 + 1] = x0i + x2i;
532 a[j1] = x1r - x3i;
533 a[j1 + 1] = x1i - x3r;
534 a[j3] = x1r + x3i;
535 a[j3 + 1] = x1i + x3r;
536 }
537 } else {
538 for (j = 0; j < l; j += 2) {
539 j1 = j + l;
540 x0r = a[j] - a[j1];
541 x0i = -a[j + 1] + a[j1 + 1];
542 a[j] += a[j1];
543 a[j + 1] = -a[j + 1] - a[j1 + 1];
544 a[j1] = x0r;
545 a[j1 + 1] = x0i;
546 }
547 }
548 }
549
cft1st(size_t n,float * a,float * w)550 void cft1st(size_t n, float* a, float* w) {
551 size_t j, k1, k2;
552 float wk1r, wk1i, wk2r, wk2i, wk3r, wk3i;
553 float x0r, x0i, x1r, x1i, x2r, x2i, x3r, x3i;
554
555 x0r = a[0] + a[2];
556 x0i = a[1] + a[3];
557 x1r = a[0] - a[2];
558 x1i = a[1] - a[3];
559 x2r = a[4] + a[6];
560 x2i = a[5] + a[7];
561 x3r = a[4] - a[6];
562 x3i = a[5] - a[7];
563 a[0] = x0r + x2r;
564 a[1] = x0i + x2i;
565 a[4] = x0r - x2r;
566 a[5] = x0i - x2i;
567 a[2] = x1r - x3i;
568 a[3] = x1i + x3r;
569 a[6] = x1r + x3i;
570 a[7] = x1i - x3r;
571 wk1r = w[2];
572 x0r = a[8] + a[10];
573 x0i = a[9] + a[11];
574 x1r = a[8] - a[10];
575 x1i = a[9] - a[11];
576 x2r = a[12] + a[14];
577 x2i = a[13] + a[15];
578 x3r = a[12] - a[14];
579 x3i = a[13] - a[15];
580 a[8] = x0r + x2r;
581 a[9] = x0i + x2i;
582 a[12] = x2i - x0i;
583 a[13] = x0r - x2r;
584 x0r = x1r - x3i;
585 x0i = x1i + x3r;
586 a[10] = wk1r * (x0r - x0i);
587 a[11] = wk1r * (x0r + x0i);
588 x0r = x3i + x1r;
589 x0i = x3r - x1i;
590 a[14] = wk1r * (x0i - x0r);
591 a[15] = wk1r * (x0i + x0r);
592 k1 = 0;
593 for (j = 16; j < n; j += 16) {
594 k1 += 2;
595 k2 = 2 * k1;
596 wk2r = w[k1];
597 wk2i = w[k1 + 1];
598 wk1r = w[k2];
599 wk1i = w[k2 + 1];
600 wk3r = wk1r - 2 * wk2i * wk1i;
601 wk3i = 2 * wk2i * wk1r - wk1i;
602 x0r = a[j] + a[j + 2];
603 x0i = a[j + 1] + a[j + 3];
604 x1r = a[j] - a[j + 2];
605 x1i = a[j + 1] - a[j + 3];
606 x2r = a[j + 4] + a[j + 6];
607 x2i = a[j + 5] + a[j + 7];
608 x3r = a[j + 4] - a[j + 6];
609 x3i = a[j + 5] - a[j + 7];
610 a[j] = x0r + x2r;
611 a[j + 1] = x0i + x2i;
612 x0r -= x2r;
613 x0i -= x2i;
614 a[j + 4] = wk2r * x0r - wk2i * x0i;
615 a[j + 5] = wk2r * x0i + wk2i * x0r;
616 x0r = x1r - x3i;
617 x0i = x1i + x3r;
618 a[j + 2] = wk1r * x0r - wk1i * x0i;
619 a[j + 3] = wk1r * x0i + wk1i * x0r;
620 x0r = x1r + x3i;
621 x0i = x1i - x3r;
622 a[j + 6] = wk3r * x0r - wk3i * x0i;
623 a[j + 7] = wk3r * x0i + wk3i * x0r;
624 wk1r = w[k2 + 2];
625 wk1i = w[k2 + 3];
626 wk3r = wk1r - 2 * wk2r * wk1i;
627 wk3i = 2 * wk2r * wk1r - wk1i;
628 x0r = a[j + 8] + a[j + 10];
629 x0i = a[j + 9] + a[j + 11];
630 x1r = a[j + 8] - a[j + 10];
631 x1i = a[j + 9] - a[j + 11];
632 x2r = a[j + 12] + a[j + 14];
633 x2i = a[j + 13] + a[j + 15];
634 x3r = a[j + 12] - a[j + 14];
635 x3i = a[j + 13] - a[j + 15];
636 a[j + 8] = x0r + x2r;
637 a[j + 9] = x0i + x2i;
638 x0r -= x2r;
639 x0i -= x2i;
640 a[j + 12] = -wk2i * x0r - wk2r * x0i;
641 a[j + 13] = -wk2i * x0i + wk2r * x0r;
642 x0r = x1r - x3i;
643 x0i = x1i + x3r;
644 a[j + 10] = wk1r * x0r - wk1i * x0i;
645 a[j + 11] = wk1r * x0i + wk1i * x0r;
646 x0r = x1r + x3i;
647 x0i = x1i - x3r;
648 a[j + 14] = wk3r * x0r - wk3i * x0i;
649 a[j + 15] = wk3r * x0i + wk3i * x0r;
650 }
651 }
652
cftmdl(size_t n,size_t l,float * a,float * w)653 void cftmdl(size_t n, size_t l, float* a, float* w) {
654 size_t j, j1, j2, j3, k, k1, k2, m, m2;
655 float wk1r, wk1i, wk2r, wk2i, wk3r, wk3i;
656 float x0r, x0i, x1r, x1i, x2r, x2i, x3r, x3i;
657
658 m = l << 2;
659 for (j = 0; j < l; j += 2) {
660 j1 = j + l;
661 j2 = j1 + l;
662 j3 = j2 + l;
663 x0r = a[j] + a[j1];
664 x0i = a[j + 1] + a[j1 + 1];
665 x1r = a[j] - a[j1];
666 x1i = a[j + 1] - a[j1 + 1];
667 x2r = a[j2] + a[j3];
668 x2i = a[j2 + 1] + a[j3 + 1];
669 x3r = a[j2] - a[j3];
670 x3i = a[j2 + 1] - a[j3 + 1];
671 a[j] = x0r + x2r;
672 a[j + 1] = x0i + x2i;
673 a[j2] = x0r - x2r;
674 a[j2 + 1] = x0i - x2i;
675 a[j1] = x1r - x3i;
676 a[j1 + 1] = x1i + x3r;
677 a[j3] = x1r + x3i;
678 a[j3 + 1] = x1i - x3r;
679 }
680 wk1r = w[2];
681 for (j = m; j < l + m; j += 2) {
682 j1 = j + l;
683 j2 = j1 + l;
684 j3 = j2 + l;
685 x0r = a[j] + a[j1];
686 x0i = a[j + 1] + a[j1 + 1];
687 x1r = a[j] - a[j1];
688 x1i = a[j + 1] - a[j1 + 1];
689 x2r = a[j2] + a[j3];
690 x2i = a[j2 + 1] + a[j3 + 1];
691 x3r = a[j2] - a[j3];
692 x3i = a[j2 + 1] - a[j3 + 1];
693 a[j] = x0r + x2r;
694 a[j + 1] = x0i + x2i;
695 a[j2] = x2i - x0i;
696 a[j2 + 1] = x0r - x2r;
697 x0r = x1r - x3i;
698 x0i = x1i + x3r;
699 a[j1] = wk1r * (x0r - x0i);
700 a[j1 + 1] = wk1r * (x0r + x0i);
701 x0r = x3i + x1r;
702 x0i = x3r - x1i;
703 a[j3] = wk1r * (x0i - x0r);
704 a[j3 + 1] = wk1r * (x0i + x0r);
705 }
706 k1 = 0;
707 m2 = 2 * m;
708 for (k = m2; k < n; k += m2) {
709 k1 += 2;
710 k2 = 2 * k1;
711 wk2r = w[k1];
712 wk2i = w[k1 + 1];
713 wk1r = w[k2];
714 wk1i = w[k2 + 1];
715 wk3r = wk1r - 2 * wk2i * wk1i;
716 wk3i = 2 * wk2i * wk1r - wk1i;
717 for (j = k; j < l + k; j += 2) {
718 j1 = j + l;
719 j2 = j1 + l;
720 j3 = j2 + l;
721 x0r = a[j] + a[j1];
722 x0i = a[j + 1] + a[j1 + 1];
723 x1r = a[j] - a[j1];
724 x1i = a[j + 1] - a[j1 + 1];
725 x2r = a[j2] + a[j3];
726 x2i = a[j2 + 1] + a[j3 + 1];
727 x3r = a[j2] - a[j3];
728 x3i = a[j2 + 1] - a[j3 + 1];
729 a[j] = x0r + x2r;
730 a[j + 1] = x0i + x2i;
731 x0r -= x2r;
732 x0i -= x2i;
733 a[j2] = wk2r * x0r - wk2i * x0i;
734 a[j2 + 1] = wk2r * x0i + wk2i * x0r;
735 x0r = x1r - x3i;
736 x0i = x1i + x3r;
737 a[j1] = wk1r * x0r - wk1i * x0i;
738 a[j1 + 1] = wk1r * x0i + wk1i * x0r;
739 x0r = x1r + x3i;
740 x0i = x1i - x3r;
741 a[j3] = wk3r * x0r - wk3i * x0i;
742 a[j3 + 1] = wk3r * x0i + wk3i * x0r;
743 }
744 wk1r = w[k2 + 2];
745 wk1i = w[k2 + 3];
746 wk3r = wk1r - 2 * wk2r * wk1i;
747 wk3i = 2 * wk2r * wk1r - wk1i;
748 for (j = k + m; j < l + (k + m); j += 2) {
749 j1 = j + l;
750 j2 = j1 + l;
751 j3 = j2 + l;
752 x0r = a[j] + a[j1];
753 x0i = a[j + 1] + a[j1 + 1];
754 x1r = a[j] - a[j1];
755 x1i = a[j + 1] - a[j1 + 1];
756 x2r = a[j2] + a[j3];
757 x2i = a[j2 + 1] + a[j3 + 1];
758 x3r = a[j2] - a[j3];
759 x3i = a[j2 + 1] - a[j3 + 1];
760 a[j] = x0r + x2r;
761 a[j + 1] = x0i + x2i;
762 x0r -= x2r;
763 x0i -= x2i;
764 a[j2] = -wk2i * x0r - wk2r * x0i;
765 a[j2 + 1] = -wk2i * x0i + wk2r * x0r;
766 x0r = x1r - x3i;
767 x0i = x1i + x3r;
768 a[j1] = wk1r * x0r - wk1i * x0i;
769 a[j1 + 1] = wk1r * x0i + wk1i * x0r;
770 x0r = x1r + x3i;
771 x0i = x1i - x3r;
772 a[j3] = wk3r * x0r - wk3i * x0i;
773 a[j3 + 1] = wk3r * x0i + wk3i * x0r;
774 }
775 }
776 }
777
rftfsub(size_t n,float * a,size_t nc,float * c)778 void rftfsub(size_t n, float* a, size_t nc, float* c) {
779 size_t j, k, kk, ks, m;
780 float wkr, wki, xr, xi, yr, yi;
781
782 m = n >> 1;
783 ks = 2 * nc / m;
784 kk = 0;
785 for (j = 2; j < m; j += 2) {
786 k = n - j;
787 kk += ks;
788 wkr = 0.5f - c[nc - kk];
789 wki = c[kk];
790 xr = a[j] - a[k];
791 xi = a[j + 1] + a[k + 1];
792 yr = wkr * xr - wki * xi;
793 yi = wkr * xi + wki * xr;
794 a[j] -= yr;
795 a[j + 1] -= yi;
796 a[k] += yr;
797 a[k + 1] -= yi;
798 }
799 }
800
rftbsub(size_t n,float * a,size_t nc,float * c)801 void rftbsub(size_t n, float* a, size_t nc, float* c) {
802 size_t j, k, kk, ks, m;
803 float wkr, wki, xr, xi, yr, yi;
804
805 a[1] = -a[1];
806 m = n >> 1;
807 ks = 2 * nc / m;
808 kk = 0;
809 for (j = 2; j < m; j += 2) {
810 k = n - j;
811 kk += ks;
812 wkr = 0.5f - c[nc - kk];
813 wki = c[kk];
814 xr = a[j] - a[k];
815 xi = a[j + 1] + a[k + 1];
816 yr = wkr * xr + wki * xi;
817 yi = wkr * xi - wki * xr;
818 a[j] -= yr;
819 a[j + 1] = yi - a[j + 1];
820 a[k] += yr;
821 a[k + 1] = yi - a[k + 1];
822 }
823 a[m + 1] = -a[m + 1];
824 }
825
826 } // namespace
827
WebRtc_rdft(size_t n,int isgn,float * a,size_t * ip,float * w)828 void WebRtc_rdft(size_t n, int isgn, float* a, size_t* ip, float* w) {
829 size_t nw, nc;
830 float xi;
831
832 nw = ip[0];
833 if (n > (nw << 2)) {
834 nw = n >> 2;
835 makewt(nw, ip, w);
836 }
837 nc = ip[1];
838 if (n > (nc << 2)) {
839 nc = n >> 2;
840 makect(nc, ip, w + nw);
841 }
842 if (isgn >= 0) {
843 if (n > 4) {
844 bitrv2(n, ip + 2, a);
845 cftfsub(n, a, w);
846 rftfsub(n, a, nc, w + nw);
847 } else if (n == 4) {
848 cftfsub(n, a, w);
849 }
850 xi = a[0] - a[1];
851 a[0] += a[1];
852 a[1] = xi;
853 } else {
854 a[1] = 0.5f * (a[0] - a[1]);
855 a[0] -= a[1];
856 if (n > 4) {
857 rftbsub(n, a, nc, w + nw);
858 bitrv2(n, ip + 2, a);
859 cftbsub(n, a, w);
860 } else if (n == 4) {
861 cftfsub(n, a, w);
862 }
863 }
864 }
865
866 } // namespace webrtc
867