-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfft.c
263 lines (221 loc) · 6.04 KB
/
fft.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/* fft.c
*
* SWIFFT - Swift Wavelet-based Inexact FFT
* Copyright (C) 2011 Felipe H. da Jornada <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <complex.h>
#include "fft.h"
void rec_fft2(double complex *in, double complex *out, int sz, long long int s);
void rec_fft3(double complex *in, double complex *out, int sz);
double complex *scratch;
double complex *w;
int fft_step;
void prepare_fft(int sz){
int i;
double alpha;
scratch = (double complex*) malloc(sizeof(double complex)*sz);
w = (double complex*) malloc(sizeof(double complex)*sz);
alpha = 2.0*M_PI/(double)sz;
for (i=0; i<sz; i++){
w[i] = cos(alpha*i) - I*sin(alpha*i);
}
fft_step=1;
}
void free_fft(){
free(scratch);
free(w);
}
//! This is the naive FFT that copies the input vector each time it has to
//! apply the permutation matrix
void fft(double complex *in, double complex *out, int sz){
int i,j, sz2;
double complex aa, bb;
//printf("Caling fft, sz=%lld, fftw_step=%lld\n", sz, fft_step);
sz2 = sz>>1;
//reorder
memcpy(scratch, in, sz*sizeof(double complex));
for (i=0; i<sz2; i++) {
in[i] = scratch[2*i];
in[i+sz2] = scratch[2*i+1];
}
if (sz>2){
fft_step = fft_step<<1;
fft(in, out, sz2);
fft(in+sz2, out+sz2, sz2);
fft_step = fft_step>>1;
} else {
out[0] = in[0];
out[1] = in[1];
}
//precalculate 0-element, since w[0]=1
aa = out[0];
bb = out[sz2];
out[0] = aa + bb;
out[sz2] = aa - bb;
j=0;
for (i=1; i<sz2; i++){
j += fft_step;
aa = out[i];
bb = w[j]*out[i+sz2];
//bb = (M_PI+M_PI*I)*out[i+sz2];
out[i] = aa + bb;
out[i+sz2] = aa - bb;
}
}
//! This is the version of the FFT that doesn't copy the input vector,
//! but deals with non-continuous memory
void fft2(double complex *in, double complex *out, int sz){
//int i,j, sz2;
rec_fft2(in, out, sz, 1);
}
void rec_fft2(double complex *in, double complex *out, int sz, long long int s){
int i,j, sz2;
double complex aa, bb;
//printf("Caling fft, sz=%lld, fftw_step=%lld\n", sz, fft_step);
sz2 = sz>>1;
if (sz>2){
fft_step = fft_step<<1;
rec_fft2(in, out, sz2, 2*s);
rec_fft2(in+s, out+sz2, sz2, 2*s);
fft_step = fft_step>>1;
} else {
out[0] = in[0];
out[1] = in[1];
}
//precalculate 0-element, since w[0]=1
aa = out[0];
bb = out[sz2];
out[0] = aa + bb;
out[sz2] = aa - bb;
j=0;
for (i=1; i<sz2; i++){
j += fft_step;
aa = out[i];
bb = w[j]*out[i+sz2];
//bb = (M_PI+M_PI*I)*out[i+sz2];
out[i] = aa + bb;
out[i+sz2] = aa - bb;
}
}
#define SWAP(a,b) tmp=b; b=a; a=tmp;
//source: http://ieeexplore.ieee.org/stamp/stamp.jsp?tp=&arnumber=115695
void frbr(double complex *x, int m) {
int br[256];
int m2,c,odd,offset,b_size,i,j,k;
double complex tmp;
m2=m>>1; c=1<<m2;
odd=0; if(m!=m2<<1) odd=1;
offset=1<<(m-1); b_size=2;
br[0]=0; br[1]=offset;
SWAP(x[1], x[offset]);
if(odd) SWAP(x[1+c], x[offset+c]);
while(b_size<c){
offset>>=1;
for(i=b_size; i<b_size<<1; i++){
br[i]=k=br[i-b_size]+offset;
SWAP(x[i], x[k]);
if(odd) SWAP(x[i+c], x[k+c]);
for(j=1; j<i; j++){
SWAP(x[i+br[j]+c], x[k+j+c]);
}
}
b_size<<=1;
}
return;
}
//http://www.katjaas.nl/bitreversal/bitreversal.html
void bitrev(double complex *real, unsigned int logN)
{
unsigned int forward, rev, toggle;
unsigned int nodd, noddrev; // to hold bitwise negated or odd values
unsigned int N, halfn, quartn, nmin1;
double complex temp;
N = 1<<logN;
halfn = N>>1; // frequently used 'constants'
quartn = N>>2;
nmin1 = N-1;
forward = halfn; // variable initialisations
rev = 1;
while(forward) // start of bitreversed permutation loop, N/4 iterations
{
// adaptation of the traditional bitreverse update method
forward -= 2;
toggle = quartn; // reset the toggle in every iteration
rev ^= toggle; // toggle one bit in reversed unconditionally
while(rev&toggle) // check if more bits in reversed must be toggled
{
toggle >>= 1;
rev ^= toggle;
}
if(forward<rev) // swap even and ~even conditionally
{
temp = real[forward];
real[forward] = real[rev];
real[rev] = temp;
nodd = nmin1 ^ forward; // compute the bitwise negations
noddrev = nmin1 ^ rev;
temp = real[nodd]; // swap bitwise-negated pairs
real[nodd] = real[noddrev];
real[noddrev] = temp;
}
nodd = forward ^ 1; // compute the odd values from the even
noddrev = rev ^ halfn;
temp = real[nodd]; // swap odd unconditionally
real[nodd] = real[noddrev];
real[noddrev] = temp;
}
// end of the bitreverse permutation loop
}
// end of bitrev function
//! This is the version with bit reversion
void fft3(double complex *in, double complex *out, int sz){
bitrev(in, round(log2(sz)));
rec_fft3(in, out, sz);
}
void rec_fft3(double complex *in, double complex *out, int sz){
int i,j, sz2;
double complex aa, bb;
//printf("Caling fft, sz=%lld, fftw_step=%lld\n", sz, fft_step);
sz2 = sz>>1;
if (sz>2) {
fft_step = fft_step<<1;
rec_fft3(in, out, sz2);
rec_fft3(in+sz2, out+sz2, sz2);
fft_step = fft_step>>1;
} else {
out[0] = in[0];
out[1] = in[1];
}
//precalculate 0-element, since w[0]=1
aa = out[0];
bb = out[sz2];
out[0] = aa + bb;
out[sz2] = aa - bb;
j=0;
for (i=1; i<sz2; i++){
j += fft_step;
aa = out[i];
bb = w[j]*out[i+sz2];
//bb = (M_PI+M_PI*I)*out[i+sz2];
out[i] = aa + bb;
out[i+sz2] = aa - bb;
}
}