首页 > 解决方案 > 进行 FFT 时提取主频返回错误的频率

问题描述

我将加速度计传感器数据(时间、x 轴、y 轴和 z 轴)保存在 csv 文件中。我正在尝试从每个轴获取 FFT。我的图表如下:

在此处输入图像描述

现在,我想从每个 FFT 图中提取主频率并获得其相应的幅度。经过一番研究,我想出了代码:

def Freq(self):
    
    freqs = arange(1, self.N, 1)[:int(self.N/2) - 1]
    Amptsx = (2/self.N)* abs( fft(self.fx)[:int(self.N/2)] )[1:]
    Amptsy = (2/self.N)* abs( fft(self.fy)[:int(self.N/2)] )[1:]
    Amptsz = (2/self.N)* abs( fft(self.fz)[:int(self.N/2)] )[1:]
    
    
    print 'The highest frequency in the x axis is:', round(np.argmax(Amptsx),6)
    print 'The highest frequency in the y axis is:', round(np.argmax(Amptsy),6)
    print 'The highest frequency in the z axis is:', round(np.argmax(Amptsz),6)
    
    print 'The highest amplitude in the x axis is:', round(np.max(Amptsx),6) 
    print 'The highest amplitude in the y axis is:', round(np.max(Amptsy),6)
    print 'The highest amplitude in the z axis is:', round(np.max(Amptsz),6) 
    
    return freqs, Amptsx, Amptsy, Amptsz

我的幅度结果是准确的,但频率不是。我的结果如下:

The highest frequency in the x axis is: 0.0.
The highest frequency in the y axis is: 1.0.
The highest frequency in the z axis is: 15.0.
The highest amplitude in the x axis is: 0.768894.
The highest amplitude in the y axis is: 0.59046.
The highest amplitude in the z axis is: 0.3679.

我的猜测是我的频率被四舍五入了。我试图修复它但没有成功。有什么建议么?

标签: pythonfftfrequency

解决方案


有两个问题:

freqs = arange(1, self.N, 1)[:int(self.N/2) - 1]
Amptsx = (2/self.N)* abs( fft(self.fx)[:int(self.N/2)] )[1:]
print 'The highest frequency in the x axis is:', round(np.argmax(Amptsx),6)
  1. 频率和幅度数组未正确对齐,这意味着与给定频率对应的幅度在数组中的索引Amptsx与频率在数组中的索引不同freqs

    原因是通过使用[:int(self.N/2) - 1]你从末尾删除一个额外的项目,freqs而通过使用[1:]你从开头删除一个额外的项目Amptsx

    您应该从两个数组的末尾或开头删除一项,具体取决于哪一项在您的应用程序中具有正确的含义。

    我怀疑您这样做是为了从数组的开头删除零频率的值(即传感器值中的恒定偏移量),因此在这种情况下您应该更改[:int(self.N/2) - 1][1:int(self.N/2)]for freqs

  2. argmax为您提供中的最大值的索引Amptsx,这不是频率。您应该使用该索引从 中获取频率值freqs


推荐阅读