I have implemented a Discrete Fourier Transform function as follows (where CVector
is a simple wrapper around an array):
template <typename T, std::size_t Width>
CVector<std::complex<T>, Width> DiscreteFourierTransform( const CVector<T, Width>& vec )
{
CVector<std::complex<T>, Width> vecResult;
const std::complex<T> cmplxPrefactor( std::complex<T>( 0, -M_PI ) / (T)(Width/2) );
for( int s = 0; s < Width; ++s )
{
vecResult[s] = std::complex<T>( T( 0.0 ), T( 0.0 ) );
for( int x = 0; x < Width; ++x )
{
vecResult[s] += vec[x] * std::exp( cmplxPrefactor * (T)(x - (int)(Width/2)) * (T)(s - (int)(Width/2)) );
}
vecResult[s] /= (T)(Width);
}
return vecResult;
}
This works fine on a single top-hat function, centered in the center of the array. However, if I displace the top-hat function by -10
units, using the following bit of code:
int main()
{
CVector<double, 500> vecSlit;
for( unsigned int i = 235; i <= 245; ++i )
{
vecSlit[i] = 1.0;
}
CVector<std::complex<double>, 500> vecFourierTransform = DiscreteFourierTransform( vecSlit );
std::cout << "Saving..." << std::endl;
if( SaveList( "offset-fourier-transform.txt", vecFourierTransform ) )
{
std::cout << "Save Successful!" << std::endl;
}
else
{
std::cout << "Save Unsuccessful!" << std::endl;
}
return 0;
}
I get the following output:
Where the first plot is the amplitude and the second is the real part of the output. The amplitude looks fine, but the real part looks incorrect, does anyone have any idea why this might be?