-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathffts.cpp
More file actions
108 lines (84 loc) · 3 KB
/
ffts.cpp
File metadata and controls
108 lines (84 loc) · 3 KB
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
#include <vector>
#include <algorithm>
#include <numeric>
#include <cassert>
#include "ffts.hpp"
std::vector<int> compute_radices(int size, int option, int threshold) {
assert( size > 0 );
assert( 1 <= option and option <= 3 );
assert( 2 <= threshold );
using std::accumulate;
using std::vector;
using std::reverse;
vector<int> factors{};
vector<int> radices{};
int n = size;
// first compute all factors of n
for (int i = 2; i * i <= n; ++i){
while (n % i == 0) {
n = n / i;
factors.push_back(i);
}
}
if (n > 1)
factors.push_back(n);
assert( accumulate(factors.begin(), factors.end(), 1, std::multiplies<int>()) == size);
// different choices of radices possible
// 1. factors in increasing order
// 2. factors in decreasing order
// 3. group factors if below threshold (starting from smallest or largest factor?)
switch (option) {
case 1:
return factors;
case 2:
reverse(factors.begin(), factors.end());
return factors;
case 3:
int radix_count = 0;
int radix = factors[0];
int factor;
for (size_t i = 1; i < factors.size(); ++i) {
factor = factors[i];
if (radix * factor <= threshold) {
radix *= factor;
}
else {
radices.push_back(radix);
++radix_count;
radix = factor;
}
}
radices.push_back(radix);
return radices;
}
assert( accumulate(radices.begin(), radices.end(), 1, std::multiplies<int>()) == size);
return radices;
}
std::vector<std::vector<std::complex<long double>>> precompute_phases(std::vector<int>& radices)
{
using std::vector;
using std::complex;
using std::polar;
using std::accumulate;
constexpr auto PI = 3.14159265358979323846264338327950288419716939937510L;
vector<vector<complex<long double>>> phase_table(radices.size(), vector<complex<long double>>{});
int radix_partial_product = accumulate(radices.begin(), radices.end(), 1, std::multiplies<int>());
auto it = radices.begin();
for (size_t i = 0; i < radices.size(); ++i)
{
++it;
phase_table[i] = vector<complex<long double>>(radix_partial_product, 1.0);
phase_table[i][1] = polar(1.0L, -2 * PI / radices[i]);
int j;
for (j = 2; j < radices[i]; ++j)
phase_table[i][j] = phase_table[i][j-1] * phase_table[i][1];
for(; j < radix_partial_product; ++j)
{
vector<int> digits = compute_digits(j, radices.begin(), it);
for (size_t k = 0; k < i; ++k)
phase_table[i][j] *= phase_table[k][digits[k]];
}
radix_partial_product /= radices[i];
}
return phase_table;
}