|
| 1 | +#include "dft.h" |
| 2 | + |
| 3 | +dft::dft() |
| 4 | +{ |
| 5 | +} |
| 6 | +dft::~dft() |
| 7 | +{ |
| 8 | +} |
| 9 | + |
| 10 | +int computeLayer(int n2) |
| 11 | +{ |
| 12 | + int m = 0; |
| 13 | + |
| 14 | + while (n2 > 1) { |
| 15 | + n2 >>= 1; |
| 16 | + m += 1; |
| 17 | + } |
| 18 | + |
| 19 | + return m; |
| 20 | +} |
| 21 | + |
| 22 | + |
| 23 | +void computeWeights(vector<comp>& weights, int n) |
| 24 | +{ |
| 25 | + double fixed_factor = -2 * PI / n; |
| 26 | + weights.clear(); |
| 27 | + int half = n / 2; |
| 28 | + |
| 29 | + for (int i = 0; i < half; ++i) { |
| 30 | + double angle = i * fixed_factor; |
| 31 | + weights.push_back(comp{cos(angle), sin(angle)}); |
| 32 | + } |
| 33 | + |
| 34 | + for (int i = half; i < n; ++i) { |
| 35 | + weights.push_back(-weights[i - half]); |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +void computeInvertCode(vector<int> dst, int layer) |
| 40 | +{ |
| 41 | + dst.clear(); |
| 42 | + int n = 1 << layer; |
| 43 | + |
| 44 | + for (int i = 0; i < n; ++i) { |
| 45 | + int index = 0, r = i; |
| 46 | + |
| 47 | + for (int j = 0; j < layer; ++j) { |
| 48 | + index <<= 1; |
| 49 | + |
| 50 | + if (r & 1) { |
| 51 | + index += 1; |
| 52 | + } |
| 53 | + |
| 54 | + r >>= 1; |
| 55 | + } |
| 56 | + |
| 57 | + dst.push_back(index); |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +bool dft::dft1d(vector<comp>& dst, vector<comp> const &src) |
| 62 | +{ |
| 63 | + // fast fourier transform |
| 64 | + int n = src.size(); |
| 65 | + |
| 66 | + if (n == 0 || ~(n & (n - 1))) |
| 67 | + return false; |
| 68 | + |
| 69 | + vector<comp> weights; |
| 70 | + computeWeights(weights, n); |
| 71 | + int layer = computeLayer(n); |
| 72 | + vector<int> invertCode; |
| 73 | + computeInvertCode(invertCode, src, layer); |
| 74 | + vector<comp> inData; |
| 75 | + |
| 76 | + for (int i = 0; i < n; ++i) |
| 77 | + inData[i] = src[invertCode[i]]; |
| 78 | + |
| 79 | + dst = vecotr<comp>(comp{0, 0}, n); |
| 80 | + |
| 81 | + // compute fast fourier transform |
| 82 | + for (int L = 1; L <= layer; L++) { |
| 83 | + int distance = 1 << (L - 1); |
| 84 | + int W = 1 << (layer - L); |
| 85 | + int B = n >> L; |
| 86 | + int N = n / B; |
| 87 | + int index; |
| 88 | + |
| 89 | + for (int i = 0; i < B; i++) { |
| 90 | + int mid = i * N; |
| 91 | + |
| 92 | + for (int j = 0; j < N / 2; j++) { |
| 93 | + index = j + mid; |
| 94 | + dst[index] = inData[index] + (Weights[j * W] * inData[index + distance]); // Fe + W*Fo |
| 95 | + } |
| 96 | + |
| 97 | + for (int j = N / 2; j < N; j++) { |
| 98 | + index = j + mid; |
| 99 | + dst[index] = inData[index - distance] + (Weights[j * W] * inData[index]); // Fe - W*Fo |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + inData = dst; |
| 104 | + } |
| 105 | + |
| 106 | + return true; |
| 107 | +} |
| 108 | +bool dft::idft1d(vector<comp>& dst, vector<comp> const &src) |
| 109 | +{ |
| 110 | + //invert fast fourier transform |
| 111 | + int n = src.size(); |
| 112 | + |
| 113 | + if (n == 0 || ~(n & (n - 1))) |
| 114 | + return false; |
| 115 | + |
| 116 | + vector<comp> weights; |
| 117 | + computeWeights(weights, n); |
| 118 | + int layer = computeLayer(n); |
| 119 | + vector<comp> inData(src); |
| 120 | + dst = vecotr<comp>(comp{0, 0}, n); |
| 121 | + |
| 122 | + // compute invert fast fourier transform |
| 123 | + for (int L = 1; L <= layer; L++) { |
| 124 | + int distance = 1 << (L - 1); |
| 125 | + int W = 1 << (layer - L); |
| 126 | + int B = n >> L; |
| 127 | + int N = n / B; |
| 128 | + int index; |
| 129 | + |
| 130 | + for (int i = 0; i < B; i++) { |
| 131 | + int mid = i * N; |
| 132 | + |
| 133 | + for (int j = 0; j < N / 2; j++) { |
| 134 | + index = j + mid; |
| 135 | + dst[index] = (inData[index] + inData[index + distance]) / 2; // Fe + W*Fo |
| 136 | + } |
| 137 | + |
| 138 | + for (int j = N / 2; j < N; j++) { |
| 139 | + index = j + mid; |
| 140 | + dst[index] = (inData[index] - inData[index + distance]) / 2; // Fe - W*Fo |
| 141 | + |
| 142 | + if (abs(weights[j * W])) |
| 143 | + dst[index] = weights[j * W] * dst[index]; //(a+bi)/(c+di) |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + inData = dst; |
| 148 | + } |
| 149 | + |
| 150 | + vecotr<int> invertCode; |
| 151 | + computeInvertCode(invertCode, src, layer); |
| 152 | + |
| 153 | + for (int i = 0; i < n; ++i) |
| 154 | + dst[i] = inData[invertCode[i]]; |
| 155 | + |
| 156 | + return true; |
| 157 | +} |
| 158 | + |
| 159 | +bool dft::_dft2d(vector<vector<comp>>& dst, vector<vector<comp>> const &src,bool isInvert) |
| 160 | +{ |
| 161 | + auto fft=dft::dft1d; |
| 162 | + if(isInvert) |
| 163 | + fft = dft::idft1d; |
| 164 | + int row = src.size(); |
| 165 | + if(row<1) |
| 166 | + return false; |
| 167 | + int col = src[0].size(); |
| 168 | + if(col<1 || ~(row&(row-1)) || ~(col&(col-1))) |
| 169 | + return false; |
| 170 | + vector<vector<comp>> dftrow(vector<comp>(),row); |
| 171 | + for(int i=0;i<n;++i) |
| 172 | + fft(dftrow[i],src[i]); |
| 173 | + dst.clear(); |
| 174 | + dst = vector<vector<comp>>(vector<comp>(comp(),col),row); |
| 175 | + for(int c=0;c<col;++c){ |
| 176 | + vector<comp> inData,outData; |
| 177 | + for(int r=0;r<row;++r) |
| 178 | + inData.push_back(dftrow[r][c]); |
| 179 | + fft(outData,inData); |
| 180 | + for(int r=0;r<row;++r) |
| 181 | + dst[r][c] = outData[r]; |
| 182 | + } |
| 183 | + return true; |
| 184 | +} |
| 185 | +} |
| 186 | +bool dft::dft2d(vector<vector<comp>>& dst, vector<vector<comp>> const &src) |
| 187 | +{ |
| 188 | + return _dft2d(dst,src); |
| 189 | +} |
| 190 | +bool dft::idft2d(vector<vector<comp>>& dst, vector<vector<comp>> const &src) |
| 191 | +{ |
| 192 | + return _dft2d(dst,src,true); |
| 193 | +} |
0 commit comments