52 lines
1,008 B
C++
52 lines
1,008 B
C++
|
#include <fftw3.h>
|
||
|
#include <cmath>
|
||
|
#include <libplotcpp/plotcpp.hpp>
|
||
|
|
||
|
double pi() {
|
||
|
return 3.1415;
|
||
|
}
|
||
|
|
||
|
std::vector<double> tfd2vect(fftw_complex* tfd, int N) {
|
||
|
std::vector<double> res;
|
||
|
auto it = tfd;
|
||
|
for (int i = 0; i != N; ++i) {
|
||
|
fftw_complex c = {*it[0], *it[1]};
|
||
|
res.push_back(sqrt(c[0]*c[0] + c[1]*c[1]));
|
||
|
it++;
|
||
|
}
|
||
|
|
||
|
return res;
|
||
|
}
|
||
|
|
||
|
int main(int argc, char** argv) {
|
||
|
QApplication app(argc, argv);
|
||
|
fftw_complex *in, *out;
|
||
|
fftw_plan p;
|
||
|
|
||
|
int N = 500;
|
||
|
in = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * N);
|
||
|
out = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * N);
|
||
|
p = fftw_plan_dft_1d(N, in, out, FFTW_FORWARD, FFTW_MEASURE);
|
||
|
|
||
|
std::vector<double> xx;
|
||
|
for (int i = 0; i != N; ++i) {
|
||
|
xx.push_back(i);
|
||
|
}
|
||
|
|
||
|
for (int i = 0; i != N; ++i) {
|
||
|
in[i][0] = sin(2*pi()*50*i/N);
|
||
|
}
|
||
|
|
||
|
fftw_execute(p); /* repeat as needed */
|
||
|
|
||
|
std::vector<double> res = tfd2vect(out, N);
|
||
|
PlotCpp g;
|
||
|
g.plot(xx, res);
|
||
|
g.draw();
|
||
|
|
||
|
|
||
|
fftw_destroy_plan(p);
|
||
|
fftw_free(in); fftw_free(out);
|
||
|
return app.exec();
|
||
|
}
|