File: fourier.cpp

package info (click to toggle)
groops 0%2Bgit20250907%2Bds-1
  • links: PTS, VCS
  • area: non-free
  • in suites: forky, sid
  • size: 11,140 kB
  • sloc: cpp: 135,607; fortran: 1,603; makefile: 20
file content (322 lines) | stat: -rw-r--r-- 8,918 bytes parent folder | download | duplicates (2)
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
/***********************************************/
/**
* @file fourier.cpp
*
* @brief FFT-functions.
*
* @author Torsten Mayer-Guerr
* @author Andreas Kvas
* @date 2004-10-25
*/
/***********************************************/

#include "base/importStd.h"
#include "base/matrix.h"
#include "base/fourier.h"

/***********************************************/

static inline void butterfly2(std::complex<Double> *f, const std::vector<std::complex<Double>> &twiddles, UInt step, UInt m)
{
  for(UInt i=0; i<m; i++)
  {
    const auto t = f[i+m] * twiddles[i*step];
    f[i+m]  = f[i]-t;
    f[i]   += t;
  }
}

/***********************************************/

static inline void butterfly3(std::complex<Double> *f, const std::vector<std::complex<Double>> &twiddles, UInt step, UInt m)
{
  const Double epi3 = twiddles[step*m].imag();
  for(UInt i=0; i<m; i++)
  {
    const auto t1 = f[i+  m] * twiddles[  i*step];
    const auto t2 = f[i+2*m] * twiddles[2*i*step];
    const auto t3 = t1 + t2;
    const auto t0 = std::complex<Double>(-epi3*(t1.imag()-t2.imag()), epi3*(t1.real()-t2.real()));
    f[i+m]    = f[i] - 0.5*t3;
    f[i+2*m]  = f[i+m] - t0;
    f[i]     += t3;
    f[i+m]   += t0;
  }
}

/***********************************************/

static inline void butterfly4(std::complex<Double> *f, const std::vector<std::complex<Double>> &twiddles, UInt step, UInt m, Bool inverse)
{
  for(UInt i=0; i<m; i++)
  {
    const auto t0 = f[i+  m] * twiddles[  i*step];
    const auto t1 = f[i+2*m] * twiddles[2*i*step];
    const auto t2 = f[i+3*m] * twiddles[3*i*step];
    const auto t3 = t0   + t2;
    const auto t4 = f[i] - t1;
    const auto t5 = std::complex<Double>(+t0.imag()-t2.imag(), -t0.real()+t2.real()) * ((inverse) ? -1. : 1.);
    f[i]    += t1;
    f[i+2*m] = f[i] - t3;
    f[i]    += t3;
    f[i+m]   = t4 + t5;
    f[i+3*m] = t4 - t5;
  }
}

/***********************************************/

static inline void butterfly5(std::complex<Double> *f, const std::vector<std::complex<Double>> &twiddles, UInt step, UInt m)
{
  const std::complex<Double> ya = twiddles[step*m];
  const std::complex<Double> yb = twiddles[step*2*m];
  for(UInt i=0; i<m; i++)
  {
    const auto t0  = f[i+1*m] * twiddles[  i*step];
    const auto t1  = f[i+2*m] * twiddles[2*i*step];
    const auto t2  = f[i+3*m] * twiddles[3*i*step];
    const auto t3  = f[i+4*m] * twiddles[4*i*step];
    const auto t4  = t0 + t3;
    const auto t5  = t1 + t2;
    const auto t6  = t1 - t2;
    const auto t7  = t0 - t3;
    const auto t8  = f[i] + t4 * ya.real() + t5 * yb.real();
    const auto t9  = f[i] + t4 * yb.real() + t5 * ya.real();
    const auto t10 = std::complex<Double>(+t7.imag(), -t7.real()) * ya.imag() + std::complex<Double>(t6.imag(), -t6.real()) * yb.imag();
    const auto t11 = std::complex<Double>(-t7.imag(), +t7.real()) * yb.imag() + std::complex<Double>(t6.imag(), -t6.real()) * ya.imag();
    f[i]     += t4 + t5;
    f[i+1*m]  = t8 - t10;
    f[i+2*m]  = t9 + t11;
    f[i+3*m]  = t9 - t11;
    f[i+4*m]  = t8 + t10;
  }
}

/***********************************************/

// perform the butterfly for one stage of a mixed radix FFT.
static inline void butterfly(std::complex<Double> *f, const std::vector<std::complex<Double>> &twiddles, UInt step, UInt m, UInt p)
{
  std::vector<std::complex<Double>> t(p);
  for(UInt u=0; u<m; u++)
  {
    for(UInt i=0; i<p; i++)
      t[i] = f[i*m+u];
    for(UInt i=0; i<p; i++)
    {
      f[i*m+u] = t[0];
      for(UInt k=1; k<p; k++)
        f[i*m+u] += t[k] * twiddles[(k*step*(i*m+u))%twiddles.size()];
    }
  }
}

/***********************************************/

static inline std::vector<UInt> computeRadix(UInt n)
{
  std::vector<UInt> factors;
  UInt p = 4; // factor out powers of 4, powers of 2, then any remaining primes
  do
  {
    while(n % p)
    {
      if(p == 4)      p  = 2;
      else if(p == 2) p  = 3;
      else            p += 2;
    }
    n /= p;
    factors.push_back(p);
    factors.push_back(n);
  }
  while(n > 1);

  return factors;
}

/***********************************************/

// recursive call: DFT of size m*p performed by doing p instances of smaller DFTs of size m, each one takes a decimated version of the input
static inline void recursiveFft(Bool inverse, std::complex<Double> *f, const std::complex<Double> *input, const UInt *factors, const std::vector<std::complex<Double>> &twiddles, UInt step)
{
  const UInt p = *(factors++); // the radix
  const UInt m = *(factors++); // stage's fft length/p

  if(m>1)
  {
    for(UInt i=0; i<p; i++)
      recursiveFft(inverse, f+(i*m), input+(i*step), factors, twiddles, p*step);
  }
  else
    for(UInt i=0; i<p; i++)
      f[i] = input[i*step];

  // recombine the p smaller DFTs
  switch(p)
  {
    case 2:  butterfly2(f, twiddles, step, m);          break;
    case 3:  butterfly3(f, twiddles, step, m);          break;
    case 4:  butterfly4(f, twiddles, step, m, inverse); break;
    case 5:  butterfly5(f, twiddles, step, m);          break;
    default: butterfly (f, twiddles, step, m, p);       break;
  }
}

/***********************************************/
/***********************************************/

std::vector<std::complex<Double>> Fourier::fft(const Vector &data)
{
  try
  {
    const UInt count = data.rows();

    std::vector<std::complex<Double>> input(count);
    for(UInt i=0; i<count; i++)
      input[i] = data(i);

    // compute twiddle factors
    std::vector<std::complex<Double>> twiddles(count);
    for(UInt i=0; i<twiddles.size(); i++)
      twiddles[i] = std::exp(std::complex<Double>(0, -2*PI*i/count));

    std::vector<std::complex<Double>> F(twiddles.size());
    std::vector<UInt> factors = computeRadix(twiddles.size());
    recursiveFft(FALSE/*inverse*/, F.data(), input.data(), factors.data(), twiddles, 1);
    F.resize((count+2)/2);
    return F;
  }
  catch(std::exception &e)
  {
    GROOPS_RETHROW(e)
  }
}

/***********************************************/

Vector Fourier::synthesis(const std::vector<std::complex<Double>> &F, Bool countEven)
{
  try
  {
    const UInt count = 2*F.size() - (countEven ? 2 : 1);

    // extent input symmetric
    std::vector<std::complex<Double>> F2(count);
    F2[0] = F[0];
    for(UInt i=1; i<F.size(); i++)
    {
      F2[i]       = F[i];
      F2[count-i] = std::conj(F[i]);
    }

    // compute twiddle factors
    std::vector<std::complex<Double>> twiddles(count);
    for(UInt i=0; i<twiddles.size(); i++)
      twiddles[i] = std::exp(std::complex<Double>(0, 2*PI*i/count));

    std::vector<std::complex<Double>> F3(twiddles.size());
    std::vector<UInt> factors = computeRadix(twiddles.size());
    recursiveFft(TRUE/*inverse*/, F3.data(), F2.data(), factors.data(), twiddles, 1);

    Vector data(count);
    for(UInt i=0; i<F3.size(); i++)
      data(i) = (1./count)*F3[i].real();
    return data;
  }
  catch(std::exception &e)
  {
    GROOPS_RETHROW(e)
  }
}

/***********************************************/

Vector Fourier::frequencies(UInt count, Double dt)
{
  Vector frequencies((count+2)/2);
  for(UInt k=0; k<(count+2)/2; k++)
    frequencies(k) = static_cast<Double>(k)/(count*dt);
  return frequencies;
}

/***********************************************/

void Fourier::complex2AmplitudePhase(const std::vector<std::complex<Double>> &F, Vector &amplitude, Vector &phase)
{
  amplitude = Vector(F.size());
  phase     = Vector(F.size());
  for(UInt k = 0; k<F.size(); k++)
  {
    phase(k)     = std::atan2(F.at(k).imag(), F.at(k).real());
    amplitude(k) = std::abs(F.at(k));
  }
}

/***********************************************/
/***********************************************/

static void cosTransformation(Vector &x)
{
  const UInt n = x.rows();
  if(n<2)
    return;

  Double c = x(0)-x(n-1);
  x(0) += x(n-1);
  for(UInt i=1; i<n/2; i++)
  {
    c += 2*std::cos(PI*i/(n-1)) * (x(i)-x(n-1-i));
    const Double t1 = (x(i)+x(n-1-i));
    const Double t2 = (x(i)-x(n-1-i)) * 2 * std::sin(PI*i/(n-1));
    x(i)     = t1 - t2;
    x(n-1-i) = t1 + t2;
  }
  if(n%2)
    x(n/2) *= 2;

  const auto F = Fourier::fft(x.row(0, n-1));

  x(0) = F[0].real();
  x(1) = c;
  for(UInt i=1; i<n/2; i++)
  {
    x(2*i+0) = F[i].real();
    x(2*i+1) = x(2*i-1) - F[i].imag();
  }
  if(n%2)
    x(n-1) = F.back().real();
}

/***********************************************/

Vector Fourier::covariance2psd(const Vector &cov, Double dt)
{
  try
  {
    Vector psd = 2*dt*cov; // one sided PSD
    cosTransformation(psd);
    return psd;
  }
  catch(std::exception &e)
  {
    GROOPS_RETHROW(e)
  }
}

/***********************************************/

Vector Fourier::psd2covariance(const Vector &psd, Double dt)
{
  try
  {
    Vector cov = 0.25/(dt*(psd.rows()-1))*psd; // from  one sided PSD
    cosTransformation(cov);
    return cov;
  }
  catch(std::exception &e)
  {
    GROOPS_RETHROW(e)
  }
}

/***********************************************/