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.

fft4g.h 33KB

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