Elements 6.1.2
A C++ base framework for the Euclid Software.
Loading...
Searching...
No Matches
Fftw.cpp
Go to the documentation of this file.
1
21#include <cmath> // for cos
22#include <cstdio>
23#include <map> // for map
24#include <string> // for string
25
26#include <boost/format.hpp> // for format
27
28#include <fftw3.h>
29
30#include "ElementsKernel/MathConstants.h" // for pi
31#include "ElementsKernel/ProgramHeaders.h" // for including all Program/related headers
32#include "ElementsKernel/Unused.h" // for ELEMENTS_UNUSED
33
34using std::map;
35using std::string;
36
37constexpr std::size_t N = 32;
38
39namespace Elements {
40namespace Examples {
41
42class Fftw : public Program {
43
44public:
46
47 auto log = Logging::getLogger("FftwExample");
48
49 fftw_complex in[N]; /* double [2] */
50 fftw_complex out[N];
51 fftw_complex in2[N];
52 fftw_plan p;
53 fftw_plan q;
54
55 using std::cos;
56
57 /* prepare a cosine wave */
58 for (size_t i = 0; i < N; i++) {
59 in[i][0] = cos(3.0 * 2.0 * Units::pi * static_cast<double>(i) / static_cast<double>(N));
60 in[i][1] = 0;
61 }
62
63 /* forward Fourier transform, save the result in 'out' */
64 p = fftw_plan_dft_1d(N, in, out, FFTW_FORWARD, FFTW_ESTIMATE);
65 fftw_execute(p);
66 for (size_t i = 0; i < N; i++) {
67 log.info() << boost::format("freq: %3d %+9.5f %+9.5f I") % i % out[i][0] % out[i][1];
68 }
69 fftw_destroy_plan(p);
70
71 /* backward Fourier transform, save the result in 'in2' */
72 printf("\nInverse transform:\n");
73 q = fftw_plan_dft_1d(N, out, in2, FFTW_BACKWARD, FFTW_ESTIMATE);
74 fftw_execute(q);
75 /* normalize */
76 for (size_t i = 0; i < N; i++) {
77 in2[i][0] *= 1. / N;
78 in2[i][1] *= 1. / N;
79 }
80 for (size_t i = 0; i < N; i++) {
81 log.info() << boost::format("recover: %3d %+9.5f %+9.5f I vs. %+9.5f %+9.5f I") % i % in[i][0] % in[i][1] %
82 in2[i][0] % in2[i][1];
83 }
84 fftw_destroy_plan(q);
85
86 fftw_cleanup();
87
88 log.info() << "This is the end of the test";
89
90 return ExitCode::OK;
91 }
92};
93
94} // namespace Examples
95} // namespace Elements
96
constexpr std::size_t N
Definition: Fftw.cpp:37
A few math constants.
Macro to silence unused variables warnings from the compiler.
ExitCode mainMethod(ELEMENTS_UNUSED map< string, VariableValue > &args) override
Definition: Fftw.cpp:45
Simple example of an Elements program.
Definition: Program.cpp:79
static Logging getLogger(const std::string &name="")
Definition: Logging.cpp:63
T cos(T... args)
ExitCode
Strongly typed exit numbers.
Definition: Exit.h:97
#define MAIN_FOR(ELEMENTS_PROGRAM_NAME)
Definition: Main.h:113
#define ELEMENTS_UNUSED
Definition: Unused.h:39
@ OK
Everything is OK.