从 FFT 获取幅度最高的频率 [英] Get frequency with highest amplitude from FFT

查看:79
本文介绍了从 FFT 获取幅度最高的频率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 x、y、z 轴形式的原始加速度计数据,这些数据经过平滑处理,并应用了带通滤波器.现在我想将其转换为频域信号并使用 scipy.fftpack.fft 来应用 FFT.

I have raw accelerometer data in form of x,y,z axes which is smoothened and I applied a band pass filter. Now I want to convert it to frequency domain signal and using the scipy.fftpack.fft to apply FFT.

sampling_frequency = 32
def fft(acc_data):
  N = len(acc_data)

  fft_data = sp.fftpack.fft(acc_data)
  freqs = sp.fftpack.fftfreq(N)

  plt.bar(freqs, np.abs(fft_data)) 
  plt.xlabel('Frequency in Hertz [Hz]')
  plt.ylabel('Magnitude')
  plt.title('FFT')
  plt.show()

这个图没有绘制点,是空的.fft 的返回值是一个复数数组.我正在使用 fftfreq 来获得最高振幅的频率.

This figure has no points plotted and is empty. The return value of fft is a complex array. I'm using fftfreq to get the frequency of highest amplitude.

有人可以指出错误的地方或举例说明如何通过应用 FFT 获得幅度最高的频率值吗?

Can someone point where its wrong or give an example of how to get the value of frequency with the highest amplitude by applying FFT?

完整代码可在此处>

Full code of this is available here

推荐答案

我建议你远离你的代码,首先掌握执行 fft 调用并理解该调用返回的结果的能力......要么读入已知频率的正弦曲线或只是编写一个函数来填充带有浮点正弦曲线的数组(这是您的时域信号)……然后将该数组输入到 fft 调用中,该调用通常会返回给您一个新数组复数......这个新数组的每个元素现在在频域中代表一个频率值......一个频率仓......该频率的幅度可以使用

I suggest you step away from your code and first master ability to perform a fft call and make sense of the result returned from that call ... either read in a sin curve of known freq or just write a function to populate an array with a floating point sin curve ( this is your time domain signal ) ... then feed that array into a fft call which will typically return back to you a new array of complex numbers ... each element of this new array which is now in the frequency domain represents one frequency value ... a frequency bin ... the magnitude of that frequency can be calculated using

nyquist_limit_index := int(number_of_samples / 2)

curr_freq := 0.0
incr_freq := flow_data_spec.sample_rate / number_of_samples

for index, curr_complex := range complex_fft { 

    if index <= nyquist_limit_index  {

        curr_real = real(curr_complex) // pluck out real portion of imaginary number
        curr_imag = imag(curr_complex) // ditto for im

        curr_mag = 2.0 * math.Sqrt(curr_real*curr_real+curr_imag*curr_imag) / number_of_samples

        curr_theta = math.Atan2(curr_imag, curr_real) // phase shift of this freq

        curr_dftt := discrete_fft { // populate a struct of current array element

            real:      2.0 * curr_real,
            imaginary: 2.0 * curr_imag,
            magnitude: curr_mag,
            theta:     curr_theta,
        }

        //  optionally stow curr_dftt for later
    }

    curr_freq += incr_freq
}

其中 number_of_samples 只是您输入 fft 调用的时域数组的长度

where number_of_samples is just the length of your time domain array you fed into the fft call

上面的代码向您展示了如何遍历从较早的 fft 调用返回给您的复数频域数组……上面是伪代码,而不是 Python,但您的过程可能非常相似

Above code shows you how to iterate across the frequency domain array of complex numbers returned back to you from an earlier fft call ... above is pseudo code not python but your process could will be very similar

要识别具有最大振幅的频率 (curr_freq),只需跟踪上述循环中哪个 curr_freq 的幅度最大......在我们的玩具设置中,您可能很清楚源输入正弦曲线的频率,因此相同频率应该弹出作为上面最大量级的 curr_freq ......在你开始工作并且它的概念被理解之后,然后将你学到的知识应用到你手头的任务中 - 祝你好运

To identify the frequency ( curr_freq ) with the largest amplitude just keep track of which curr_freq had maximum magnitude in above loop ... In our toy setup you may well know the frequency of your source input sin curve so that same frequency should pop out as the curr_freq with the largest magnitude in above ... after you get this working and its concepts sink in then apply what you have learned to your task at hand - good luck

傅立叶分析及其各种咒语非常强大,可以打开许多扇门.这是一个需要思考的话题,但如果我们允许自己简单地将一些 api 调用插入到一起来使某些东西工作,我们确实错过了一些非常神奇的东西

Fourier Analysis and its various incantations are extremely powerful and can open many doors. Its a topic which demands thinking, yet if we allow ourselves to simply plug some api calls together to get something working we have missed something very magical indeed

这篇关于从 FFT 获取幅度最高的频率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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