gsl_fft__hc_init.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /* fft/hc_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_halfcomplex_wavetable) *
  20. FUNCTION(gsl_fft_halfcomplex_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_halfcomplex_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_halfcomplex_wavetable) *)
  33. malloc(sizeof(TYPE(gsl_fft_halfcomplex_wavetable)));
  34. if (wavetable == NULL)
  35. {
  36. GSL_ERROR_VAL ("failed to allocate struct", GSL_ENOMEM, 0);
  37. }
  38. wavetable->trig = (TYPE(gsl_complex) *) malloc (n * sizeof (TYPE(gsl_complex)));
  39. if (wavetable->trig == NULL)
  40. {
  41. /* error in constructor, prevent memory leak */
  42. free(wavetable) ;
  43. GSL_ERROR_VAL ("failed to allocate trigonometric lookup table",
  44. GSL_ENOMEM, 0);
  45. }
  46. wavetable->n = n ;
  47. status = fft_halfcomplex_factorize (n, &n_factors, wavetable->factor);
  48. if (status)
  49. {
  50. /* error in constructor, prevent memory leak */
  51. free(wavetable->trig) ;
  52. free(wavetable) ;
  53. GSL_ERROR_VAL ("factorization failed", GSL_EFACTOR, 0);
  54. }
  55. wavetable->nf = n_factors;
  56. d_theta = 2.0 * M_PI / ((double) n);
  57. t = 0;
  58. product = 1;
  59. for (i = 0; i < n_factors; i++)
  60. {
  61. size_t j;
  62. const size_t factor = wavetable->factor[i];
  63. wavetable->twiddle[i] = wavetable->trig + t;
  64. product_1 = product; /* product_1 = p_(i-1) */
  65. product *= factor;
  66. q = n / product;
  67. for (j = 1; j < factor; j++)
  68. {
  69. size_t k;
  70. size_t m = 0;
  71. for (k = 1; k < (q + 1) / 2; k++)
  72. {
  73. double theta;
  74. m = m + j * product_1;
  75. m = m % n;
  76. theta = d_theta * m; /* d_theta*j*k*product_1 */
  77. GSL_REAL(wavetable->trig[t]) = cos (theta);
  78. GSL_IMAG(wavetable->trig[t]) = sin (theta);
  79. t++;
  80. }
  81. }
  82. }
  83. if (t > (n / 2))
  84. {
  85. /* error in constructor, prevent memory leak */
  86. free(wavetable->trig) ;
  87. free(wavetable) ;
  88. GSL_ERROR_VAL ("overflowed trigonometric lookup table", GSL_ESANITY, 0);
  89. }
  90. return wavetable;
  91. }
  92. void
  93. FUNCTION(gsl_fft_halfcomplex_wavetable,free) (TYPE(gsl_fft_halfcomplex_wavetable) * wavetable)
  94. {
  95. /* release trigonometric lookup tables */
  96. free (wavetable->trig);
  97. wavetable->trig = NULL;
  98. free (wavetable);
  99. }