TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
inverse_fast_fourier_transform.cpp
Go to the documentation of this file.
1
14#include <cassert>
15#include <cmath>
16#include <complex>
17#include <cstdint>
18#include <iostream>
19#include <vector>
20
25namespace numerical_methods {
34std::complex<double> *InverseFastFourierTransform(std::complex<double> *p,
35 uint8_t n) {
36 if (n == 1) {
37 return p;
38 }
39
40 double pi = 2 * asin(1.0);
41
42 std::complex<double> om = std::complex<double>(
43 cos(2 * pi / n), sin(2 * pi / n));
44
45 om.real(om.real() / n);
46 om.imag(om.imag() / n);
47
48 auto *pe = new std::complex<double>[n / 2];
49
50 auto *po = new std::complex<double>[n / 2];
51
52 int k1 = 0, k2 = 0;
53 for (int j = 0; j < n; j++) {
54 if (j % 2 == 0) {
55 pe[k1++] = p[j];
56
57 } else {
58 po[k2++] = p[j];
59 }
60 }
61
62 std::complex<double> *ye =
64
65 std::complex<double> *yo =
67
68 auto *y = new std::complex<double>[n];
69
70 k1 = 0, k2 = 0;
71
72 for (int i = 0; i < n / 2; i++) {
73 y[i] =
74 ye[k1] + pow(om, i) * yo[k2];
75 y[i + n / 2] =
76 ye[k1] - pow(om, i) * yo[k2];
77
78 k1++;
79 k2++;
80 }
81
82 if (n != 2) {
83 delete[] pe;
84 delete[] po;
85 }
86
87 delete[] ye;
88 delete[] yo;
89 return y;
90}
91
92} // namespace numerical_methods
93
101static void test() {
102 /* descriptions of the following test */
103
104 auto *t1 = new std::complex<double>[2];
105 auto *t2 = new std::complex<double>[4];
106
107 t1[0] = {3, 0};
108 t1[1] = {-1, 0};
109 t2[0] = {10, 0};
110 t2[1] = {-2, -2};
111 t2[2] = {-2, 0};
112 t2[3] = {-2, 2};
113
114 uint8_t n1 = 2;
115 uint8_t n2 = 4;
116 std::vector<std::complex<double>> r1 = {
117 {1, 0}, {2, 0}};
118
119 std::vector<std::complex<double>> r2 = {
120 {1, 0}, {2, 0}, {3, 0}, {4, 0}};
121
122 std::complex<double> *o1 =
124
125 std::complex<double> *o2 =
127
128 for (uint8_t i = 0; i < n1; i++) {
129 assert((r1[i].real() - o1[i].real() < 0.000000000001) &&
130 (r1[i].imag() - o1[i].imag() <
131 0.000000000001));
133 }
134
135 for (uint8_t i = 0; i < n2; i++) {
136 assert((r2[i].real() - o2[i].real() < 0.000000000001) &&
137 (r2[i].imag() - o2[i].imag() <
138 0.000000000001));
140 }
141
142 delete[] t1;
143 delete[] t2;
144 delete[] o1;
145 delete[] o2;
146 std::cout << "All tests have successfully passed!\n";
147}
148
157int main(int argc, char const *argv[]) {
158 test(); // run self-test implementations
159 // with 2 defined test cases
160 return 0;
161}
int main()
Main function.
static void test()
Self-test implementations.
std::complex< double > * InverseFastFourierTransform(std::complex< double > *p, uint8_t n)
InverseFastFourierTransform is a recursive function which returns list of complex numbers.