-
Notifications
You must be signed in to change notification settings - Fork 0
/
fft_sequential.cpp
82 lines (67 loc) · 2.06 KB
/
fft_sequential.cpp
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
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <vector>
namespace global
{
std::vector<float> input;
int input_size{};
} // namespace global
void initInputValues(const char*);
void showResults(const std::vector<float>&, const std::vector<float>&, std::clock_t, std::clock_t);
int main(int argc, char** argv)
{
initInputValues("../res/input.txt");
std::vector<float> result_real(global::input_size);
std::vector<float> result_img(global::input_size);
std::clock_t starttime = std::clock();
for(int res = 0; res < global::input_size; res++)
{
result_real[res] = 0.0;
result_img[res] = 0.0;
for(int inp = 0; inp < global::input_size; inp++)
{
const float temp = -2 * M_PI * inp * res / global::input_size;
result_real[res] += (global::input[inp] * std::cos(temp));
result_img[res] += (global::input[inp] * std::sin(temp));
}
}
std::clock_t endtime = std::clock();
showResults(result_real, result_img, starttime, endtime);
return 0;
}
void initInputValues(const char* path)
{
std::ifstream file(path);
if(not file.is_open())
{
return;
}
float temp{};
while(file >> temp)
{
global::input.push_back(temp);
}
global::input_size = global::input.size();
}
void showResults(const std::vector<float>& result_real,
const std::vector<float>& result_img,
std::clock_t starttime,
std::clock_t endtime)
{
std::printf("\n");
for(int result_iter = 0; result_iter < global::input_size; result_iter++)
{
const char img_sign = result_img[result_iter] >= 0 ? '+' : '-';
std::printf("X[%3d] = %6.2f %c i%-6.2f\n",
result_iter,
result_real[result_iter],
img_sign,
std::fabs(result_img[result_iter]));
}
std::printf("\n");
double cpu_time_used = (static_cast<double>(endtime - starttime)) / CLOCKS_PER_SEC;
std::printf("\nSequential FFT computation time: %.4lf ms\n\n", cpu_time_used * 1000);
}