【问题标题】:Using Librosa to plot a mel-spectrogram使用 Librosa 绘制梅尔谱图
【发布时间】:2017-09-04 06:28:05
【问题描述】:

我在 librosa 中使用我的声音的自定义文件路径创建 mel 频谱图时遇到问题。

我正在关注此文档: https://librosa.github.io/librosa/generated/librosa.feature.melspectrogram.html

我查看了这个堆栈溢出帖子: Spectrograms generated using Librosa don't look consistent with Kaldi?

但是,这些都没有帮助我解决我的问题。

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()

有人可以告诉我如何修复此代码,以便正确显示 mel-spectrogram 并将其保存为 jpg 文件吗?谢谢!

【问题讨论】:

    标签: python spectrogram librosa


    【解决方案1】:

    哦,你的问题主要是关于如何保存为jpg? 如果只想显示图片,只需要添加一行代码: plt.show()

    如果你想保存一个 jpg,没有轴,没有白边:

    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()
    

    【讨论】:

    • 其实这个解决方案对python3不起作用,因为我无法下载scikit.audiolab。您能否更新您的答案以使其与 python 3 兼容?
    • 我使用 scikits.audiolab 因为我认为 librosa.load() 性能低下。如果你不介意,你可以使用它,或者阅读 wav 库。它的作用是只读取 wav 数据
    • 当我用 librosa.load("path to wav file") 替换 wavread 时出现错误 ValueError: not enough values to unpack (expected 3, got 2)
    • 应该是:sig, fs = librosa.load("path_to_my_wav_file"),否:sig, fs, enc = librosa.load('path_to_my_wav_file')
    • 我修改了代码以确保它在 python3 中有效
    猜你喜欢
    • 2021-07-30
    • 2020-11-27
    • 1970-01-01
    • 2018-07-19
    • 2021-10-27
    • 2019-05-24
    • 1970-01-01
    • 1970-01-01
    • 2020-06-23
    相关资源
    最近更新 更多