miniprojet/tests/examples/test-fft.cpp

50 lines
1.2 KiB
C++

#include <math.hpp>
#include <cmath>
#include <fstream>
#include <ctime>
void create_plot_file(std::string filename, const math::csignal& tfd) {
std::ofstream data_file(filename + ".data");
for (int i=0; i<tfd.size(); ++i) {
data_file << tfd[i].real()
<< " "
<< tfd[i].imag()
<< std::endl;
}
data_file.close();
}
int main(int argc, char** argv) {
math::csignal s;
double fe = 6000;
double f0 = 400;
int n = 30;
if (argc > 1) {
n = atoi(argv[1]);
}
for (int i=0; i<100; ++i) {
s.push_back(math::complex(std::sin(2*math::pi()*f0*float(i)/fe), 0));
}
math::csignal tfd;
clock_t begin = std::clock();
for (int i=0; i<n; ++i) {
tfd = math::fft(s, 2000);
}
clock_t end = clock();
std::cout << "Time to compute " << n << " fft: "<< double(end-begin) / CLOCKS_PER_SEC << std::endl;
std::cout << "Average time: " << double(end-begin) / CLOCKS_PER_SEC / n << std::endl;
math::csignal mod;
for (int i=0; i<tfd.size(); ++i) {
double R = tfd[i].real();
double I = tfd[i].imag();
double a = std::sqrt(R*R + I*I);
mod.push_back(math::complex(float(i)/tfd.size()*fe, a));
}
create_plot_file("graph", mod);
return 0;
}