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