gsl_fft__c_radix2.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /* fft/c_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_complex,radix2_forward) (TYPE(gsl_complex_packed_array) data,
  21. const size_t stride, const size_t n)
  22. {
  23. gsl_fft_direction sign = gsl_fft_forward;
  24. int status = FUNCTION(gsl_fft_complex,radix2_transform) (data, stride, n, sign);
  25. return status;
  26. }
  27. int
  28. FUNCTION(gsl_fft_complex,radix2_backward) (TYPE(gsl_complex_packed_array) data,
  29. const size_t stride, const size_t n)
  30. {
  31. gsl_fft_direction sign = gsl_fft_backward;
  32. int status = FUNCTION(gsl_fft_complex,radix2_transform) (data, stride, n, sign);
  33. return status;
  34. }
  35. int
  36. FUNCTION(gsl_fft_complex,radix2_inverse) (TYPE(gsl_complex_packed_array) data,
  37. const size_t stride, const size_t n)
  38. {
  39. gsl_fft_direction sign = gsl_fft_backward;
  40. int status = FUNCTION(gsl_fft_complex,radix2_transform) (data, stride, n, sign);
  41. if (status)
  42. {
  43. return status;
  44. }
  45. /* normalize inverse fft with 1/n */
  46. {
  47. const ATOMIC norm = 1.0 / n;
  48. size_t i;
  49. for (i = 0; i < n; i++)
  50. {
  51. REAL(data,stride,i) *= norm;
  52. IMAG(data,stride,i) *= norm;
  53. }
  54. }
  55. return status;
  56. }
  57. int
  58. FUNCTION(gsl_fft_complex,radix2_transform) (TYPE(gsl_complex_packed_array) data,
  59. const size_t stride,
  60. const size_t n,
  61. const gsl_fft_direction sign)
  62. {
  63. int result ;
  64. size_t dual;
  65. size_t bit;
  66. size_t logn = 0;
  67. int status;
  68. if (n == 1) /* identity operation */
  69. {
  70. return 0 ;
  71. }
  72. /* make sure that n is a power of 2 */
  73. result = fft_binary_logn(n) ;
  74. if (result == -1)
  75. {
  76. GSL_ERROR ("n is not a power of 2", GSL_EINVAL);
  77. }
  78. else
  79. {
  80. logn = result ;
  81. }
  82. /* bit reverse the ordering of input data for decimation in time algorithm */
  83. status = FUNCTION(fft_complex,bitreverse_order) (data, stride, n, logn) ;
  84. /* apply fft recursion */
  85. dual = 1;
  86. for (bit = 0; bit < logn; bit++)
  87. {
  88. ATOMIC w_real = 1.0;
  89. ATOMIC w_imag = 0.0;
  90. const double theta = 2.0 * ((int) sign) * M_PI / (2.0 * (double) dual);
  91. const ATOMIC s = sin (theta);
  92. const ATOMIC t = sin (theta / 2.0);
  93. const ATOMIC s2 = 2.0 * t * t;
  94. size_t a, b;
  95. /* a = 0 */
  96. for (b = 0; b < n; b += 2 * dual)
  97. {
  98. const size_t i = b ;
  99. const size_t j = b + dual;
  100. const ATOMIC z1_real = REAL(data,stride,j) ;
  101. const ATOMIC z1_imag = IMAG(data,stride,j) ;
  102. const ATOMIC wd_real = z1_real ;
  103. const ATOMIC wd_imag = z1_imag ;
  104. REAL(data,stride,j) = REAL(data,stride,i) - wd_real;
  105. IMAG(data,stride,j) = IMAG(data,stride,i) - wd_imag;
  106. REAL(data,stride,i) += wd_real;
  107. IMAG(data,stride,i) += wd_imag;
  108. }
  109. /* a = 1 .. (dual-1) */
  110. for (a = 1; a < dual; a++)
  111. {
  112. /* trignometric recurrence for w-> exp(i theta) w */
  113. {
  114. const ATOMIC tmp_real = w_real - s * w_imag - s2 * w_real;
  115. const ATOMIC tmp_imag = w_imag + s * w_real - s2 * w_imag;
  116. w_real = tmp_real;
  117. w_imag = tmp_imag;
  118. }
  119. for (b = 0; b < n; b += 2 * dual)
  120. {
  121. const size_t i = b + a;
  122. const size_t j = b + a + dual;
  123. const ATOMIC z1_real = REAL(data,stride,j) ;
  124. const ATOMIC z1_imag = IMAG(data,stride,j) ;
  125. const ATOMIC wd_real = w_real * z1_real - w_imag * z1_imag;
  126. const ATOMIC wd_imag = w_real * z1_imag + w_imag * z1_real;
  127. REAL(data,stride,j) = REAL(data,stride,i) - wd_real;
  128. IMAG(data,stride,j) = IMAG(data,stride,i) - wd_imag;
  129. REAL(data,stride,i) += wd_real;
  130. IMAG(data,stride,i) += wd_imag;
  131. }
  132. }
  133. dual *= 2;
  134. }
  135. return 0;
  136. }
  137. int
  138. FUNCTION(gsl_fft_complex,radix2_dif_forward) (TYPE(gsl_complex_packed_array) data,
  139. const size_t stride,
  140. const size_t n)
  141. {
  142. gsl_fft_direction sign = gsl_fft_forward;
  143. int status = FUNCTION(gsl_fft_complex,radix2_dif_transform) (data, stride, n, sign);
  144. return status;
  145. }
  146. int
  147. FUNCTION(gsl_fft_complex,radix2_dif_backward) (TYPE(gsl_complex_packed_array) data,
  148. const size_t stride,
  149. const size_t n)
  150. {
  151. gsl_fft_direction sign = gsl_fft_backward;
  152. int status = FUNCTION(gsl_fft_complex,radix2_dif_transform) (data, stride, n, sign);
  153. return status;
  154. }
  155. int
  156. FUNCTION(gsl_fft_complex,radix2_dif_inverse) (TYPE(gsl_complex_packed_array) data,
  157. const size_t stride,
  158. const size_t n)
  159. {
  160. gsl_fft_direction sign = gsl_fft_backward;
  161. int status = FUNCTION(gsl_fft_complex,radix2_dif_transform) (data, stride, n, sign);
  162. if (status)
  163. {
  164. return status;
  165. }
  166. /* normalize inverse fft with 1/n */
  167. {
  168. const ATOMIC norm = 1.0 / n;
  169. size_t i;
  170. for (i = 0; i < n; i++)
  171. {
  172. REAL(data,stride,i) *= norm;
  173. IMAG(data,stride,i) *= norm;
  174. }
  175. }
  176. return status;
  177. }
  178. int
  179. FUNCTION(gsl_fft_complex,radix2_dif_transform) (TYPE(gsl_complex_packed_array) data,
  180. const size_t stride,
  181. const size_t n,
  182. const gsl_fft_direction sign)
  183. {
  184. int result ;
  185. size_t dual;
  186. size_t bit;
  187. size_t logn = 0;
  188. int status;
  189. if (n == 1) /* identity operation */
  190. {
  191. return 0 ;
  192. }
  193. /* make sure that n is a power of 2 */
  194. result = fft_binary_logn(n) ;
  195. if (result == -1)
  196. {
  197. GSL_ERROR ("n is not a power of 2", GSL_EINVAL);
  198. }
  199. else
  200. {
  201. logn = result ;
  202. }
  203. /* apply fft recursion */
  204. dual = n / 2;
  205. for (bit = 0; bit < logn; bit++)
  206. {
  207. ATOMIC w_real = 1.0;
  208. ATOMIC w_imag = 0.0;
  209. const double theta = 2.0 * ((int) sign) * M_PI / ((double) (2 * dual));
  210. const ATOMIC s = sin (theta);
  211. const ATOMIC t = sin (theta / 2.0);
  212. const ATOMIC s2 = 2.0 * t * t;
  213. size_t a, b;
  214. for (b = 0; b < dual; b++)
  215. {
  216. for (a = 0; a < n; a+= 2 * dual)
  217. {
  218. const size_t i = b + a;
  219. const size_t j = b + a + dual;
  220. const ATOMIC t1_real = REAL(data,stride,i) + REAL(data,stride,j);
  221. const ATOMIC t1_imag = IMAG(data,stride,i) + IMAG(data,stride,j);
  222. const ATOMIC t2_real = REAL(data,stride,i) - REAL(data,stride,j);
  223. const ATOMIC t2_imag = IMAG(data,stride,i) - IMAG(data,stride,j);
  224. REAL(data,stride,i) = t1_real;
  225. IMAG(data,stride,i) = t1_imag;
  226. REAL(data,stride,j) = w_real*t2_real - w_imag * t2_imag;
  227. IMAG(data,stride,j) = w_real*t2_imag + w_imag * t2_real;
  228. }
  229. /* trignometric recurrence for w-> exp(i theta) w */
  230. {
  231. const ATOMIC tmp_real = w_real - s * w_imag - s2 * w_real;
  232. const ATOMIC tmp_imag = w_imag + s * w_real - s2 * w_imag;
  233. w_real = tmp_real;
  234. w_imag = tmp_imag;
  235. }
  236. }
  237. dual /= 2;
  238. }
  239. /* bit reverse the ordering of output data for decimation in
  240. frequency algorithm */
  241. status = FUNCTION(fft_complex,bitreverse_order)(data, stride, n, logn) ;
  242. return 0;
  243. }