Theoretica
Mathematical Library
Loading...
Searching...
No Matches
fft.h
Go to the documentation of this file.
1
5
6#ifndef THEORETICA_FFT_H
7#define THEORETICA_FFT_H
8
9#include "../core/bit_op.h"
10#include "../algebra/algebra_types.h"
11#include "../algebra/algebra.h"
12#include "../complex/complex.h"
13
14
15namespace theoretica {
16
17
19 namespace signal {
20
21
28 template<typename ReturnVector = cvec, typename InputVector = cvec>
29 inline ReturnVector fft(const InputVector& x, bool inverse = false) {
30
31 if (x.size() == 0) {
33 return algebra::make_error<ReturnVector>(1);
34 }
35
36 // Resulting vector in the frequency domain
37 ReturnVector k = x;
38 const unsigned int N = x.size();
39 const real sign = (inverse ? 1.0 : -1.0);
40
41 // Compute the logarithm of the size
42 const unsigned int log2N = ilog2(N);
43
44 // Enforce power of 2 vector size
45 if (N != (unsigned int) (1 << log2N)) {
46 return algebra::vec_error(k);
47 }
48
49 // Bit reverse
50 swap_bit_reverse(k, log2N);
51
52 for (unsigned int p = 1; p <= log2N; p++) {
53
54 const unsigned int m = 1 << p;
55 const unsigned int offset = m / 2;
56
57 complex<real> w (1.0, 0.0);
58
59 // Phase shift between iterations
60 const complex<real> phase = complex<real>(
61 cos(sign * 2 * PI / m),
62 sin(sign * 2 * PI / m)
63 );
64
65 for (unsigned int j = 0; j < offset; j++) {
66
67 for (unsigned int i = j; i < N; i += m) {
68
69 const complex<real> t = w * k[i + offset];
70 k[i + offset] = k[i] - t;
71 k[i] += t;
72 }
73
74 w *= phase;
75 }
76 }
77
78 // The normalization constant is 1/N
79 if (inverse)
80 k /= N;
81
82 return k;
83 }
84
85
91 template<typename ReturnVector = cvec, typename InputVector = cvec>
92 inline ReturnVector ifft(const InputVector& k) {
93 return fft(k, true);
94 }
95 }
96}
97
98
99#endif
#define TH_MATH_ERROR(F_NAME, VALUE, EXCEPTION)
TH_MATH_ERROR is a macro which throws exceptions or modifies errno (depending on which compilation op...
Definition error.h:238
Vector & vec_error(Vector &v)
Overwrite the given vector with the error vector with NaN values.
Definition algebra.h:62
ReturnVector fft(const InputVector &x, bool inverse=false)
Compute the Fast Fourier Transform of a set of data points.
Definition fft.h:29
ReturnVector ifft(const InputVector &k)
Compute the Inverse Fast Fourier Transform of a set of data points.
Definition fft.h:92
Main namespace of the library which contains all functions and objects.
Definition algebra.h:27
double real
A real number, defined as a floating point type.
Definition constants.h:207
complex< T > inverse(complex< T > z)
Compute the conjugate of a complex number.
Definition complex_analysis.h:35
UnsignedIntType ilog2(UnsignedIntType x)
Find the integer logarithm of x.
Definition real_analysis.h:550
dual2 cos(dual2 x)
Compute the cosine of a second order dual number.
Definition dual2_functions.h:86
@ InvalidArgument
Invalid argument.
dual2 sin(dual2 x)
Compute the sine of a second order dual number.
Definition dual2_functions.h:72
constexpr real PI
The Pi mathematical constant.
Definition constants.h:225
void swap_bit_reverse(Vector &x, unsigned int m)
Swap the elements of a vector pair-wise, by exchanging elements with indices related by bit reversion...
Definition bit_op.h:90