在 python 中将时间转换为 Hz
converting time to Hz in python
我有一个由时间戳(毫秒)、x、y 和 z 组成的数据集。我想将它转换为频域(傅里叶)。我用了 numpy.fft.fft(a, n=None, axis=-1, norm=None)[source]
我的密码是
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)
我对 y 和 z 也做了同样的事情。
这种方式是否正确?我不确定我将时间更改为 Hz 的方法是否正确。
这是 10 行数据集:
来自numpy.fft.fftfreq
's reference:
The returned float array f contains the frequency bin centers in cycles per unit of the sample spacing (with zero at the start). For instance, if the sample spacing is in seconds, then the frequency unit is cycles/second.
由于您的时间戳以毫秒为单位,因此使用 data[1,2] - data[0,2]
指定时间间隔将给出以 cycles/milliseconds 或 kHz 为单位的频率。要获得以赫兹为单位的频率,您应该改用:
freq = numpy.fft.fftfreq(len(data), 0.001*(data[1,2] - data[0,2]))
那么你应该使用 freq
:
绘制它
plt.plot(freq,xf)
作为给未来读者的旁注,使用 data[1,2] - data[0,2]
作为时间步长隐含地假设数据在时间上被均匀采样(鉴于提供的样本,这里似乎就是这种情况数据集).
我有一个由时间戳(毫秒)、x、y 和 z 组成的数据集。我想将它转换为频域(傅里叶)。我用了 numpy.fft.fft(a, n=None, axis=-1, norm=None)[source]
我的密码是
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)
我对 y 和 z 也做了同样的事情。 这种方式是否正确?我不确定我将时间更改为 Hz 的方法是否正确。
这是 10 行数据集:
来自numpy.fft.fftfreq
's reference:
The returned float array f contains the frequency bin centers in cycles per unit of the sample spacing (with zero at the start). For instance, if the sample spacing is in seconds, then the frequency unit is cycles/second.
由于您的时间戳以毫秒为单位,因此使用 data[1,2] - data[0,2]
指定时间间隔将给出以 cycles/milliseconds 或 kHz 为单位的频率。要获得以赫兹为单位的频率,您应该改用:
freq = numpy.fft.fftfreq(len(data), 0.001*(data[1,2] - data[0,2]))
那么你应该使用 freq
:
plt.plot(freq,xf)
作为给未来读者的旁注,使用 data[1,2] - data[0,2]
作为时间步长隐含地假设数据在时间上被均匀采样(鉴于提供的样本,这里似乎就是这种情况数据集).