#include "vnl/algo/vnl_fft_1d.h"
#include <fstream>
int main(int argc, char * argv[] )
{
if( argc < 2 )
{
std::cerr << "Missing arguments" << std::endl;
std::cerr << "Usage: " << std::endl;
std::cerr << argv[0] << "inputFileWithPointCoordinates" << std::endl;
return EXIT_FAILURE;
}
typedef vnl_fft_1d< double > FFTCalculator;
PointsContainer::Pointer points = PointsContainer::New();
std::ifstream inputFile;
inputFile.open( argv[1] );
if( inputFile.fail() )
{
std::cerr << "Problems opening file " << argv[1] << std::endl;
}
unsigned int numberOfPoints;
inputFile >> numberOfPoints;
points->Reserve( numberOfPoints );
typedef PointsContainer::Iterator PointIterator;
PointIterator pointItr = points->Begin();
PointType point;
for( unsigned int pt=0; pt<numberOfPoints; pt++)
{
inputFile >> point[0] >> point[1];
pointItr.Value() = point;
++pointItr;
}
typedef std::complex<double> FFTCoefficientType;
typedef std::vector< FFTCoefficientType > FFTSpectrumType;
const unsigned int powerOfTwo =
(
unsigned int)
std::ceil( std::log( (
double)(numberOfPoints)) /
std::log( (double)(2.0)) );
const unsigned int spectrumSize = 1 << powerOfTwo;
FFTCalculator fftCalculator( spectrumSize );
FFTSpectrumType signal( spectrumSize );
pointItr = points->Begin();
for(unsigned int p=0; p<numberOfPoints; p++)
{
signal[p] = FFTCoefficientType( pointItr.Value()[0], pointItr.Value()[1] );
++pointItr;
}
for(unsigned int pad=numberOfPoints; pad<spectrumSize; pad++)
{
signal[pad] = 0.0;
}
std::cout << "Input to the FFT transform" << std::endl;
for(unsigned int s=0; s<spectrumSize; s++)
{
std::cout << s << " : ";
std::cout << signal[s] << std::endl;
}
fftCalculator.fwd_transform( signal );
std::cout << std::endl;
std::cout << "Result from the FFT transform" << std::endl;
for(unsigned int k=0; k<spectrumSize; k++)
{
const double real = signal[k].real();
const double imag = signal[k].imag();
const double magnitude = std::sqrt( real * real + imag * imag );
std::cout << k << " " << magnitude << std::endl;
}
return EXIT_SUCCESS;
}