如何将波形信号分割成帧

本文关键字:分割 信号 波形 | 更新日期: 2023-09-27 18:24:09

我正在做一个关于和弦识别的项目。我正在参考别人的日记,但我对DSP领域的掌握仍然很少。在她的参考中,第一件事是我需要将wav文件中的信号分割成多个帧。在我的情况下,我需要分为每帧65毫秒,每帧2866个样本。

我已经搜索了如何将信号分割成帧,但我发现它们不够清晰,我无法理解。到目前为止,这些是我在WavProcessing类中的一些代码:

 public void SetFileName(String fileNameWithPath) //called first in the form, to get the FileStream
    {
        _fileNameWithPath = fileNameWithPath;
        strm = File.OpenRead(_fileNameWithPath);
    }
 public double getLengthTime(uint wavSize, uint sampleRate, int bitRate, int channels)  
    {
        wavTimeLength = ((strm.Length - 44) / (sampleRate * (bitRate / 8))) / channels;
        return wavTimeLength;
    }
public int getNumberOfFrames() //return number of frames, I just divided total length time with interval time between frames. (in my case, 3000ms / 65 ms = 46 frames)
    { 
        numOfFrames = (int) (wavTimeLength * 1000 / _sampleFrameTime);
        return numOfFrames; 
    }
 public int getSamplePerFrame(UInt32 sampleRate, int sampleFrameTime) // return the sample per frame value (in my case, it's 2866)
    {
        _sampleRate = sampleRate;
        _sampleFrameTime = sampleFrameTime;
        sFr = (int)(sampleRate * (sampleFrameTime / 1000.0 ));
        return sFr; 
    }

我仍然不知道如何在C#中将信号分割为每帧65毫秒。我需要拆分FileStream并将其分解为帧并将其保存到数组中吗?或者其他什么?

如何将波形信号分割成帧

使用NAudio,您可以这样做:

using (var reader = new AudioFileReader("myfile.wav"))
{
    float[] sampleBuffer = new float[2866];
    int samplesRead = reader.Read(sampleBuffer, 0, sampleBuffer.Length);
}

正如其他人所评论的那样,如果你计划将样本数传递到FFT中,那么你读取的样本数应该是2的幂。此外,如果文件是立体声的,则左右采样将交错,因此FFT需要能够处理此问题。