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.

400 lines
12KB

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