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