python - 进行 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.
我的猜测是我的频率被四舍五入了。我试图修复它但没有成功。有什么建议么?
解决方案
有两个问题:
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)
频率和幅度数组未正确对齐,这意味着与给定频率对应的幅度在数组中的索引
Amptsx
与频率在数组中的索引不同freqs
。原因是通过使用
[:int(self.N/2) - 1]
你从末尾删除一个额外的项目,freqs
而通过使用[1:]
你从开头删除一个额外的项目Amptsx
。您应该从两个数组的末尾或开头删除一项,具体取决于哪一项在您的应用程序中具有正确的含义。
我怀疑您这样做是为了从数组的开头删除零频率的值(即传感器值中的恒定偏移量),因此在这种情况下您应该更改
[:int(self.N/2) - 1]
为[1:int(self.N/2)]
forfreqs
。argmax
为您提供中的最大值的索引Amptsx
,这不是频率。您应该使用该索引从 中获取频率值freqs
。
推荐阅读
- android - Firebase 实时数据库查询问题
- vue.js - Visual Studio Code 使用双引号格式化代码,尽管 linter 代码样式需要单引号
- azure-application-insights - 使用 ServiceBus 集成的组件的端到端事务视图
- excel - 查找和替换翻译宏
- javascript - 警报框触发后,“未定义”出现在文档一侧
- kentico - Kentico 错误文件 '/CMSModules/Chat/Controls/SupportChatHeader.ascx' 不存在
- linux - 如何从外部计算机访问 Citadel 管理界面
- r - 当数据框中存在列名“n”时使用`dplyr::tally`
- r - 如何在 flexdashboard 中正确渲染 Sankeynetwork
- javascript - 单页应用程序类列表问题