1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
|
#include <iostream>
#include <fstream>
#include <iomanip>
#include <complex>
#include <cmath>
using namespace std;
const double pi = 3.14159265358979323846;
void FFT ( int N, complex<double> *f , complex<double> *ftilde );
void iFFT( int N, complex<double> *ftilde, complex<double> *f );
void DFT ( int N, complex<double> *f , complex<double> *ftilde );
void iDFT( int N, complex<double> *ftilde, complex<double> *f );
void writeFile( string filename, int N, complex<double> *z );
//======================================================================
int main()
{
string infile = "file.txt";
string oufile = "fourier.out";
string line;
double re, im;
// First pass - count data (assumes one per line)
int N = 0;
ifstream in( infile.c_str() );
while( getline( in, line ) ) N++;
cout << "Number of points read from file: " << N << endl;
// Reposition at start
in.close();
in.open( infile.c_str() );
// Reread, filling f
complex<double> *f = new complex<double>[N];
for ( int i = 0; i < N; i++ )
{
in >> re; im = 0.0; // adjust if you have complex data
f[i] = complex<double>( re, im );
}
in.close();
// Fourier transform
complex<double> *ftilde = new complex<double>[N];
FFT( N, f, ftilde );
// Output to file
writeFile( oufile, N, ftilde );
// Tidy up
delete [] f;
delete [] ftilde;
}
//======================================================================
void FFT( int N, complex<double> *f, complex<double> *ftilde ) // fast fourier transform
{
if ( N % 2 == 0 ) // recursion if a multiple of 2
{
int N2 = N / 2;
complex<double> *even = new complex<double>[N2];
complex<double> *odd = new complex<double>[N2];
complex<double> *g = new complex<double>[N2];
complex<double> *h = new complex<double>[N2];
for ( int m = 0; m < N2; m++ )
{
even[m] = f[2*m ];
odd [m] = f[2*m+1];
}
FFT( N2, even, g );
FFT( N2, odd , h );
complex<double> w = polar( 1.0, -2.0 * pi / N );
complex<double> wn = complex<double>( 1.0 );
for ( int n = 0; n < N2; n++ )
{
int nplus = n + N2;
ftilde[n] = g[n] + wn * h[n];
ftilde[nplus] = g[n] - wn * h[n];
wn *= w;
}
delete [] even;
delete [] odd ;
delete [] g ;
delete [] h ;
}
else // otherwise just do a DFT
{
DFT( N, f, ftilde );
}
}
//======================================================================
void iFFT( int N, complex<double> *ftilde, complex<double> *f ) // inverse fast fourier transform
{
complex<double> * ftildeConjugate = new complex<double>[N];
for ( int m = 0; m < N; m++ ) ftildeConjugate[m] = conj( ftilde[m] );
FFT( N, ftildeConjugate, f );
for ( int m = 0; m < N; m++ ) f[m] = conj( f[m] ) / (double) N;
delete [] ftildeConjugate;
}
//======================================================================
void DFT( int N, complex<double> *f, complex<double> *ftilde ) // discrete Fourier transform
{
for ( int n = 0; n < N; n++ )
{
ftilde[n] = complex<double>( 0.0 );
complex<double> w = polar( 1.0, -2.0 * pi * n / N );
complex<double> wm( 1.0 );
for ( int m = 0; m < N; m++ )
{
ftilde[n] += f[m] * wm;
wm *= w;
}
}
}
//======================================================================
void iDFT( int N, complex<double> *ftilde, complex<double> *f ) // inverse discrete Fourier transform
{
complex<double> * ftildeConjugate = new complex<double>[N];
for ( int m = 0; m < N; m++ ) ftildeConjugate[m] = conj( ftilde[m] );
DFT( N, ftildeConjugate, f );
for ( int m = 0; m < N; m++ ) f[m] = conj( f[m] ) / (double) N;
delete [] ftildeConjugate;
}
//======================================================================
void writeFile( string filename, int N, complex<double> *z )
{
double freq, re, im, magsq;
ofstream out( filename.c_str() );
for ( int m = 0; m < N; m++ )
{
re = real( z[m] );
im = imag( z[m] );
magsq = re * re + im * im;
out << right
<< setw(7) << m << " "
<< scientific
<< setprecision(6) << setw(15) << re << " "
<< setprecision(6) << setw(15) << im << " "
<< setprecision(6) << setw(15) << magsq << endl;
}
out.close();
}
//======================================================================
|