CppNoddy  0.92
Loading...
Searching...
No Matches
FT.cpp
Go to the documentation of this file.
1/// \file FT.cpp
2/// \ingroup Tests
3/// \ingroup FT
4/// A simple 1D DFT test case. Takes a signal, evaluates the
5/// (slow) DFT, then inverts the same DFT and subtracts the
6/// result from the original signal data.
7
8#include <FT.h>
9#include <OneD_Node_Mesh.h>
10#include <Types.h>
11#include <Utility.h>
12
13using namespace CppNoddy;
14using namespace std;
15
16int main()
17{
18
19 cout << "\n";
20 cout << "=== FT: one-dimensional Fourier Transform (slow) ====\n";
21 cout << "\n";
22
23 // physical domain size
24 const double xMax(20);
25 // number of points/wavenumbers/frequencies
26 std::size_t N(256);
27 // nodes
28 const DenseVector<double> xNodes = Utility::uniform_node_vector(-xMax,xMax,N);
29 // the physical signal
30 OneD_Node_Mesh<D_complex> h( xNodes, 2 );
31 // two *complex* values stored across the mesh
32 for ( std::size_t i = 0; i < N; ++i ) {
33 // sum of 2 "humps" of the form 1/(1+x^2)
34 h(i,0) = 1./(1+(xNodes[i]-0)*(xNodes[i]-0))
35 + D_complex(0.,1.)/(1+(xNodes[i]-4)*(xNodes[i]-4));
36 // a simple sinusoid
37 h(i,1) = sin(xNodes[i]);
38 }
39 // original (physical) signal
40 // h.dump_gnu("./DATA/h.dat");
41
43 testft.dump_gnu("./DATA/testft.dat");
44
46 testf.dump_gnu("./DATA/testf.dat");
47
48
49
50 // Fourier transformed signal
52 // hft.dump_gnu("./DATA/hft.dat");
53
54 // shifted Fourier transformed signal
55 // this puts the spectrum in a -k_max to +k_max format
56 OneD_Node_Mesh<D_complex> hft_shifted = FT::shift(hft);
57 // hft_shifted.dump_gnu("./DATA/hft_shifted.dat");
58
59 // invert the DFT (must be done using the UNshifted spectrum)
60 // the starting (physcial)_ x-coordinate is included as
61 // an optional second argument here, otherwise the reconstruction
62 // will start at x=0.
63 OneD_Node_Mesh<D_complex> hReconstructed = FT::idft(hft,-xMax);
64 // hReconstructed.dump_gnu("./DATA/h_reconstruct.dat");
65
66 // subtract the initial and reconstructed data for both data elements
67 // which should give zero
68 for ( std::size_t i = 0; i < N; ++i ) {
69 h(i,0) -= hReconstructed(i,0);
70 h(i,1) -= hReconstructed(i,1);
71 }
72
73 // check the biggest error in the reconstruction
74 if ( max(h.max_abs(0),h.max_abs(1)) < 1.e-12 ) {
75 cout << "\033[1;32;48m * PASSED \033[0m\n";
76 return 0;
77 }
78 cout << "\033[1;31;48m * FAILED \033[0m\n";
79 cout << " Final |error| = " << h.max_abs(0) << "\n";
80 return 1;
81
82}
A spec for a collection of Fourier methods that act on Noddy containers.
A specification for a one dimensional mesh object.
int main()
Definition: FT.cpp:16
A spec for a collection of utility functions.
An DenseVector class – a dense vector object.
Definition: DenseVector.h:34
A one dimensional mesh utility object.
void dump_gnu(std::string filename, int precision=10) const
A simple method for dumping data to a file for gnuplot.
OneD_Node_Mesh< D_complex > dft(const OneD_Node_Mesh< D_complex > &f)
(Slow) DFT of the real data (x_i,f_i), i = 0, ... N-1; N must be EVEN.
Definition: FT.cpp:88
OneD_Node_Mesh< D_complex > dft_with_shift(const OneD_Node_Mesh< D_complex > &f)
A wrapper that calls the 'dft' method above followed by the 'shift' method.
Definition: FT.cpp:48
OneD_Node_Mesh< D_complex > idft_with_ishift(const OneD_Node_Mesh< D_complex > &ft, double origin=0)
A wrapper that calls the 'ishift' method above followed by the 'idft' method.
Definition: FT.cpp:68
OneD_Node_Mesh< D_complex > shift(const OneD_Node_Mesh< D_complex > &ft)
Shift the frequency spectrum obtained from dft to give positive and negative freq.
Definition: FT.cpp:15
OneD_Node_Mesh< D_complex > idft(const OneD_Node_Mesh< D_complex > &ft, double origin=0)
(Slow) Inverse DFT of the complex data (omega_i,F_i), i = 0,...,N-1; N must be EVEN.
Definition: FT.cpp:153
DenseVector< double > uniform_node_vector(const double &lower, const double &upper, const std::size_t &N)
Return a DENSE vector with the nodal points of a uniform mesh distributed between the upper/lower bou...
Definition: Utility.cpp:113
A collection of OO numerical routines aimed at simple (typical) applied problems in continuum mechani...
std::complex< double > D_complex
A complex double precision number using std::complex.
Definition: Types.h:98

© 2012

R.E. Hewitt