-
Notifications
You must be signed in to change notification settings - Fork 1
/
ls_fft.c
299 lines (275 loc) · 7.49 KB
/
ls_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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/*
Copyright (c) 2005-2011, Max-Planck-Society
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the Max-Planck-Society nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE MAX-PLANCK-SOCIETY BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* libfftpack is being developed at the Max-Planck-Institut fuer Astrophysik
* and financially supported by the Deutsches Zentrum fuer Luft- und Raumfahrt
* (DLR).
*/
/*
* \author Martin Reinecke
*/
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include "bluestein.h"
#include "fftpack.h"
#include "ls_fft.h"
complex_plan make_complex_plan (size_t length)
{
complex_plan plan = RALLOC(complex_plan_i,1);
size_t pfsum = prime_factor_sum(length);
double comp1 = (double)(length*pfsum);
double comp2 = 2*3*length*log(3.*length);
comp2*=3.; /* fudge factor that appears to give good overall performance */
plan->length=length;
plan->bluestein = (comp2<comp1);
if (plan->bluestein)
bluestein_i (length,&(plan->work),&(plan->worksize));
else
{
plan->worksize=4*length+15;
plan->work=RALLOC(double,4*length+15);
cffti(length, plan->work);
}
return plan;
}
complex_plan copy_complex_plan (complex_plan plan)
{
if (!plan) return NULL;
{
complex_plan newplan = RALLOC(complex_plan_i,1);
*newplan = *plan;
newplan->work=RALLOC(double,newplan->worksize);
memcpy(newplan->work,plan->work,sizeof(double)*newplan->worksize);
return newplan;
}
}
void kill_complex_plan (complex_plan plan)
{
DEALLOC(plan->work);
DEALLOC(plan);
}
void complex_plan_forward (complex_plan plan, double *data)
{
if (plan->bluestein)
bluestein (plan->length, data, plan->work, -1);
else
cfftf (plan->length, data, plan->work);
}
void complex_plan_backward (complex_plan plan, double *data)
{
if (plan->bluestein)
bluestein (plan->length, data, plan->work, 1);
else
cfftb (plan->length, data, plan->work);
}
real_plan make_real_plan (size_t length)
{
real_plan plan = RALLOC(real_plan_i,1);
size_t pfsum = prime_factor_sum(length);
double comp1 = .5*length*pfsum;
double comp2 = 2*3*length*log(3.*length);
comp2*=3; /* fudge factor that appears to give good overall performance */
plan->length=length;
plan->bluestein = (comp2<comp1);
if (plan->bluestein)
bluestein_i (length,&(plan->work),&(plan->worksize));
else
{
plan->worksize=2*length+15;
plan->work=RALLOC(double,2*length+15);
rffti(length, plan->work);
}
return plan;
}
real_plan copy_real_plan (real_plan plan)
{
if (!plan) return NULL;
{
real_plan newplan = RALLOC(real_plan_i,1);
*newplan = *plan;
newplan->work=RALLOC(double,newplan->worksize);
memcpy(newplan->work,plan->work,sizeof(double)*newplan->worksize);
return newplan;
}
}
void kill_real_plan (real_plan plan)
{
DEALLOC(plan->work);
DEALLOC(plan);
}
void real_plan_forward_fftpack (real_plan plan, double *data)
{
if (plan->bluestein)
{
size_t m;
size_t n=plan->length;
double *tmp = RALLOC(double,2*n);
for (m=0; m<n; ++m)
{
tmp[2*m] = data[m];
tmp[2*m+1] = 0.;
}
bluestein(n,tmp,plan->work,-1);
data[0] = tmp[0];
memcpy (data+1, tmp+2, (n-1)*sizeof(double));
DEALLOC(tmp);
}
else
rfftf (plan->length, data, plan->work);
}
void fftpack2halfcomplex (double *data, size_t n)
{
size_t m;
double *tmp = RALLOC(double,n);
tmp[0]=data[0];
for (m=1; m<(n+1)/2; ++m)
{
tmp[m]=data[2*m-1];
tmp[n-m]=data[2*m];
}
if (!(n&1))
tmp[n/2]=data[n-1];
memcpy (data,tmp,n*sizeof(double));
DEALLOC(tmp);
}
void halfcomplex2fftpack (double *data, size_t n)
{
size_t m;
double *tmp = RALLOC(double,n);
tmp[0]=data[0];
for (m=1; m<(n+1)/2; ++m)
{
tmp[2*m-1]=data[m];
tmp[2*m]=data[n-m];
}
if (!(n&1))
tmp[n-1]=data[n/2];
memcpy (data,tmp,n*sizeof(double));
DEALLOC(tmp);
}
void real_plan_forward_fftw (real_plan plan, double *data)
{
real_plan_forward_fftpack (plan, data);
fftpack2halfcomplex (data,plan->length);
}
void real_plan_backward_fftpack (real_plan plan, double *data)
{
if (plan->bluestein)
{
size_t m;
size_t n=plan->length;
double *tmp = RALLOC(double,2*n);
tmp[0]=data[0];
tmp[1]=0.;
memcpy (tmp+2,data+1, (n-1)*sizeof(double));
if ((n&1)==0) tmp[n+1]=0.;
for (m=2; m<n; m+=2)
{
tmp[2*n-m]=tmp[m];
tmp[2*n-m+1]=-tmp[m+1];
}
bluestein (n, tmp, plan->work, 1);
for (m=0; m<n; ++m)
data[m] = tmp[2*m];
DEALLOC(tmp);
}
else
rfftb (plan->length, data, plan->work);
}
void real_plan_backward_fftw (real_plan plan, double *data)
{
halfcomplex2fftpack (data,plan->length);
real_plan_backward_fftpack (plan, data);
}
void real_plan_forward_c (real_plan plan, double *data)
{
size_t m;
size_t n=plan->length;
if (plan->bluestein)
{
for (m=1; m<2*n; m+=2)
data[m]=0;
bluestein (plan->length, data, plan->work, -1);
data[1]=0;
for (m=2; m<n; m+=2)
{
double avg;
avg = 0.5*(data[2*n-m]+data[m]);
data[2*n-m] = data[m] = avg;
avg = 0.5*(data[2*n-m+1]-data[m+1]);
data[2*n-m+1] = avg;
data[m+1] = -avg;
}
if ((n&1)==0) data[n+1] = 0.;
}
else
{
/* using "m+m" instead of "2*m" to avoid a nasty bug in Intel's compiler */
for (m=0; m<n; ++m) data[m+1] = data[m+m];
rfftf (n, data+1, plan->work);
data[0] = data[1];
data[1] = 0;
for (m=2; m<n; m+=2)
{
data[2*n-m] = data[m];
data[2*n-m+1] = -data[m+1];
}
if ((n&1)==0) data[n+1] = 0.;
}
}
void real_plan_backward_c (real_plan plan, double *data)
{
size_t n=plan->length;
if (plan->bluestein)
{
size_t m;
data[1]=0;
for (m=2; m<n; m+=2)
{
double avg;
avg = 0.5*(data[2*n-m]+data[m]);
data[2*n-m] = data[m] = avg;
avg = 0.5*(data[2*n-m+1]-data[m+1]);
data[2*n-m+1] = avg;
data[m+1] = -avg;
}
if ((n&1)==0) data[n+1] = 0.;
bluestein (plan->length, data, plan->work, 1);
for (m=1; m<2*n; m+=2)
data[m]=0;
}
else
{
ptrdiff_t m;
data[1] = data[0];
rfftb (n, data+1, plan->work);
for (m=n-1; m>=0; --m)
{
data[2*m] = data[m+1];
data[2*m+1] = 0.;
}
}
}