如何将波信号分成帧

发布于 2025-01-04 12:42:50 字数 1323 浏览 1 评论 0原文

我正在做一个关于和弦识别的项目。我正在使用某人的日记作为参考,但我对 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 并将它们分成帧并将它们保存到数组中?还是其他什么?

I'm working a project about chord recognition. I'm using someone's journal as a reference but I still have little grasp in field of DSP. In her reference, first thing is I need to split the signal from wav file into number of frames. In my case, I need to split into 65 ms each frame, with 2866 sample per frame.

I have searched how to split signal into frames but I don't find them clear enough for me to understand.
So far these are some of my codes in WavProcessing class:

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

I just still don't get the idea how to split the signal into 65 ms per frame in C#.
Do I need to split the FileStream and break them into frames and save them into array? Or anything else?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

杀お生予夺 2025-01-11 12:42:50

使用 NAudio,您可以这样做:

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

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

with NAudio you would do it like this:

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

As others have commented, the number of samples you read ought to be a power of 2 if you plan to pass it into an FFT. Also, if the file is stereo, you will have left and right samples interleaved, so your FFT will need to be able to cope with this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文