如何将Librosa频谱图保存为特定尺寸的图像?
Librosa是一个Python包,用于分析音频和音乐文件。此包还有助于创建音乐检索信息系统。在本文中,我们将了解如何将Librosa频谱图保存为特定大小的图像。
步骤
设置图形大小并调整子图之间和周围的填充。
创建一个图形和一组子图。
初始化三个不同的变量,**hl**、**hi**、**wi**,分别存储频谱图中的每段时间样本数、图像的高度和宽度。
加载演示曲目。
创建一个**窗口**,即音频时间序列的列表。
使用**melspectrogram()**和步骤3数据计算梅尔标度频谱图。
使用**power_to_db()**方法将功率谱图(振幅平方)转换为分贝 (dB) 单位。
将频谱图显示为**img**(我们可以在此处保存它)。
使用**savefig()**保存img。
使用**plt.show()**方法显示图像。
示例
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()输出

广告
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP