Reputation: 73
Other questions such as How to convert a mel spectrogram to log-scaled mel spectrogram have asked how to get the log-scaled mel spectrogram in python. My code below produces said spectrogram
ps = librosa.feature.melspectrogram(y=y, sr=sr)
ps_db= librosa.power_to_db(ps, ref=np.max)
librosa.display.specshow(ps_db, x_axis='s', y_axis='log')
If I plot this, I get the spectrogram I am looking for.
However, if I don't use librosa's display.specshow and just perform
import matplotlib.pyplot as plt
plt.imshow(ps_db)
I get this
My question is, what transformation is display.specshow doing to produce the first plot and how can I recreate this just using ps_db and numpy such that my plt.imshow() call aligns with display.specshow?
Upvotes: 2
Views: 8790
Reputation: 1488
As suggested in the comment, you need to change the origin to lower, change the colormap to magma (I guess; could also be "plasma"
or "inferno"
, chose here)
import matplotlib.pyplot as plt
fig, ax = plt.figure()
plt.imshow(ps_db, origin="lower", cmap=plt.get_cmap("magma"))
Regarding the logarithmic scale, as far as I can see, the data you get is already in logarithmic scale, just the ticks are wrong. If that is not the case, the you need to resample the data with a meshgrid
adapted from here:
h, w = ps_db.shape
x = np.linspace(0, 2, w)
y = np.logspace(1, 8, h)
X, Y = np.meshgrid(x,y)
Upvotes: 2