Elements 6.0.1
A C++ base framework for the Euclid Software.
Loading...
Searching...
No Matches
FftwExample.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 FftwExample : public Program {
43
44public:
46
47 auto log = Logging::getLogger("FftwExample");
48
49 fftw_complex in[N], out[N], in2[N]; /* double [2] */
50 fftw_plan p, q;
51
52 using std::cos;
53
54 /* prepare a cosine wave */
55 for (size_t i = 0; i < N; i++) {
56 in[i][0] = cos(3.0 * 2.0 * Units::pi * static_cast<double>(i) / static_cast<double>(N));
57 in[i][1] = 0;
58 }
59
60 /* forward Fourier transform, save the result in 'out' */
61 p = fftw_plan_dft_1d(N, in, out, FFTW_FORWARD, FFTW_ESTIMATE);
62 fftw_execute(p);
63 for (size_t i = 0; i < N; i++) {
64 log.info() << boost::format("freq: %3d %+9.5f %+9.5f I") % i % out[i][0] % out[i][1];
65 }
66 fftw_destroy_plan(p);
67
68 /* backward Fourier transform, save the result in 'in2' */
69 printf("\nInverse transform:\n");
70 q = fftw_plan_dft_1d(N, out, in2, FFTW_BACKWARD, FFTW_ESTIMATE);
71 fftw_execute(q);
72 /* normalize */
73 for (size_t i = 0; i < N; i++) {
74 in2[i][0] *= 1. / N;
75 in2[i][1] *= 1. / N;
76 }
77 for (size_t i = 0; i < N; i++) {
78 log.info() << boost::format("recover: %3d %+9.5f %+9.5f I vs. %+9.5f %+9.5f I") % i % in[i][0] % in[i][1] %
79 in2[i][0] % in2[i][1];
80 }
81 fftw_destroy_plan(q);
82
83 fftw_cleanup();
84
85 log.info() << "This is the end of the test";
86
87 return ExitCode::OK;
88 }
89};
90
91} // namespace Examples
92} // namespace Elements
93
constexpr std::size_t N
Definition: FftwExample.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: FftwExample.cpp:45
static Logging getLogger(const std::string &name="")
Definition: Logging.cpp:63
Abstract class for all Elements programs.
Definition: Program.h:52
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.