ModelicaFFT.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  1. /* ModelicaFFT.c - FFT functions
  2. Copyright (C) 2003-2010, Mark Borgerding
  3. Copyright (C) 2015-2016, Modelica Association and DLR
  4. All rights reserved.
  5. Redistribution and use in source and binary forms, with or without
  6. modification, are permitted provided that the following conditions are met:
  7. 1. Redistributions of source code must retain the above copyright notice,
  8. this list of conditions and the following disclaimer.
  9. 2. Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in the
  11. documentation and/or other materials provided with the distribution.
  12. 3. Neither the author nor the names of any contributors may be used to
  13. endorse or promote products derived from this software without specific
  14. prior written permission.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  16. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  17. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. /* Adapted to the needs of the Modelica Standard Library library:
  27. The functions in this file are non-portable. The following #define's are used
  28. to define the system calls of the operating system
  29. __GNUC__ : GNU C compiler
  30. MODELICA_EXPORT: Prefix used for function calls. If not defined, blank is used
  31. Useful definitions:
  32. - "static" that is all functions become static
  33. (useful if file is included with other C-sources for an
  34. embedded system)
  35. - "__declspec(dllexport)" if included in a DLL and the
  36. functions shall be visible outside of the DLL
  37. Release Notes:
  38. Dec. 02, 2015: by Martin Otter, DLR
  39. Combined the C- and Header files of Kiss-FFT as needed for MSL
  40. Adapted the memory allocation scheme so that for exponents of 2,3,5
  41. memory has to be provided from the calling function
  42. (for if length of vector is not a multiple of 2,3,5, the function
  43. allocates additional memory, whenever it is called, and frees it before
  44. the function is left)
  45. */
  46. #ifndef MRKISS_FTR_H
  47. #define MRKISS_FTR_H
  48. #include <math.h>
  49. #include <limits.h>
  50. #include <stdlib.h>
  51. #include <string.h>
  52. #if !defined(MODELICA_EXPORT)
  53. # define MODELICA_EXPORT DYMOLA_STATIC
  54. #endif
  55. #define MRKISS_FFT_TMP_ALLOC malloc
  56. #define MRKISS_FFT_TMP_FREE free
  57. #define mrkiss_fft_scalar double
  58. #define MAXFACTORS 32
  59. /* e.g. an fft of length 128 has 4 factors
  60. as far as kissfft is concerned
  61. 4*4*4*2
  62. */
  63. typedef struct {
  64. mrkiss_fft_scalar r;
  65. mrkiss_fft_scalar i;
  66. } mrkiss_fft_cpx;
  67. struct mrkiss_fft_state {
  68. int nfft;
  69. int inverse;
  70. int factors[2*MAXFACTORS];
  71. mrkiss_fft_cpx *twiddles; /* twiddles[nfft] */
  72. };
  73. typedef struct mrkiss_fft_state* mrkiss_fft_cfg;
  74. struct mrkiss_fftr_state {
  75. mrkiss_fft_cfg substate;
  76. mrkiss_fft_cpx * tmpbuf;
  77. mrkiss_fft_cpx * super_twiddles;
  78. };
  79. typedef struct mrkiss_fftr_state* mrkiss_fftr_cfg;
  80. /*
  81. * Non-null pointers need to be passed to external functions.
  82. *
  83. * The following macros handle nonnull attributes for GNU C and Microsoft SAL.
  84. */
  85. #if defined(__GNUC__)
  86. #define MODELICA_NONNULLATTR __attribute__((nonnull))
  87. #else
  88. #define MODELICA_NONNULLATTR
  89. #endif
  90. #if !defined(__ATTR_SAL)
  91. #define _In_
  92. #define _Out_
  93. #endif
  94. MODELICA_EXPORT int ModelicaFFT_kiss_fftr(_In_ double u[], size_t nu, _In_ double work[], size_t nwork,
  95. _Out_ double *amplitudes, _Out_ double *phases) MODELICA_NONNULLATTR;
  96. /* include from _kiss_fft_guts.h ------------------------------------------ */
  97. /*
  98. Explanation of macros dealing with complex math:
  99. C_MUL(m,a,b) : m = a*b
  100. C_FIXDIV( c , div ) : if a fixed point impl., c /= div. noop otherwise
  101. C_SUB( res, a,b) : res = a - b
  102. C_SUBFROM( res , a) : res -= a
  103. C_ADDTO( res , a) : res += a
  104. * */
  105. #define S_MUL(a,b) ( (a)*(b) )
  106. #define C_MUL(m,a,b) \
  107. do{ (m).r = (a).r*(b).r - (a).i*(b).i;\
  108. (m).i = (a).r*(b).i + (a).i*(b).r; }while(0)
  109. #define C_FIXDIV(c,div) /* NOOP */
  110. #define C_MULBYSCALAR( c, s ) \
  111. do{ (c).r *= (s);\
  112. (c).i *= (s); }while(0)
  113. #ifndef CHECK_OVERFLOW_OP
  114. # define CHECK_OVERFLOW_OP(a,op,b) /* noop */
  115. #endif
  116. #define C_ADD( res, a,b)\
  117. do { \
  118. CHECK_OVERFLOW_OP((a).r,+,(b).r)\
  119. CHECK_OVERFLOW_OP((a).i,+,(b).i)\
  120. (res).r=(a).r+(b).r; (res).i=(a).i+(b).i; \
  121. }while(0)
  122. #define C_SUB( res, a,b)\
  123. do { \
  124. CHECK_OVERFLOW_OP((a).r,-,(b).r)\
  125. CHECK_OVERFLOW_OP((a).i,-,(b).i)\
  126. (res).r=(a).r-(b).r; (res).i=(a).i-(b).i; \
  127. }while(0)
  128. #define C_ADDTO( res , a)\
  129. do { \
  130. CHECK_OVERFLOW_OP((res).r,+,(a).r)\
  131. CHECK_OVERFLOW_OP((res).i,+,(a).i)\
  132. (res).r += (a).r; (res).i += (a).i;\
  133. }while(0)
  134. #define C_SUBFROM( res , a)\
  135. do {\
  136. CHECK_OVERFLOW_OP((res).r,-,(a).r)\
  137. CHECK_OVERFLOW_OP((res).i,-,(a).i)\
  138. (res).r -= (a).r; (res).i -= (a).i; \
  139. }while(0)
  140. #define MRKISS_FFT_COS(phase) (mrkiss_fft_scalar) cos(phase)
  141. #define MRKISS_FFT_SIN(phase) (mrkiss_fft_scalar) sin(phase)
  142. #define HALF_OF(x) ((x)*.5)
  143. #define kf_cexp(x,phase) \
  144. do{ \
  145. (x)->r = MRKISS_FFT_COS(phase);\
  146. (x)->i = MRKISS_FFT_SIN(phase);\
  147. }while(0)
  148. /* end of include from _kiss_fft_guts.h ------------------------------------------ */
  149. /* include of kiss_fft.c -------------------------------------------------------- */
  150. static void kf_bfly2(
  151. mrkiss_fft_cpx * Fout,
  152. const size_t fstride,
  153. const mrkiss_fft_cfg st,
  154. int m
  155. ) {
  156. mrkiss_fft_cpx * Fout2;
  157. mrkiss_fft_cpx * tw1 = st->twiddles;
  158. mrkiss_fft_cpx t;
  159. Fout2 = Fout + m;
  160. do {
  161. C_FIXDIV(*Fout,2);
  162. C_FIXDIV(*Fout2,2);
  163. C_MUL (t, *Fout2 , *tw1);
  164. tw1 += fstride;
  165. C_SUB( *Fout2 , *Fout , t );
  166. C_ADDTO( *Fout , t );
  167. ++Fout2;
  168. ++Fout;
  169. } while (--m);
  170. }
  171. static void kf_bfly4(
  172. mrkiss_fft_cpx * Fout,
  173. const size_t fstride,
  174. const mrkiss_fft_cfg st,
  175. const size_t m
  176. ) {
  177. mrkiss_fft_cpx *tw1,*tw2,*tw3;
  178. mrkiss_fft_cpx scratch[6];
  179. size_t k=m;
  180. const size_t m2=2*m;
  181. const size_t m3=3*m;
  182. tw3 = tw2 = tw1 = st->twiddles;
  183. do {
  184. C_FIXDIV(*Fout,4);
  185. C_FIXDIV(Fout[m],4);
  186. C_FIXDIV(Fout[m2],4);
  187. C_FIXDIV(Fout[m3],4);
  188. C_MUL(scratch[0],Fout[m] , *tw1 );
  189. C_MUL(scratch[1],Fout[m2] , *tw2 );
  190. C_MUL(scratch[2],Fout[m3] , *tw3 );
  191. C_SUB( scratch[5] , *Fout, scratch[1] );
  192. C_ADDTO(*Fout, scratch[1]);
  193. C_ADD( scratch[3] , scratch[0] , scratch[2] );
  194. C_SUB( scratch[4] , scratch[0] , scratch[2] );
  195. C_SUB( Fout[m2], *Fout, scratch[3] );
  196. tw1 += fstride;
  197. tw2 += fstride*2;
  198. tw3 += fstride*3;
  199. C_ADDTO( *Fout , scratch[3] );
  200. if(st->inverse) {
  201. Fout[m].r = scratch[5].r - scratch[4].i;
  202. Fout[m].i = scratch[5].i + scratch[4].r;
  203. Fout[m3].r = scratch[5].r + scratch[4].i;
  204. Fout[m3].i = scratch[5].i - scratch[4].r;
  205. } else {
  206. Fout[m].r = scratch[5].r + scratch[4].i;
  207. Fout[m].i = scratch[5].i - scratch[4].r;
  208. Fout[m3].r = scratch[5].r - scratch[4].i;
  209. Fout[m3].i = scratch[5].i + scratch[4].r;
  210. }
  211. ++Fout;
  212. } while(--k);
  213. }
  214. static void kf_bfly3(
  215. mrkiss_fft_cpx * Fout,
  216. const size_t fstride,
  217. const mrkiss_fft_cfg st,
  218. size_t m
  219. ) {
  220. size_t k=m;
  221. const size_t m2 = 2*m;
  222. mrkiss_fft_cpx *tw1,*tw2;
  223. mrkiss_fft_cpx scratch[5];
  224. mrkiss_fft_cpx epi3;
  225. epi3 = st->twiddles[fstride*m];
  226. tw1=tw2=st->twiddles;
  227. do {
  228. C_FIXDIV(*Fout,3);
  229. C_FIXDIV(Fout[m],3);
  230. C_FIXDIV(Fout[m2],3);
  231. C_MUL(scratch[1],Fout[m] , *tw1);
  232. C_MUL(scratch[2],Fout[m2] , *tw2);
  233. C_ADD(scratch[3],scratch[1],scratch[2]);
  234. C_SUB(scratch[0],scratch[1],scratch[2]);
  235. tw1 += fstride;
  236. tw2 += fstride*2;
  237. Fout[m].r = Fout->r - HALF_OF(scratch[3].r);
  238. Fout[m].i = Fout->i - HALF_OF(scratch[3].i);
  239. C_MULBYSCALAR( scratch[0] , epi3.i );
  240. C_ADDTO(*Fout,scratch[3]);
  241. Fout[m2].r = Fout[m].r + scratch[0].i;
  242. Fout[m2].i = Fout[m].i - scratch[0].r;
  243. Fout[m].r -= scratch[0].i;
  244. Fout[m].i += scratch[0].r;
  245. ++Fout;
  246. } while(--k);
  247. }
  248. static void kf_bfly5(
  249. mrkiss_fft_cpx * Fout,
  250. const size_t fstride,
  251. const mrkiss_fft_cfg st,
  252. int m
  253. ) {
  254. mrkiss_fft_cpx *Fout0,*Fout1,*Fout2,*Fout3,*Fout4;
  255. int u;
  256. mrkiss_fft_cpx scratch[13];
  257. mrkiss_fft_cpx * twiddles = st->twiddles;
  258. mrkiss_fft_cpx *tw;
  259. mrkiss_fft_cpx ya,yb;
  260. ya = twiddles[fstride*m];
  261. yb = twiddles[fstride*2*m];
  262. Fout0=Fout;
  263. Fout1=Fout0+m;
  264. Fout2=Fout0+2*m;
  265. Fout3=Fout0+3*m;
  266. Fout4=Fout0+4*m;
  267. tw=st->twiddles;
  268. for ( u=0; u<m; ++u ) {
  269. C_FIXDIV( *Fout0,5);
  270. C_FIXDIV( *Fout1,5);
  271. C_FIXDIV( *Fout2,5);
  272. C_FIXDIV( *Fout3,5);
  273. C_FIXDIV( *Fout4,5);
  274. scratch[0] = *Fout0;
  275. C_MUL(scratch[1] ,*Fout1, tw[u*fstride]);
  276. C_MUL(scratch[2] ,*Fout2, tw[2*u*fstride]);
  277. C_MUL(scratch[3] ,*Fout3, tw[3*u*fstride]);
  278. C_MUL(scratch[4] ,*Fout4, tw[4*u*fstride]);
  279. C_ADD( scratch[7],scratch[1],scratch[4]);
  280. C_SUB( scratch[10],scratch[1],scratch[4]);
  281. C_ADD( scratch[8],scratch[2],scratch[3]);
  282. C_SUB( scratch[9],scratch[2],scratch[3]);
  283. Fout0->r += scratch[7].r + scratch[8].r;
  284. Fout0->i += scratch[7].i + scratch[8].i;
  285. scratch[5].r = scratch[0].r + S_MUL(scratch[7].r,ya.r) + S_MUL(scratch[8].r,yb.r);
  286. scratch[5].i = scratch[0].i + S_MUL(scratch[7].i,ya.r) + S_MUL(scratch[8].i,yb.r);
  287. scratch[6].r = S_MUL(scratch[10].i,ya.i) + S_MUL(scratch[9].i,yb.i);
  288. scratch[6].i = -S_MUL(scratch[10].r,ya.i) - S_MUL(scratch[9].r,yb.i);
  289. C_SUB(*Fout1,scratch[5],scratch[6]);
  290. C_ADD(*Fout4,scratch[5],scratch[6]);
  291. scratch[11].r = scratch[0].r + S_MUL(scratch[7].r,yb.r) + S_MUL(scratch[8].r,ya.r);
  292. scratch[11].i = scratch[0].i + S_MUL(scratch[7].i,yb.r) + S_MUL(scratch[8].i,ya.r);
  293. scratch[12].r = - S_MUL(scratch[10].i,yb.i) + S_MUL(scratch[9].i,ya.i);
  294. scratch[12].i = S_MUL(scratch[10].r,yb.i) - S_MUL(scratch[9].r,ya.i);
  295. C_ADD(*Fout2,scratch[11],scratch[12]);
  296. C_SUB(*Fout3,scratch[11],scratch[12]);
  297. ++Fout0;
  298. ++Fout1;
  299. ++Fout2;
  300. ++Fout3;
  301. ++Fout4;
  302. }
  303. }
  304. /* perform the butterfly for one stage of a mixed radix FFT */
  305. static void kf_bfly_generic(
  306. mrkiss_fft_cpx * Fout,
  307. const size_t fstride,
  308. const mrkiss_fft_cfg st,
  309. int m,
  310. int p
  311. ) {
  312. int u,k,q1,q;
  313. mrkiss_fft_cpx * twiddles = st->twiddles;
  314. mrkiss_fft_cpx t;
  315. int Norig = st->nfft;
  316. mrkiss_fft_cpx * scratch = (mrkiss_fft_cpx*)MRKISS_FFT_TMP_ALLOC(sizeof(mrkiss_fft_cpx)*p);
  317. for ( u=0; u<m; ++u ) {
  318. k=u;
  319. for ( q1=0 ; q1<p ; ++q1 ) {
  320. scratch[q1] = Fout[ k ];
  321. C_FIXDIV(scratch[q1],p);
  322. k += m;
  323. }
  324. k=u;
  325. for ( q1=0 ; q1<p ; ++q1 ) {
  326. int twidx=0;
  327. Fout[ k ] = scratch[0];
  328. for (q=1; q<p; ++q ) {
  329. twidx += fstride * k;
  330. if (twidx>=Norig) twidx-=Norig;
  331. C_MUL(t,scratch[q] , twiddles[twidx] );
  332. C_ADDTO( Fout[ k ] ,t);
  333. }
  334. k += m;
  335. }
  336. }
  337. MRKISS_FFT_TMP_FREE(scratch);
  338. }
  339. static void kf_work(
  340. mrkiss_fft_cpx * Fout,
  341. const mrkiss_fft_cpx * f,
  342. const size_t fstride,
  343. int in_stride,
  344. int * factors,
  345. const mrkiss_fft_cfg st
  346. ) {
  347. mrkiss_fft_cpx * Fout_beg=Fout;
  348. const int p=*factors++; /* the radix */
  349. const int m=*factors++; /* stage's fft length/p */
  350. const mrkiss_fft_cpx * Fout_end = Fout + p*m;
  351. if (m==1) {
  352. do {
  353. *Fout = *f;
  354. f += fstride*in_stride;
  355. } while(++Fout != Fout_end );
  356. } else {
  357. do {
  358. /* recursive call:
  359. DFT of size m*p performed by doing
  360. p instances of smaller DFTs of size m,
  361. each one takes a decimated version of the input */
  362. kf_work( Fout , f, fstride*p, in_stride, factors,st);
  363. f += fstride*in_stride;
  364. } while( (Fout += m) != Fout_end );
  365. }
  366. Fout=Fout_beg;
  367. /* recombine the p smaller DFTs */
  368. switch (p) {
  369. case 2:
  370. kf_bfly2(Fout,fstride,st,m);
  371. break;
  372. case 3:
  373. kf_bfly3(Fout,fstride,st,m);
  374. break;
  375. case 4:
  376. kf_bfly4(Fout,fstride,st,m);
  377. break;
  378. case 5:
  379. kf_bfly5(Fout,fstride,st,m);
  380. break;
  381. default:
  382. kf_bfly_generic(Fout,fstride,st,m,p);
  383. break;
  384. }
  385. }
  386. /* facbuf is populated by p1,m1,p2,m2, ...
  387. where
  388. p[i] * m[i] = m[i-1]
  389. m0 = n */
  390. static void kf_factor(int n,int * facbuf) {
  391. int p=4;
  392. double floor_sqrt;
  393. floor_sqrt = floor( sqrt((double)n) );
  394. /* factor out powers of 4, powers of 2, then any remaining primes */
  395. do {
  396. while (n % p) {
  397. switch (p) {
  398. case 4:
  399. p = 2;
  400. break;
  401. case 2:
  402. p = 3;
  403. break;
  404. default:
  405. p += 2;
  406. break;
  407. }
  408. if (p > floor_sqrt)
  409. p = n; /* no more factors, skip to end */
  410. }
  411. n /= p;
  412. *facbuf++ = p;
  413. *facbuf++ = n;
  414. } while (n > 1);
  415. }
  416. static void mrkiss_fft_stride(mrkiss_fft_cfg st,const mrkiss_fft_cpx *fin,mrkiss_fft_cpx *fout,int in_stride) {
  417. if (fin == fout) {
  418. /* NOTE: this is not really an in-place FFT algorithm. */
  419. /* It just performs an out-of-place FFT into a temp buffer */
  420. mrkiss_fft_cpx * tmpbuf = (mrkiss_fft_cpx*)MRKISS_FFT_TMP_ALLOC( sizeof(mrkiss_fft_cpx)*st->nfft);
  421. kf_work(tmpbuf,fin,1,in_stride, st->factors,st);
  422. memcpy(fout,tmpbuf,sizeof(mrkiss_fft_cpx)*st->nfft);
  423. MRKISS_FFT_TMP_FREE(tmpbuf);
  424. } else {
  425. kf_work( fout, fin, 1,in_stride, st->factors,st );
  426. }
  427. }
  428. static void mrkiss_fft(mrkiss_fft_cfg cfg,const mrkiss_fft_cpx *fin,mrkiss_fft_cpx *fout) {
  429. mrkiss_fft_stride(cfg,fin,fout,1);
  430. }
  431. /* end of include from kiss_fft.c --------------------------------------------------*/
  432. static void mrkiss_fft_alloc(int nfft, mrkiss_fft_cfg cfg) {
  433. int i;
  434. cfg->nfft = nfft;
  435. cfg->inverse = 0;
  436. for (i=0; i<nfft; ++i) {
  437. const double pi=3.141592653589793238462643383279502884197169399375105820974944;
  438. double phase = -2*pi*i / nfft;
  439. kf_cexp(cfg->twiddles+i, phase);
  440. }
  441. kf_factor(nfft, cfg->factors);
  442. }
  443. static void mrkiss_fftr(mrkiss_fftr_cfg st, const mrkiss_fft_scalar *timedata, mrkiss_fft_cpx *freqdata) {
  444. /* input buffer timedata is stored row-wise */
  445. int k,ncfft;
  446. mrkiss_fft_cpx fpnk,fpk,f1k,f2k,tw,tdc;
  447. ncfft = st->substate->nfft;
  448. /*perform the parallel fft of two real signals packed in real,imag*/
  449. mrkiss_fft( st->substate , (const mrkiss_fft_cpx*)timedata, st->tmpbuf );
  450. tdc.r = st->tmpbuf[0].r;
  451. tdc.i = st->tmpbuf[0].i;
  452. C_FIXDIV(tdc,2);
  453. CHECK_OVERFLOW_OP(tdc.r ,+, tdc.i);
  454. CHECK_OVERFLOW_OP(tdc.r ,-, tdc.i);
  455. freqdata[0].r = tdc.r + tdc.i;
  456. freqdata[ncfft].r = tdc.r - tdc.i;
  457. freqdata[ncfft].i = freqdata[0].i = 0;
  458. for ( k=1; k <= ncfft/2 ; ++k ) {
  459. fpk = st->tmpbuf[k];
  460. fpnk.r = st->tmpbuf[ncfft-k].r;
  461. fpnk.i = - st->tmpbuf[ncfft-k].i;
  462. C_FIXDIV(fpk,2);
  463. C_FIXDIV(fpnk,2);
  464. C_ADD( f1k, fpk , fpnk );
  465. C_SUB( f2k, fpk , fpnk );
  466. C_MUL( tw , f2k , st->super_twiddles[k-1]);
  467. freqdata[k].r = HALF_OF(f1k.r + tw.r);
  468. freqdata[k].i = HALF_OF(f1k.i + tw.i);
  469. freqdata[ncfft-k].r = HALF_OF(f1k.r - tw.r);
  470. freqdata[ncfft-k].i = HALF_OF(tw.i - f1k.i);
  471. }
  472. }
  473. MODELICA_EXPORT int ModelicaFFT_kiss_fftr(double u[], size_t nu, double work[], size_t nwork,
  474. double *amplitudes, double *phases) {
  475. /* Compute real FFT with mrkiss_fftr
  476. -> u[nu] : Real data at sample points; nu must be even
  477. -> work[nwork] : Work array; nwork >= 3*nu + 2*nf (nf = nu/2+1)
  478. <- amplitude[nf]: Amplitudes; nf = nu/2+1
  479. <- phases [nf]: phases
  480. <- return : info = 0: computation o.k.
  481. = 1: nu is not even
  482. = 2: nwork is wrong
  483. = 3: another error
  484. */
  485. int i;
  486. int nu2 = nu / 2;
  487. int nf = nu2+1;
  488. struct mrkiss_fft_state fft_obj;
  489. struct mrkiss_fftr_state fftr_obj;
  490. mrkiss_fft_cpx *freqdata;
  491. /* Check dimensions */
  492. if ( nu % 2 != 0 ) return 1;
  493. if ( nwork < 3*nu + 2*(nu/2+1) ) return 2;
  494. /* Set values of struct fft_obj */
  495. fft_obj.twiddles = (mrkiss_fft_cpx *) &work[0]; /* length nu (2*nu2) */
  496. mrkiss_fft_alloc(nu2, &fft_obj);
  497. /* Set values of struct fftr_obj */
  498. fftr_obj.substate = &fft_obj;
  499. fftr_obj.tmpbuf = (mrkiss_fft_cpx *) &work[nu]; /* length: nu */
  500. fftr_obj.super_twiddles = (mrkiss_fft_cpx *) &work[nu+nu]; /* length: nu */
  501. for (i = 0; i < nu2/2; ++i) {
  502. double phase =
  503. -3.14159265358979323846264338327 * ((double) (i+1) / nu2 + .5);
  504. kf_cexp (fftr_obj.super_twiddles+i,phase);
  505. }
  506. /* Compute FFT */
  507. freqdata = (mrkiss_fft_cpx *) &work[nu+nu+nu]; /* length: 2*nf */
  508. mrkiss_fftr(&fftr_obj, u, freqdata);
  509. for (i=0; i<nf; i++) {
  510. amplitudes[i] = sqrt (freqdata[i].r*freqdata[i].r + freqdata[i].i*freqdata[i].i) / nf;
  511. phases[i] = atan2(freqdata[i].i, freqdata[i].r);
  512. }
  513. return 0;
  514. }
  515. #endif