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)