Skip to content

Instantly share code, notes, and snippets.

@mailletf
Created April 18, 2015 16:55
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save mailletf/3484932dd29d62b36092 to your computer and use it in GitHub Desktop.
Save mailletf/3484932dd29d62b36092 to your computer and use it in GitHub Desktop.
Display a mel-scaled power spectrogram using librosa
# Mostly taken from: http://nbviewer.ipython.org/github/bmcfee/librosa/blob/master/examples/LibROSA%20demo.ipynb
import librosa
import matplotlib.pyplot as plt
# Load sound file
y, sr = librosa.load("filename.mp3")
# Let's make and display a mel-scaled power (energy-squared) spectrogram
S = librosa.feature.melspectrogram(y, sr=sr, n_mels=128)
# Convert to log scale (dB). We'll use the peak power as reference.
log_S = librosa.logamplitude(S, ref_power=np.max)
# Make a new figure
plt.figure(figsize=(12,4))
# Display the spectrogram on a mel scale
# sample rate and hop length parameters are used to render the time axis
librosa.display.specshow(log_S, sr=sr, x_axis='time', y_axis='mel')
# Put a descriptive title on the plot
plt.title('mel power spectrogram')
# draw a color bar
plt.colorbar(format='%+02.0f dB')
# Make the figure layout compact
plt.tight_layout()
@Pzoom522
Copy link

Nice work. In v6.0, librosa.logamplitude(S, ref_power=np.max) is replaced by librosa.amplitude_to_db(S, ref=np.max)

@sijinkim
Copy link

Nice work. In v6.0, librosa.logamplitude(S, ref_power=np.max) is replaced by librosa.amplitude_to_db(S, ref=np.max)

Thanks!!

@pratyushmp
Copy link

Nice work. In v6.0, librosa.logamplitude(S, ref_power=np.max) is replaced by librosa.amplitude_to_db(S, ref=np.max)

you can also use librosa.amplitude.power_to_db(S, ref = np.max)

@drobotnik
Copy link

drobotnik commented Dec 22, 2021

you also need import librosa.display instead of import librosa

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment