[python]librosa安装后测试代码plot_display

import numpy as np
import matplotlib.pyplot as plt
import librosa
y, sr = librosa.load(librosa.ex('trumpet'))
D = librosa.stft(y)  
S_db = librosa.amplitude_to_db(np.abs(D), ref=np.max)
plt.figure()
librosa.display.specshow(S_db)
plt.colorbar()
fig, ax = plt.subplots()
img = librosa.display.specshow(S_db, ax=ax)
fig.colorbar(img, ax=ax)
fig, ax = plt.subplots()
img = librosa.display.specshow(S_db, x_axis='time', y_axis='linear', ax=ax)
ax.set(title='Now with labeled axes!')
fig.colorbar(img, ax=ax, format="%+2.f dB")
fig, ax = plt.subplots()
img = librosa.display.specshow(S_db, x_axis='time', y_axis='log', ax=ax)
ax.set(title='Using a logarithmic frequency axis')
fig.colorbar(img, ax=ax, format="%+2.f dB")
fig, ax = plt.subplots()
D_highres = librosa.stft(y, hop_length=256, n_fft=4096)
S_db_hr = librosa.amplitude_to_db(np.abs(D_highres), ref=np.max)
img = librosa.display.specshow(S_db_hr, hop_length=256, x_axis='time', y_axis='log',
                               ax=ax)
ax.set(title='Higher time and frequency resolution')
fig.colorbar(img, ax=ax, format="%+2.f dB")
fig, ax = plt.subplots()
M = librosa.feature.melspectrogram(y=y, sr=sr)
M_db = librosa.power_to_db(M, ref=np.max)
img = librosa.display.specshow(M_db, y_axis='mel', x_axis='time', ax=ax)
ax.set(title='Mel spectrogram display')
fig.colorbar(img, ax=ax, format="%+2.f dB")
C = librosa.cqt(y=y, sr=sr)
C_db = librosa.amplitude_to_db(np.abs(C), ref=np.max)
fig, ax = plt.subplots()
librosa.display.specshow(C_db, y_axis='cqt_hz', x_axis='time', ax=ax)
ax.set(title='Frequency (Hz) axis decoration')
fig, ax = plt.subplots()
librosa.display.specshow(C_db, y_axis='cqt_note', x_axis='time', ax=ax)
ax.set(title='Pitch axis decoration')
chroma = librosa.feature.chroma_cqt(y=y, sr=sr)
fig, ax = plt.subplots()
img = librosa.display.specshow(chroma, y_axis='chroma', x_axis='time', ax=ax)
ax.set(title='Chromagram demonstration')
fig.colorbar(img, ax=ax)
fig, ax = plt.subplots()
img = librosa.display.specshow(chroma, y_axis='chroma', x_axis='time',
                               key='C:min', ax=ax)
ax.set(title='Chromagram explicitly in F:dorian')
fig.colorbar(img, ax=ax)
fig, ax = plt.subplots()
img = librosa.display.specshow(chroma, y_axis='chroma_h', x_axis='time',
                               Sa=5, thaat='kafi', ax=ax)
ax.set(title='Chromagram with Hindustani notation')
fig.colorbar(img, ax=ax)
fig, ax = plt.subplots()
img = librosa.display.specshow(chroma, y_axis='chroma_c', x_axis='time',
                               Sa=5, mela=22, ax=ax)
ax.set(title='Chromagram with Carnatic notation')
fig.colorbar(img, ax=ax)
Sa = librosa.note_to_hz('F4')
fig, ax = plt.subplots()
librosa.display.specshow(C_db, y_axis='cqt_svara', Sa=Sa, x_axis='time', ax=ax)
ax.set(title='Hindustani decoration',
       ylim=[Sa, 2*Sa])
fig, ax = plt.subplots()
librosa.display.specshow(C_db, y_axis='cqt_svara', Sa=Sa, mela=22, x_axis='time', ax=ax)
ax.set(title='Carnatic decoration',
       ylim=[Sa, 2*Sa])
R = librosa.segment.recurrence_matrix(chroma, mode='affinity')
fig, ax = plt.subplots()
img = librosa.display.specshow(R, y_axis='time', x_axis='time', ax=ax)
ax.set(title='Recurrence / self-similarity')
fig.colorbar(img, ax=ax)
ccov = np.cov(chroma)
fig, ax = plt.subplots()
img = librosa.display.specshow(ccov, y_axis='chroma', x_axis='chroma',
                               key='Eb:maj', ax=ax)
ax.set(title='Chroma covariance')
fig.colorbar(img, ax=ax)
fig, ax = plt.subplots()
img = librosa.display.specshow(S_db, cmap='gray_r', y_axis='log', x_axis='time', ax=ax)
ax.set(title='Inverted grayscale')
fig.colorbar(img, ax=ax, format="%+2.f dB")
max_var = np.max(np.abs(ccov))
fig, ax = plt.subplots()
img = librosa.display.specshow(ccov, vmin=-max_var, vmax=max_var,
                               y_axis='chroma', x_axis='chroma',
                               key='Eb:maj', ax=ax)
ax.set(title='Chroma covariance')
fig.colorbar(img, ax=ax)
fig, ax = plt.subplots(nrows=3, ncols=1, sharex=True)
img1 = librosa.display.specshow(S_db, x_axis='time', y_axis='log', ax=ax[0])
ax[0].set(title='STFT (log scale)')
img2 = librosa.display.specshow(M_db, x_axis='time', y_axis='mel', ax=ax[1])
ax[1].set(title='Mel')
img3 = librosa.display.specshow(chroma, x_axis='time', y_axis='chroma', 
                                key='Eb:maj', ax=ax[2])
ax[2].set(title='Chroma')
for ax_i in ax:
    ax_i.label_outer()
fig.colorbar(img1, ax=[ax[0], ax[1]])
fig.colorbar(img3, ax=[ax[2]])
ax[0].set(xlim=[1, 3])  
y, sr = librosa.load(librosa.ex('nutcracker'))
chroma = librosa.feature.chroma_cqt(y=y, sr=sr)
tempo, beats = librosa.beat.beat_track(y=y, sr=sr)
beats = librosa.util.fix_frames(beats, x_min=0)
chroma_sync = librosa.util.sync(chroma, beats, aggregate=np.median)
beat_times = librosa.frames_to_time(beats)
fig, ax = plt.subplots(nrows=2, sharex=True)
img = librosa.display.specshow(chroma, y_axis='chroma', x_axis='time', ax=ax[0],
                               key='Eb:maj')
ax[0].set(title='Uniform time sampling')
ax[0].label_outer()
librosa.display.specshow(chroma_sync, y_axis='chroma', x_axis='time',
                         x_coords=beat_times, ax=ax[1], key='Eb:maj')
ax[1].set(title='Beat-synchronous sampling')
fig.colorbar(img, ax=ax)
ax[1].set(xlim=[10, 25])

代码参考:

librosa/docs/examples/plot_display.py at main · librosa/librosa · GitHub

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: 这个错误通常是因为您使用的 librosa 版本不支持 `display` 模块。您可以尝试更新 librosa 到最新版本,或者使用其他可视化音频的库来代替 `librosa.display`。 如果您想要使用 `librosa.display`,请确保您的 librosa 版本是 0.8.0 或以上。您可以使用以下命令更新 librosa: ``` pip install --upgrade librosa ``` 如果您不想更新 librosa,则可以使用其他可视化音频的库,例如 matplotlib。您可以使用以下代码来代替 `librosa.display`: ```python import matplotlib.pyplot as plt def display_audio(audio, sr): plt.figure(figsize=(14, 5)) plt.plot(audio) plt.show() ``` 这个函数将绘制给定音频的波形图。 ### 回答2: attributeError :module 'librosa' has no attribute 'display'这个错误通常是由于librosa库版本问题引起的。 首先,确保你的librosa库已经正确安装,并且是最新版本。你可以使用以下命令升级librosa库: pip install --upgrade librosa 如果升级后仍然出现此错误,那么有可能是你的代码中使用了librosa库中已经被弃用或重命名的方法或属性。你需要检查你的代码中是否使用了display这个方法或属性。 如果你的代码中确实使用了display方法或属性,那么你可以尝试使用其他可替代的方法或属性。你可以查看librosa官方文档或搜索相关博客和问题解答平台来寻找解决方法。 如果你的代码中并没有使用display这个方法或属性,那么可能是其他部分的代码出现了错误,导致错误信息指向了错误的位置。你可以仔细检查你的代码,尤其是和librosa库相关的部分,排查其他可能的错误。 总之,attributeError :module 'librosa' has no attribute 'display'错误通常是由于librosa库版本问题或者代码中使用了被弃用或重命名的方法或属性引起的。你需要升级librosa库、检查代码中的使用情况,或者排查其他可能的错误。 ### 回答3: attributeError :module 'librosa' has no attribute 'display' 出现的原因是因为在librosa模块中没有名为'display'的属性。 Librosa是一个用于音频分析和处理的Python库,但没有内置的图形显示函数。所以当你尝试使用librosa.display时,Python会报错。 要解决这个问题,你可以考虑使用其他的图形库来显示音频数据,比如matplotlib。以下是一个示例代码,用于使用matplotlib显示音频数据: ``` import librosa import matplotlib.pyplot as plt # 读取音频文件 audio_path = 'your_audio_file.wav' audio_data, sr = librosa.load(audio_path) # 显示音频数据 plt.figure(figsize=(14, 5)) librosa.display.waveplot(audio_data, sr=sr) plt.xlabel('Time') plt.ylabel('Amplitude') plt.show() ``` 在这个例子中,我们使用librosa加载音频文件并获取音频数据和采样率。然后,使用librosa.display.waveplot函数将音频数据绘制成波形图,并使用matplotlib显示出来。 希望这个回答能够帮助到你。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

FL1623863129

你的打赏是我写文章最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值