简体   繁体   中英

C# MathNet FFT Definition

I have some problem when testing FFT from MathNet: The idea is that if I apply FFT to the characteristic function of a gaussian variable I should find the gaussian density function.

When I plot VectorFFT the figure does seems a density function but in zero it does not have value 1, it has value 1.4689690914109.

There must be some problems with the scaling. I tried out all type of FourierOptions in Fourier.Inverse and all type of divisions/multiplications for PI, 2PI, sqrt(2PI) but nothing gives me the value 1 at the center of the density function.

Also, since various definitions of Fourier Transform and its inverse exists, I was wondering which one is implemented by MathNet, I could not find it in the documentation.

Any ideas?

public void DensityGaussian()
    {
        double eta = 0.1;   //step in discrete integral
        int pow2 = 256;     // N^2
        double mu = 0;      // centred gaussian
        double sigma = 1;   // with unitary variance

        //FFT
        double lambda = 2 * System.Math.PI / (pow2 * eta);
        double b = 0.5 * pow2 * lambda;

        Complex[] VectorToFFT = new Complex[pow2];
        for (int j = 0; j < pow2; j++)
        {
            double z = eta * j;

            if (z == 0) { z = 0.00000000000001; }

            VectorToFFT[j] = System.Numerics.Complex.Exp(new Complex(0, b * z));
            VectorToFFT[j] *= (System.Numerics.Complex.Exp(new Complex(
                              -sigma*sigma*z*z, mu * z))); //char function of gaussian
        }

        Fourier.Inverse(VectorToFFT, FourierOptions.NoScaling);

        //scaling
        for (int i = 0; i < pow2; i++)
        {
            VectorToFFT[i] /= (2 * System.Math.PI); //test
        }


        Console.WriteLine("Is density?");
        Assert.IsTrue(1 == 1);
    }

Math.NET Numerics supports all common DFT definitions, controllable with the FourierOptions flags enum. They essentially vary on the exponent and on the scaling.

The FourierOptions docs give some hints on how the options affect the effective definition, essentially:

  • InverseExponent: use a negative sign in the exponent (default uses a positive sign). A prominent implementation with a negative sign is numerical recipes.
  • AsymmetricScaling/NoScaling: instead of the default symmetric scaling sqrt(1/N) either only scale in the inverse transformation 1/N (like Matlab) or no scaling at all (like numerical recipes). Obviously, without scaling ifft(fft(x)) != x .

Maybe the answer in Calculating a density from the characteristic function using fft in R can help on the specific use case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM