.wav文件的峰值频率 [英] Peak frequencies from .wav file

查看:190
本文介绍了.wav文件的峰值频率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个.wav文件,当我弹吉他音符时由我录制.然后,我使用下面的程序读取我的.wav文件数据.我使用了Naudio库.

I have a .wav file which recorded by me when I was playing guitar notes. Then I used below program to read my .wav file data. I used Naudio library.

AudioFileReader readertest = new AudioFileReader(@"E:\song\music.wav");
int bytesnumber = (int)readertest.Length;
var buffer = new float[bytesnumber];
readertest.Read(buffer, 0, bytesnumber);

for (int i = 0; i < buffer.Length; i++)
{
    Console.Write(buffer[i] + "\n");
}

它的输出如下.(部分输出).

it outputs like below.(part of output).

       0.00567627
       0.007659912
       0.005187988
       0.005706787
       0.005218506
       0.003051758
       0.004669189
       0.0007324219
       0.004180908
      -0.001586914
       0.00402832
      -0.003479004
       0.003143311
      -0.004577637
       0.001037598
      -0.005432129
      -0.001800537
      -0.005157471

我对此输出数据包含的内容感到困惑.我想在演奏音符的峰值频率.如何将以上数据转换为频率?

I'm confused about what this output data contains. I want to take peak frequencies where the notes are played. How can I convert the above data to frequencies?

推荐答案

您看到的数据是浮点格式的原始样本.这是代表音频信号的波形数据.发送到播放设备后,它会发出声音.

The data you are seeing is the raw samples in floating point format. This is the waveform data that represents the audio signal. When sent to the playback device it produces the sound.

要获取频率图,您将需要通过FFT函数传递样本数据块以获得基本分析,并以每个频率仓的一对值(XY)形式返回.由此可以计算出信号频率的功率电平.对于数组中的每个元素,幂函数基本上是10 * Log10(Sqrt(X*X + Y*Y)). (而且您可能从未想到过在Trig类之外使用毕达哥拉斯定理!)

To get a frequency map you will need to pass blocks of sample data through an FFT function to get the base analysis, returned as a pair of values (X and Y) for each frequency bin. From this you can calculate the power level for the frequencies in the signal. The power function is basically 10 * Log10(Sqrt(X*X + Y*Y)) for each element in the array. (And you probably never thought you'd use Pythagoras Theorem outside of Trig class!)

结果数组中的项目数与传递给FFT的项目数相同.每个值表示频率n * Fs / N,其中n是数组的偏移量,N是数组的长度,而Fs是该采样率.抽取样本的下半部分,然后使用它们.阵列上半部分的任何内容都对您毫无用处,因此请确保您的采样率足够高,以使您感兴趣的频率小于采样率的一半.

The resultant array will have the same number of items in it as you passed to the FFT. Each value represents the frequency n * Fs / N where n is the offset into the array, N is the array length and Fs is that sample rate. Take the bottom half of the samples and work with those. Anything in the top half of the array will be of no use to you, so make sure your sample rate is high enough that the frequencies you are interested in are less than half the sampling rate.

您传递给FFT的缓冲区的大小将在频率分辨率,响应时间和开窗功能的余量之间进行权衡.缓冲区太短会导致讨厌的频谱流血,并且您的频率分辨率超出窗口范围,时间过长,可能会延迟识别音调.当然,对于FFT,它必须是2的幂,因此选择正确的值可能会花费一些工作.测试各种选项,然后查看哪一种最适合您.

The size of the buffer you pass to the FFT is going to be a trade-off between frequency resolution, response time and allowance for the windowing function. Too short a buffer will get nasty spectral bleed and your frequency resolution goes out the window, too long and it can be late recognizing the tones. And of course it needs to be a power of two for the FFT, so picking the right value is probably going to take some work. Test the various options and see which one fits best for you.

Mark在 NAudioWpfDemo 示例应用程序中为FFT可视化编写了一些代码.看看 SpectrumAnalyzer 自定义控件包含幂函数(在 SpectrumAnalyzer.GetYPosLong ).还要查看 SampleAggregator 类,其中包含采样到FFT聚合代码.

Mark has written some code for FFT visualization in the NAudioWpfDemo sample application. Have a look at the SpectrumAnalyzer custom control which contains the power function (in SpectrumAnalyzer.GetYPosLong). Also look at the SampleAggregator class which contains the sample to FFT aggregation code.

这篇关于.wav文件的峰值频率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆