C# 如何处理FFT的NaN结果?
我正在尝试实现一个函数,它接收一个wav文件,通过FFT由一个RGE运行百分之一秒的音频。当我改变偏移量以改变通过FFT计算的音频中的位置时,有时我会得到可以在图形中显示的结果,但大多数时候我会得到一个复杂的NaN数组。为什么会这样 这是我的密码C# 如何处理FFT的NaN结果?,c#,signal-processing,fft,C#,Signal Processing,Fft,我正在尝试实现一个函数,它接收一个wav文件,通过FFT由一个RGE运行百分之一秒的音频。当我改变偏移量以改变通过FFT计算的音频中的位置时,有时我会得到可以在图形中显示的结果,但大多数时候我会得到一个复杂的NaN数组。为什么会这样 这是我的密码 public double[] test() { OpenFileDialog file = new OpenFileDialog(); file.ShowDialog(); WaveF
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;
}
公共双[]测试()
{
OpenFileDialog文件=新建OpenFileDialog();
ShowDialog()文件;
WaveFileReader=新的WaveFileReader(file.FileName);
字节[]数据=新字节[reader.Length];
reader.Read(数据,0,数据长度);
samepleRate=reader.WaveFormat.SampleRate;
bitDepth=reader.WaveFormat.BitsPerSample;
通道=reader.WaveFormat.channels;
Console.WriteLine(“音频有“+通道+”通道,采样率为“+samepleRate+”,比特深度为“+bitdepth+”);
float[]floats=新的float[data.Length/sizeof(float)];
BlockCopy(数据,0,浮点,0,数据.长度);
尺寸=2048;
int inputSamples=samepleRate/100;
int offset=samepleRate*15*通道;
int y=0;
复杂[]复杂数据=新的复杂[大小];
float[]窗口=CalcWindowFunction(inputSamples);
对于(int i=0;i