kiss_fftr.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. #ifdef HAVE_CONFIG_H
  11. #include "config.h"
  12. #endif
  13. #include "os_support.h"
  14. #include "kiss_fftr.h"
  15. #include "_kiss_fft_guts.h"
  16. struct kiss_fftr_state{
  17. kiss_fft_cfg substate;
  18. kiss_fft_cpx * tmpbuf;
  19. kiss_fft_cpx * super_twiddles;
  20. #ifdef USE_SIMD
  21. long pad;
  22. #endif
  23. };
  24. kiss_fftr_cfg kiss_fftr_alloc(int nfft,int inverse_fft,void * mem,size_t * lenmem)
  25. {
  26. int i;
  27. kiss_fftr_cfg st = NULL;
  28. size_t subsize, memneeded;
  29. if (nfft & 1) {
  30. speex_warning("Real FFT optimization must be even.\n");
  31. return NULL;
  32. }
  33. nfft >>= 1;
  34. kiss_fft_alloc (nfft, inverse_fft, NULL, &subsize);
  35. memneeded = sizeof(struct kiss_fftr_state) + subsize + sizeof(kiss_fft_cpx) * ( nfft * 2);
  36. if (lenmem == NULL) {
  37. st = (kiss_fftr_cfg) KISS_FFT_MALLOC (memneeded);
  38. } else {
  39. if (*lenmem >= memneeded)
  40. st = (kiss_fftr_cfg) mem;
  41. *lenmem = memneeded;
  42. }
  43. if (!st)
  44. return NULL;
  45. st->substate = (kiss_fft_cfg) (st + 1); /*just beyond kiss_fftr_state struct */
  46. st->tmpbuf = (kiss_fft_cpx *) (((char *) st->substate) + subsize);
  47. st->super_twiddles = st->tmpbuf + nfft;
  48. kiss_fft_alloc(nfft, inverse_fft, st->substate, &subsize);
  49. #ifdef FIXED_POINT
  50. for (i=0;i<nfft;++i) {
  51. spx_word32_t phase = i+(nfft>>1);
  52. if (!inverse_fft)
  53. phase = -phase;
  54. kf_cexp2(st->super_twiddles+i, DIV32(SHL32(phase,16),nfft));
  55. }
  56. #else
  57. for (i=0;i<nfft;++i) {
  58. const double pi=3.14159265358979323846264338327;
  59. double phase = pi*(((double)i) /nfft + .5);
  60. if (!inverse_fft)
  61. phase = -phase;
  62. kf_cexp(st->super_twiddles+i, phase );
  63. }
  64. #endif
  65. return st;
  66. }
  67. void kiss_fftr(kiss_fftr_cfg st,const kiss_fft_scalar *timedata,kiss_fft_cpx *freqdata)
  68. {
  69. /* input buffer timedata is stored row-wise */
  70. int k,ncfft;
  71. kiss_fft_cpx fpnk,fpk,f1k,f2k,tw,tdc;
  72. if ( st->substate->inverse) {
  73. speex_fatal("kiss fft usage error: improper alloc\n");
  74. }
  75. ncfft = st->substate->nfft;
  76. /*perform the parallel fft of two real signals packed in real,imag*/
  77. kiss_fft( st->substate , (const kiss_fft_cpx*)timedata, st->tmpbuf );
  78. /* The real part of the DC element of the frequency spectrum in st->tmpbuf
  79. * contains the sum of the even-numbered elements of the input time sequence
  80. * The imag part is the sum of the odd-numbered elements
  81. *
  82. * The sum of tdc.r and tdc.i is the sum of the input time sequence.
  83. * yielding DC of input time sequence
  84. * The difference of tdc.r - tdc.i is the sum of the input (dot product) [1,-1,1,-1...
  85. * yielding Nyquist bin of input time sequence
  86. */
  87. tdc.r = st->tmpbuf[0].r;
  88. tdc.i = st->tmpbuf[0].i;
  89. C_FIXDIV(tdc,2);
  90. CHECK_OVERFLOW_OP(tdc.r ,+, tdc.i);
  91. CHECK_OVERFLOW_OP(tdc.r ,-, tdc.i);
  92. freqdata[0].r = tdc.r + tdc.i;
  93. freqdata[ncfft].r = tdc.r - tdc.i;
  94. #ifdef USE_SIMD
  95. freqdata[ncfft].i = freqdata[0].i = _mm_set1_ps(0);
  96. #else
  97. freqdata[ncfft].i = freqdata[0].i = 0;
  98. #endif
  99. for ( k=1;k <= ncfft/2 ; ++k ) {
  100. fpk = st->tmpbuf[k];
  101. fpnk.r = st->tmpbuf[ncfft-k].r;
  102. fpnk.i = - st->tmpbuf[ncfft-k].i;
  103. C_FIXDIV(fpk,2);
  104. C_FIXDIV(fpnk,2);
  105. C_ADD( f1k, fpk , fpnk );
  106. C_SUB( f2k, fpk , fpnk );
  107. C_MUL( tw , f2k , st->super_twiddles[k]);
  108. freqdata[k].r = HALF_OF(f1k.r + tw.r);
  109. freqdata[k].i = HALF_OF(f1k.i + tw.i);
  110. freqdata[ncfft-k].r = HALF_OF(f1k.r - tw.r);
  111. freqdata[ncfft-k].i = HALF_OF(tw.i - f1k.i);
  112. }
  113. }
  114. void kiss_fftri(kiss_fftr_cfg st,const kiss_fft_cpx *freqdata, kiss_fft_scalar *timedata)
  115. {
  116. /* input buffer timedata is stored row-wise */
  117. int k, ncfft;
  118. if (st->substate->inverse == 0) {
  119. speex_fatal("kiss fft usage error: improper alloc\n");
  120. }
  121. ncfft = st->substate->nfft;
  122. st->tmpbuf[0].r = freqdata[0].r + freqdata[ncfft].r;
  123. st->tmpbuf[0].i = freqdata[0].r - freqdata[ncfft].r;
  124. /*C_FIXDIV(st->tmpbuf[0],2);*/
  125. for (k = 1; k <= ncfft / 2; ++k) {
  126. kiss_fft_cpx fk, fnkc, fek, fok, tmp;
  127. fk = freqdata[k];
  128. fnkc.r = freqdata[ncfft - k].r;
  129. fnkc.i = -freqdata[ncfft - k].i;
  130. /*C_FIXDIV( fk , 2 );
  131. C_FIXDIV( fnkc , 2 );*/
  132. C_ADD (fek, fk, fnkc);
  133. C_SUB (tmp, fk, fnkc);
  134. C_MUL (fok, tmp, st->super_twiddles[k]);
  135. C_ADD (st->tmpbuf[k], fek, fok);
  136. C_SUB (st->tmpbuf[ncfft - k], fek, fok);
  137. #ifdef USE_SIMD
  138. st->tmpbuf[ncfft - k].i *= _mm_set1_ps(-1.0);
  139. #else
  140. st->tmpbuf[ncfft - k].i *= -1;
  141. #endif
  142. }
  143. kiss_fft (st->substate, st->tmpbuf, (kiss_fft_cpx *) timedata);
  144. }
  145. void kiss_fftr2(kiss_fftr_cfg st,const kiss_fft_scalar *timedata,kiss_fft_scalar *freqdata)
  146. {
  147. /* input buffer timedata is stored row-wise */
  148. int k,ncfft;
  149. kiss_fft_cpx f2k,tdc;
  150. spx_word32_t f1kr, f1ki, twr, twi;
  151. if ( st->substate->inverse) {
  152. speex_fatal("kiss fft usage error: improper alloc\n");
  153. }
  154. ncfft = st->substate->nfft;
  155. /*perform the parallel fft of two real signals packed in real,imag*/
  156. kiss_fft( st->substate , (const kiss_fft_cpx*)timedata, st->tmpbuf );
  157. /* The real part of the DC element of the frequency spectrum in st->tmpbuf
  158. * contains the sum of the even-numbered elements of the input time sequence
  159. * The imag part is the sum of the odd-numbered elements
  160. *
  161. * The sum of tdc.r and tdc.i is the sum of the input time sequence.
  162. * yielding DC of input time sequence
  163. * The difference of tdc.r - tdc.i is the sum of the input (dot product) [1,-1,1,-1...
  164. * yielding Nyquist bin of input time sequence
  165. */
  166. tdc.r = st->tmpbuf[0].r;
  167. tdc.i = st->tmpbuf[0].i;
  168. C_FIXDIV(tdc,2);
  169. CHECK_OVERFLOW_OP(tdc.r ,+, tdc.i);
  170. CHECK_OVERFLOW_OP(tdc.r ,-, tdc.i);
  171. freqdata[0] = tdc.r + tdc.i;
  172. freqdata[2*ncfft-1] = tdc.r - tdc.i;
  173. for ( k=1;k <= ncfft/2 ; ++k )
  174. {
  175. /*fpk = st->tmpbuf[k];
  176. fpnk.r = st->tmpbuf[ncfft-k].r;
  177. fpnk.i = - st->tmpbuf[ncfft-k].i;
  178. C_FIXDIV(fpk,2);
  179. C_FIXDIV(fpnk,2);
  180. C_ADD( f1k, fpk , fpnk );
  181. C_SUB( f2k, fpk , fpnk );
  182. C_MUL( tw , f2k , st->super_twiddles[k]);
  183. freqdata[2*k-1] = HALF_OF(f1k.r + tw.r);
  184. freqdata[2*k] = HALF_OF(f1k.i + tw.i);
  185. freqdata[2*(ncfft-k)-1] = HALF_OF(f1k.r - tw.r);
  186. freqdata[2*(ncfft-k)] = HALF_OF(tw.i - f1k.i);
  187. */
  188. /*f1k.r = PSHR32(ADD32(EXTEND32(st->tmpbuf[k].r), EXTEND32(st->tmpbuf[ncfft-k].r)),1);
  189. f1k.i = PSHR32(SUB32(EXTEND32(st->tmpbuf[k].i), EXTEND32(st->tmpbuf[ncfft-k].i)),1);
  190. f2k.r = PSHR32(SUB32(EXTEND32(st->tmpbuf[k].r), EXTEND32(st->tmpbuf[ncfft-k].r)),1);
  191. f2k.i = SHR32(ADD32(EXTEND32(st->tmpbuf[k].i), EXTEND32(st->tmpbuf[ncfft-k].i)),1);
  192. C_MUL( tw , f2k , st->super_twiddles[k]);
  193. freqdata[2*k-1] = HALF_OF(f1k.r + tw.r);
  194. freqdata[2*k] = HALF_OF(f1k.i + tw.i);
  195. freqdata[2*(ncfft-k)-1] = HALF_OF(f1k.r - tw.r);
  196. freqdata[2*(ncfft-k)] = HALF_OF(tw.i - f1k.i);
  197. */
  198. f2k.r = SHR32(SUB32(EXTEND32(st->tmpbuf[k].r), EXTEND32(st->tmpbuf[ncfft-k].r)),1);
  199. f2k.i = PSHR32(ADD32(EXTEND32(st->tmpbuf[k].i), EXTEND32(st->tmpbuf[ncfft-k].i)),1);
  200. f1kr = SHL32(ADD32(EXTEND32(st->tmpbuf[k].r), EXTEND32(st->tmpbuf[ncfft-k].r)),13);
  201. f1ki = SHL32(SUB32(EXTEND32(st->tmpbuf[k].i), EXTEND32(st->tmpbuf[ncfft-k].i)),13);
  202. twr = SHR32(SUB32(MULT16_16(f2k.r,st->super_twiddles[k].r),MULT16_16(f2k.i,st->super_twiddles[k].i)), 1);
  203. twi = SHR32(ADD32(MULT16_16(f2k.i,st->super_twiddles[k].r),MULT16_16(f2k.r,st->super_twiddles[k].i)), 1);
  204. #ifdef FIXED_POINT
  205. freqdata[2*k-1] = PSHR32(f1kr + twr, 15);
  206. freqdata[2*k] = PSHR32(f1ki + twi, 15);
  207. freqdata[2*(ncfft-k)-1] = PSHR32(f1kr - twr, 15);
  208. freqdata[2*(ncfft-k)] = PSHR32(twi - f1ki, 15);
  209. #else
  210. freqdata[2*k-1] = .5f*(f1kr + twr);
  211. freqdata[2*k] = .5f*(f1ki + twi);
  212. freqdata[2*(ncfft-k)-1] = .5f*(f1kr - twr);
  213. freqdata[2*(ncfft-k)] = .5f*(twi - f1ki);
  214. #endif
  215. }
  216. }
  217. void kiss_fftri2(kiss_fftr_cfg st,const kiss_fft_scalar *freqdata,kiss_fft_scalar *timedata)
  218. {
  219. /* input buffer timedata is stored row-wise */
  220. int k, ncfft;
  221. if (st->substate->inverse == 0) {
  222. speex_fatal ("kiss fft usage error: improper alloc\n");
  223. }
  224. ncfft = st->substate->nfft;
  225. st->tmpbuf[0].r = freqdata[0] + freqdata[2*ncfft-1];
  226. st->tmpbuf[0].i = freqdata[0] - freqdata[2*ncfft-1];
  227. /*C_FIXDIV(st->tmpbuf[0],2);*/
  228. for (k = 1; k <= ncfft / 2; ++k) {
  229. kiss_fft_cpx fk, fnkc, fek, fok, tmp;
  230. fk.r = freqdata[2*k-1];
  231. fk.i = freqdata[2*k];
  232. fnkc.r = freqdata[2*(ncfft - k)-1];
  233. fnkc.i = -freqdata[2*(ncfft - k)];
  234. /*C_FIXDIV( fk , 2 );
  235. C_FIXDIV( fnkc , 2 );*/
  236. C_ADD (fek, fk, fnkc);
  237. C_SUB (tmp, fk, fnkc);
  238. C_MUL (fok, tmp, st->super_twiddles[k]);
  239. C_ADD (st->tmpbuf[k], fek, fok);
  240. C_SUB (st->tmpbuf[ncfft - k], fek, fok);
  241. #ifdef USE_SIMD
  242. st->tmpbuf[ncfft - k].i *= _mm_set1_ps(-1.0);
  243. #else
  244. st->tmpbuf[ncfft - k].i *= -1;
  245. #endif
  246. }
  247. kiss_fft (st->substate, st->tmpbuf, (kiss_fft_cpx *) timedata);
  248. }