Search code examples
c#fftunity-game-enginenaninfinity

Magnitude to Decibel always returns NaN in C#


So my question has changed from returning Infinity to returning NaN. If your FFT is always returning Infinity this may help (http://gerrybeauregard.wordpress.com/2011/04/01/an-fft-in-c/#comment-196). So I think it is returning NaN because C# is trying to get the Square Root of a negative number HOWEVER, the number should not be negative based on the code below, because I am squaring both numbers before getting the square root (which should make them positive). The number returned is negative, however. I have tried it the inefficient of using multiple variables to get re * re and im * im and the two results added together, but the results are negative as well. Math.Abs was no good either. I have contacted the creator of the FFT Class (see my link above) and am waiting for his next reply. I took some of the code below from an AS3 version of this I did before. If I get the answer from the class creator before I get one here then I will post that. Any insight is most helpful and thank you to everyone who has helped me so far in this. I am an AS3 programmer coming to C# (because it's much more capable), so it's possible I missed something simple in my newbness. I am using Unity.

 private const uint LOGN = 11; // Log2 FFT Length

    private const uint N = 1 << (int)LOGN; // FFT Length

    private const uint BUF_LEN = N; // Audio buffer length

    public FFT2 fft; // FFT Object

    private double[] tempIm = new double[N]; // Temporary Imaginary Number array

    private double[] m_mag = new double[N/2]; // Magnitude array

    private double[] m_win = new double[N]; // Hanning Window 

    private int fftCount = 0; // How many times the FFT has been performed

    private double SCALE = (double)20/System.Math.Log(10); // used to convert magnitude from FFT to usable dB

    private double MIN_VALUE = (double)System.Double.MinValue;

...

    // Hanning analysis window
    for (int i = 0; i < N; i++) // for i < 2048
        m_win[i] = (4.0/N) * 0.5*(1-Mathf.Cos(2*Mathf.PI*i/N)); // Hanning Vector [1] = 1 / 4595889085.750801

// Perform FFT
fft.run(tempRe, tempIm);

fftCount++;

// Convert from Decibel to Magnitude
for (int i = 0; i < N/2; i++) {

double re = tempRe[i]; // get the Real FFT Number at position i
double im = tempIm[i]; // get the Imaginary FFT Number at position i

m_mag[i] = Math.Sqrt(re * re + im * im); // Convert magnitude to decibels

m_mag[i] = SCALE * Math.Log(m_mag[i] + MIN_VALUE);

if (fftCount == 50 && i == 400) print ("dB @ 399: " + m_mag[399]);
}

The prior code prints:

dB @ 400: NaN

-5.56725062513722E+33

Thank you!!


Solution

  • You defined MIN_VALUE to be System.Double.MinValue, which is the smallest possible double precision number. Adding anything other than System.Double.MaxValue or positive infinity will give a negative result. Taking the logarithm of a negative number returns NaN.

    I'm guessing you want MIN_VALUE to be the smallest positive number. In C#, you use System.Double.Epsilon for that. (I know... it shouldn't be called that...)

    Any other tiny value, like 1e-100, will work as well.