gsl_fft__hc_radix2.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /* fft/hc_radix2.c
  2. *
  3. * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2007 Brian Gough
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. */
  19. int
  20. FUNCTION(gsl_fft_halfcomplex,radix2_backward) (BASE data[],
  21. const size_t stride,
  22. const size_t n)
  23. {
  24. int status = FUNCTION(gsl_fft_halfcomplex,radix2_transform) (data, stride, n) ;
  25. return status ;
  26. }
  27. int
  28. FUNCTION(gsl_fft_halfcomplex,radix2_inverse) (BASE data[],
  29. const size_t stride,
  30. const size_t n)
  31. {
  32. int status = FUNCTION(gsl_fft_halfcomplex,radix2_transform) (data, stride, n);
  33. if (status)
  34. {
  35. return status;
  36. }
  37. /* normalize inverse fft with 1/n */
  38. {
  39. const ATOMIC norm = 1.0 / n;
  40. size_t i;
  41. for (i = 0; i < n; i++)
  42. {
  43. data[stride*i] *= norm;
  44. }
  45. }
  46. return status;
  47. }
  48. int
  49. FUNCTION(gsl_fft_halfcomplex,radix2_transform) (BASE data[],
  50. const size_t stride,
  51. const size_t n)
  52. {
  53. int result ;
  54. size_t p, p_1, q;
  55. size_t i;
  56. size_t logn = 0;
  57. int status;
  58. if (n == 1) /* identity operation */
  59. {
  60. return 0 ;
  61. }
  62. /* make sure that n is a power of 2 */
  63. result = fft_binary_logn(n) ;
  64. if (result == -1)
  65. {
  66. GSL_ERROR ("n is not a power of 2", GSL_EINVAL);
  67. }
  68. else
  69. {
  70. logn = result ;
  71. }
  72. /* apply fft recursion */
  73. p = n; q = 1 ; p_1 = n/2 ;
  74. for (i = 1; i <= logn; i++)
  75. {
  76. size_t a, b;
  77. /* a = 0 */
  78. for (b = 0; b < q; b++)
  79. {
  80. const ATOMIC z0 = VECTOR(data,stride,b*p);
  81. const ATOMIC z1 = VECTOR(data,stride,b*p + p_1);
  82. const ATOMIC t0_real = z0 + z1 ;
  83. const ATOMIC t1_real = z0 - z1 ;
  84. VECTOR(data,stride,b*p) = t0_real;
  85. VECTOR(data,stride,b*p + p_1) = t1_real ;
  86. }
  87. /* a = 1 ... p_{i-1}/2 - 1 */
  88. {
  89. ATOMIC w_real = 1.0;
  90. ATOMIC w_imag = 0.0;
  91. const ATOMIC theta = 2.0 * M_PI / p;
  92. const ATOMIC s = sin (theta);
  93. const ATOMIC t = sin (theta / 2.0);
  94. const ATOMIC s2 = 2.0 * t * t;
  95. for (a = 1; a < (p_1)/2; a++)
  96. {
  97. /* trignometric recurrence for w-> exp(i theta) w */
  98. {
  99. const ATOMIC tmp_real = w_real - s * w_imag - s2 * w_real;
  100. const ATOMIC tmp_imag = w_imag + s * w_real - s2 * w_imag;
  101. w_real = tmp_real;
  102. w_imag = tmp_imag;
  103. }
  104. for (b = 0; b < q; b++)
  105. {
  106. ATOMIC z0_real = VECTOR(data,stride,b*p + a) ;
  107. ATOMIC z0_imag = VECTOR(data,stride,b*p + p - a) ;
  108. ATOMIC z1_real = VECTOR(data,stride,b*p + p_1 - a) ;
  109. ATOMIC z1_imag = -VECTOR(data,stride,b*p + p_1 + a) ;
  110. /* t0 = z0 + z1 */
  111. ATOMIC t0_real = z0_real + z1_real;
  112. ATOMIC t0_imag = z0_imag + z1_imag;
  113. /* t1 = (z0 - z1) */
  114. ATOMIC t1_real = z0_real - z1_real;
  115. ATOMIC t1_imag = z0_imag - z1_imag;
  116. VECTOR(data,stride,b*p + a) = t0_real ;
  117. VECTOR(data,stride,b*p + p_1 - a) = t0_imag ;
  118. VECTOR(data,stride,b*p + p_1 + a) = (w_real * t1_real - w_imag * t1_imag) ;
  119. VECTOR(data,stride,b*p + p - a) = (w_real * t1_imag + w_imag * t1_real) ;
  120. }
  121. }
  122. }
  123. if (p_1 > 1) {
  124. for (b = 0; b < q; b++) {
  125. VECTOR(data,stride,b*p + p_1/2) *= 2 ;
  126. VECTOR(data,stride,b*p + p_1 + p_1/2) *= -2 ;
  127. }
  128. }
  129. p_1 = p_1 / 2 ;
  130. p = p / 2 ;
  131. q = q * 2 ;
  132. }
  133. /* bit reverse the ordering of output data for decimation in
  134. frequency algorithm */
  135. status = FUNCTION(fft_real,bitreverse_order)(data, stride, n, logn) ;
  136. return 0;
  137. }