gsl_fft__c_pass_n.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /* fft/c_pass_n.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. static int
  20. FUNCTION(fft_complex,pass_n) (BASE in[],
  21. const size_t istride,
  22. BASE out[],
  23. const size_t ostride,
  24. const gsl_fft_direction sign,
  25. const size_t factor,
  26. const size_t product,
  27. const size_t n,
  28. const TYPE(gsl_complex) twiddle[])
  29. {
  30. size_t i = 0, j = 0;
  31. size_t k, k1;
  32. const size_t m = n / factor;
  33. const size_t q = n / product;
  34. const size_t p_1 = product / factor;
  35. const size_t jump = (factor - 1) * p_1;
  36. size_t e, e1;
  37. for (i = 0; i < m; i++)
  38. {
  39. REAL(out,ostride,i) = REAL(in,istride,i);
  40. IMAG(out,ostride,i) = IMAG(in,istride,i);
  41. }
  42. for (e = 1; e < (factor - 1) / 2 + 1; e++)
  43. {
  44. for (i = 0; i < m; i++)
  45. {
  46. const size_t idx = i + e * m;
  47. const size_t idxc = i + (factor - e) * m;
  48. REAL(out,ostride,idx) = REAL(in,istride,idx) + REAL(in,istride,idxc);
  49. IMAG(out,ostride,idx) = IMAG(in,istride,idx) + IMAG(in,istride,idxc);
  50. REAL(out,ostride,idxc) = REAL(in,istride,idx) - REAL(in,istride,idxc);
  51. IMAG(out,ostride,idxc) = IMAG(in,istride,idx) - IMAG(in,istride,idxc);
  52. }
  53. }
  54. /* e = 0 */
  55. for (i=0 ; i<m; i++)
  56. {
  57. REAL(in,istride,i) = REAL(out,ostride,i);
  58. IMAG(in,istride,i) = IMAG(out,ostride,i);
  59. }
  60. for (e1 = 1; e1 < (factor - 1) / 2 + 1; e1++)
  61. {
  62. for (i = 0; i < m; i++)
  63. {
  64. REAL(in,istride,i) += REAL(out,ostride,i + e1*m) ;
  65. IMAG(in,istride,i) += IMAG(out,ostride,i + e1*m) ;
  66. }
  67. }
  68. for (e = 1; e < (factor-1)/2 + 1; e++)
  69. {
  70. size_t idx = e*q ;
  71. const size_t idx_step = e * q ;
  72. ATOMIC w_real, w_imag ;
  73. const size_t em = e * m ;
  74. const size_t ecm = (factor - e) * m ;
  75. for (i = 0; i < m; i++)
  76. {
  77. REAL(in,istride,i+em) = REAL(out,ostride,i) ;
  78. IMAG(in,istride,i+em) = IMAG(out,ostride,i) ;
  79. REAL(in,istride,i+ecm) = REAL(out,ostride,i) ;
  80. IMAG(in,istride,i+ecm) = IMAG(out,ostride,i) ;
  81. }
  82. for (e1 = 1; e1 < (factor - 1) / 2 + 1; e1++)
  83. {
  84. if (idx == 0) {
  85. w_real = 1 ;
  86. w_imag = 0 ;
  87. } else {
  88. if (sign == gsl_fft_forward) {
  89. w_real = GSL_REAL(twiddle[idx - 1]) ;
  90. w_imag = GSL_IMAG(twiddle[idx - 1]) ;
  91. } else {
  92. w_real = GSL_REAL(twiddle[idx - 1]) ;
  93. w_imag = -GSL_IMAG(twiddle[idx - 1]) ;
  94. }
  95. }
  96. for (i = 0; i < m; i++)
  97. {
  98. const ATOMIC xp_real = REAL(out,ostride,i + e1 * m);
  99. const ATOMIC xp_imag = IMAG(out,ostride,i + e1 * m);
  100. const ATOMIC xm_real = REAL(out,ostride,i + (factor - e1) *m);
  101. const ATOMIC xm_imag = IMAG(out,ostride,i + (factor - e1) *m);
  102. const ATOMIC ap = w_real * xp_real ;
  103. const ATOMIC am = w_imag * xm_imag ;
  104. ATOMIC sum_real = ap - am;
  105. ATOMIC sumc_real = ap + am;
  106. const ATOMIC bp = w_real * xp_imag ;
  107. const ATOMIC bm = w_imag * xm_real ;
  108. ATOMIC sum_imag = bp + bm;
  109. ATOMIC sumc_imag = bp - bm;
  110. REAL(in,istride,i + em) += sum_real;
  111. IMAG(in,istride,i + em) += sum_imag;
  112. REAL(in,istride,i + ecm) += sumc_real;
  113. IMAG(in,istride,i + ecm) += sumc_imag;
  114. }
  115. idx += idx_step ;
  116. idx %= factor * q ;
  117. }
  118. }
  119. i = 0;
  120. j = 0;
  121. /* k = 0 */
  122. for (k1 = 0; k1 < p_1; k1++)
  123. {
  124. REAL(out,ostride,k1) = REAL(in,istride,k1);
  125. IMAG(out,ostride,k1) = IMAG(in,istride,k1);
  126. }
  127. for (e1 = 1; e1 < factor; e1++)
  128. {
  129. for (k1 = 0; k1 < p_1; k1++)
  130. {
  131. REAL(out,ostride,k1 + e1 * p_1) = REAL(in,istride,k1 + e1 * m) ;
  132. IMAG(out,ostride,k1 + e1 * p_1) = IMAG(in,istride,k1 + e1 * m) ;
  133. }
  134. }
  135. i = p_1 ;
  136. j = product ;
  137. for (k = 1; k < q; k++)
  138. {
  139. for (k1 = 0; k1 < p_1; k1++)
  140. {
  141. REAL(out,ostride,j) = REAL(in,istride,i);
  142. IMAG(out,ostride,j) = IMAG(in,istride,i);
  143. i++;
  144. j++;
  145. }
  146. j += jump;
  147. }
  148. i = p_1 ;
  149. j = product ;
  150. for (k = 1; k < q; k++)
  151. {
  152. for (k1 = 0; k1 < p_1; k1++)
  153. {
  154. for (e1 = 1; e1 < factor; e1++)
  155. {
  156. ATOMIC x_real = REAL(in, istride,i + e1 * m);
  157. ATOMIC x_imag = IMAG(in, istride,i + e1 * m);
  158. ATOMIC w_real, w_imag ;
  159. if (sign == gsl_fft_forward) {
  160. w_real = GSL_REAL(twiddle[(e1-1)*q + k-1]) ;
  161. w_imag = GSL_IMAG(twiddle[(e1-1)*q + k-1]) ;
  162. } else {
  163. w_real = GSL_REAL(twiddle[(e1-1)*q + k-1]) ;
  164. w_imag = -GSL_IMAG(twiddle[(e1-1)*q + k-1]) ;
  165. }
  166. REAL(out,ostride,j + e1 * p_1) = w_real * x_real - w_imag * x_imag;
  167. IMAG(out,ostride,j + e1 * p_1) = w_real * x_imag + w_imag * x_real;
  168. }
  169. i++;
  170. j++;
  171. }
  172. j += jump;
  173. }
  174. return 0;
  175. }