简体   繁体   English

如何使用 python 将样本中的 x 轴转换为时间 (s) 和频率 (Hz)?

[英]How to convert x-axis from samples into time (s) and frequency (Hz) with python?

here I have plotted an ecg signal in time-domain and frequency-domain with fft() : time-domain在这里,我用fft()域和频域中绘制了一个心电图信号:

frequency-domain频域

but as you can see, the x-axis of both images are still in samples.但如您所见,两张图片的 x 轴仍在样本中。

I have searched some references how to convert samples into time (s) and frequency (Hz), but it fails, not only the x-axis changes but also the plot shape.我搜索了一些如何将样本转换为时间 (s) 和频率 (Hz) 的参考资料,但它失败了,不仅 x 轴发生了变化,而且 plot 形状也发生了变化。

Can you help me to solve this problem?你能帮我解决这个问题吗? Thank you.谢谢你。

The Code can look like this:代码可能如下所示:

signal, sample_rate = librosa.load(blues_1)
max_time = signal.size/sample_rate

time_steps = np.linspace(0, max_time, signal.size)
plt.figure()
plt.plot(time_steps, signal)
plt.xlabel("Time [s]")
plt.ylabel("Amplitude")

f = np.abs(np.fft.fft(signal))
freq_steps = np.fft.fftfreq(signal.size, d=1/sample_rate)
plt.figure()
plt.plot(freq_steps, f)
plt.xlabel("Frequency [Hz]")
plt.ylabel("Amplitude")

时域

频域

Understandably you can also plot only half of the values in frequency domain.可以理解,您也可以 plot 只有频域值的一半。

A variation with Scipy Scipy 的变体

sample_rate, signal = scipy.io.wavfile.read( pathname1+file)
max_time = signal.size/sample_rate
time_steps = np.linspace(0, max_time, signal.size)
plt.plot(time_steps,signal)

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

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