如何用python画出语谱图(spectrogram)和mel谱图(mel spectrogram)
如何用python画出语谱图(spectrogram)和mel谱图(mel spectrogram)1.准备环境①python②libsora③matplotlibNotes:pip install 直接一步到位2.具体代码①语谱图(spectrogram)import librosaimport numpy as npimport matplotlib.pyplot as pltpath = "
·
如何用python画出语谱图(spectrogram)和mel谱图(mel spectrogram)
1.准备环境
①python
②libsora
③matplotlib
Notes:pip install 直接一步到位
2.具体代码
①语谱图(spectrogram)
import librosa
import numpy as np
import matplotlib.pyplot as plt
path = "./test.wav"
# sr=None声音保持原采样频率, mono=False声音保持原通道数
data, fs = librosa.load(path, sr=None, mono=False)
L = len(data)
print('Time:', L / fs)
#0.025s
framelength = 0.025
#NFFT点数=0.025*fs
framesize = int(framelength * fs)
print("NFFT:", framesize)
#画语谱图
plt.specgram(data, NFFT=framesize, Fs=fs, window=np.hanning(M=framesize))
plt.ylabel('Frequency')
plt.xlabel('Time(s)')
plt.title('Spectrogram')
plt.show()
②mel谱图
import librosa.display
import numpy as np
import matplotlib.pyplot as plt
path = "./test.wav"
# sr=None声音保持原采样频率, mono=False声音保持原通道数
data, fs = librosa.load(path, sr=None, mono=False)
L = len(data)
print('Time:', L / fs)
# 0.025s
framelength = 0.025
# NFFT点数=0.025*fs
framesize = int(framelength * fs)
print("NFFT:", framesize)
#提取mel特征
mel_spect = librosa.feature.melspectrogram(data, sr=fs, n_fft=framesize)
#转化为log形式
mel_spect = librosa.power_to_db(mel_spect, ref=np.max)
#画mel谱图
librosa.display.specshow(mel_spect, sr=fs, x_axis='time', y_axis='mel')
plt.ylabel('Mel Frequency')
plt.xlabel('Time(s)')
plt.title('Mel Spectrogram')
plt.show()
更多推荐
已为社区贡献6条内容
所有评论(0)