Elements  6.2
A C++ base framework for the Euclid Software.
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 
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 Fftw : public Program {
43 
44 public:
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 
Elements::ExitCode::OK
@ OK
Everything is OK.
std::string
STL class.
std::cos
T cos(T... args)
Elements::Examples::Fftw
Definition: Fftw.cpp:42
MathConstants.h
A few math constants.
Elements::Kernel::Units::pi
constexpr double pi
Definition: MathConstants.h:34
Elements::ExitCode
ExitCode
Strongly typed exit numbers.
Definition: Exit.h:97
Elements::Examples::Program
Simple example of an Elements program.
Definition: Program.cpp:79
N
constexpr std::size_t N
Definition: Fftw.cpp:37
std::map
STL class.
ProgramHeaders.h
Elements::Examples::log
auto log
Definition: BackTrace.cpp:36
Elements::Logging::getLogger
static Logging getLogger(const std::string &name="")
Definition: Logging.cpp:63
Elements::Examples::Fftw::mainMethod
ExitCode mainMethod(ELEMENTS_UNUSED map< string, VariableValue > &args) override
Definition: Fftw.cpp:45
MAIN_FOR
#define MAIN_FOR(ELEMENTS_PROGRAM_NAME)
Definition: Main.h:113
std::size_t
Unused.h
Macro to silence unused variables warnings from the compiler.
ELEMENTS_UNUSED
#define ELEMENTS_UNUSED
Definition: Unused.h:39
Elements
Definition: callBackExample.h:35