NEURON
nrngsl_real_radix2.cpp
Go to the documentation of this file.
1 /* fft/real_radix2.cpp
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 /* Hines: for greater self-containment */
21 #include "nrngsl.h"
22 /* from gsl/fft/factorize.cpp */
23 static int
24 fft_binary_logn (const size_t n)
25 {
26  size_t ntest ;
27  size_t binary_logn = 0 ;
28  size_t k = 1;
29 
30  while (k < n)
31  {
32  k *= 2;
33  binary_logn++;
34  }
35 
36  ntest = (1 << binary_logn) ;
37 
38  if (n != ntest )
39  {
40  return -1 ; /* n is not a power of 2 */
41  }
42 
43  return binary_logn;
44 }
45 
46 /* from gsl/fft/bitreverse.cpp */
47 static int
48 FUNCTION(fft_real,bitreverse_order) (BASE data[],
49  const size_t stride,
50  const size_t n,
51  size_t logn)
52 {
53  /* This is the Goldrader bit-reversal algorithm */
54 
55  size_t i;
56  size_t j = 0;
57 
58  logn = 0 ; /* not needed for this algorithm */
59 
60  for (i = 0; i < n - 1; i++)
61  {
62  size_t k = n / 2 ;
63 
64  if (i < j)
65  {
66  const BASE tmp = VECTOR(data,stride,i);
68  VECTOR(data,stride,j) = tmp;
69  }
70 
71  while (k <= j)
72  {
73  j = j - k ;
74  k = k / 2 ;
75  }
76 
77  j += k ;
78  }
79 
80  return 0;
81 }
82 /*-----------------------------------------*/
83 
84 int
85 FUNCTION(gsl_fft_real,radix2_transform) (BASE data[], const size_t stride, const size_t n)
86 {
87  int result ;
88  size_t p, p_1, q;
89  size_t i;
90  size_t logn = 0;
91  int status;
92 
93  if (n == 1) /* identity operation */
94  {
95  return 0 ;
96  }
97 
98  /* make sure that n is a power of 2 */
99 
100  result = fft_binary_logn(n) ;
101 
102  if (result == -1)
103  {
104  GSL_ERROR ("n is not a power of 2", GSL_EINVAL);
105  }
106  else
107  {
108  logn = result ;
109  }
110 
111  /* bit reverse the ordering of input data for decimation in time algorithm */
112 
113  status = FUNCTION(fft_real,bitreverse_order)(data, stride, n, logn) ;
114 
115  /* apply fft recursion */
116 
117  p = 1; q = n ;
118 
119  for (i = 1; i <= logn; i++)
120  {
121  size_t a, b;
122 
123  p_1 = p ;
124  p = 2 * p ;
125  q = q / 2 ;
126 
127  /* a = 0 */
128 
129  for (b = 0; b < q; b++)
130  {
131  ATOMIC t0_real = VECTOR(data,stride,b*p) + VECTOR(data,stride,b*p + p_1) ;
132  ATOMIC t1_real = VECTOR(data,stride,b*p) - VECTOR(data,stride,b*p + p_1) ;
133 
134  VECTOR(data,stride,b*p) = t0_real ;
135  VECTOR(data,stride,b*p + p_1) = t1_real ;
136  }
137 
138  /* a = 1 ... p_{i-1}/2 - 1 */
139 
140  {
141  ATOMIC w_real = 1.0;
142  ATOMIC w_imag = 0.0;
143 
144  const double theta = - 2.0 * M_PI / p;
145 
146  const ATOMIC s = sin (theta);
147  const ATOMIC t = sin (theta / 2.0);
148  const ATOMIC s2 = 2.0 * t * t;
149 
150  for (a = 1; a < (p_1)/2; a++)
151  {
152  /* trignometric recurrence for w-> exp(i theta) w */
153 
154  {
155  const ATOMIC tmp_real = w_real - s * w_imag - s2 * w_real;
156  const ATOMIC tmp_imag = w_imag + s * w_real - s2 * w_imag;
157  w_real = tmp_real;
158  w_imag = tmp_imag;
159  }
160 
161  for (b = 0; b < q; b++)
162  {
163  ATOMIC z0_real = VECTOR(data,stride,b*p + a) ;
164  ATOMIC z0_imag = VECTOR(data,stride,b*p + p_1 - a) ;
165  ATOMIC z1_real = VECTOR(data,stride,b*p + p_1 + a) ;
166  ATOMIC z1_imag = VECTOR(data,stride,b*p + p - a) ;
167 
168  /* t0 = z0 + w * z1 */
169 
170  ATOMIC t0_real = z0_real + w_real * z1_real - w_imag * z1_imag;
171  ATOMIC t0_imag = z0_imag + w_real * z1_imag + w_imag * z1_real;
172 
173  /* t1 = z0 - w * z1 */
174 
175  ATOMIC t1_real = z0_real - w_real * z1_real + w_imag * z1_imag;
176  ATOMIC t1_imag = z0_imag - w_real * z1_imag - w_imag * z1_real;
177 
178  VECTOR(data,stride,b*p + a) = t0_real ;
179  VECTOR(data,stride,b*p + p - a) = t0_imag ;
180 
181  VECTOR(data,stride,b*p + p_1 - a) = t1_real ;
182  VECTOR(data,stride,b*p + p_1 + a) = -t1_imag ;
183  }
184  }
185  }
186 
187  if (p_1 > 1)
188  {
189  for (b = 0; b < q; b++)
190  {
191  /* a = p_{i-1}/2 */
192 
193  VECTOR(data,stride,b*p + p - p_1/2) *= -1 ;
194  }
195  }
196  }
197  return 0;
198 }
#define data
Definition: rbtqueue.cpp:49
#define ATOMIC
Definition: nrngsl.h:7
#define VECTOR(a, stride, i)
Definition: nrngsl.h:8
#define GSL_ERROR(a, b)
Definition: nrngsl.h:5
static int const size_t const size_t n
static philox4x32_key_t k
Definition: nrnran123.cpp:11
sin
Definition: extdef.h:3
size_t p
#define M_PI
Definition: ivocvect.cpp:57
int status
_CONST char * s
Definition: system.cpp:74
#define BASE
Definition: nrngsl.h:4
static int const size_t stride
size_t i
size_t j
static int FUNCTION(fft_real, bitreverse_order)(BASE data[]
static int const size_t const size_t size_t logn
static int fft_binary_logn(const size_t n)
size_t q
size_t p_1
double t
Definition: init.cpp:123