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.

151 lines
5.1KB

  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. /* kiss_fft.h
  11. defines kiss_fft_scalar as either short or a float type
  12. and defines
  13. typedef struct { kiss_fft_scalar r; kiss_fft_scalar i; }kiss_fft_cpx; */
  14. #include "kiss_fft.h"
  15. #include <limits.h>
  16. #define MAXFACTORS 32
  17. /* e.g. an fft of length 128 has 4 factors
  18. as far as kissfft is concerned
  19. 4*4*4*2
  20. */
  21. struct kiss_fft_state{
  22. int nfft;
  23. int inverse;
  24. int factors[2*MAXFACTORS];
  25. kiss_fft_cpx twiddles[1];
  26. };
  27. /*
  28. Explanation of macros dealing with complex math:
  29. C_MUL(m,a,b) : m = a*b
  30. C_FIXDIV( c , div ) : if a fixed point impl., c /= div. noop otherwise
  31. C_SUB( res, a,b) : res = a - b
  32. C_SUBFROM( res , a) : res -= a
  33. C_ADDTO( res , a) : res += a
  34. * */
  35. #ifdef FIXED_POINT
  36. #if (FIXED_POINT==32)
  37. # define FRACBITS 31
  38. # define SAMPPROD int64_t
  39. #define SAMP_MAX 2147483647
  40. #else
  41. # define FRACBITS 15
  42. # define SAMPPROD int32_t
  43. #define SAMP_MAX 32767
  44. #endif
  45. #define SAMP_MIN -SAMP_MAX
  46. #if defined(CHECK_OVERFLOW)
  47. # define CHECK_OVERFLOW_OP(a,op,b) \
  48. if ( (SAMPPROD)(a) op (SAMPPROD)(b) > SAMP_MAX || (SAMPPROD)(a) op (SAMPPROD)(b) < SAMP_MIN ) { \
  49. fprintf(stderr,"WARNING:overflow @ " __FILE__ "(%d): (%d " #op" %d) = %ld\n",__LINE__,(a),(b),(SAMPPROD)(a) op (SAMPPROD)(b) ); }
  50. #endif
  51. # define smul(a,b) ( (SAMPPROD)(a)*(b) )
  52. # define sround( x ) (kiss_fft_scalar)( ( (x) + (1<<(FRACBITS-1)) ) >> FRACBITS )
  53. # define S_MUL(a,b) sround( smul(a,b) )
  54. # define C_MUL(m,a,b) \
  55. do{ (m).r = sround( smul((a).r,(b).r) - smul((a).i,(b).i) ); \
  56. (m).i = sround( smul((a).r,(b).i) + smul((a).i,(b).r) ); }while(0)
  57. # define DIVSCALAR(x,k) \
  58. (x) = sround( smul( x, SAMP_MAX/k ) )
  59. # define C_FIXDIV(c,div) \
  60. do { DIVSCALAR( (c).r , div); \
  61. DIVSCALAR( (c).i , div); }while (0)
  62. # define C_MULBYSCALAR( c, s ) \
  63. do{ (c).r = sround( smul( (c).r , s ) ) ;\
  64. (c).i = sround( smul( (c).i , s ) ) ; }while(0)
  65. #else /* not FIXED_POINT*/
  66. # define S_MUL(a,b) ( (a)*(b) )
  67. #define C_MUL(m,a,b) \
  68. do{ (m).r = (a).r*(b).r - (a).i*(b).i;\
  69. (m).i = (a).r*(b).i + (a).i*(b).r; }while(0)
  70. # define C_FIXDIV(c,div) /* NOOP */
  71. # define C_MULBYSCALAR( c, s ) \
  72. do{ (c).r *= (s);\
  73. (c).i *= (s); }while(0)
  74. #endif
  75. #ifndef CHECK_OVERFLOW_OP
  76. # define CHECK_OVERFLOW_OP(a,op,b) /* noop */
  77. #endif
  78. #define C_ADD( res, a,b)\
  79. do { \
  80. CHECK_OVERFLOW_OP((a).r,+,(b).r)\
  81. CHECK_OVERFLOW_OP((a).i,+,(b).i)\
  82. (res).r=(a).r+(b).r; (res).i=(a).i+(b).i; \
  83. }while(0)
  84. #define C_SUB( res, a,b)\
  85. do { \
  86. CHECK_OVERFLOW_OP((a).r,-,(b).r)\
  87. CHECK_OVERFLOW_OP((a).i,-,(b).i)\
  88. (res).r=(a).r-(b).r; (res).i=(a).i-(b).i; \
  89. }while(0)
  90. #define C_ADDTO( res , a)\
  91. do { \
  92. CHECK_OVERFLOW_OP((res).r,+,(a).r)\
  93. CHECK_OVERFLOW_OP((res).i,+,(a).i)\
  94. (res).r += (a).r; (res).i += (a).i;\
  95. }while(0)
  96. #define C_SUBFROM( res , a)\
  97. do {\
  98. CHECK_OVERFLOW_OP((res).r,-,(a).r)\
  99. CHECK_OVERFLOW_OP((res).i,-,(a).i)\
  100. (res).r -= (a).r; (res).i -= (a).i; \
  101. }while(0)
  102. #ifdef FIXED_POINT
  103. # define KISS_FFT_COS(phase) floor(.5+SAMP_MAX * cos (phase))
  104. # define KISS_FFT_SIN(phase) floor(.5+SAMP_MAX * sin (phase))
  105. # define HALF_OF(x) ((x)>>1)
  106. #elif defined(USE_SIMD)
  107. # define KISS_FFT_COS(phase) _mm_set1_ps( cos(phase) )
  108. # define KISS_FFT_SIN(phase) _mm_set1_ps( sin(phase) )
  109. # define HALF_OF(x) ((x)*_mm_set1_ps(.5))
  110. #else
  111. # define KISS_FFT_COS(phase) (kiss_fft_scalar) cos(phase)
  112. # define KISS_FFT_SIN(phase) (kiss_fft_scalar) sin(phase)
  113. # define HALF_OF(x) ((x)*.5)
  114. #endif
  115. #define kf_cexp(x,phase) \
  116. do{ \
  117. (x)->r = KISS_FFT_COS(phase);\
  118. (x)->i = KISS_FFT_SIN(phase);\
  119. }while(0)
  120. /* a debugging function */
  121. #define pcpx(c)\
  122. fprintf(stderr,"%g + %gi\n",(double)((c)->r),(double)((c)->i) )