C# 获取 .wav 文件的频谱线

C# get frequency spectrum lines of a .wav file

我想显示 .wav(或 .mp3)文件的频谱。 我现在卡住了一点,找不到任何好东西。 我使用带有 naudio nuget 的 C# 来处理音频数据,并使用 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);
  1. 如何获取音频文件的所有频谱点?
  2. 如何将这些“热图”线从频谱中去除?这是一个只有 FFT 的过程吗?(图片 1)

好的,最后我发现了一个很好的 nuget 来实现:

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);

线索是设置正确的强度。从嘈杂的频谱图中过滤掉响亮的语音线。