- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to save a Librosa spectrogram plot as a specific sized image?
63 Lectures 6 hours
Librosa is a Python package that helps to analyse audio and music files. This package also helps to create music retrieval information systems. In this article, we will see how to save a Librosa spectrogram plot as an image of specific size.
Set the figure size and adjust the padding between and around the subplots..
Create a figure and a set of subplots.
Initialize three different variables, hl, hi, wi, to store samples per time in the spectrogram, height and width of the images.
Load a demo track.
Create a window, i.e., a list for audio time series..
Compute a mel-scaled spectrogram, using melspectrogram() with window and step 3 data.
Convert the power spectrogram (amplitude squared) to decibel (dB) units, using power_to_db() method..
Display the spectrogram as img (we can save it here).
Save the img using savefig().
Display the image using plt.show() method.
import numpy as np import matplotlib.pyplot as plt import librosa.display plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True fig, ax = plt.subplots() hl = 512 # number of samples per time-step in spectrogram hi = 128 # Height of image wi = 384 # Width of image # Loading demo track y, sr = librosa.load(librosa.ex('trumpet')) window = y[0:wi*hl] S = librosa.feature.melspectrogram(y=window, sr=sr, n_mels=hi, fmax=8000, hop_length=hl) S_dB = librosa.power_to_db(S, ref=np.max) img = librosa.display.specshow(S_dB, x_axis='time', y_axis='mel', sr=sr, fmax=8000, ax=ax) plt.savefig("out.png") plt.show()
- How to save an array as a grayscale image with Matplotlib/Numpy?
- How to save a plot as SVG created with ggplot2 in R?
- How to save HTML Canvas as an Image with canvas.toDataURL()?
- How to save a histogram plot in Python?
- How to save DIV as Image with HTM5 canvas to image with the extension?
- How to save a plot in pdf in R?
- How to save a canvas as PNG in Selenium?
- How to save DIV as Image with canvas2image with extension in HTML?
- How to save a plot in Seaborn with Python (Matplotlib)?
- How to plot a watermark image in Matplotlib?
- How to save a vector in R as CSV file?
- How to save a matrix as CSV file using R?
- How to save an image with matplotlib.pyplot?
- How to convert a .wav file to a spectrogram in Python3?
- How to save the plot to a numpy array in RGB format?