【问题标题】:Get frequency with highest amplitude from FFT从 FFT 中获取幅度最高的频率
【发布时间】:2019-09-05 23:13:12
【问题描述】:

我有 x、y、z 轴形式的原始加速度计数据,这些数据经过平滑处理,并应用了带通滤波器。现在我想将其转换为频域信号并使用scipy.fftpack.fft 来应用 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 来获得最高振幅的频率。

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

完整代码可在here获取

【问题讨论】:

    标签: python scipy fft accelerometer heartrate


    【解决方案1】:

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

    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 调用的时域数组的长度

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-05-04
      • 1970-01-01
      • 1970-01-01
      • 2011-07-16
      • 2012-05-05
      • 2011-08-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多