Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ExampleFFT.cpp
1 /*
2 This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4 Copyright (C) 2003-2009 (see file CONTACT for details)
5  Multimediale Systeme der Informationsverarbeitung
6  Institut fuer Informatik
7  Christian-Albrechts-Universitaet Kiel
8 
9 
10 BIAS is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
14 
15 BIAS is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU Lesser General Public License for more details.
19 
20 You should have received a copy of the GNU Lesser General Public License
21 along with BIAS; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24 
25 /** @example ExampleFFT.cpp
26  @relates FFT
27  @ingroup g_examples
28  @brief Example for FFT usage
29  @author MIP
30 */
31 
32 
33 
34 #include <MathAlgo/FFT.hh>
35 #include <Base/Math/Random.hh>
36 #include <iostream>
37 #include <iomanip>
38 
39 using namespace BIAS;
40 using namespace std;
41 
42 int main(int argc, char *argv[])
43 {
44  FFT fft;
45  Random ran;
46  bool debug=false;
47 
48  int data_length=100;
49  // generate some data by multiplying some sin of random freq.
50  int num_sin=5;
51  double const_offset=0.0;
52  double *sin_freq=new double[num_sin], *phase_offs=new double[num_sin];
53  ran.GetUniformDistributed(0.0, 0.5, num_sin, sin_freq);
54  ran.GetUniformDistributed(0.0, 2.0*M_PI, num_sin, phase_offs);
55 
56  cout << "#using sin waves with freq and phase offset : "<<endl;
57  for (int i=0; i<num_sin; i++)
58  cout <<"#freq: "<< sin_freq[i]<<" \tphase offset: "<<phase_offs[i]<<endl;
59 
60  // initialize FFT
61  fft.Init(data_length);
62 
63  // now generate data
64  double *in=fft.GetInput();
65  double *p=in;
66  for (int l=0; l<data_length; l++, p++){
67  *p=const_offset;
68  for (int i=0; i<num_sin; i++)
69  *p+=sin(2*M_PI*sin_freq[i]*l+phase_offs[i]);
70  if (debug)
71  cout <<"sample "<< setw(4) << l <<" : "<<setw(15)<<*p<<endl;
72  }
73 
74  // now calculate fft
75  fft.Compute();
76 
77  // now get result
78  int res_length=(data_length>>1)+1;
79  double *phase=new double[res_length], *mag=new double[res_length];
80 
81  fft.GetPhase(phase);
82  fft.GetMagnitude(mag);
83 
84  cout << "#result of fft: "<<endl;
85  cout <<setw(4)<<"#num"<<setw(15)<<"freq"<<setw(15)<<"mag"<<setw(15)
86  <<"phase"<<endl;
87  for (int i=0; i<res_length; i++){
88  cout <<setw(4)<<i
89  <<setw(15)<<(double)i/(double)data_length
90  <<setw(15)<<mag[i]
91  <<setw(15)<<phase[i]<<"\n";
92  }
93 
94  if (argc>0)
95  cout << "# pipe outputt into file: "<<argv[0]<<" > tmp.dat"<<endl
96  << "# and view with gnuplot : echo \"plot \\\"tmp.dat\\\" u 2:3 t \\\"magnitude\\\" w l, \\\"tmp.dat\\\" u 2:4 t \\\"phase\\\" w l; pause 99999;\" |gnuplot"<<endl;
97 
98  // or easier
99  fft.DumpResult();
100 
101  delete[] sin_freq;
102  delete[] phase_offs;
103  delete[] phase;
104  delete[] mag;
105 
106  return 0;
107 }
void GetMagnitude(double *mag)
returns the resulting magnitude, mag must be of length size/2+1 (rounded downwards) ...
Definition: FFT.cpp:105
void Compute(double *in)
slow, memcopys in to internal input data
Definition: FFT.cpp:77
double GetUniformDistributed(const double min, const double max)
on succesive calls return uniform distributed random variable between min and max ...
Definition: Random.hh:84
Wrapper to the fftw3 library.
Definition: FFT.hh:57
double * GetInput()
returns the internal data.
Definition: FFT.hh:72
void Init(int size)
initializes the internal variables.
Definition: FFT.cpp:49
void DumpResult(std::ostream &os=std::cout)
Definition: FFT.cpp:138
void GetPhase(double *phase)
returns the resulting phase, phase must be of length size/2+1 (rounded downwards) ...
Definition: FFT.cpp:94
class for producing random numbers from different distributions
Definition: Random.hh:51