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)
#归一化
data = data * 1.0 / max(data)
#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()
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)
# 归一化
data = data * 1.0 / max(data)
# 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()