gsl_fft__real_main.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /* fft/real_main.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. #include "gsl__config.h"
  20. #include <stdlib.h>
  21. #include <math.h>
  22. #include "gsl_errno.h"
  23. #include "gsl_complex.h"
  24. #include "gsl_fft_real.h"
  25. #include "gsl_fft__real_pass.h"
  26. int
  27. FUNCTION(gsl_fft_real,transform) (BASE data[], const size_t stride, const size_t n,
  28. const TYPE(gsl_fft_real_wavetable) * wavetable,
  29. TYPE(gsl_fft_real_workspace) * work)
  30. {
  31. const size_t nf = wavetable->nf;
  32. size_t i;
  33. size_t q, product = 1;
  34. size_t tskip;
  35. size_t product_1;
  36. BASE *const scratch = work->scratch;
  37. TYPE(gsl_complex) *twiddle1, *twiddle2, *twiddle3, *twiddle4;
  38. size_t state = 0;
  39. BASE *in = data;
  40. size_t istride = stride ;
  41. BASE *out = scratch;
  42. size_t ostride = 1 ;
  43. if (n == 0)
  44. {
  45. GSL_ERROR ("length n must be positive integer", GSL_EDOM);
  46. }
  47. if (n == 1)
  48. { /* FFT of one data point is the identity */
  49. return 0;
  50. }
  51. if (n != wavetable->n)
  52. {
  53. GSL_ERROR ("wavetable does not match length of data", GSL_EINVAL);
  54. }
  55. if (n != work->n)
  56. {
  57. GSL_ERROR ("workspace does not match length of data", GSL_EINVAL);
  58. }
  59. for (i = 0; i < nf; i++)
  60. {
  61. const size_t factor = wavetable->factor[i];
  62. product_1 = product;
  63. product *= factor;
  64. q = n / product;
  65. tskip = (product_1 + 1) / 2 - 1;
  66. if (state == 0)
  67. {
  68. in = data;
  69. istride = stride;
  70. out = scratch;
  71. ostride = 1;
  72. state = 1;
  73. }
  74. else
  75. {
  76. in = scratch;
  77. istride = 1;
  78. out = data;
  79. ostride = stride;
  80. state = 0;
  81. }
  82. if (factor == 2)
  83. {
  84. twiddle1 = wavetable->twiddle[i];
  85. FUNCTION(fft_real,pass_2) (in, istride, out, ostride, product, n, twiddle1);
  86. }
  87. else if (factor == 3)
  88. {
  89. twiddle1 = wavetable->twiddle[i];
  90. twiddle2 = twiddle1 + tskip;
  91. FUNCTION(fft_real,pass_3) (in, istride, out, ostride, product, n, twiddle1,
  92. twiddle2);
  93. }
  94. else if (factor == 4)
  95. {
  96. twiddle1 = wavetable->twiddle[i];
  97. twiddle2 = twiddle1 + tskip;
  98. twiddle3 = twiddle2 + tskip;
  99. FUNCTION(fft_real,pass_4) (in, istride, out, ostride, product, n, twiddle1,
  100. twiddle2, twiddle3);
  101. }
  102. else if (factor == 5)
  103. {
  104. twiddle1 = wavetable->twiddle[i];
  105. twiddle2 = twiddle1 + tskip;
  106. twiddle3 = twiddle2 + tskip;
  107. twiddle4 = twiddle3 + tskip;
  108. FUNCTION(fft_real,pass_5) (in, istride, out, ostride, product, n, twiddle1,
  109. twiddle2, twiddle3, twiddle4);
  110. }
  111. else
  112. {
  113. twiddle1 = wavetable->twiddle[i];
  114. FUNCTION(fft_real,pass_n) (in, istride, out, ostride, factor, product, n,
  115. twiddle1);
  116. }
  117. }
  118. if (state == 1) /* copy results back from scratch to data */
  119. {
  120. for (i = 0; i < n; i++)
  121. {
  122. data[stride*i] = scratch[i] ;
  123. }
  124. }
  125. return 0;
  126. }