gsl_fft__real_init.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /* fft/real_init.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. TYPE(gsl_fft_real_wavetable) *
  20. FUNCTION(gsl_fft_real_wavetable,alloc) (size_t n)
  21. {
  22. int status;
  23. size_t i;
  24. size_t n_factors;
  25. size_t t, product, product_1, q;
  26. double d_theta;
  27. TYPE(gsl_fft_real_wavetable) * wavetable;
  28. if (n == 0)
  29. {
  30. GSL_ERROR_VAL ("length n must be positive integer", GSL_EDOM, 0);
  31. }
  32. wavetable = (TYPE(gsl_fft_real_wavetable) *)
  33. malloc(sizeof(TYPE(gsl_fft_real_wavetable)));
  34. if (wavetable == NULL)
  35. {
  36. GSL_ERROR_VAL ("failed to allocate struct", GSL_ENOMEM, 0);
  37. }
  38. if (n == 1)
  39. {
  40. wavetable->trig = 0;
  41. }
  42. else
  43. {
  44. wavetable->trig = (TYPE(gsl_complex) *)
  45. malloc ((n / 2) * sizeof (TYPE(gsl_complex)));
  46. if (wavetable->trig == NULL)
  47. {
  48. /* error in constructor, prevent memory leak */
  49. free(wavetable) ;
  50. GSL_ERROR_VAL ("failed to allocate trigonometric lookup table",
  51. GSL_ENOMEM, 0);
  52. }
  53. }
  54. wavetable->n = n;
  55. status = fft_real_factorize (n, &n_factors, wavetable->factor);
  56. if (status)
  57. {
  58. /* error in constructor, prevent memory leak */
  59. free(wavetable->trig);
  60. free(wavetable) ;
  61. GSL_ERROR_VAL ("factorization failed", GSL_EFACTOR, 0);
  62. }
  63. wavetable->nf = n_factors;
  64. d_theta = 2.0 * M_PI / ((double) n);
  65. t = 0;
  66. product = 1;
  67. for (i = 0; i < wavetable->nf; i++)
  68. {
  69. size_t j;
  70. const size_t factor = wavetable->factor[i];
  71. wavetable->twiddle[i] = wavetable->trig + t;
  72. product_1 = product; /* product_1 = p_(i-1) */
  73. product *= factor;
  74. q = n / product;
  75. for (j = 1; j < factor; j++)
  76. {
  77. size_t k;
  78. size_t m = 0;
  79. for (k = 1; k < (product_1 + 1) / 2; k++)
  80. {
  81. double theta;
  82. m = m + j * q;
  83. m = m % n;
  84. theta = d_theta * m; /* d_theta*j*k*q */
  85. GSL_REAL(wavetable->trig[t]) = cos (theta);
  86. GSL_IMAG(wavetable->trig[t]) = sin (theta);
  87. t++;
  88. }
  89. }
  90. }
  91. if (t > (n / 2))
  92. {
  93. /* error in constructor, prevent memory leak */
  94. free(wavetable->trig);
  95. free(wavetable) ;
  96. GSL_ERROR_VAL ("overflowed trigonometric lookup table",
  97. GSL_ESANITY, 0);
  98. }
  99. return wavetable;
  100. }
  101. TYPE(gsl_fft_real_workspace) *
  102. FUNCTION(gsl_fft_real_workspace,alloc) (size_t n)
  103. {
  104. TYPE(gsl_fft_real_workspace) * workspace;
  105. if (n == 0)
  106. {
  107. GSL_ERROR_VAL ("length n must be positive integer", GSL_EDOM, 0);
  108. }
  109. workspace = (TYPE(gsl_fft_real_workspace) *)
  110. malloc(sizeof(TYPE(gsl_fft_real_workspace)));
  111. if (workspace == NULL)
  112. {
  113. GSL_ERROR_VAL ("failed to allocate struct", GSL_ENOMEM, 0);
  114. }
  115. workspace->n = n;
  116. workspace->scratch = (BASE *) malloc (n * sizeof (BASE));
  117. if (workspace->scratch == NULL)
  118. {
  119. /* error in constructor, prevent memory leak */
  120. free(workspace) ;
  121. GSL_ERROR_VAL ("failed to allocate scratch space", GSL_ENOMEM, 0);
  122. }
  123. return workspace;
  124. }
  125. void
  126. FUNCTION(gsl_fft_real_wavetable,free) (TYPE(gsl_fft_real_wavetable) * wavetable)
  127. {
  128. /* release trigonometric lookup tables */
  129. free (wavetable->trig);
  130. wavetable->trig = NULL;
  131. free (wavetable) ;
  132. }
  133. void
  134. FUNCTION(gsl_fft_real_workspace,free) (TYPE(gsl_fft_real_workspace) * workspace)
  135. {
  136. /* release scratch space */
  137. free (workspace->scratch);
  138. workspace->scratch = NULL;
  139. free (workspace) ;
  140. }