简体   繁体   中英

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

frequency-domain

but as you can see, the x-axis of both images are still in samples.

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.

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.

A variation with 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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