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.

160 lines
5.8KB

  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_fftr.h"
  11. #include "_kiss_fft_guts.h"
  12. struct kiss_fftr_state{
  13. kiss_fft_cfg substate;
  14. kiss_fft_cpx * tmpbuf;
  15. kiss_fft_cpx * super_twiddles;
  16. #ifdef USE_SIMD
  17. long pad;
  18. #endif
  19. };
  20. kiss_fftr_cfg kiss_fftr_alloc(int nfft,int inverse_fft,void * mem,size_t * lenmem)
  21. {
  22. int i;
  23. kiss_fftr_cfg st = NULL;
  24. size_t subsize, memneeded;
  25. if (nfft & 1) {
  26. fprintf(stderr,"Real FFT optimization must be even.\n");
  27. return NULL;
  28. }
  29. nfft >>= 1;
  30. kiss_fft_alloc (nfft, inverse_fft, NULL, &subsize);
  31. memneeded = sizeof(struct kiss_fftr_state) + subsize + sizeof(kiss_fft_cpx) * ( nfft * 2);
  32. if (lenmem == NULL) {
  33. st = (kiss_fftr_cfg) KISS_FFT_MALLOC (memneeded);
  34. } else {
  35. if (*lenmem >= memneeded)
  36. st = (kiss_fftr_cfg) mem;
  37. *lenmem = memneeded;
  38. }
  39. if (!st)
  40. return NULL;
  41. st->substate = (kiss_fft_cfg) (st + 1); /*just beyond kiss_fftr_state struct */
  42. st->tmpbuf = (kiss_fft_cpx *) (((char *) st->substate) + subsize);
  43. st->super_twiddles = st->tmpbuf + nfft;
  44. kiss_fft_alloc(nfft, inverse_fft, st->substate, &subsize);
  45. for (i = 0; i < nfft; ++i) {
  46. double phase =
  47. -3.14159265358979323846264338327 * ((double) i / nfft + .5);
  48. if (inverse_fft)
  49. phase *= -1;
  50. kf_cexp (st->super_twiddles+i,phase);
  51. }
  52. return st;
  53. }
  54. void kiss_fftr(kiss_fftr_cfg st,const kiss_fft_scalar *timedata,kiss_fft_cpx *freqdata)
  55. {
  56. /* input buffer timedata is stored row-wise */
  57. int k,ncfft;
  58. kiss_fft_cpx fpnk,fpk,f1k,f2k,tw,tdc;
  59. if ( st->substate->inverse) {
  60. fprintf(stderr,"kiss fft usage error: improper alloc\n");
  61. exit(1);
  62. }
  63. ncfft = st->substate->nfft;
  64. /*perform the parallel fft of two real signals packed in real,imag*/
  65. kiss_fft( st->substate , (const kiss_fft_cpx*)timedata, st->tmpbuf );
  66. /* The real part of the DC element of the frequency spectrum in st->tmpbuf
  67. * contains the sum of the even-numbered elements of the input time sequence
  68. * The imag part is the sum of the odd-numbered elements
  69. *
  70. * The sum of tdc.r and tdc.i is the sum of the input time sequence.
  71. * yielding DC of input time sequence
  72. * The difference of tdc.r - tdc.i is the sum of the input (dot product) [1,-1,1,-1...
  73. * yielding Nyquist bin of input time sequence
  74. */
  75. tdc.r = st->tmpbuf[0].r;
  76. tdc.i = st->tmpbuf[0].i;
  77. C_FIXDIV(tdc,2);
  78. CHECK_OVERFLOW_OP(tdc.r ,+, tdc.i);
  79. CHECK_OVERFLOW_OP(tdc.r ,-, tdc.i);
  80. freqdata[0].r = tdc.r + tdc.i;
  81. freqdata[ncfft].r = tdc.r - tdc.i;
  82. #ifdef USE_SIMD
  83. freqdata[ncfft].i = freqdata[0].i = _mm_set1_ps(0);
  84. #else
  85. freqdata[ncfft].i = freqdata[0].i = 0;
  86. #endif
  87. for ( k=1;k <= ncfft/2 ; ++k ) {
  88. fpk = st->tmpbuf[k];
  89. fpnk.r = st->tmpbuf[ncfft-k].r;
  90. fpnk.i = - st->tmpbuf[ncfft-k].i;
  91. C_FIXDIV(fpk,2);
  92. C_FIXDIV(fpnk,2);
  93. C_ADD( f1k, fpk , fpnk );
  94. C_SUB( f2k, fpk , fpnk );
  95. C_MUL( tw , f2k , st->super_twiddles[k]);
  96. freqdata[k].r = HALF_OF(f1k.r + tw.r);
  97. freqdata[k].i = HALF_OF(f1k.i + tw.i);
  98. freqdata[ncfft-k].r = HALF_OF(f1k.r - tw.r);
  99. freqdata[ncfft-k].i = HALF_OF(tw.i - f1k.i);
  100. }
  101. }
  102. void kiss_fftri(kiss_fftr_cfg st,const kiss_fft_cpx *freqdata,kiss_fft_scalar *timedata)
  103. {
  104. /* input buffer timedata is stored row-wise */
  105. int k, ncfft;
  106. if (st->substate->inverse == 0) {
  107. fprintf (stderr, "kiss fft usage error: improper alloc\n");
  108. exit (1);
  109. }
  110. ncfft = st->substate->nfft;
  111. st->tmpbuf[0].r = freqdata[0].r + freqdata[ncfft].r;
  112. st->tmpbuf[0].i = freqdata[0].r - freqdata[ncfft].r;
  113. C_FIXDIV(st->tmpbuf[0],2);
  114. for (k = 1; k <= ncfft / 2; ++k) {
  115. kiss_fft_cpx fk, fnkc, fek, fok, tmp;
  116. fk = freqdata[k];
  117. fnkc.r = freqdata[ncfft - k].r;
  118. fnkc.i = -freqdata[ncfft - k].i;
  119. C_FIXDIV( fk , 2 );
  120. C_FIXDIV( fnkc , 2 );
  121. C_ADD (fek, fk, fnkc);
  122. C_SUB (tmp, fk, fnkc);
  123. C_MUL (fok, tmp, st->super_twiddles[k]);
  124. C_ADD (st->tmpbuf[k], fek, fok);
  125. C_SUB (st->tmpbuf[ncfft - k], fek, fok);
  126. #ifdef USE_SIMD
  127. st->tmpbuf[ncfft - k].i *= _mm_set1_ps(-1.0);
  128. #else
  129. st->tmpbuf[ncfft - k].i *= -1;
  130. #endif
  131. }
  132. kiss_fft (st->substate, st->tmpbuf, (kiss_fft_cpx *) timedata);
  133. }