Typedefs | Functions
test_DuneFFT.cxx File Reference
#include "dunecore/DuneCommon/Utility/DuneFFT.h"
#include <string>
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <iomanip>
#include <cmath>
#include <cassert>

Go to the source code of this file.

Typedefs

using Index = unsigned int
 
using FloatVector = DuneFFT::FloatVector
 
using DFT = DuneFFT::DFT
 

Functions

int test_DuneFFT (Index ignorm, Index itnorm, int loglev, Index len)
 
int main (int argc, char *argv[])
 

Typedef Documentation

using DFT = DuneFFT::DFT

Definition at line 31 of file test_DuneFFT.cxx.

Definition at line 30 of file test_DuneFFT.cxx.

using Index = unsigned int

Definition at line 29 of file test_DuneFFT.cxx.

Function Documentation

int main ( int  argc,
char *  argv[] 
)

Definition at line 136 of file test_DuneFFT.cxx.

136  {
137  Index ignorm = 1;
138  Index itnorm = 1;
139  int loglev = 0;
140  Index len = 0;
141  if ( argc > 1 ) {
142  string sarg(argv[1]);
143  if ( sarg == "-h" ) {
144  cout << "Usage: " << argv[0] << " [ignorm [itnorm [loglev [LEN]]]]" << endl;
145  return 0;
146  }
147  ignorm = std::stoi(sarg);
148  }
149  if ( argc > 2 ) {
150  string sarg(argv[2]);
151  itnorm = std::stoi(sarg);
152  }
153  if ( argc > 3 ) {
154  string sarg(argv[3]);
155  loglev = std::stoi(sarg);
156  }
157  if ( argc > 4 ) {
158  string sarg(argv[4]);
159  len = std::stoi(sarg);
160  }
161  return test_DuneFFT(ignorm, itnorm, loglev, len);
162 }
unsigned int Index
int test_DuneFFT(Index ignorm, Index itnorm, int loglev, Index len)
QTextStream & endl(QTextStream &s)
int test_DuneFFT ( Index  ignorm,
Index  itnorm,
int  loglev,
Index  len 
)

Definition at line 35 of file test_DuneFFT.cxx.

35  {
36  const string myname = "test_DuneFFT: ";
37 #ifdef NDEBUG
38  cout << myname << "NDEBUG must be off." << endl;
39  abort();
40 #endif
41  string line = "-----------------------------";
42 
43  RealDftNormalization norm(ignorm, itnorm);
44  cout << line << endl;
45  cout << myname << " Global norm: " << ignorm << endl;
46  cout << myname << " Term norm: " << itnorm << endl;
47 
48  cout << myname << line << endl;
49  cout << myname << "Create data." << endl;
50  vector<float> sams = { 3.0, 6.0, 16.1, 28.6, 30.2, 27.7, 16.3, 9.6, 4.2, -1.0,
51  -2.3, -4.2, -9.2, -18.6, -21.9, -29.0, -24.3, -14.2, -5.0, -3.0};
52  if ( len > 0 ) sams.resize(len, 0.0);
53  Index nsam = sams.size();
54  assert( sams.size() == nsam );
55  cout << myname << "Sample length: " << nsam << endl;
56  float samsum = 0.0;
57  for ( float sam : sams ) samsum += sam;
58  cout << myname << "Sample mean: " << samsum/nsam << endl;
59  Index namp = (nsam+2)/2;
60  Index npha = (nsam+1)/2;
61  cout << myname << " # samples: " << nsam << endl;
62  cout << myname << "Expected amp size: " << namp << endl;
63  cout << myname << "Expected pha size: " << npha << endl;
64 
65  cout << myname << line << endl;
66  cout << myname << "Create empty DFT." << endl;
67  DFT dft(norm);
68  assert( dft.size() == 0 );
69 
70  cout << myname << line << endl;
71  cout << myname << "Transform forward." << endl;
72  assert( DuneFFT::fftForward(sams, dft, loglev) == 0 );
73  assert( sams.size() == nsam );
74  cout << myname << "DFT size: " << dft.size() << endl;
75  assert( dft.size() == nsam );
76  assert( dft.nCompact() == namp );
77  assert( dft.nAmplitude() == namp );
78  assert( dft.nPhase() == npha );
79  cout << "Frequency components:" << endl;
80  for ( Index ifrq=0; ifrq<namp; ++ifrq ) {
81  cout << myname << setw(4) << ifrq << ": " << dft.amplitude(ifrq);
82  if ( ifrq < npha ) cout << " @ " << dft.phase(ifrq);
83  cout << endl;
84  }
85 
86  cout << myname << line << endl;
87  cout << myname << "Check power." << endl;
88  float pwr1 = 0.0;
89  for ( float sam : sams ) pwr1 += sam*sam;
90  float pwr2 = dft.power();
91  float pwr3 = 0.0;
92  float pwr4 = 0.0;
93  for ( Index ifrq=0; ifrq<nsam; ++ifrq ) {
94  float amp = dft.amplitude(ifrq);
95  float xre = dft.real(ifrq);
96  float xim = dft.imag(ifrq);
97  if ( !dft.normalization().isPower() || ifrq < dft.nCompact() ) {
98  pwr3 += amp*amp;
99  pwr4 += xre*xre + xim*xim;
100  }
101  }
102  double pfac = 1.0;
103  if ( dft.normalization().isStandard() ) pfac = 1.0/nsam;
104  if ( dft.normalization().isBin() ) pfac = nsam;
105  pwr3 *= pfac;
106  pwr4 *= pfac;
107  cout << myname << "Tick power: " << pwr1 << endl;
108  cout << myname << " DFT power: " << pwr2 << endl;
109  cout << myname << " Amp power: " << pwr3 << endl;
110  cout << myname << " ReI power: " << pwr4 << endl;
111  assert( fabs(pwr2 - pwr1) < 1.e-5*pwr1 );
112 
113  cout << myname << line << endl;
114  cout << myname << "Call inverse." << endl;
115  FloatVector sams2, xres2, xims2;
116  int rstat = DuneFFT::fftInverse(dft, sams2, loglev);
117  if ( rstat != 0 ) {
118  cout << myname << "FFT invert returned " << rstat << endl;
119  assert( false );
120  }
121 
122  assert( sams2.size() == nsam );
123  for ( Index isam=0; isam<nsam; ++isam ) {
124  cout << setw(4) << isam << ":" << setw(10) << fixed << sams[isam]
125  << " ?= " << setw(10) << fixed << sams2[isam] << endl;
126  assert( fabs(sams2[isam] - sams[isam]) < 1.e-4 );
127  }
128 
129  cout << myname << line << endl;
130  cout << myname << "Done." << endl;
131  return 0;
132 }
unsigned int Index
const double e
static int fftForward(Index ntick, const float *psam, DFT &dft, Index logLevel=0)
Definition: DuneFFT.cxx:23
size_t size
Definition: lodepng.cpp:55
auto norm(Vector const &v)
Return norm of the specified vector.
Q_EXPORT QTSManip setw(int w)
Definition: qtextstream.h:331
static int fftInverse(const DFT &dft, FloatVector &sams, Index logLevel=0)
Definition: DuneFFT.cxx:79
void line(double t, double *p, double &x, double &y, double &z)
Dft::FloatVector FloatVector
QTextStream & endl(QTextStream &s)