这段代码是一个语音信号的端点检测,利用能零比或能熵比方法进行检测,并绘制结果图像。

  1. 首先从不同的文件夹中导入所需的函数和库,包括声音基础处理('soundBase')、特征提取('end_detection')和读取音频文件的函数('wavfile')。

  2. 然后读取音频文件 'C4_1_y.wav' 并将数据归一化到 [-1, 1] 范围内。

  3. 设置端点检测所需的参数,包括帧长('wlen')、帧移('inc')、重叠长度('overlap')和能量熵比('IS')。

  4. 根据设定的参数运行端点检测函数 'vad_pro()',该函数返回语音段的起始和结束位置以及语音段的长度、静音段的长度和能量参数。

  5. 最后,根据语音段的起始和结束位置绘制语音信号的波形图和能量熵比的曲线图,同时在图像上标注语音段的起始和结束位置,并将绘制好的图像保存并展示。

注:'mode' 为选择能量熵比或能零比的模式,1 表示能零比,2 表示能熵比。

from chapter2_基础.soundBase import *
from chapter4_特征提取.end_detection import *
from scipy.io import wavfile
from chapter4_特征提取.end_detection import *

fs, data = wavfile.read('C4_1_y.wav')
data = data / 32768.0  # 将数据归一化到 [-1, 1] 范围内
IS = 0.25
wlen = 200
inc = 80
N = len(data)
time = [i / fs for i in range(N)]
wn = np.hamming(wlen)
overlap = wlen - inc
NIS = int((IS * fs - wlen) // inc + 1)

mode = 2
if mode == 1:
    thr1 = 3
    thr2 = 4
    tlabel = '能零比'
elif mode == 2:
    thr1 = 0.05
    thr2 = 0.1
    tlabel = '能熵比'

voiceseg, vsl, SF, NF, Epara = vad_pro(data, wnd, inc, NIS, thr1, thr2, mode)

fn = len(SF)
frameTime = FrameTimeC(fn, wlen, inc, fs)

plt.subplot(2, 1, 1)
plt.plot(time, data)
plt.subplot(2, 1, 2)
plt.plot(frameTime, Epara)

for i in range(vsl):
    plt.subplot(2, 1, 1)
    plt.plot([frameTime[voiceseg[i]['start']], frameTime[voiceseg[i]['start']]], [min(data), max(data)], color='black',linestyle='--')
    plt.plot([frameTime[voiceseg[i]['end']], frameTime[voiceseg[i]['end']]], [min(data), max(data)], color='red',linestyle='--')
    plt.legend(['signal', 'start', 'end'])

    plt.subplot(2, 1, 2)
    plt.plot([frameTime[voiceseg[i]['start']], frameTime[voiceseg[i]['start']]], [min(Epara), max(Epara)], color='black',linestyle='--')
    plt.plot([frameTime[voiceseg[i]['end']], frameTime[voiceseg[i]['end']]], [min(Epara), max(Epara)], color='red',linestyle='--')
    plt.legend([tlabel, 'start', 'end'])

plt.savefig('images/{}.png'.format(tlabel))
plt.show()
语音信号端点检测代码解析及可视化