user21064805
user21064805

Reputation: 23

Specgram function in python not able to plot logarithmic y axis?

I am plotting the spectrogram for the electric field data. However the plot does not give the correct logarithmic y axis.

I have tried using specgram function in both matplotlib as well as scipy signal. However I am unable to get the correct logarithmic axis. I have also tried using librosa.

fig3, ax3 = plt.subplots()
# Compute the spectrogram
f, t, Sxx = signal.spectrogram(e_mfa_new[:, 2], fs)
Sxx_db = 10 * np.log10(Sxx)
# Plot the spectrogram
plt.pcolormesh(t, f, Sxx_db)
plt.yscale('log')
plt.colorbar()
plt.ylabel('Frequency [Hz]')
plt.xlabel('Time [sec]')
plt.show()

The plot I got is shown here enter image description here

# Compute STFT
S = librosa.stft(e_mfa_new[:, 2])

# Convert amplitude to dB
S_db = librosa.amplitude_to_db(abs(S), ref=np.max)

# Plot spectrogram
fig, ax = plt.subplots()
img = librosa.display.specshow(S_db, x_axis='time', y_axis='log', ax=ax)
fig.colorbar(img, ax=ax)
# plt.title('Spectrogram')
plt.show()

enter image description here

If I am setting the y scale as log I get something of this form

enter image description here

The correct plot should of this form

enter image description here

Upvotes: 0

Views: 16

Answers (0)

Related Questions