1.设计GUI
??命令行输入下面的命令打开GUI设计界面。
guide
??以下面的语言识别系统为例,下面为效果图。
2.回调函数
??CreateFcn和Callback区别,CreateFcn是在建立这个对象控件时就触发,Callback是在点击或者按下或者选中时才触发的回调函数。 ??1.初始化GUI界面函数。
function dtReSystem_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
set(handles.arSystem, 'Name', 'DTW连续数字语音识别系统');
handles.ncoeff = 12; %MFCC参数阶数
handles.N = 10; %10个数字
handles.Word = char('0','1','2','3','4','5','6','7','8','9');
handles.reInfo = []; %表格数据
guidata(hObject, handles);
??2.训练语音采集回调函数。
function trAuRecoding_Callback(hObject, eventdata, handles)
delete(allchild(handles.originalAudio));
delete(allchild(handles.analysisAudio));
axes(handles.originalAudio);title('');
axes(handles.analysisAudio);title('');
set(handles.digReResultText, 'String', '');
set(handles.controlPanel, 'Title', '训练语音采集');
set(handles.originalAudio, 'Visible', 'off');
set(handles.analysisAudio, 'Visible', 'off');
set(handles.startTrRecoding, 'Visible', 'on');
set(handles.recodingInfo, 'Visible', 'on');
set(handles.exitRecoding, 'Visible', 'on');
set(handles.trAuRecoding, 'Enable', 'off');
set(handles.teAuRecoding, 'Enable', 'off');
set(handles.auRecording, 'Enable', 'off');
set(handles.endpointCheck, 'Enable', 'off');
set(handles.extractMfcc, 'Enable', 'off');
set(handles.auRecording, 'Enable', 'off');
set(handles.trainModel, 'Enable', 'off');
set(handles.extractMfcc, 'Enable', 'off');
set(handles.digRecognition, 'Enable', 'off');
??3.开始采集训练语音。
function startTrRecoding_Callback(hObject, eventdata, handles)
set(handles.startTrRecoding, 'Enable', 'off');
set(handles.exitRecoding, 'Enable', 'off');
[allPaths, count] = getAllPath();
i = 1;
while i <= 10
fs = 16000; %采样频率
duration = 2; %时间长度
set(handles.recodingInfo, 'String', ...
['请按空格开始录音(' num2str(duration) 's):' num2str(i-1)])
pause
set(handles.recodingInfo, 'String', '记录中......')
speech = audiorecorder(fs,16,1);
recordblocking(speech,duration); % duration*fs 为采样点数
mypeechIn = getaudiodata(speech);
set(handles.recodingInfo, 'String', '完成! 1s后开始播放刚刚的录音。');
pause(1)
play(speech, fs);
figure(1);
plot(mypeechIn);
button = questdlg(['是否保存' num2str(i-1) '的语音'], '保存', '是', '否', '是');
if button == '是'
audiowrite([allPaths{1,i} '\' num2str(count(i)+1) '.wav'], mypeechIn, fs);
i = i + 1;
else
continue
end
end
set(handles.recodingInfo, 'String', '采集完毕!');
set(handles.startTrRecoding, 'Enable', 'on');
set(handles.exitRecoding, 'Enable', 'on');
??4.选择框回调函数。
function choseReTime_Callback(hObject, eventdata, handles)
handles.teReTime = get(handles.choseReTime, 'Value');
guidata(hObject, handles);
function choseReTime_CreateFcn(hObject, eventdata, handles)
handles.teReTime = 1;
guidata(hObject, handles);
% isequal是判断相等函数,括号里判断的是当前对象的背景色和ui界面的默认的背景色(一般为灰色)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
% 设置当前对象背景色为白色
set(hObject,'BackgroundColor','white');
end
??5.选择测试语音文件函数。
function auRecording_Callback(hObject, eventdata, handles)
set(handles.digReResultText, 'String', '');
[wavName, wavPath] = uigetfile({'*.wav';'*.*'},'选择测试语音',[pwd '\TestSpeechData']);
if ~wavName
errordlg('未选择测试文件!');
else
??6.语音识别函数。
function digRecognition_Callback(hObject, eventdata, handles)
reResult = [];
if ~isfield(handles, 'auDigInfo')
errordlg('请先选择待识别语音信号');
elseif ~isfield(handles, 'digReResult')
errordlg('请先进行端点检测');
else
reWaitFh = waitbar(0, '系统正在识别中...');
for i = 1:length(handles.digReResult)
speechIn = my_vad(handles.digReResult{i}); %端点检测
rMatrix1 = mfccf(handles.ncoeff, speechIn, handles.auDigInfo.fs); %采用MFCC系数作为特征矢量
rMatrix = CMN(rMatrix1); %归一化处理
Sco = DTWScores(rMatrix, handles.N); %计算DTW值
[~, EIndex] = sort(Sco', 2); %按行递增排序,并返回对应的原始次序
Nbr = EIndex(:,1:2); %得到每个模板匹配的2个最低值对应的次序
[Modal,Freq] = mode(Nbr(:));
if mean(abs(speechIn)) < 0.01
msg = '语音无效';
elseif (Freq <2) %频率太低不确定
msg = '不能识别';
else
msg = handles.Word(Modal,:);
end
reResult = [reResult msg];
waitbar(i/length(handles.digReResult));
end
close(reWaitFh);
set(handles.digReResultText, 'String', ['识别结果:' reResult]);
count = 0;
for index = 1:length(handles.digReResult)
if reResult(index) == handles.auDigInfo.label(index)
count = count + 1;
end
end
accuracy = [num2str(count / length(handles.digReResult) * 100,'%.2f') '%'];
handles.reInfo = [handles.reInfo; {handles.auDigInfo.label},{reResult},{accuracy}];
set(handles.reInfoTable, 'Data', handles.reInfo);
guidata(hObject, handles);
end
??7.退出系统回调函数。
function arSystem_CloseRequestFcn(hObject, eventdata, handles)
button1 = questdlg('是否退出系统', '退出系统', '是', '否', '是');
if(button1 == '是')
delete(hObject);
end
3.对象显示
??1.axes对象显示图片。
img = imread('./picture.png');
axes(handles.axes1);
imshow(img);
??2.表格显示。
handles.reInfo = [handles.reInfo; {handles.auDigInfo.label},{reResult},{accuracy}];
set(handles.reInfoTable, 'Data', handles.reInfo);
guidata(hObject, handles);
4.简单弹窗
??1.错误弹窗。
errordlg('请选择采集时间!');
??2.提示信息。
msgbox('选择成功');
??3.确认弹窗。
button = questdlg(['是否保存' num2str(i-1) '的语音'], '保存', '是', '否', '是');
??4.进度条弹窗。
for i=1:5
waitbar(i/5);
end
close(reWaitFh);
??5.警告对话框
warndlg('警告');
??6.输入对话框
ret = inputdlg({'请输入姓名','请输入性别'},'输入');
% 带有默认值
ret = inputdlg({'请输入姓名','请输入性别'},'例子',1,{'llyy', '男'}, 'on')
??7.获得目录对话框
udirs = uigetdir('C:\','浏览');
??8.列表选择对话框
[Sel, OK] = listdlg(...
'ListString', {'A', 'B', 'C', 'D'}, ...
'OKString', '确定', ...
'CancelString', '取消', ...
'name', '选择', ...
'SelectionMode', 'single');
5.倒计时设计
??开始倒计时按钮回调函数。
function control_start_Callback(hObject, eventdata, handles)
val = 59;
show_rate = get(handles.popupmenuRate, 'String');
show_rate = round(str2double(show_rate));
disp(show_rate)
while (val>=0)
set(handles.timeWE, 'String', num2str(val));
pause(1);%这里pause要放在set后面,不然会多一秒
val = val-1*show_rate;
end
|