kiss_fft.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. /*
  2. Copyright (c) 2003-2010 Mark Borgerding
  3. Copyright (c) 2017 Mark Straver BASc
  4. Redistribution and use in source and binary forms, with or without modification,
  5. are permitted provided that the following conditions are met:
  6. * Redistributions of source code must retain the above copyright notice,
  7. this list of conditions and the following disclaimer.
  8. * Redistributions in binary form must reproduce the above copyright notice,
  9. this list of conditions and the following disclaimer in the documentation
  10. and/or other materials provided with the distribution.
  11. * Neither the author nor the names of any contributors may be used to
  12. endorse or promote products derived from this software without specific
  13. prior written permission.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  15. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  16. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  17. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  18. ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  19. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  20. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  21. ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  23. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include "_kiss_fft_guts.h"
  26. /* The guts header contains all the multiplication and addition macros that are defined for
  27. fixed or floating point complex numbers. It also delares the kf_ internal functions.
  28. */
  29. static void kf_bfly2(
  30. kiss_fft_cpx * Fout,
  31. const size_t fstride,
  32. const kiss_fft_cfg st,
  33. int m
  34. )
  35. {
  36. kiss_fft_cpx * Fout2;
  37. kiss_fft_cpx * tw1 = st->twiddles;
  38. kiss_fft_cpx t;
  39. Fout2 = Fout + m;
  40. do{
  41. C_FIXDIV(*Fout,2); C_FIXDIV(*Fout2,2);
  42. C_MUL (t, *Fout2 , *tw1);
  43. tw1 += fstride;
  44. C_SUB( *Fout2 , *Fout , t );
  45. C_ADDTO( *Fout , t );
  46. ++Fout2;
  47. ++Fout;
  48. }while (--m);
  49. }
  50. static void kf_bfly4(
  51. kiss_fft_cpx * Fout,
  52. const size_t fstride,
  53. const kiss_fft_cfg st,
  54. const size_t m
  55. )
  56. {
  57. kiss_fft_cpx *tw1,*tw2,*tw3;
  58. kiss_fft_cpx scratch[6];
  59. size_t k=m;
  60. const size_t m2=2*m;
  61. const size_t m3=3*m;
  62. tw3 = tw2 = tw1 = st->twiddles;
  63. do {
  64. C_FIXDIV(*Fout,4); C_FIXDIV(Fout[m],4); C_FIXDIV(Fout[m2],4); C_FIXDIV(Fout[m3],4);
  65. C_MUL(scratch[0],Fout[m] , *tw1 );
  66. C_MUL(scratch[1],Fout[m2] , *tw2 );
  67. C_MUL(scratch[2],Fout[m3] , *tw3 );
  68. C_SUB( scratch[5] , *Fout, scratch[1] );
  69. C_ADDTO(*Fout, scratch[1]);
  70. C_ADD( scratch[3] , scratch[0] , scratch[2] );
  71. C_SUB( scratch[4] , scratch[0] , scratch[2] );
  72. C_SUB( Fout[m2], *Fout, scratch[3] );
  73. tw1 += fstride;
  74. tw2 += fstride*2;
  75. tw3 += fstride*3;
  76. C_ADDTO( *Fout , scratch[3] );
  77. if(st->inverse) {
  78. Fout[m].r = scratch[5].r - scratch[4].i;
  79. Fout[m].i = scratch[5].i + scratch[4].r;
  80. Fout[m3].r = scratch[5].r + scratch[4].i;
  81. Fout[m3].i = scratch[5].i - scratch[4].r;
  82. }else{
  83. Fout[m].r = scratch[5].r + scratch[4].i;
  84. Fout[m].i = scratch[5].i - scratch[4].r;
  85. Fout[m3].r = scratch[5].r - scratch[4].i;
  86. Fout[m3].i = scratch[5].i + scratch[4].r;
  87. }
  88. ++Fout;
  89. }while(--k);
  90. }
  91. static void kf_bfly3(
  92. kiss_fft_cpx * Fout,
  93. const size_t fstride,
  94. const kiss_fft_cfg st,
  95. size_t m
  96. )
  97. {
  98. size_t k=m;
  99. const size_t m2 = 2*m;
  100. kiss_fft_cpx *tw1,*tw2;
  101. kiss_fft_cpx scratch[5];
  102. kiss_fft_cpx epi3;
  103. epi3 = st->twiddles[fstride*m];
  104. tw1=tw2=st->twiddles;
  105. do{
  106. C_FIXDIV(*Fout,3); C_FIXDIV(Fout[m],3); C_FIXDIV(Fout[m2],3);
  107. C_MUL(scratch[1],Fout[m] , *tw1);
  108. C_MUL(scratch[2],Fout[m2] , *tw2);
  109. C_ADD(scratch[3],scratch[1],scratch[2]);
  110. C_SUB(scratch[0],scratch[1],scratch[2]);
  111. tw1 += fstride;
  112. tw2 += fstride*2;
  113. Fout[m].r = Fout->r - HALF_OF(scratch[3].r);
  114. Fout[m].i = Fout->i - HALF_OF(scratch[3].i);
  115. C_MULBYSCALAR( scratch[0] , epi3.i );
  116. C_ADDTO(*Fout,scratch[3]);
  117. Fout[m2].r = Fout[m].r + scratch[0].i;
  118. Fout[m2].i = Fout[m].i - scratch[0].r;
  119. Fout[m].r -= scratch[0].i;
  120. Fout[m].i += scratch[0].r;
  121. ++Fout;
  122. }while(--k);
  123. }
  124. static void kf_bfly5(
  125. kiss_fft_cpx * Fout,
  126. const size_t fstride,
  127. const kiss_fft_cfg st,
  128. int m
  129. )
  130. {
  131. kiss_fft_cpx *Fout0,*Fout1,*Fout2,*Fout3,*Fout4;
  132. int u;
  133. kiss_fft_cpx scratch[13];
  134. kiss_fft_cpx * twiddles = st->twiddles;
  135. kiss_fft_cpx *tw;
  136. kiss_fft_cpx ya,yb;
  137. ya = twiddles[fstride*m];
  138. yb = twiddles[fstride*2*m];
  139. Fout0=Fout;
  140. Fout1=Fout0+m;
  141. Fout2=Fout0+2*m;
  142. Fout3=Fout0+3*m;
  143. Fout4=Fout0+4*m;
  144. tw=st->twiddles;
  145. for ( u=0; u<m; ++u ) {
  146. C_FIXDIV( *Fout0,5); C_FIXDIV( *Fout1,5); C_FIXDIV( *Fout2,5); C_FIXDIV( *Fout3,5); C_FIXDIV( *Fout4,5);
  147. scratch[0] = *Fout0;
  148. C_MUL(scratch[1] ,*Fout1, tw[u*fstride]);
  149. C_MUL(scratch[2] ,*Fout2, tw[2*u*fstride]);
  150. C_MUL(scratch[3] ,*Fout3, tw[3*u*fstride]);
  151. C_MUL(scratch[4] ,*Fout4, tw[4*u*fstride]);
  152. C_ADD( scratch[7],scratch[1],scratch[4]);
  153. C_SUB( scratch[10],scratch[1],scratch[4]);
  154. C_ADD( scratch[8],scratch[2],scratch[3]);
  155. C_SUB( scratch[9],scratch[2],scratch[3]);
  156. Fout0->r += scratch[7].r + scratch[8].r;
  157. Fout0->i += scratch[7].i + scratch[8].i;
  158. scratch[5].r = scratch[0].r + S_MUL(scratch[7].r,ya.r) + S_MUL(scratch[8].r,yb.r);
  159. scratch[5].i = scratch[0].i + S_MUL(scratch[7].i,ya.r) + S_MUL(scratch[8].i,yb.r);
  160. scratch[6].r = S_MUL(scratch[10].i,ya.i) + S_MUL(scratch[9].i,yb.i);
  161. scratch[6].i = -S_MUL(scratch[10].r,ya.i) - S_MUL(scratch[9].r,yb.i);
  162. C_SUB(*Fout1,scratch[5],scratch[6]);
  163. C_ADD(*Fout4,scratch[5],scratch[6]);
  164. scratch[11].r = scratch[0].r + S_MUL(scratch[7].r,yb.r) + S_MUL(scratch[8].r,ya.r);
  165. scratch[11].i = scratch[0].i + S_MUL(scratch[7].i,yb.r) + S_MUL(scratch[8].i,ya.r);
  166. scratch[12].r = - S_MUL(scratch[10].i,yb.i) + S_MUL(scratch[9].i,ya.i);
  167. scratch[12].i = S_MUL(scratch[10].r,yb.i) - S_MUL(scratch[9].r,ya.i);
  168. C_ADD(*Fout2,scratch[11],scratch[12]);
  169. C_SUB(*Fout3,scratch[11],scratch[12]);
  170. ++Fout0;++Fout1;++Fout2;++Fout3;++Fout4;
  171. }
  172. }
  173. /* perform the butterfly for one stage of a mixed radix FFT */
  174. static void kf_bfly_generic(
  175. kiss_fft_cpx * Fout,
  176. const size_t fstride,
  177. const kiss_fft_cfg st,
  178. int m,
  179. int p
  180. )
  181. {
  182. #ifdef _OPENMP
  183. #pragma omp critical (bfly_generic)
  184. {
  185. #endif
  186. int u,k,q1,q;
  187. kiss_fft_cpx * twiddles = st->twiddles;
  188. kiss_fft_cpx t;
  189. int Norig = st->nfft;
  190. kiss_fft_cpx * scratch = (kiss_fft_cpx*)KISS_FFT_TMP_ALLOC(sizeof(kiss_fft_cpx)*p);
  191. for ( u=0; u<m; ++u ) {
  192. k=u;
  193. for ( q1=0 ; q1<p ; ++q1 ) {
  194. scratch[q1] = Fout[ k ];
  195. C_FIXDIV(scratch[q1],p);
  196. k += m;
  197. }
  198. k=u;
  199. for ( q1=0 ; q1<p ; ++q1 ) {
  200. int twidx=0;
  201. Fout[ k ] = scratch[0];
  202. for (q=1;q<p;++q ) {
  203. twidx += fstride * k;
  204. if (twidx>=Norig) twidx-=Norig;
  205. C_MUL(t,scratch[q] , twiddles[twidx] );
  206. C_ADDTO( Fout[ k ] ,t);
  207. }
  208. k += m;
  209. }
  210. }
  211. KISS_FFT_TMP_FREE(scratch);
  212. #ifdef _OPENMP
  213. }
  214. #endif
  215. }
  216. static
  217. void kf_work(
  218. kiss_fft_cpx * Fout,
  219. const kiss_fft_cpx * f,
  220. const size_t fstride,
  221. int in_stride,
  222. int * factors,
  223. const kiss_fft_cfg st
  224. )
  225. {
  226. kiss_fft_cpx * Fout_beg=Fout;
  227. const int p=*factors++; /* the radix */
  228. const int m=*factors++; /* stage's fft length/p */
  229. const kiss_fft_cpx * Fout_end = Fout + p*m;
  230. #ifdef _OPENMP
  231. // use openmp extensions at the
  232. // top-level (not recursive)
  233. if (fstride==1 && p<=5 && m!=1)
  234. {
  235. int k;
  236. // execute the p different work units in different threads
  237. # pragma omp parallel for
  238. for (k=0;k<p;++k)
  239. kf_work( Fout +k*m, f+ fstride*in_stride*k,fstride*p,in_stride,factors,st);
  240. // all threads have joined by this point
  241. switch (p) {
  242. case 2: kf_bfly2(Fout,fstride,st,m); break;
  243. case 3: kf_bfly3(Fout,fstride,st,m); break;
  244. case 4: kf_bfly4(Fout,fstride,st,m); break;
  245. case 5: kf_bfly5(Fout,fstride,st,m); break;
  246. default: kf_bfly_generic(Fout,fstride,st,m,p); break;
  247. }
  248. return;
  249. }
  250. #endif
  251. if (m==1) {
  252. do{
  253. *Fout = *f;
  254. f += fstride*in_stride;
  255. }while(++Fout != Fout_end );
  256. }else{
  257. do{
  258. // recursive call:
  259. // DFT of size m*p performed by doing
  260. // p instances of smaller DFTs of size m,
  261. // each one takes a decimated version of the input
  262. kf_work( Fout , f, fstride*p, in_stride, factors,st);
  263. f += fstride*in_stride;
  264. }while( (Fout += m) != Fout_end );
  265. }
  266. Fout=Fout_beg;
  267. // recombine the p smaller DFTs
  268. switch (p) {
  269. case 2: kf_bfly2(Fout,fstride,st,m); break;
  270. case 3: kf_bfly3(Fout,fstride,st,m); break;
  271. case 4: kf_bfly4(Fout,fstride,st,m); break;
  272. case 5: kf_bfly5(Fout,fstride,st,m); break;
  273. default: kf_bfly_generic(Fout,fstride,st,m,p); break;
  274. }
  275. }
  276. /* facbuf is populated by p1,m1,p2,m2, ...
  277. where
  278. p[i] * m[i] = m[i-1]
  279. m0 = n */
  280. static
  281. void kf_factor(int n,int * facbuf)
  282. {
  283. int p=4;
  284. double floor_sqrt;
  285. floor_sqrt = floor( sqrt((double)n) );
  286. /*factor out powers of 4, powers of 2, then any remaining primes */
  287. do {
  288. while (n % p) {
  289. switch (p) {
  290. case 4: p = 2; break;
  291. case 2: p = 3; break;
  292. default: p += 2; break;
  293. }
  294. if (p > floor_sqrt)
  295. p = n; /* no more factors, skip to end */
  296. }
  297. n /= p;
  298. *facbuf++ = p;
  299. *facbuf++ = n;
  300. } while (n > 1);
  301. }
  302. /*
  303. *
  304. * User-callable function to allocate all necessary storage space for the fft.
  305. *
  306. * The return value is a contiguous block of memory, allocated with malloc. As such,
  307. * It can be freed with free(), rather than a kiss_fft-specific function.
  308. * */
  309. kiss_fft_cfg kiss_fft_alloc(int nfft,int inverse_fft,void * mem,size_t * lenmem )
  310. {
  311. kiss_fft_cfg st=NULL;
  312. size_t memneeded = sizeof(struct kiss_fft_state)
  313. + sizeof(kiss_fft_cpx)*(nfft-1); /* twiddle factors*/
  314. if ( lenmem==NULL ) {
  315. st = ( kiss_fft_cfg)KISS_FFT_MALLOC( memneeded );
  316. }else{
  317. if (mem != NULL && *lenmem >= memneeded)
  318. st = (kiss_fft_cfg)mem;
  319. *lenmem = memneeded;
  320. }
  321. if (st) {
  322. int i;
  323. st->nfft=nfft;
  324. st->inverse = inverse_fft;
  325. for (i=0;i<nfft;++i) {
  326. const double pi=3.141592653589793238462643383279502884197169399375105820974944;
  327. double phase = -2*pi*i / nfft;
  328. if (st->inverse)
  329. phase *= -1;
  330. kf_cexp(st->twiddles+i, phase );
  331. }
  332. kf_factor(nfft,st->factors);
  333. }
  334. return st;
  335. }
  336. void kiss_fft_stride(kiss_fft_cfg st,const kiss_fft_cpx *fin,kiss_fft_cpx *fout,int in_stride)
  337. {
  338. if (fin == fout) {
  339. //NOTE: this is not really an in-place FFT algorithm.
  340. //It just performs an out-of-place FFT into a temp buffer
  341. kiss_fft_cpx * tmpbuf = (kiss_fft_cpx*)KISS_FFT_TMP_ALLOC( sizeof(kiss_fft_cpx)*st->nfft);
  342. kf_work(tmpbuf,fin,1,in_stride, st->factors,st);
  343. memcpy(fout,tmpbuf,sizeof(kiss_fft_cpx)*st->nfft);
  344. KISS_FFT_TMP_FREE(tmpbuf);
  345. }else{
  346. kf_work( fout, fin, 1,in_stride, st->factors,st );
  347. }
  348. }
  349. void kiss_fft(kiss_fft_cfg cfg,const kiss_fft_cpx *fin,kiss_fft_cpx *fout)
  350. {
  351. kiss_fft_stride(cfg,fin,fout,1);
  352. }
  353. void kiss_fft_cleanup(void)
  354. {
  355. // nothing needed any more
  356. }
  357. int kiss_fft_next_fast_size(int n)
  358. {
  359. while(1) {
  360. int m=n;
  361. while ( (m%2) == 0 ) m/=2;
  362. while ( (m%3) == 0 ) m/=3;
  363. while ( (m%5) == 0 ) m/=5;
  364. if (m<=1)
  365. break; /* n is completely factorable by twos, threes, and fives */
  366. n++;
  367. }
  368. return n;
  369. }