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_complex_wavetable) *
21 FUNCTION(gsl_fft_complex_wavetable,alloc) (size_t n)
26 size_t t, product, product_1, q;
29 TYPE(gsl_fft_complex_wavetable) * wavetable ;
33 GSL_ERROR_VAL ("length n must be positive integer", GSL_EDOM, 0);
36 wavetable = (TYPE(gsl_fft_complex_wavetable) *)
37 malloc(sizeof(TYPE(gsl_fft_complex_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 free(wavetable) ; /* error in constructor, prevent memory leak */
50 GSL_ERROR_VAL ("failed to allocate trigonometric lookup table",
56 status = fft_complex_factorize (n, &n_factors, wavetable->factor);
60 /* exception in constructor, avoid memory leak */
62 free (wavetable->trig);
65 GSL_ERROR_VAL ("factorization failed", GSL_EFACTOR, 0);
68 wavetable->nf = n_factors;
70 d_theta = -2.0 * M_PI / ((double) n);
74 for (i = 0; i < n_factors; i++)
77 const size_t factor = wavetable->factor[i];
78 wavetable->twiddle[i] = wavetable->trig + t;
79 product_1 = product; /* product_1 = p_(i-1) */
83 for (j = 1; j < factor; j++)
87 for (k = 1; k <= q; k++)
90 m = m + j * product_1;
92 theta = d_theta * m; /* d_theta*j*k*p_(i-1) */
93 GSL_REAL(wavetable->trig[t]) = cos (theta);
94 GSL_IMAG(wavetable->trig[t]) = sin (theta);
103 /* exception in constructor, avoid memory leak */
105 free (wavetable->trig);
108 GSL_ERROR_VAL ("overflowed trigonometric lookup table",
116 TYPE(gsl_fft_complex_workspace) *
117 FUNCTION(gsl_fft_complex_workspace,alloc) (size_t n)
119 TYPE(gsl_fft_complex_workspace) * workspace ;
123 GSL_ERROR_VAL ("length n must be positive integer", GSL_EDOM, 0);
126 workspace = (TYPE(gsl_fft_complex_workspace) *)
127 malloc(sizeof(TYPE(gsl_fft_complex_workspace)));
129 if (workspace == NULL)
131 GSL_ERROR_VAL ("failed to allocate struct", GSL_ENOMEM, 0);
136 workspace->scratch = (BASE *) malloc (2 * n * sizeof (BASE));
138 if (workspace->scratch == NULL)
140 free(workspace) ; /* error in constructor, prevent memory leak */
142 GSL_ERROR_VAL ("failed to allocate scratch space", GSL_ENOMEM, 0);
150 FUNCTION(gsl_fft_complex_wavetable,free) (TYPE(gsl_fft_complex_wavetable) * wavetable)
153 /* release trigonometric lookup tables */
155 free (wavetable->trig);
156 wavetable->trig = NULL;
162 FUNCTION(gsl_fft_complex_workspace,free) (TYPE(gsl_fft_complex_workspace) * workspace)
164 /* release scratch space */
166 free (workspace->scratch);
167 workspace->scratch = NULL;
173 FUNCTION(gsl_fft_complex,memcpy) (TYPE(gsl_fft_complex_wavetable) * dest,
174 TYPE(gsl_fft_complex_wavetable) * src)
178 if (dest->n != src->n)
180 GSL_ERROR ("length of src and dest do not match", GSL_EINVAL);
186 memcpy(dest->trig, src->trig, n * sizeof (double)) ;
188 for (i = 0 ; i < nf ; i++)
190 dest->twiddle[i] = dest->trig + (src->twiddle[i] - src->trig) ;