-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfft.cpp
322 lines (280 loc) · 13.1 KB
/
fft.cpp
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#include<iostream>
#include<vector>
#include<tuple>
#include<complex>
#include<numeric>
#include<cmath>
#include<memory>
#include<bitset>
#include<chrono>
#include<cstdlib>
#include"matplotlibcpp.h"
#include<fftw3.h>
#include<omp.h>
const double EPSILON = std::pow(2, -52);
const double PI = std::acos(-1.0);
const double TWO_PI = 2.0 * PI;
typedef double mcomplex[2];
const unsigned BIT_REVERSE_TALBE8 [] = {
0, 128, 64, 192, 32, 160, 96, 224, 16, 144, 80, 208, 48, 176, 112, 240,
8, 136, 72, 200, 40, 168, 104, 232, 24, 152, 88, 216, 56, 184, 120, 248,
4, 132, 68, 196, 36, 164, 100, 228, 20, 148, 84, 212, 52, 180, 116, 244,
12, 140, 76, 204, 44, 172, 108, 236, 28, 156, 92, 220, 60, 188, 124, 252,
2, 130, 66, 194, 34, 162, 98, 226, 18, 146, 82, 210, 50, 178, 114, 242,
10, 138, 74, 202, 42, 170, 106, 234, 26, 154, 90, 218, 58, 186, 122, 250,
6, 134, 70, 198, 38, 166, 102, 230, 22, 150, 86, 214, 54, 182, 118, 246,
14, 142, 78, 206, 46, 174, 110, 238, 30, 158, 94, 222, 62, 190, 126, 254,
1, 129, 65, 193, 33, 161, 97, 225, 17, 145, 81, 209, 49, 177, 113, 241,
9, 137, 73, 201, 41, 169, 105, 233, 25, 153, 89, 217, 57, 185, 121, 249,
5, 133, 69, 197, 37, 165, 101, 229, 21, 149, 85, 213, 53, 181, 117, 245,
13, 141, 77, 205, 45, 173, 109, 237, 29, 157, 93, 221, 61, 189, 125, 253,
3, 131, 67, 195, 35, 163, 99, 227, 19, 147, 83, 211, 51, 179, 115, 243,
11, 139, 75, 203, 43, 171, 107, 235, 27, 155, 91, 219, 59, 187, 123, 251,
7, 135, 71, 199, 39, 167, 103, 231, 23, 151, 87, 215, 55, 183, 119, 247,
15, 143, 79, 207, 47, 175, 111, 239, 31, 159, 95, 223, 63, 191, 127, 255
};
unsigned bitReversePerByte(const unsigned& orig, const unsigned& numOfBytes) {
unsigned result = 0;
for (unsigned i = 0; i < numOfBytes; ++i) {
result = result | (BIT_REVERSE_TALBE8[((orig << (i * 8)) >> (8 * (numOfBytes - 1)))] << (i * 8));
}
return result;
}
//compatible with unsigned now
unsigned bitReverseInt(const unsigned& orig, const unsigned& numOfBits) {
if (numOfBits > 8 * sizeof(unsigned)) {
std::cerr << "The size of bits to be reversed should be smaller than unsigned's" << std::endl;
std::exit(-1);
}
return bitReversePerByte(orig, sizeof(unsigned)) >> (8 * sizeof(unsigned) - numOfBits);
}
/* The size should be smaller than 2^32.Otherwise, the code needs modifying.
* The size should be power of 2.
* Optimization:
* 1. reduce number of multiplications.
* 2. reduce presicion.
* 3. preprocess the data to make it friendly to multiplicaitons.
* None of above optimizations is done;
*/
std::vector<std::complex<double>> cfft(const std::vector<std::complex<double>>& samples, const bool& isInverse) {
auto begin = std::chrono::steady_clock::now();
//omp_set_num_threads(omp_get_max_threads());
const unsigned sampleSize = unsigned(samples.size());
auto samplesBuffer1 = std::vector<std::complex<double>>(sampleSize);
auto samplesBuffer2 = std::vector<std::complex<double>>(sampleSize);
const unsigned numOfBits = unsigned(std::log2(sampleSize));
const double inverseFactor = isInverse ? -1.0 : 1.0;
auto phaseFcts = std::vector<std::complex<double>>(sampleSize);
auto sampleSizeReciprocal = 1.0 / double(sampleSize);
//#pragma omp parallel for schedule(static)
for (unsigned i = 0; i < sampleSize; ++i) {
samplesBuffer1[i] = samples[bitReverseInt(i, numOfBits)];
phaseFcts[i] = std::exp(inverseFactor * std::complex<double>(0, 1) * TWO_PI * double(i) * sampleSizeReciprocal);
}
auto currentBuffer = &samplesBuffer1;
auto nextBuffer = &samplesBuffer2;
auto end = std::chrono::steady_clock::now();
std::chrono::duration<double> timeCost = end - begin;
std::cout << "init_mine : " << timeCost.count() << "s." << std::endl;
for (unsigned i = 0; i < numOfBits; ++i) {
const unsigned factionSize = 1 << (i + 1);
const unsigned numOfFactions = sampleSize >> (i + 1);
//#pragma omp parallel for schedule(static)
for (unsigned j = 0; j < numOfFactions; ++j) {
const unsigned halfFactionSize = factionSize >> 1;
for (unsigned k = 0; k < halfFactionSize; ++k) {
auto temp = phaseFcts[j * factionSize + k] * (*currentBuffer)[j * factionSize + k + halfFactionSize];
(*nextBuffer)[j * factionSize + k] = (*currentBuffer)[j * factionSize + k] + temp;
(*nextBuffer)[j * factionSize + k + halfFactionSize] = (*currentBuffer)[j * factionSize + k] - temp;
}
}
std::swap(currentBuffer, nextBuffer);
}
if (isInverse) {
//"parallel for" is not compatible with "for (auto x : xs)".
const double sampleSizeReciprocal = 1.0 / double(sampleSize);
//#pragma omp parallel for schedule(static)
for(unsigned i = 0; i < sampleSize; ++i) {
(*nextBuffer)[i] *= sampleSizeReciprocal;
}
}
return *nextBuffer;
}
//changing the complex multiplication to 3*5+ does not make it faster. Neither does reusing oddFactor memory.
mcomplex* cfftm(mcomplex* samples, const unsigned& order, const bool& isInverse) {
auto begin = std::chrono::steady_clock::now();
//omp_set_num_threads(omp_get_max_threads());
mcomplex* sampleBuffer1 = (mcomplex*)malloc(sizeof(mcomplex) * order);
mcomplex* sampleBuffer2 = (mcomplex*)malloc(sizeof(mcomplex) * order);
const unsigned numOfBits = unsigned(std::log2(order));
const double inverseFactor = isInverse ? -1.0 : 1.0;
mcomplex* phaseFcts = (mcomplex*)malloc(sizeof(mcomplex) * order);
const double yayFactor = TWO_PI / double(order);
//#pragma omp parallel for schedule(static)
for (unsigned i = 0; i < order; ++i) {
sampleBuffer1[i][0] = samples[bitReverseInt(i, numOfBits)][0];
sampleBuffer1[i][1] = samples[bitReverseInt(i, numOfBits)][1];
phaseFcts[i][0] = std::cos(double(i) * yayFactor);
phaseFcts[i][1] = inverseFactor * std::sin(double(i) * yayFactor);
}
auto currentBuffer = sampleBuffer1;
auto nextBuffer = sampleBuffer2;
auto end = std::chrono::steady_clock::now();
std::chrono::duration<double> timeCost = end - begin;
std::cout << "init_minc : " << timeCost.count() << "s." << std::endl;
for (unsigned i = 0; i < numOfBits; ++i) {
const unsigned factionSize = 1 << (i + 1);
const unsigned numOfFactions = order >> (i + 1);
//#pragma omp parallel for schedule(static)
for (unsigned j = 0; j < numOfFactions; ++j) {
const unsigned halfFactionSize = factionSize >> 1;
for (unsigned k = 0; k < halfFactionSize; ++k) {
const double tempr = phaseFcts[j * factionSize + k][0] * currentBuffer[j * factionSize + k + halfFactionSize][0] - phaseFcts[j * factionSize + k][1] * currentBuffer[j * factionSize + k + halfFactionSize][1];
const double tempi = phaseFcts[j * factionSize + k][0] * currentBuffer[j * factionSize + k + halfFactionSize][1] + phaseFcts[j * factionSize + k][1] * currentBuffer[j * factionSize + k + halfFactionSize][0];
nextBuffer[j * factionSize + k][0] = currentBuffer[j * factionSize + k][0] + tempr;
nextBuffer[j * factionSize + k][1] = currentBuffer[j * factionSize + k][1] + tempi;
nextBuffer[j * factionSize + k + halfFactionSize][0] = currentBuffer[j * factionSize + k][0] - tempr;
nextBuffer[j * factionSize + k + halfFactionSize][1] = currentBuffer[j * factionSize + k][1] - tempi;
}
}
std::swap(currentBuffer, nextBuffer);
}
free(phaseFcts);
if (isInverse) {
//"parallel for" is not compatible with "for (auto x : xs)".
const double sampleSizeReciprocal = 1.0 / double(order);
//#pragma omp parallel for schedule(static)
for(unsigned i = 0; i < order; ++i) {
nextBuffer[i][0] *= sampleSizeReciprocal;
nextBuffer[i][1] *= sampleSizeReciprocal;
}
}
free(currentBuffer);
return nextBuffer;
}
std::complex<double> preBesselF(const std::complex<double>& z, const unsigned& numOfPoints, const unsigned& inputIndex) {
using namespace std::complex_literals;
return std::exp(1i * z * std::cos(2 * PI * double(inputIndex) / double(numOfPoints)));
}
std::vector<std::complex<double>> besselM(const std::complex<double>& z, const unsigned& order) {
if ((order & (order - 1)) != 0) {
std::cerr << "order must be power of 2" << std::endl;
std::exit(-1);
}
mcomplex* xs = (mcomplex*)malloc(sizeof(mcomplex) * order);
for (unsigned i = 0; i < order; ++i) {
const auto x = preBesselF(z, order, i);
xs[i][0] = x.real();
xs[i][1] = x.imag();
}
auto begin = std::chrono::steady_clock::now();
auto Xs = cfftm(xs, order, true);
auto end = std::chrono::steady_clock::now();
std::chrono::duration<double> timeCost = end - begin;
std::cout << " minc : " << timeCost.count() << "s." << std::endl;
auto Xsv = std::vector<std::complex<double>>(order);
using namespace std::complex_literals;
for (unsigned l = 0; l < order; ++l) {
switch (l % 4) {
case 0: Xsv[l] = Xs[l][0] * 2 + Xs[l][1] * 2i; break;
case 1: Xsv[l] = Xs[l][1] * 2 - Xs[l][0] * 2i; break;
case 2: Xsv[l] = Xs[l][0] * (-2) + Xs[l][1] * (-2i); break;
case 3: Xsv[l] = Xs[l][1] * (-2) + Xs[l][0] * 2i; break;
}
}
free(xs);
free(Xs);
return Xsv;
}
std::vector<std::complex<double>> bessel(const std::complex<double>& z, const unsigned& order) {
if ((order & (order - 1)) != 0) {
std::cerr << "order must be power of 2" << std::endl;
std::exit(-1);
}
auto xs = std::vector<std::complex<double>>(order);
for (unsigned i = 0; i < xs.capacity(); ++i) {
xs[i] = preBesselF(z, order, i);
}
auto begin = std::chrono::steady_clock::now();
auto Xs = cfft(xs, true);
auto end = std::chrono::steady_clock::now();
std::chrono::duration<double> timeCost = end - begin;
std::cout << " mine : " << timeCost.count() << "s." << std::endl;
using namespace std::complex_literals;
for (unsigned l = 0; l < order; ++l) {
Xs[l] *= std::pow(1i, -l) * 2.0;
}
return Xs;
}
std::vector<std::complex<double>> besselW(const std::complex<double>& z, const unsigned& order) {
if ((order & (order - 1)) != 0) {
std::cerr << "order must be power of 2" << std::endl;
std::exit(-1);
}
if (fftw_init_threads() == 0) {
std::cerr << "fftw failed to init multithreads" << std::endl;
std::exit(-1);
}
fftw_complex *in, *out;
in = (fftw_complex*)fftw_malloc(sizeof(fftw_complex) * order);
out = (fftw_complex*)fftw_malloc(sizeof(fftw_complex) * order);
for (unsigned i = 0; i < order; ++i) {
auto xs = preBesselF(z, order, i);
in[i][0] = xs.real();
in[i][1] = xs.imag();
}
fftw_plan_with_nthreads(omp_get_max_threads());
// auto begin = std::chrono::steady_clock::now();
fftw_plan p = fftw_plan_dft_1d(int(order), in, out, FFTW_FORWARD, FFTW_ESTIMATE);
// auto end = std::chrono::steady_clock::now();
// std::chrono::duration<double> timeCost = end - begin;
// std::cout << "init_fftw : " << timeCost.count() << "s." << std::endl;
auto begin = std::chrono::steady_clock::now();
fftw_execute(p);
auto end = std::chrono::steady_clock::now();
std::chrono::duration<double> timeCost = end - begin;
std::cout << " fftw : " << timeCost.count() << "s." << std::endl;
auto Xs = std::vector<std::complex<double>>(order);
using namespace std::complex_literals;
for (unsigned l = 0; l < order; ++l) {
Xs[l] = (out[l][0] + out[l][1] * 1i) * std::pow(1i, -l) * 2.0;
}
fftw_destroy_plan(p);
fftw_cleanup_threads();
fftw_free(in);
fftw_free(out);
return Xs;
}
//results are more accurate than homework1.
int main()
{
namespace plt = matplotlibcpp;
const unsigned ORDER = unsigned(std::pow(2, 16));
// std::vector<double> xs(ORDER);
// std::iota(xs.begin(), xs.end(), 0);
// for (unsigned i = 0; i < 3; ++i) {
// auto ys = bessel(std::complex<double>(std::pow(10, i), 0), ORDER);
// auto ysr = std::vector<double>(ORDER);
// for (unsigned j = 0; j < ORDER; ++j) {
// ysr[j] = ys[j].real();
// }
// plt::named_plot(std::to_string(std::pow(10, i)), xs, ysr);
// std::cout << "First ten terms of J(" << std::pow(10, i) << ", n)" << std::endl;
// std::cout << "n : J" << std::endl;
// for (unsigned j = 0; j < 10; ++j) {
// std::cout << j << " : " << ys[j] << std::endl;
// }
// }
// plt::legend();
// plt::xlim(0, 32);
// plt::show();
for (unsigned i = 0; i < 3; ++i) {
auto fftwys = besselW(std::complex<double>(std::pow(10, i), 0), ORDER);
auto myys = bessel(std::complex<double>(std::pow(10, i), 0), ORDER);
auto mycys = besselM(std::complex<double>(std::pow(10, i), 0), ORDER);
for (unsigned j = 0; j < 10; ++j) {
std::cout << fftwys[j].real() << " " << myys[j].real() << " " << mycys[j].real() << std::endl;
}
}
return 0;
}