如何从fft结果中获取频率?

编程入门 行业动态 更新时间:2024-10-09 23:19:49
本文介绍了如何从fft结果中获取频率?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我已经在 Android 手机上记录了来自麦克风的数据数组 [1024],将其通过真实数据的一维前向 DFT(将另外的 1024 位设置为 0).我将数组保存到一个文本文件中,并重复了 8 次.

I have recorded an array[1024] of data from my mic on my Android phone, passed it through a 1D forward DFT of the real data (setting a further 1024 bits to 0). I saved the array to a text file, and repeated this 8 times.

我得到了 16384 个结果.我在 Excel 中打开了文本文件并制作了一个图表来查看它的样子(x=数组索引,y=返回的数字大小).在 110、232 附近有一些巨大的尖峰(正和负),小尖峰以这种方式持续到 1817 年和 1941 年左右,尖峰再次变大,然后再次下降.

I got back 16384 results. I opened the text file in Excel and made a graph to see what it looked like(x=index of array, y=size of number returned). There are some massive spikes (both positive and negative) in magnitude around 110, 232, and small spikes continuing in that fashion until around 1817 and 1941 where the spikes get big again, then drop again.

我的问题是,无论我在哪里寻求有关该主题的帮助,它都会提到获取实数和虚数,但我只有一个一维数组,这是从 Piotr Wendykier 的课程中​​使用的方法返回的:

My problem is that wherever I look for help on the topic it mentions gettng the real and imaginary numbers, I only have a 1D array, that I got back from the method I used from Piotr Wendykier's class:

DoubleFFT_1D.realForwardFull(audioDataArray); // from the library JTransforms.

我的问题是:我需要对这些数据做什么才能返回频率?录制的声音是我在吉他的底部弦(第 5 品)上弹奏A"(大约 440Hz).

My question is: What do I need to do to this data to return a frequency? The sound recorded was me playing an 'A' on the bottom string (5th fret) of my guitar (at roughly 440Hz) .

推荐答案

复数数据是交错的,实部在偶数索引处,虚部在奇数索引处,即实部在索引 2*i,虚部位于索引 2*i+1.

The complex data is interleaved, with real components at even indices and imaginary components at odd indices, i.e. the real components are at index 2*i, the imaginary components are at index 2*i+1.

要获得索引 i 处的频谱幅度,您需要:

To get the magnitude of the spectrum at index i, you want:

re = fft[2*i]; im = fft[2*i+1]; magnitude[i] = sqrt(re*re+im*im);

然后您可以为 i = 0 到 N/2 绘制幅度 [i] 以获得功率谱.根据音频输入的性质,您应该会在频谱中看到一个或多个峰值.

Then you can plot magnitude[i] for i = 0 to N / 2 to get the power spectrum. Depending on the nature of your audio input you should see one or more peaks in the spectrum.

要获得任何给定峰值的近似频率,您可以按如下方式转换峰值的索引:

To get the approximate frequency of any given peak you can convert the index of the peak as follows:

freq = i * Fs / N;

哪里:

freq = frequency in Hz i = index of peak Fs = sample rate in Hz (e.g. 44100 Hz, or whatever you are using) N = size of FFT (e.g. 1024 in your case)

注意:如果您之前没有对时域应用合适的窗函数输入数据然后你会得到一定量的频谱泄漏,功率谱看起来相当";涂抹".

Note: if you have not previously applied a suitable window function to the time-domain input data then you will get a certain amount of spectral leakage and the power spectrum will look rather "smeared".

为了进一步扩展,这里是一个完整示例的伪代码,我们获取音频数据并识别最大峰值的频率:

To expand on this further, here is pseudo-code for a complete example where we take audio data and identify the frequency of the largest peak:

N = 1024 // size of FFT and sample window Fs = 44100 // sample rate = 44.1 kHz data[N] // input PCM data buffer fft[N * 2] // FFT complex buffer (interleaved real/imag) magnitude[N / 2] // power spectrum // capture audio in data[] buffer // ... // apply window function to data[] // ... // copy real input data to complex FFT buffer for i = 0 to N - 1 fft[2*i] = data[i] fft[2*i+1] = 0 // perform in-place complex-to-complex FFT on fft[] buffer // ... // calculate power spectrum (magnitude) values from fft[] for i = 0 to N / 2 - 1 re = fft[2*i] im = fft[2*i+1] magnitude[i] = sqrt(re*re+im*im) // find largest peak in power spectrum max_magnitude = -INF max_index = -1 for i = 0 to N / 2 - 1 if magnitude[i] > max_magnitude max_magnitude = magnitude[i] max_index = i // convert index of largest peak to frequency freq = max_index * Fs / N

更多推荐

如何从fft结果中获取频率?

本文发布于:2023-11-24 11:55:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1625104.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:频率   fft

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!