Elements  6.0.1
A C++ base framework for the Euclid Software.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 
34 using std::map;
35 using std::string;
36 
37 constexpr std::size_t N = 32;
38 
39 namespace Elements {
40 namespace Examples {
41 
42 class FftwExample : public Program {
43 
44 public:
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 
ExitCode
Strongly typed exit numbers.
Definition: Exit.h:97
Macro to silence unused variables warnings from the compiler.
ExitCode mainMethod(ELEMENTS_UNUSED map< string, VariableValue > &args) override
Definition: FftwExample.cpp:45
Everything is OK.
Abstract class for all Elements programs.
Definition: Program.h:52
STL class.
STL class.
#define MAIN_FOR(ELEMENTS_PROGRAM_NAME)
Definition: Main.h:113
T cos(T...args)
constexpr std::size_t N
Definition: FftwExample.cpp:37
constexpr double pi
Definition: MathConstants.h:34
#define ELEMENTS_UNUSED
Definition: Unused.h:39
static Logging getLogger(const std::string &name="")
Definition: Logging.cpp:63
A few math constants.