You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

942 lines
26 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright(c)1995,97 Mark Olesen <olesen@me.QueensU.CA>
  3. * Queen's Univ at Kingston (Canada)
  4. *
  5. * Permission to use, copy, modify, and distribute this software for
  6. * any purpose without fee is hereby granted, provided that this
  7. * entire notice is included in all copies of any software which is
  8. * or includes a copy or modification of this software and in all
  9. * copies of the supporting documentation for such software.
  10. *
  11. * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
  12. * IMPLIED WARRANTY. IN PARTICULAR, NEITHER THE AUTHOR NOR QUEEN'S
  13. * UNIVERSITY AT KINGSTON MAKES ANY REPRESENTATION OR WARRANTY OF ANY
  14. * KIND CONCERNING THE MERCHANTABILITY OF THIS SOFTWARE OR ITS
  15. * FITNESS FOR ANY PARTICULAR PURPOSE.
  16. *
  17. * All of which is to say that you can do what you like with this
  18. * source code provided you don't try to sell it as your own and you
  19. * include an unaltered copy of this message (including the
  20. * copyright).
  21. *
  22. * It is also implicitly understood that bug fixes and improvements
  23. * should make their way back to the general Internet community so
  24. * that everyone benefits.
  25. *
  26. * Changes:
  27. * Trivial type modifications by the WebRTC authors.
  28. */
  29. /*
  30. * File:
  31. * WebRtcIsac_Fftn.c
  32. *
  33. * Public:
  34. * WebRtcIsac_Fftn / fftnf ();
  35. *
  36. * Private:
  37. * WebRtcIsac_Fftradix / fftradixf ();
  38. *
  39. * Descript:
  40. * multivariate complex Fourier transform, computed in place
  41. * using mixed-radix Fast Fourier Transform algorithm.
  42. *
  43. * Fortran code by:
  44. * RC Singleton, Stanford Research Institute, Sept. 1968
  45. *
  46. * translated by f2c (version 19950721).
  47. *
  48. * int WebRtcIsac_Fftn (int ndim, const int dims[], REAL Re[], REAL Im[],
  49. * int iSign, double scaling);
  50. *
  51. * NDIM = the total number dimensions
  52. * DIMS = a vector of array sizes
  53. * if NDIM is zero then DIMS must be zero-terminated
  54. *
  55. * RE and IM hold the real and imaginary components of the data, and return
  56. * the resulting real and imaginary Fourier coefficients. Multidimensional
  57. * data *must* be allocated contiguously. There is no limit on the number
  58. * of dimensions.
  59. *
  60. * ISIGN = the sign of the complex exponential (ie, forward or inverse FFT)
  61. * the magnitude of ISIGN (normally 1) is used to determine the
  62. * correct indexing increment (see below).
  63. *
  64. * SCALING = normalizing constant by which the final result is *divided*
  65. * if SCALING == -1, normalize by total dimension of the transform
  66. * if SCALING < -1, normalize by the square-root of the total dimension
  67. *
  68. * example:
  69. * tri-variate transform with Re[n1][n2][n3], Im[n1][n2][n3]
  70. *
  71. * int dims[3] = {n1,n2,n3}
  72. * WebRtcIsac_Fftn (3, dims, Re, Im, 1, scaling);
  73. *
  74. *-----------------------------------------------------------------------*
  75. * int WebRtcIsac_Fftradix (REAL Re[], REAL Im[], size_t nTotal, size_t nPass,
  76. * size_t nSpan, int iSign, size_t max_factors,
  77. * size_t max_perm);
  78. *
  79. * RE, IM - see above documentation
  80. *
  81. * Although there is no limit on the number of dimensions, WebRtcIsac_Fftradix() must
  82. * be called once for each dimension, but the calls may be in any order.
  83. *
  84. * NTOTAL = the total number of complex data values
  85. * NPASS = the dimension of the current variable
  86. * NSPAN/NPASS = the spacing of consecutive data values while indexing the
  87. * current variable
  88. * ISIGN - see above documentation
  89. *
  90. * example:
  91. * tri-variate transform with Re[n1][n2][n3], Im[n1][n2][n3]
  92. *
  93. * WebRtcIsac_Fftradix (Re, Im, n1*n2*n3, n1, n1, 1, maxf, maxp);
  94. * WebRtcIsac_Fftradix (Re, Im, n1*n2*n3, n2, n1*n2, 1, maxf, maxp);
  95. * WebRtcIsac_Fftradix (Re, Im, n1*n2*n3, n3, n1*n2*n3, 1, maxf, maxp);
  96. *
  97. * single-variate transform,
  98. * NTOTAL = N = NSPAN = (number of complex data values),
  99. *
  100. * WebRtcIsac_Fftradix (Re, Im, n, n, n, 1, maxf, maxp);
  101. *
  102. * The data can also be stored in a single array with alternating real and
  103. * imaginary parts, the magnitude of ISIGN is changed to 2 to give correct
  104. * indexing increment, and data [0] and data [1] used to pass the initial
  105. * addresses for the sequences of real and imaginary values,
  106. *
  107. * example:
  108. * REAL data [2*NTOTAL];
  109. * WebRtcIsac_Fftradix ( &data[0], &data[1], NTOTAL, nPass, nSpan, 2, maxf, maxp);
  110. *
  111. * for temporary allocation:
  112. *
  113. * MAX_FACTORS >= the maximum prime factor of NPASS
  114. * MAX_PERM >= the number of prime factors of NPASS. In addition,
  115. * if the square-free portion K of NPASS has two or more prime
  116. * factors, then MAX_PERM >= (K-1)
  117. *
  118. * storage in FACTOR for a maximum of 15 prime factors of NPASS. if NPASS
  119. * has more than one square-free factor, the product of the square-free
  120. * factors must be <= 210 array storage for maximum prime factor of 23 the
  121. * following two constants should agree with the array dimensions.
  122. *
  123. *----------------------------------------------------------------------*/
  124. #include <stdlib.h>
  125. #include <math.h>
  126. #include "modules/third_party/fft/fft.h"
  127. /* double precision routine */
  128. static int
  129. WebRtcIsac_Fftradix (double Re[], double Im[],
  130. size_t nTotal, size_t nPass, size_t nSpan, int isign,
  131. int max_factors, unsigned int max_perm,
  132. FFTstr *fftstate);
  133. #ifndef M_PI
  134. # define M_PI 3.14159265358979323846264338327950288
  135. #endif
  136. #ifndef SIN60
  137. # define SIN60 0.86602540378443865 /* sin(60 deg) */
  138. # define COS72 0.30901699437494742 /* cos(72 deg) */
  139. # define SIN72 0.95105651629515357 /* sin(72 deg) */
  140. #endif
  141. # define REAL double
  142. # define FFTN WebRtcIsac_Fftn
  143. # define FFTNS "fftn"
  144. # define FFTRADIX WebRtcIsac_Fftradix
  145. # define FFTRADIXS "fftradix"
  146. int WebRtcIsac_Fftns(unsigned int ndim, const int dims[],
  147. double Re[],
  148. double Im[],
  149. int iSign,
  150. double scaling,
  151. FFTstr *fftstate)
  152. {
  153. size_t nSpan, nPass, nTotal;
  154. unsigned int i;
  155. int ret, max_factors, max_perm;
  156. /*
  157. * tally the number of elements in the data array
  158. * and determine the number of dimensions
  159. */
  160. nTotal = 1;
  161. if (ndim && dims [0])
  162. {
  163. for (i = 0; i < ndim; i++)
  164. {
  165. if (dims [i] <= 0)
  166. {
  167. return -1;
  168. }
  169. nTotal *= dims [i];
  170. }
  171. }
  172. else
  173. {
  174. ndim = 0;
  175. for (i = 0; dims [i]; i++)
  176. {
  177. if (dims [i] <= 0)
  178. {
  179. return -1;
  180. }
  181. nTotal *= dims [i];
  182. ndim++;
  183. }
  184. }
  185. /* determine maximum number of factors and permuations */
  186. #if 1
  187. /*
  188. * follow John Beale's example, just use the largest dimension and don't
  189. * worry about excess allocation. May be someone else will do it?
  190. */
  191. max_factors = max_perm = 1;
  192. for (i = 0; i < ndim; i++)
  193. {
  194. nSpan = dims [i];
  195. if ((int)nSpan > max_factors)
  196. {
  197. max_factors = (int)nSpan;
  198. }
  199. if ((int)nSpan > max_perm)
  200. {
  201. max_perm = (int)nSpan;
  202. }
  203. }
  204. #else
  205. /* use the constants used in the original Fortran code */
  206. max_factors = 23;
  207. max_perm = 209;
  208. #endif
  209. /* loop over the dimensions: */
  210. nPass = 1;
  211. for (i = 0; i < ndim; i++)
  212. {
  213. nSpan = dims [i];
  214. nPass *= nSpan;
  215. ret = FFTRADIX (Re, Im, nTotal, nSpan, nPass, iSign,
  216. max_factors, max_perm, fftstate);
  217. /* exit, clean-up already done */
  218. if (ret)
  219. return ret;
  220. }
  221. /* Divide through by the normalizing constant: */
  222. if (scaling && scaling != 1.0)
  223. {
  224. if (iSign < 0) iSign = -iSign;
  225. if (scaling < 0.0)
  226. {
  227. scaling = (double)nTotal;
  228. if (scaling < -1.0)
  229. scaling = sqrt (scaling);
  230. }
  231. scaling = 1.0 / scaling; /* multiply is often faster */
  232. for (i = 0; i < nTotal; i += iSign)
  233. {
  234. Re [i] *= scaling;
  235. Im [i] *= scaling;
  236. }
  237. }
  238. return 0;
  239. }
  240. /*
  241. * singleton's mixed radix routine
  242. *
  243. * could move allocation out to WebRtcIsac_Fftn(), but leave it here so that it's
  244. * possible to make this a standalone function
  245. */
  246. static int FFTRADIX (REAL Re[],
  247. REAL Im[],
  248. size_t nTotal,
  249. size_t nPass,
  250. size_t nSpan,
  251. int iSign,
  252. int max_factors,
  253. unsigned int max_perm,
  254. FFTstr *fftstate)
  255. {
  256. int ii, mfactor, kspan, ispan, inc;
  257. int j, jc, jf, jj, k, k1, k2, k3, k4, kk, kt, nn, ns, nt;
  258. REAL radf;
  259. REAL c1, c2, c3, cd, aa, aj, ak, ajm, ajp, akm, akp;
  260. REAL s1, s2, s3, sd, bb, bj, bk, bjm, bjp, bkm, bkp;
  261. REAL *Rtmp = NULL; /* temp space for real part*/
  262. REAL *Itmp = NULL; /* temp space for imaginary part */
  263. REAL *Cos = NULL; /* Cosine values */
  264. REAL *Sin = NULL; /* Sine values */
  265. REAL s60 = SIN60; /* sin(60 deg) */
  266. REAL c72 = COS72; /* cos(72 deg) */
  267. REAL s72 = SIN72; /* sin(72 deg) */
  268. REAL pi2 = M_PI; /* use PI first, 2 PI later */
  269. fftstate->SpaceAlloced = 0;
  270. fftstate->MaxPermAlloced = 0;
  271. // initialize to avoid warnings
  272. k3 = c2 = c3 = s2 = s3 = 0.0;
  273. if (nPass < 2)
  274. return 0;
  275. /* allocate storage */
  276. if (fftstate->SpaceAlloced < max_factors * sizeof (REAL))
  277. {
  278. #ifdef SUN_BROKEN_REALLOC
  279. if (!fftstate->SpaceAlloced) /* first time */
  280. {
  281. fftstate->SpaceAlloced = max_factors * sizeof (REAL);
  282. }
  283. else
  284. {
  285. #endif
  286. fftstate->SpaceAlloced = max_factors * sizeof (REAL);
  287. #ifdef SUN_BROKEN_REALLOC
  288. }
  289. #endif
  290. }
  291. else
  292. {
  293. /* allow full use of alloc'd space */
  294. max_factors = fftstate->SpaceAlloced / sizeof (REAL);
  295. }
  296. if (fftstate->MaxPermAlloced < max_perm)
  297. {
  298. #ifdef SUN_BROKEN_REALLOC
  299. if (!fftstate->MaxPermAlloced) /* first time */
  300. else
  301. #endif
  302. fftstate->MaxPermAlloced = max_perm;
  303. }
  304. else
  305. {
  306. /* allow full use of alloc'd space */
  307. max_perm = fftstate->MaxPermAlloced;
  308. }
  309. /* assign pointers */
  310. Rtmp = (REAL *) fftstate->Tmp0;
  311. Itmp = (REAL *) fftstate->Tmp1;
  312. Cos = (REAL *) fftstate->Tmp2;
  313. Sin = (REAL *) fftstate->Tmp3;
  314. /*
  315. * Function Body
  316. */
  317. inc = iSign;
  318. if (iSign < 0) {
  319. s72 = -s72;
  320. s60 = -s60;
  321. pi2 = -pi2;
  322. inc = -inc; /* absolute value */
  323. }
  324. /* adjust for strange increments */
  325. nt = inc * (int)nTotal;
  326. ns = inc * (int)nSpan;
  327. kspan = ns;
  328. nn = nt - inc;
  329. jc = ns / (int)nPass;
  330. radf = pi2 * (double) jc;
  331. pi2 *= 2.0; /* use 2 PI from here on */
  332. ii = 0;
  333. jf = 0;
  334. /* determine the factors of n */
  335. mfactor = 0;
  336. k = (int)nPass;
  337. while (k % 16 == 0) {
  338. mfactor++;
  339. fftstate->factor [mfactor - 1] = 4;
  340. k /= 16;
  341. }
  342. j = 3;
  343. jj = 9;
  344. do {
  345. while (k % jj == 0) {
  346. mfactor++;
  347. fftstate->factor [mfactor - 1] = j;
  348. k /= jj;
  349. }
  350. j += 2;
  351. jj = j * j;
  352. } while (jj <= k);
  353. if (k <= 4) {
  354. kt = mfactor;
  355. fftstate->factor [mfactor] = k;
  356. if (k != 1)
  357. mfactor++;
  358. } else {
  359. if (k - (k / 4 << 2) == 0) {
  360. mfactor++;
  361. fftstate->factor [mfactor - 1] = 2;
  362. k /= 4;
  363. }
  364. kt = mfactor;
  365. j = 2;
  366. do {
  367. if (k % j == 0) {
  368. mfactor++;
  369. fftstate->factor [mfactor - 1] = j;
  370. k /= j;
  371. }
  372. j = ((j + 1) / 2 << 1) + 1;
  373. } while (j <= k);
  374. }
  375. if (kt) {
  376. j = kt;
  377. do {
  378. mfactor++;
  379. fftstate->factor [mfactor - 1] = fftstate->factor [j - 1];
  380. j--;
  381. } while (j);
  382. }
  383. /* test that mfactors is in range */
  384. if (mfactor > FFT_NFACTOR)
  385. {
  386. return -1;
  387. }
  388. /* compute fourier transform */
  389. for (;;) {
  390. sd = radf / (double) kspan;
  391. cd = sin(sd);
  392. cd = 2.0 * cd * cd;
  393. sd = sin(sd + sd);
  394. kk = 0;
  395. ii++;
  396. switch (fftstate->factor [ii - 1]) {
  397. case 2:
  398. /* transform for factor of 2 (including rotation factor) */
  399. kspan /= 2;
  400. k1 = kspan + 2;
  401. do {
  402. do {
  403. k2 = kk + kspan;
  404. ak = Re [k2];
  405. bk = Im [k2];
  406. Re [k2] = Re [kk] - ak;
  407. Im [k2] = Im [kk] - bk;
  408. Re [kk] += ak;
  409. Im [kk] += bk;
  410. kk = k2 + kspan;
  411. } while (kk < nn);
  412. kk -= nn;
  413. } while (kk < jc);
  414. if (kk >= kspan)
  415. goto Permute_Results_Label; /* exit infinite loop */
  416. do {
  417. c1 = 1.0 - cd;
  418. s1 = sd;
  419. do {
  420. do {
  421. do {
  422. k2 = kk + kspan;
  423. ak = Re [kk] - Re [k2];
  424. bk = Im [kk] - Im [k2];
  425. Re [kk] += Re [k2];
  426. Im [kk] += Im [k2];
  427. Re [k2] = c1 * ak - s1 * bk;
  428. Im [k2] = s1 * ak + c1 * bk;
  429. kk = k2 + kspan;
  430. } while (kk < (nt-1));
  431. k2 = kk - nt;
  432. c1 = -c1;
  433. kk = k1 - k2;
  434. } while (kk > k2);
  435. ak = c1 - (cd * c1 + sd * s1);
  436. s1 = sd * c1 - cd * s1 + s1;
  437. c1 = 2.0 - (ak * ak + s1 * s1);
  438. s1 *= c1;
  439. c1 *= ak;
  440. kk += jc;
  441. } while (kk < k2);
  442. k1 += inc + inc;
  443. kk = (k1 - kspan + 1) / 2 + jc - 1;
  444. } while (kk < (jc + jc));
  445. break;
  446. case 4: /* transform for factor of 4 */
  447. ispan = kspan;
  448. kspan /= 4;
  449. do {
  450. c1 = 1.0;
  451. s1 = 0.0;
  452. do {
  453. do {
  454. k1 = kk + kspan;
  455. k2 = k1 + kspan;
  456. k3 = k2 + kspan;
  457. akp = Re [kk] + Re [k2];
  458. akm = Re [kk] - Re [k2];
  459. ajp = Re [k1] + Re [k3];
  460. ajm = Re [k1] - Re [k3];
  461. bkp = Im [kk] + Im [k2];
  462. bkm = Im [kk] - Im [k2];
  463. bjp = Im [k1] + Im [k3];
  464. bjm = Im [k1] - Im [k3];
  465. Re [kk] = akp + ajp;
  466. Im [kk] = bkp + bjp;
  467. ajp = akp - ajp;
  468. bjp = bkp - bjp;
  469. if (iSign < 0) {
  470. akp = akm + bjm;
  471. bkp = bkm - ajm;
  472. akm -= bjm;
  473. bkm += ajm;
  474. } else {
  475. akp = akm - bjm;
  476. bkp = bkm + ajm;
  477. akm += bjm;
  478. bkm -= ajm;
  479. }
  480. /* avoid useless multiplies */
  481. if (s1 == 0.0) {
  482. Re [k1] = akp;
  483. Re [k2] = ajp;
  484. Re [k3] = akm;
  485. Im [k1] = bkp;
  486. Im [k2] = bjp;
  487. Im [k3] = bkm;
  488. } else {
  489. Re [k1] = akp * c1 - bkp * s1;
  490. Re [k2] = ajp * c2 - bjp * s2;
  491. Re [k3] = akm * c3 - bkm * s3;
  492. Im [k1] = akp * s1 + bkp * c1;
  493. Im [k2] = ajp * s2 + bjp * c2;
  494. Im [k3] = akm * s3 + bkm * c3;
  495. }
  496. kk = k3 + kspan;
  497. } while (kk < nt);
  498. c2 = c1 - (cd * c1 + sd * s1);
  499. s1 = sd * c1 - cd * s1 + s1;
  500. c1 = 2.0 - (c2 * c2 + s1 * s1);
  501. s1 *= c1;
  502. c1 *= c2;
  503. /* values of c2, c3, s2, s3 that will get used next time */
  504. c2 = c1 * c1 - s1 * s1;
  505. s2 = 2.0 * c1 * s1;
  506. c3 = c2 * c1 - s2 * s1;
  507. s3 = c2 * s1 + s2 * c1;
  508. kk = kk - nt + jc;
  509. } while (kk < kspan);
  510. kk = kk - kspan + inc;
  511. } while (kk < jc);
  512. if (kspan == jc)
  513. goto Permute_Results_Label; /* exit infinite loop */
  514. break;
  515. default:
  516. /* transform for odd factors */
  517. #ifdef FFT_RADIX4
  518. return -1;
  519. break;
  520. #else /* FFT_RADIX4 */
  521. k = fftstate->factor [ii - 1];
  522. ispan = kspan;
  523. kspan /= k;
  524. switch (k) {
  525. case 3: /* transform for factor of 3 (optional code) */
  526. do {
  527. do {
  528. k1 = kk + kspan;
  529. k2 = k1 + kspan;
  530. ak = Re [kk];
  531. bk = Im [kk];
  532. aj = Re [k1] + Re [k2];
  533. bj = Im [k1] + Im [k2];
  534. Re [kk] = ak + aj;
  535. Im [kk] = bk + bj;
  536. ak -= 0.5 * aj;
  537. bk -= 0.5 * bj;
  538. aj = (Re [k1] - Re [k2]) * s60;
  539. bj = (Im [k1] - Im [k2]) * s60;
  540. Re [k1] = ak - bj;
  541. Re [k2] = ak + bj;
  542. Im [k1] = bk + aj;
  543. Im [k2] = bk - aj;
  544. kk = k2 + kspan;
  545. } while (kk < (nn - 1));
  546. kk -= nn;
  547. } while (kk < kspan);
  548. break;
  549. case 5: /* transform for factor of 5 (optional code) */
  550. c2 = c72 * c72 - s72 * s72;
  551. s2 = 2.0 * c72 * s72;
  552. do {
  553. do {
  554. k1 = kk + kspan;
  555. k2 = k1 + kspan;
  556. k3 = k2 + kspan;
  557. k4 = k3 + kspan;
  558. akp = Re [k1] + Re [k4];
  559. akm = Re [k1] - Re [k4];
  560. bkp = Im [k1] + Im [k4];
  561. bkm = Im [k1] - Im [k4];
  562. ajp = Re [k2] + Re [k3];
  563. ajm = Re [k2] - Re [k3];
  564. bjp = Im [k2] + Im [k3];
  565. bjm = Im [k2] - Im [k3];
  566. aa = Re [kk];
  567. bb = Im [kk];
  568. Re [kk] = aa + akp + ajp;
  569. Im [kk] = bb + bkp + bjp;
  570. ak = akp * c72 + ajp * c2 + aa;
  571. bk = bkp * c72 + bjp * c2 + bb;
  572. aj = akm * s72 + ajm * s2;
  573. bj = bkm * s72 + bjm * s2;
  574. Re [k1] = ak - bj;
  575. Re [k4] = ak + bj;
  576. Im [k1] = bk + aj;
  577. Im [k4] = bk - aj;
  578. ak = akp * c2 + ajp * c72 + aa;
  579. bk = bkp * c2 + bjp * c72 + bb;
  580. aj = akm * s2 - ajm * s72;
  581. bj = bkm * s2 - bjm * s72;
  582. Re [k2] = ak - bj;
  583. Re [k3] = ak + bj;
  584. Im [k2] = bk + aj;
  585. Im [k3] = bk - aj;
  586. kk = k4 + kspan;
  587. } while (kk < (nn-1));
  588. kk -= nn;
  589. } while (kk < kspan);
  590. break;
  591. default:
  592. if (k != jf) {
  593. jf = k;
  594. s1 = pi2 / (double) k;
  595. c1 = cos(s1);
  596. s1 = sin(s1);
  597. if (jf > max_factors){
  598. return -1;
  599. }
  600. Cos [jf - 1] = 1.0;
  601. Sin [jf - 1] = 0.0;
  602. j = 1;
  603. do {
  604. Cos [j - 1] = Cos [k - 1] * c1 + Sin [k - 1] * s1;
  605. Sin [j - 1] = Cos [k - 1] * s1 - Sin [k - 1] * c1;
  606. k--;
  607. Cos [k - 1] = Cos [j - 1];
  608. Sin [k - 1] = -Sin [j - 1];
  609. j++;
  610. } while (j < k);
  611. }
  612. do {
  613. do {
  614. k1 = kk;
  615. k2 = kk + ispan;
  616. ak = aa = Re [kk];
  617. bk = bb = Im [kk];
  618. j = 1;
  619. k1 += kspan;
  620. do {
  621. k2 -= kspan;
  622. j++;
  623. Rtmp [j - 1] = Re [k1] + Re [k2];
  624. ak += Rtmp [j - 1];
  625. Itmp [j - 1] = Im [k1] + Im [k2];
  626. bk += Itmp [j - 1];
  627. j++;
  628. Rtmp [j - 1] = Re [k1] - Re [k2];
  629. Itmp [j - 1] = Im [k1] - Im [k2];
  630. k1 += kspan;
  631. } while (k1 < k2);
  632. Re [kk] = ak;
  633. Im [kk] = bk;
  634. k1 = kk;
  635. k2 = kk + ispan;
  636. j = 1;
  637. do {
  638. k1 += kspan;
  639. k2 -= kspan;
  640. jj = j;
  641. ak = aa;
  642. bk = bb;
  643. aj = 0.0;
  644. bj = 0.0;
  645. k = 1;
  646. do {
  647. k++;
  648. ak += Rtmp [k - 1] * Cos [jj - 1];
  649. bk += Itmp [k - 1] * Cos [jj - 1];
  650. k++;
  651. aj += Rtmp [k - 1] * Sin [jj - 1];
  652. bj += Itmp [k - 1] * Sin [jj - 1];
  653. jj += j;
  654. if (jj > jf) {
  655. jj -= jf;
  656. }
  657. } while (k < jf);
  658. k = jf - j;
  659. Re [k1] = ak - bj;
  660. Im [k1] = bk + aj;
  661. Re [k2] = ak + bj;
  662. Im [k2] = bk - aj;
  663. j++;
  664. } while (j < k);
  665. kk += ispan;
  666. } while (kk < nn);
  667. kk -= nn;
  668. } while (kk < kspan);
  669. break;
  670. }
  671. /* multiply by rotation factor (except for factors of 2 and 4) */
  672. if (ii == mfactor)
  673. goto Permute_Results_Label; /* exit infinite loop */
  674. kk = jc;
  675. do {
  676. c2 = 1.0 - cd;
  677. s1 = sd;
  678. do {
  679. c1 = c2;
  680. s2 = s1;
  681. kk += kspan;
  682. do {
  683. do {
  684. ak = Re [kk];
  685. Re [kk] = c2 * ak - s2 * Im [kk];
  686. Im [kk] = s2 * ak + c2 * Im [kk];
  687. kk += ispan;
  688. } while (kk < nt);
  689. ak = s1 * s2;
  690. s2 = s1 * c2 + c1 * s2;
  691. c2 = c1 * c2 - ak;
  692. kk = kk - nt + kspan;
  693. } while (kk < ispan);
  694. c2 = c1 - (cd * c1 + sd * s1);
  695. s1 += sd * c1 - cd * s1;
  696. c1 = 2.0 - (c2 * c2 + s1 * s1);
  697. s1 *= c1;
  698. c2 *= c1;
  699. kk = kk - ispan + jc;
  700. } while (kk < kspan);
  701. kk = kk - kspan + jc + inc;
  702. } while (kk < (jc + jc));
  703. break;
  704. #endif /* FFT_RADIX4 */
  705. }
  706. }
  707. /* permute the results to normal order---done in two stages */
  708. /* permutation for square factors of n */
  709. Permute_Results_Label:
  710. fftstate->Perm [0] = ns;
  711. if (kt) {
  712. k = kt + kt + 1;
  713. if (mfactor < k)
  714. k--;
  715. j = 1;
  716. fftstate->Perm [k] = jc;
  717. do {
  718. fftstate->Perm [j] = fftstate->Perm [j - 1] / fftstate->factor [j - 1];
  719. fftstate->Perm [k - 1] = fftstate->Perm [k] * fftstate->factor [j - 1];
  720. j++;
  721. k--;
  722. } while (j < k);
  723. k3 = fftstate->Perm [k];
  724. kspan = fftstate->Perm [1];
  725. kk = jc;
  726. k2 = kspan;
  727. j = 1;
  728. if (nPass != nTotal) {
  729. /* permutation for multivariate transform */
  730. Permute_Multi_Label:
  731. do {
  732. do {
  733. k = kk + jc;
  734. do {
  735. /* swap Re [kk] <> Re [k2], Im [kk] <> Im [k2] */
  736. ak = Re [kk]; Re [kk] = Re [k2]; Re [k2] = ak;
  737. bk = Im [kk]; Im [kk] = Im [k2]; Im [k2] = bk;
  738. kk += inc;
  739. k2 += inc;
  740. } while (kk < (k-1));
  741. kk += ns - jc;
  742. k2 += ns - jc;
  743. } while (kk < (nt-1));
  744. k2 = k2 - nt + kspan;
  745. kk = kk - nt + jc;
  746. } while (k2 < (ns-1));
  747. do {
  748. do {
  749. k2 -= fftstate->Perm [j - 1];
  750. j++;
  751. k2 = fftstate->Perm [j] + k2;
  752. } while (k2 > fftstate->Perm [j - 1]);
  753. j = 1;
  754. do {
  755. if (kk < (k2-1))
  756. goto Permute_Multi_Label;
  757. kk += jc;
  758. k2 += kspan;
  759. } while (k2 < (ns-1));
  760. } while (kk < (ns-1));
  761. } else {
  762. /* permutation for single-variate transform (optional code) */
  763. Permute_Single_Label:
  764. do {
  765. /* swap Re [kk] <> Re [k2], Im [kk] <> Im [k2] */
  766. ak = Re [kk]; Re [kk] = Re [k2]; Re [k2] = ak;
  767. bk = Im [kk]; Im [kk] = Im [k2]; Im [k2] = bk;
  768. kk += inc;
  769. k2 += kspan;
  770. } while (k2 < (ns-1));
  771. do {
  772. do {
  773. k2 -= fftstate->Perm [j - 1];
  774. j++;
  775. k2 = fftstate->Perm [j] + k2;
  776. } while (k2 >= fftstate->Perm [j - 1]);
  777. j = 1;
  778. do {
  779. if (kk < k2)
  780. goto Permute_Single_Label;
  781. kk += inc;
  782. k2 += kspan;
  783. } while (k2 < (ns-1));
  784. } while (kk < (ns-1));
  785. }
  786. jc = k3;
  787. }
  788. if ((kt << 1) + 1 >= mfactor)
  789. return 0;
  790. ispan = fftstate->Perm [kt];
  791. /* permutation for square-free factors of n */
  792. j = mfactor - kt;
  793. fftstate->factor [j] = 1;
  794. do {
  795. fftstate->factor [j - 1] *= fftstate->factor [j];
  796. j--;
  797. } while (j != kt);
  798. kt++;
  799. nn = fftstate->factor [kt - 1] - 1;
  800. if (nn > (int) max_perm) {
  801. return -1;
  802. }
  803. j = jj = 0;
  804. for (;;) {
  805. k = kt + 1;
  806. k2 = fftstate->factor [kt - 1];
  807. kk = fftstate->factor [k - 1];
  808. j++;
  809. if (j > nn)
  810. break; /* exit infinite loop */
  811. jj += kk;
  812. while (jj >= k2) {
  813. jj -= k2;
  814. k2 = kk;
  815. k++;
  816. kk = fftstate->factor [k - 1];
  817. jj += kk;
  818. }
  819. fftstate->Perm [j - 1] = jj;
  820. }
  821. /* determine the permutation cycles of length greater than 1 */
  822. j = 0;
  823. for (;;) {
  824. do {
  825. j++;
  826. kk = fftstate->Perm [j - 1];
  827. } while (kk < 0);
  828. if (kk != j) {
  829. do {
  830. k = kk;
  831. kk = fftstate->Perm [k - 1];
  832. fftstate->Perm [k - 1] = -kk;
  833. } while (kk != j);
  834. k3 = kk;
  835. } else {
  836. fftstate->Perm [j - 1] = -j;
  837. if (j == nn)
  838. break; /* exit infinite loop */
  839. }
  840. }
  841. max_factors *= inc;
  842. /* reorder a and b, following the permutation cycles */
  843. for (;;) {
  844. j = k3 + 1;
  845. nt -= ispan;
  846. ii = nt - inc + 1;
  847. if (nt < 0)
  848. break; /* exit infinite loop */
  849. do {
  850. do {
  851. j--;
  852. } while (fftstate->Perm [j - 1] < 0);
  853. jj = jc;
  854. do {
  855. kspan = jj;
  856. if (jj > max_factors) {
  857. kspan = max_factors;
  858. }
  859. jj -= kspan;
  860. k = fftstate->Perm [j - 1];
  861. kk = jc * k + ii + jj;
  862. k1 = kk + kspan - 1;
  863. k2 = 0;
  864. do {
  865. k2++;
  866. Rtmp [k2 - 1] = Re [k1];
  867. Itmp [k2 - 1] = Im [k1];
  868. k1 -= inc;
  869. } while (k1 != (kk-1));
  870. do {
  871. k1 = kk + kspan - 1;
  872. k2 = k1 - jc * (k + fftstate->Perm [k - 1]);
  873. k = -fftstate->Perm [k - 1];
  874. do {
  875. Re [k1] = Re [k2];
  876. Im [k1] = Im [k2];
  877. k1 -= inc;
  878. k2 -= inc;
  879. } while (k1 != (kk-1));
  880. kk = k2 + 1;
  881. } while (k != j);
  882. k1 = kk + kspan - 1;
  883. k2 = 0;
  884. do {
  885. k2++;
  886. Re [k1] = Rtmp [k2 - 1];
  887. Im [k1] = Itmp [k2 - 1];
  888. k1 -= inc;
  889. } while (k1 != (kk-1));
  890. } while (jj);
  891. } while (j != 1);
  892. }
  893. return 0; /* exit point here */
  894. }
  895. /* ---------------------- end-of-file (c source) ---------------------- */