所以我的问题已经从返回 Infinity 变为返回 NaN。如果您的 FFT 总是返回 Infinity,这可能会有所帮助(http://gerrybeauregard.wordpress.com/2011/04/01/an-fft-in-c/#comment-196)。所以我认为它返回 NaN 因为 C# 试图获得负数的平方根但是,根据下面的代码,这个数字不应该是负数,因为我在获得平方根之前对两个数字进行平方(这应该使它们积极的)。然而,返回的数字是负数。我已经尝试过使用多个变量来获得 re * re 和 im * im 的效率低下,并将两个结果加在一起,但结果也是否定的。Math.Abs 也不好。我已经联系了 FFT 类的创建者(见我上面的链接),正在等待他的下一个回复。我从我之前做的 AS3 版本中获取了下面的一些代码。如果我在这里得到答案之前从课程创建者那里得到答案,那么我会发布。任何见解都是最有帮助的,并感谢迄今为止在这方面帮助过我的每个人。我是一名使用 C# 的 AS3 程序员(因为它功能更强大),所以我可能错过了一些简单的新手。我正在使用统一。
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
…</p>
// 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]);
}
之前的代码打印:
dB @ 400: NaN
-5.56725062513722E+33
谢谢!!