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

查看:592
本文介绍了从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?

完整的代码可用这里

推荐答案

我建议您远离代码,并首先掌握执行代码的能力。 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

上面的代码sho请问您如何遍历从先前的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天全站免登陆