4

I am having trouble creating a mel-spectrogram in librosa using a custom file path to my sound.

I am following this documentation: https://librosa.github.io/librosa/generated/librosa.feature.melspectrogram.html

And I have looked at this stack overflow post: Spectrograms generated using Librosa don't look consistent with Kaldi?

However none of this helped me solve my issue.

import librosa
y, sr = librosa.load("path_to_my_wav_file")
librosa.feature.melspectrogram(y=y, sr=sr)
import matplotlib.pyplot as plt
plt.figure(figsize=(10, 4))
librosa.display.specshow(librosa.power_to_db(y,                                              
ref=np.max), y_axis='mel', fmax=8000, x_axis='time')
plt.colorbar(format='%+2.0f dB')
plt.title('Mel spectrogram')
plt.tight_layout()

Can someone tell me how to fix this code so that it properly displays and saves the mel-spectrogram to jpg file? Thanks!

molbdnilo
  • 64,751
  • 3
  • 43
  • 82
Sreehari R
  • 919
  • 4
  • 11
  • 21

1 Answers1

14

oh, Your question is mainly about how to save it as jpg? If you just want to display pictures,You just need to add a line of code: plt.show()

if you want save a jpg, no axis, no white edge:

import os
import matplotlib
matplotlib.use('Agg') # No pictures displayed 
import pylab
import librosa
import librosa.display
import numpy as np

sig, fs = librosa.load('path_to_my_wav_file')   
# make pictures name 
save_path = 'test.jpg'

pylab.axis('off') # no axis
pylab.axes([0., 0., 1., 1.], frameon=False, xticks=[], yticks=[]) # Remove the white edge
S = librosa.feature.melspectrogram(y=sig, sr=fs)
librosa.display.specshow(librosa.power_to_db(S, ref=np.max))
pylab.savefig(save_path, bbox_inches=None, pad_inches=0)
pylab.close()
zxf
  • 156
  • 1
  • 4
  • Actually, this solution doesn't work for python3 because I can't download scikit.audiolab. Can you please update your answer to make it python 3 compatible? – Sreehari R Sep 10 '17 at 06:37
  • I use scikits.audiolab because I think librosa.load() low performance. If you don't mind, you can use it, or else read the wav Library. Its does is to read only the wav data – zxf Sep 10 '17 at 07:56
  • When I replace wavread with librosa.load("path to wav file") I get the error ValueError: not enough values to unpack (expected 3, got 2) – Sreehari R Sep 10 '17 at 23:54
  • Should be: sig, fs = librosa.load("path_to_my_wav_file"), no: sig, fs, enc = librosa.load('path_to_my_wav_file') – zxf Sep 11 '17 at 03:32
  • I modified the code to make sure it works, in python3 – zxf Sep 11 '17 at 03:40
  • how to modify code , to generate spectrogram for all files in a folder and save them to a folder as .pngs – kRazzy R Feb 08 '18 at 21:53
  • you can also use `matplotlib.pyplot.savefig(path)` https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.pyplot.savefig.html – Mohammed Jul 24 '20 at 14:50