| 1 | /* fft/real_radix2.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 | int |
| 21 | FUNCTION(gsl_fft_real,radix2_transform) (BASE data[], const size_t stride, const size_t n) |
| 22 | { |
| 23 | int result ; |
| 24 | size_t p, p_1, q; |
| 25 | size_t i; |
| 26 | size_t logn = 0; |
| 27 | |
| 28 | if (n == 1) /* identity operation */ |
| 29 | { |
| 30 | return 0 ; |
| 31 | } |
| 32 | |
| 33 | /* make sure that n is a power of 2 */ |
| 34 | |
| 35 | result = fft_binary_logn(n) ; |
| 36 | |
| 37 | if (result == -1) |
| 38 | { |
| 39 | GSL_ERROR ("n is not a power of 2", GSL_EINVAL); |
| 40 | } |
| 41 | else |
| 42 | { |
| 43 | logn = result ; |
| 44 | } |
| 45 | |
| 46 | /* bit reverse the ordering of input data for decimation in time algorithm */ |
| 47 | |
| 48 | (void) FUNCTION(fft_real,bitreverse_order)(data, stride, n, logn) ; |
| 49 | |
| 50 | /* apply fft recursion */ |
| 51 | |
| 52 | p = 1; q = n ; |
| 53 | |
| 54 | for (i = 1; i <= logn; i++) |
| 55 | { |
| 56 | size_t a, b; |
| 57 | |
| 58 | p_1 = p ; |
| 59 | p = 2 * p ; |
| 60 | q = q / 2 ; |
| 61 | |
| 62 | /* a = 0 */ |
| 63 | |
| 64 | for (b = 0; b < q; b++) |
| 65 | { |
| 66 | ATOMIC t0_real = VECTOR(data,stride,b*p) + VECTOR(data,stride,b*p + p_1) ; |
| 67 | ATOMIC t1_real = VECTOR(data,stride,b*p) - VECTOR(data,stride,b*p + p_1) ; |
| 68 | |
| 69 | VECTOR(data,stride,b*p) = t0_real ; |
| 70 | VECTOR(data,stride,b*p + p_1) = t1_real ; |
| 71 | } |
| 72 | |
| 73 | /* a = 1 ... p_{i-1}/2 - 1 */ |
| 74 | |
| 75 | { |
| 76 | ATOMIC w_real = 1.0; |
| 77 | ATOMIC w_imag = 0.0; |
| 78 | |
| 79 | const double theta = - 2.0 * M_PI / p; |
| 80 | |
| 81 | const ATOMIC s = sin (theta); |
| 82 | const ATOMIC t = sin (theta / 2.0); |
| 83 | const ATOMIC s2 = 2.0 * t * t; |
| 84 | |
| 85 | for (a = 1; a < (p_1)/2; a++) |
| 86 | { |
| 87 | /* trignometric recurrence for w-> exp(i theta) w */ |
| 88 | |
| 89 | { |
| 90 | const ATOMIC tmp_real = w_real - s * w_imag - s2 * w_real; |
| 91 | const ATOMIC tmp_imag = w_imag + s * w_real - s2 * w_imag; |
| 92 | w_real = tmp_real; |
| 93 | w_imag = tmp_imag; |
| 94 | } |
| 95 | |
| 96 | for (b = 0; b < q; b++) |
| 97 | { |
| 98 | ATOMIC z0_real = VECTOR(data,stride,b*p + a) ; |
| 99 | ATOMIC z0_imag = VECTOR(data,stride,b*p + p_1 - a) ; |
| 100 | ATOMIC z1_real = VECTOR(data,stride,b*p + p_1 + a) ; |
| 101 | ATOMIC z1_imag = VECTOR(data,stride,b*p + p - a) ; |
| 102 | |
| 103 | /* t0 = z0 + w * z1 */ |
| 104 | |
| 105 | ATOMIC t0_real = z0_real + w_real * z1_real - w_imag * z1_imag; |
| 106 | ATOMIC t0_imag = z0_imag + w_real * z1_imag + w_imag * z1_real; |
| 107 | |
| 108 | /* t1 = z0 - w * z1 */ |
| 109 | |
| 110 | ATOMIC t1_real = z0_real - w_real * z1_real + w_imag * z1_imag; |
| 111 | ATOMIC t1_imag = z0_imag - w_real * z1_imag - w_imag * z1_real; |
| 112 | |
| 113 | VECTOR(data,stride,b*p + a) = t0_real ; |
| 114 | VECTOR(data,stride,b*p + p - a) = t0_imag ; |
| 115 | |
| 116 | VECTOR(data,stride,b*p + p_1 - a) = t1_real ; |
| 117 | VECTOR(data,stride,b*p + p_1 + a) = -t1_imag ; |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | if (p_1 > 1) |
| 123 | { |
| 124 | for (b = 0; b < q; b++) |
| 125 | { |
| 126 | /* a = p_{i-1}/2 */ |
| 127 | |
| 128 | VECTOR(data,stride,b*p + p - p_1/2) *= -1 ; |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | return 0; |
| 133 | } |