title: Magnitude to Decibel always returns NaN in C#
body: <p>So my question has changed from returning Infinity to returning NaN.  If your FFT is always returning Infinity this may help (<a href="http://gerrybeauregard.wordpress.com/2011/04/01/an-fft-in-c/#comment-196" rel="nofollow">http://gerrybeauregard.wordpress.com/2011/04/01/an-fft-in-c/#comment-196</a>).  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.</p>
  
  <pre><code> private const uint LOGN = 11; // Log2 FFT Length
  
      private const uint N = 1 &lt;&lt; (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;
  </code></pre>
  
  <p>...</p>
  
  <pre><code>    // Hanning analysis window
      for (int i = 0; i &lt; N; i++) // for i &lt; 2048
          m_win[i] = (4.0/N) * 0.5*(1-Mathf.Cos(2*Mathf.PI*i/N)); // Hanning Vector [1] = 1 / 4595889085.750801
  </code></pre>
  
  <p>…</p>
  
  <pre><code>// Perform FFT
  fft.run(tempRe, tempIm);
  
  fftCount++;
  
  // Convert from Decibel to Magnitude
  for (int i = 0; i &lt; 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 &amp;&amp; i == 400) print ("dB @ 399: " + m_mag[399]);
  }
  </code></pre>
  
  <p>The prior code prints:</p>
  
  <pre><code>dB @ 400: NaN
  
  -5.56725062513722E+33
  </code></pre>
  
  <p>Thank you!!</p>