3 * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2007 Brian Gough
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.
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.
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.
20 TYPE(gsl_fft_halfcomplex_wavetable) *
21 FUNCTION(gsl_fft_halfcomplex_wavetable,alloc) (size_t n)
26 size_t t, product, product_1, q;
29 TYPE(gsl_fft_halfcomplex_wavetable) * wavetable ;
33 GSL_ERROR_VAL ("length n must be positive integer", GSL_EDOM, 0);
36 wavetable = (TYPE(gsl_fft_halfcomplex_wavetable) *)
37 malloc(sizeof(TYPE(gsl_fft_halfcomplex_wavetable)));
39 if (wavetable == NULL)
41 GSL_ERROR_VAL ("failed to allocate struct", GSL_ENOMEM, 0);
44 wavetable->trig = (TYPE(gsl_complex) *) malloc (n * sizeof (TYPE(gsl_complex)));
46 if (wavetable->trig == NULL)
48 /* error in constructor, prevent memory leak */
52 GSL_ERROR_VAL ("failed to allocate trigonometric lookup table",
58 status = fft_halfcomplex_factorize (n, &n_factors, wavetable->factor);
62 /* error in constructor, prevent memory leak */
64 free(wavetable->trig) ;
67 GSL_ERROR_VAL ("factorization failed", GSL_EFACTOR, 0);
70 wavetable->nf = n_factors;
72 d_theta = 2.0 * M_PI / ((double) n);
76 for (i = 0; i < n_factors; i++)
79 const size_t factor = wavetable->factor[i];
80 wavetable->twiddle[i] = wavetable->trig + t;
81 product_1 = product; /* product_1 = p_(i-1) */
85 for (j = 1; j < factor; j++)
89 for (k = 1; k < (q + 1) / 2; k++)
92 m = m + j * product_1;
94 theta = d_theta * m; /* d_theta*j*k*product_1 */
95 GSL_REAL(wavetable->trig[t]) = cos (theta);
96 GSL_IMAG(wavetable->trig[t]) = sin (theta);
105 /* error in constructor, prevent memory leak */
107 free(wavetable->trig) ;
110 GSL_ERROR_VAL ("overflowed trigonometric lookup table", GSL_ESANITY, 0);
118 FUNCTION(gsl_fft_halfcomplex_wavetable,free) (TYPE(gsl_fft_halfcomplex_wavetable) * wavetable)
121 /* release trigonometric lookup tables */
123 free (wavetable->trig);
124 wavetable->trig = NULL;