0

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:

Discrete Fourier Transform

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?

Thomas Russell
  • 5,870
  • 4
  • 33
  • 68
  • @Bathsheba Oh, okay then - I was expecting a cleaner Sinc function – Thomas Russell May 14 '15 at 12:06
  • 1
    look here [How to compute Discrete Fourier Transform](http://stackoverflow.com/a/26355569/2521214) and take the source code for DFT or DFFT in C++ from linked answers ... then add it to your code and compare the results ... if not the same analyze further (like compare the precomputed W ... – Spektre May 14 '15 at 14:22
  • Is your example `CVector` initialized to zeros with `CVector vecSlit;`? – renonsz Oct 15 '15 at 07:21

0 Answers0