我想显示.wav(或.mp3)文件的频谱。 我现在卡住了一点,找不到任何好的东西。 我将C#与naudio nuget一起使用来处理音频数据,并使用oxyplot显示点(和图形)。
初始化:
AudioFileReader fileStream;
fileStream = new AudioFileReader(fileName);
private static List<List<double>> spec_data;
spec_data = new List<List<double>>();
private static List<short> unanalyzed_values = new List<short>();
音频阅读:
//8192, 4096, 2048, 1024
BUFFERSIZE = 4096;
var buffer = new byte[BUFFERSIZE];
int bytes_read = fileStream.Read(buffer, 0, buffer.Length);
int BYTES_PER_POINT = fileStream.WaveFormat.BitsPerSample / 8; //8Bit = 1Byte
for (int n = 0; n < BYTES_PER_POINT; n ++)
{
short[] values = new short[buffer.Length / BYTES_PER_POINT];
for (int i = 0; i < bytes_read; i += BYTES_PER_POINT)
{
//each byte become one value
values[i / BYTES_PER_POINT] = (short)((buffer[i + 1] << 8) | buffer[i + 0]);
}
unanalyzed_values.AddRange(values);
}
大块分析:
// insert new data to the right-most (newest) position
List<double> new_data = new List<double>();
Complex[] fft_buffer = new Complex[BUFFERSIZE];
for (int i = 0; i < BUFFERSIZE; i++)
{
fft_buffer[i].X = (float)(unanalyzed_values[i] * FastFourierTransform.HammingWindow(i, BUFFERSIZE));
fft_buffer[i].Y = 0;
}
FastFourierTransform.FFT(true, (int)Math.Log(BUFFERSIZE, 2.0), fft_buffer);
for (int i = 0; i < fft_buffer.Length -1; i++)
{
double val;
val = (double)fft_buffer[i].X + (double)fft_buffer[i].Y;
val = Math.Abs(val);
new_data.Add(val);
}
new_data.Reverse();
spec_data.Insert(spec_data.Count, new_data);
当我插入所有spec_data [col] [row]点时,我只有一个col充满了数据(图1) 我使用与图1中的家伙完全相同的.wav进行分析。
答案 0 :(得分:1)
好,最后我发现了一个很好的小窍门:
https://github.com/swharden/Spectrogram
https://www.nuget.org/packages/Spectrogram/
var spec = new Spectrogram.Spectrogram(
sampleRate: 44100,
fftSize: 4096, //resolution size for the values
step: 200);
float[] values = Spectrogram.Tools.ReadWav(fileName);
spec.AddExtend(values);
// convert FFT to an image
bmp = spec.GetBitmap(
intensity: 0.5, //loudness filter
freqHigh: 200,
freqLow: 50,
showTicks: true,
colormap: Spectrogram.Colormap.grayscale);
提示是设置正确的强度。可以将嘈杂的声音线从嘈杂的频谱图中滤除。