如何处理FFT的NaN结果?

时间:2014-11-24 23:58:44

标签: c# signal-processing fft

我正在尝试实现一个带有wav文件的函数,通过AForge通过FFT运行100秒的音频。当我改变偏移量以改变我通过FFT计算的音频中的位置时,有时我会得到我可以在我的图表中显示的结果,但大多数时候我得到一个复杂的NaN阵列。为什么会这样?

这是我的代码。

    public double[] test()
    {

        OpenFileDialog file = new OpenFileDialog();
        file.ShowDialog();
        WaveFileReader reader = new WaveFileReader(file.FileName);

        byte[] data = new byte[reader.Length];
        reader.Read(data, 0, data.Length);

        samepleRate = reader.WaveFormat.SampleRate;
        bitDepth = reader.WaveFormat.BitsPerSample;
        channels = reader.WaveFormat.Channels;

        Console.WriteLine("audio has " + channels + " channels, a sample rate of " + samepleRate + " and bitdepth of " + bitDepth + ".");


        float[] floats = new float[data.Length / sizeof(float)];
        Buffer.BlockCopy(data, 0, floats, 0, data.Length);

        size = 2048;

        int inputSamples = samepleRate / 100;
        int offset = samepleRate * 15 * channels;
        int y = 0;
        Complex[] complexData = new Complex[size];


        float[] window = CalcWindowFunction(inputSamples);
        for (int i = 0; i < inputSamples; i++)
        {

            complexData[y] = new Complex(floats[i * channels + offset] * window[i], 0);
            y++;
        }

        while (y < size)
        {
            complexData[y] = new Complex(0, 0);
            y++;
        }


        FourierTransform.FFT(complexData, FourierTransform.Direction.Forward);


        double[] arr = new double[complexData.Length];

        for (int i = 0; i < complexData.Length; i++)
        {
            arr[i] = complexData[i].Magnitude;
        }

        Console.Write("complete, ");

        return arr;

    }

    private float[] CalcWindowFunction(int inputSamples)
    {
        float[] arr = new float[size];
        for(int i =0; i<size;i++){

         arr[i] = 1;   

        }
        return arr;
    }

1 个答案:

答案 0 :(得分:1)

复杂的NaN阵列通常是FFT的一个输入是NaN的结果。要进行调试,您可以在FFT之前检查输入数组中的所有值,以确保它们在某个有效范围内,并给定音频输入缩放。