简体   繁体   English

在python中将时间转换为Hz

[英]converting time to Hz in python

I have a dataset consists of timestamp(ms), x , y and z. 我有一个数据集,由timestamp(ms),x,y和z组成。 I want to transform it to the frequency domain (Fourier). 我想将其转换为频域(傅立叶)。 I used numpy.fft.fft(a, n=None, axis=-1, norm=None)[source] 我用numpy.fft.fft(a, n=None, axis=-1, norm=None)[source]

my code is 我的代码是

import panda as pd
from scipy.fftpack import fft
import matplotlib.pyplot as plt

data=pd.read_csv('/home/Desktop/dataset.csv')
data = data.as_matrix()
xf=fft(data[:,3])
freq = numpy.fft.fftfreq(len(data), data[1,2] - data[0-1,2])
plt.plot(t,xf)

And I did the same for y and z. 我对y和z也做同样的事情。 Is this way correct? 这样正确吗? I am not sure the way that I used for changing time to Hz is correct or not. 我不确定将时间更改为Hz的方式是否正确。

These is 10 rows of dataset: 这是数据集的10行:

在此处输入图片说明

From numpy.fft.fftfreq 's reference : numpy.fft.fftfreq的参考

The returned float array f contains the frequency bin centers in cycles per unit of the sample spacing (with zero at the start). 返回的浮点数组f包含频率单元中心,该频率单元中心以每单位采样间隔的周期为周期(开始时为零)。 For instance, if the sample spacing is in seconds, then the frequency unit is cycles/second. 例如,如果样本间隔为秒,则频率单位为周期/秒。

Since your timestamps are in milliseconds, specifying the time spacing with data[1,2] - data[0,2] would give frequencies in units of cycles/milliseconds or kHz. 由于您的时间戳以毫秒为单位,因此使用data[1,2] - data[0,2]指定时间间隔将以周期/毫秒或kHz为单位给出频率。 To get frequencies in Hz, you should instead use: 要获得以Hz为单位的频率,您应该改用:

freq = numpy.fft.fftfreq(len(data), 0.001*(data[1,2] - data[0,2]))

Then you should plot it using freq : 然后,您应该使用freq绘制:

plt.plot(freq,xf)

As a side note for future readers, using data[1,2] - data[0,2] as the time step implicitly assumes that the data is evenly sampled in time (which seems to be the case here given the provided sample dataset). 作为未来读者的补充说明,使用data[1,2] - data[0,2]作为时间步长隐含地假设数据是在时间上均匀采样的(在这种情况下,提供的样本数据集似乎就是这种情况) 。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM