如何用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()

在这里插入图片描述

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐