1 """Python繪制語譜圖"""
2
3 # 導入相應的包
4 import matplotlib.pyplot as plt
5 import matplotlib.mlab as mlab
6 import numpy as np
7 import wave
8 import os
9
10
11 filepath = 'E:/普米/2/' # 添加路徑
12 for root, dirs, files in os.walk(filepath):
13 for i in range(len(files)):
14 label = np.array(int(files[i].split('-')[0][3:]))
15 Time = np.array(int(files[i].split('-')[1][:1]))
16
17 f = wave.open(root + "/" + files[i], 'rb') # 調用wave模塊中的open函數,打開語音文件。
18 params = f.getparams() # 得到語音參數
19 nchannels, sampwidth, framerate, nframes = params[:4] # nchannels:音頻通道數,sampwidth:每個音頻樣本的字節數,framerate:采樣率,nframes:音頻采樣點數
20 strData = f.readframes(nframes) # 讀取音頻,字符串格式
21 wavaData = np.fromstring(strData, dtype=np.int16) # 得到的數據是字符串,將字符串轉為int型
22 wavaData = wavaData * 1.0/max(abs(wavaData)) # wave幅值歸一化
23 wavaData = np.reshape(wavaData, [nframes, nchannels]).T # .T 表示轉置
24 f.close()
25
26 # 繪制語譜圖
27 spec, freqs, t = mlab.specgram(x=wavaData[0], Fs=framerate, scale_by_freq=True, mode='psd', sides='default', NFFT=320)
28 spec = 10. * np.log10(spec)
29 spec = np.flipud(spec)
30 plt.imsave('E:/prim/2/Prim{}_Time{}_Person2.jpg'.format(label, Time), spec)