#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;
}
using FFTCalculator = vnl_fft_1d< double >;
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 );
using PointIterator = PointsContainer::Iterator;
PointIterator pointItr = points->Begin();
for( unsigned int pt=0; pt<numberOfPoints; pt++)
{
inputFile >> point[0] >> point[1];
pointItr.Value() = point;
++pointItr;
}
using FFTCoefficientType = std::complex<double>;
using FFTSpectrumType = std::vector< FFTCoefficientType >;
const auto 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;
}