gsl_fft__real_radix2.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /* fft/real_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_real,radix2_transform) (BASE data[], const size_t stride, const size_t n)
  21. {
  22. int result ;
  23. size_t p, p_1, q;
  24. size_t i;
  25. size_t logn = 0;
  26. int status;
  27. if (n == 1) /* identity operation */
  28. {
  29. return 0 ;
  30. }
  31. /* make sure that n is a power of 2 */
  32. result = fft_binary_logn(n) ;
  33. if (result == -1)
  34. {
  35. GSL_ERROR ("n is not a power of 2", GSL_EINVAL);
  36. }
  37. else
  38. {
  39. logn = result ;
  40. }
  41. /* bit reverse the ordering of input data for decimation in time algorithm */
  42. status = FUNCTION(fft_real,bitreverse_order)(data, stride, n, logn) ;
  43. /* apply fft recursion */
  44. p = 1; q = n ;
  45. for (i = 1; i <= logn; i++)
  46. {
  47. size_t a, b;
  48. p_1 = p ;
  49. p = 2 * p ;
  50. q = q / 2 ;
  51. /* a = 0 */
  52. for (b = 0; b < q; b++)
  53. {
  54. ATOMIC t0_real = VECTOR(data,stride,b*p) + VECTOR(data,stride,b*p + p_1) ;
  55. ATOMIC t1_real = VECTOR(data,stride,b*p) - VECTOR(data,stride,b*p + p_1) ;
  56. VECTOR(data,stride,b*p) = t0_real ;
  57. VECTOR(data,stride,b*p + p_1) = t1_real ;
  58. }
  59. /* a = 1 ... p_{i-1}/2 - 1 */
  60. {
  61. ATOMIC w_real = 1.0;
  62. ATOMIC w_imag = 0.0;
  63. const double theta = - 2.0 * M_PI / p;
  64. const ATOMIC s = sin (theta);
  65. const ATOMIC t = sin (theta / 2.0);
  66. const ATOMIC s2 = 2.0 * t * t;
  67. for (a = 1; a < (p_1)/2; a++)
  68. {
  69. /* trignometric recurrence for w-> exp(i theta) w */
  70. {
  71. const ATOMIC tmp_real = w_real - s * w_imag - s2 * w_real;
  72. const ATOMIC tmp_imag = w_imag + s * w_real - s2 * w_imag;
  73. w_real = tmp_real;
  74. w_imag = tmp_imag;
  75. }
  76. for (b = 0; b < q; b++)
  77. {
  78. ATOMIC z0_real = VECTOR(data,stride,b*p + a) ;
  79. ATOMIC z0_imag = VECTOR(data,stride,b*p + p_1 - a) ;
  80. ATOMIC z1_real = VECTOR(data,stride,b*p + p_1 + a) ;
  81. ATOMIC z1_imag = VECTOR(data,stride,b*p + p - a) ;
  82. /* t0 = z0 + w * z1 */
  83. ATOMIC t0_real = z0_real + w_real * z1_real - w_imag * z1_imag;
  84. ATOMIC t0_imag = z0_imag + w_real * z1_imag + w_imag * z1_real;
  85. /* t1 = z0 - w * z1 */
  86. ATOMIC t1_real = z0_real - w_real * z1_real + w_imag * z1_imag;
  87. ATOMIC t1_imag = z0_imag - w_real * z1_imag - w_imag * z1_real;
  88. VECTOR(data,stride,b*p + a) = t0_real ;
  89. VECTOR(data,stride,b*p + p - a) = t0_imag ;
  90. VECTOR(data,stride,b*p + p_1 - a) = t1_real ;
  91. VECTOR(data,stride,b*p + p_1 + a) = -t1_imag ;
  92. }
  93. }
  94. }
  95. if (p_1 > 1)
  96. {
  97. for (b = 0; b < q; b++)
  98. {
  99. /* a = p_{i-1}/2 */
  100. VECTOR(data,stride,b*p + p - p_1/2) *= -1 ;
  101. }
  102. }
  103. }
  104. return 0;
  105. }