Fourier library: Operations in the Fourier domain
cxxfftwtest.cc
Go to the documentation of this file.
1 
35 #define CXXFFTWTEST_VERSION \
36  "CXXFFTWTEST V1.1 a small test program for fftw when called from C++"
37 
38 #include <iostream>
39 #include <tfxx/commandline.h>
40 #include <fourier/fftwaff.h>
41 #include <fourier/error.h>
42 
43 using std::cout;
44 using std::cerr;
45 using std::endl;
46 
47 using namespace fourier::fft;
50 
51 void fill(Tseries& a, int n, int m)
52 {
53  a=Tseries(n);
54  int k;
55  double f1, f2;
56  f1=3.141592653589/n;
57  f2=f1*m*2;
58  /* fill input array */
59  for (k=0; k<n; ++k)
60  {
61  a(a.f()+k)=sin(k*f1)*sin(k*f1)*sin(k*f2);
62  }
63  cout << "array filled" << endl;
64 }
65 
66 /*----------------------------------------------------------------------*/
67 
68 void process(int n, int m, const bool& verbose, const bool& debug)
69 {
70  cout << "test with " << n << " samples" << endl;
71  cout << "test with frequency " << m << endl;
72  Tseries a(n);
73  fill(a, n, m);
74  if (verbose) { cout << "array is filled " << endl; }
75  DRFFTWAFF fftprocessor(n);
76  if (verbose) { cout << "processor is created " << endl; }
77  Tspectrum b=fftprocessor(a, debug);
78  if (verbose) { cout << "forward transform is finished" << endl; }
79  Tseries c=fftprocessor(b, debug);
80  if (verbose) { cout << "backward transform is finished" << endl; }
81  cout << "time series:" << endl;
82  for (int i=0; i<n ; ++i)
83  {
84  cout.width(4);
85  cout << i << " ";
86  cout.width(15);
87  cout.precision(6);
88  cout << a(i) << " ";
89  cout.width(15);
90  cout.precision(6);
91  cout << c(i) << endl;
92  }
93  cout << "power spectrum:" << endl;
94  for (int i=0; i<b.size() ; ++i)
95  {
96  cout.width(4);
97  cout << i << " " << abs(b(i)*conj(b(i))) << endl;
98  }
99 }
100 
101 /*----------------------------------------------------------------------*/
102 struct Options {
103  bool verbose, debug;
104  int n, m;
105 }; // Options
106 
107 /*----------------------------------------------------------------------*/
108 
109 int main(int iargc, char* argv[])
110 {
111 
112  // define usage information
113  char usage_text[]=
114  {
116  "usage: cxxfftwtest [-n n] [-m f]" "\n"
117  " or: cxxfftwtest --help|-h" "\n"
118  };
119 
120  // define full help text
121  char help_text[]=
122  {
123  "\n"
124  "-n n set number of samples to n\n"
125  "-m f set frequency to f\n"
126  };
127 
128  // define commandline options
129  using namespace tfxx::cmdline;
130  static Declare options[]=
131  {
132  // 0: print help
133  {"help",arg_no,"-"},
134  // 1: verbose mode
135  {"v",arg_no,"-"},
136  // 2: debug mode
137  {"DEBUG",arg_no,"-"},
138  // 3: number of samples
139  {"n",arg_yes,"50"},
140  // 4: frequency
141  {"m",arg_yes,"3"},
142  {NULL}
143  };
144 
145  // no arguments? print usage...
146  if (iargc<1)
147  {
148  cerr << usage_text << endl;
149  exit(0);
150  }
151 
152  // collect options from commandline
153  Commandline cmdline(iargc, argv, options);
154 
155  // help requested? print full help text...
156  if (cmdline.optset(0))
157  {
158  cerr << usage_text << endl;
159  cerr << help_text << endl;
160  exit(0);
161  }
162 
163  Options opt;
164  opt.verbose=cmdline.optset(1);
165  opt.debug=cmdline.optset(2);
166  opt.n=cmdline.int_arg(3);
167  opt.m=cmdline.int_arg(4);
168 
169  FOURIER_assert(opt.n>0, "illegal number of samples")
170  FOURIER_assert(opt.m>0, "illegal frequency")
171 
172  process(opt.n, opt.m, opt.verbose, opt.debug);
173 }
174 
175 /* ----- END OF cxxfftwtest.cc ----- */
aff::Series< Tsample > Tseries
Definition: fftwaff.h:133
#define FOURIER_assert(C, M)
Check an assertion and report by throwing an exception.
Definition: error.h:141
DRFFTWAFF::Tspectrum Tspectrum
Definition: cxxfftwtest.cc:49
aff::Series< Tcoeff > Tspectrum
Definition: fftwaff.h:134
error handling for libfourier (prototypes)
#define CXXFFTWTEST_VERSION
Definition: cxxfftwtest.cc:35
bool debug
Definition: cxxfftwtest.cc:103
bool verbose
void process(int n, int m, const bool &verbose, const bool &debug)
Definition: cxxfftwtest.cc:68
int main(int iargc, char *argv[])
Definition: cxxfftwtest.cc:109
aff::Series< Tvalue > Tseries
use fftw together with aff containers (prototypes)
void fill(Tseries &a, int n, int m)
Definition: cxxfftwtest.cc:51
DRFFTWAFF::Tseries Tseries
Definition: cxxfftwtest.cc:48