【发布时间】:2014-11-24 23:58:44
【问题描述】:
我正在尝试实现一个函数,该函数采用 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;
}
【问题讨论】:
-
扔掉吧,没有意义。
-
但是出现这个问题的时候FFT出来的结果都是Nan,我需要真实的结果。
-
@hotpaw2 如果我通过说除以某个数字来缩放所有输入,如果我要在之后转换回时域(通过乘法放大),这会对音频产生重大影响跨度>
标签: c# signal-processing fft