alcomplex.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #include "config.h"
  2. #include "alcomplex.h"
  3. #include "math_defs.h"
  4. extern inline ALcomplex complex_add(ALcomplex a, ALcomplex b);
  5. extern inline ALcomplex complex_sub(ALcomplex a, ALcomplex b);
  6. extern inline ALcomplex complex_mult(ALcomplex a, ALcomplex b);
  7. void complex_fft(ALcomplex *FFTBuffer, ALsizei FFTSize, ALdouble Sign)
  8. {
  9. ALsizei i, j, k, mask, step, step2;
  10. ALcomplex temp, u, w;
  11. ALdouble arg;
  12. /* Bit-reversal permutation applied to a sequence of FFTSize items */
  13. for(i = 1;i < FFTSize-1;i++)
  14. {
  15. for(mask = 0x1, j = 0;mask < FFTSize;mask <<= 1)
  16. {
  17. if((i&mask) != 0)
  18. j++;
  19. j <<= 1;
  20. }
  21. j >>= 1;
  22. if(i < j)
  23. {
  24. temp = FFTBuffer[i];
  25. FFTBuffer[i] = FFTBuffer[j];
  26. FFTBuffer[j] = temp;
  27. }
  28. }
  29. /* Iterative form of Danielson–Lanczos lemma */
  30. for(i = 1, step = 2;i < FFTSize;i<<=1, step<<=1)
  31. {
  32. step2 = step >> 1;
  33. arg = M_PI / step2;
  34. w.Real = cos(arg);
  35. w.Imag = sin(arg) * Sign;
  36. u.Real = 1.0;
  37. u.Imag = 0.0;
  38. for(j = 0;j < step2;j++)
  39. {
  40. for(k = j;k < FFTSize;k+=step)
  41. {
  42. temp = complex_mult(FFTBuffer[k+step2], u);
  43. FFTBuffer[k+step2] = complex_sub(FFTBuffer[k], temp);
  44. FFTBuffer[k] = complex_add(FFTBuffer[k], temp);
  45. }
  46. u = complex_mult(u, w);
  47. }
  48. }
  49. }
  50. void complex_hilbert(ALcomplex *Buffer, ALsizei size)
  51. {
  52. const ALdouble inverse_size = 1.0/(ALdouble)size;
  53. ALsizei todo, i;
  54. for(i = 0;i < size;i++)
  55. Buffer[i].Imag = 0.0;
  56. complex_fft(Buffer, size, 1.0);
  57. todo = size >> 1;
  58. Buffer[0].Real *= inverse_size;
  59. Buffer[0].Imag *= inverse_size;
  60. for(i = 1;i < todo;i++)
  61. {
  62. Buffer[i].Real *= 2.0*inverse_size;
  63. Buffer[i].Imag *= 2.0*inverse_size;
  64. }
  65. Buffer[i].Real *= inverse_size;
  66. Buffer[i].Imag *= inverse_size;
  67. i++;
  68. for(;i < size;i++)
  69. {
  70. Buffer[i].Real = 0.0;
  71. Buffer[i].Imag = 0.0;
  72. }
  73. complex_fft(Buffer, size, -1.0);
  74. }