且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用FFT计算频率错误的价值观

更新时间:2021-06-30 23:17:19

您源$ C ​​$ C几乎是罚款。唯一的问题是,你可以通过全谱,即从0通过Fs的搜索峰/ 2到fs。

Your source code is almost fine. The only problem is that you search for the peaks through the full spectrum, i.e. from 0 via Fs/2 to Fs.

对于任何实数输入信号(你有)FS / 2和FS(=采样频率)之间的频谱是0和FS / 2之间的频谱的精确镜像(我发现的这个漂亮的背景解释)。因此,对于每个频率存在两个峰, 几乎相同的幅度。我在写几乎,因为由于有限的机器precision他们不一定究竟相同。因此,你随机发现在含有奈奎斯特频率以下的频率的频谱的第一半峰值(= FS / 2)或与奈奎斯特频率以上的频率的频谱的第二半

For any real-valued input signal (which you have) the spectrum between Fs/2 and Fs (=sample frequency) is an exact mirror of the spectrum between 0 and Fs/2 (I found this nice background explanation). Thus, for each frequency there exist two peaks with almost identical amplitude. I'm writing 'almost' because due to limited machine precision they are not necessarily exactly identical. So, you randomly find the peak in the first half of the spectrum which contains the frequencies below the Nyquist frequency (=Fs/2) or in the second half of the spectrum with the frequencies above the Nyquist frequency.

如果你想自己纠正错误,停止阅读这里。否则继续:

If you want to correct the mistake yourself, stop reading here. Otherwise continue:

只需更换

for(int j=0;j<bufferSize;j++){

for(int j=0;j<=bufferSize/2;j++){

在源$ C ​​$ C你presented。

in the source code you presented.

P.S。:通常情况下,***是一个窗口函数应用到分析缓冲液(例如,汉明窗),但对自己的高峰的应用采摘它不会改变结果非常

P.S.: Typically, it is better to apply a window function to the analysis buffer (e.g. a Hamming window) but for your application of peak picking it won't change results very much.