# 【语音识别】基于matlab GUI HMM 0~9数字语音识别【含Matlab源码 1393期】

·  阅读 823

## 一、隐马尔可夫模型简介

2 隐马尔可夫模型的定义 根据上面的例子，这里给出隐马尔可夫的定义。隐马尔科夫模型是关于时序的概率模型，描述由一个隐藏的马尔可夫链随机生成不可观测的状态随机序列，再由各个状态生成一个可观测的随机序列的过程，隐藏的马尔可夫链随机生成的状态序列，称为状态序列(也就上面例子中的D6，D8等)；每个状态生成一个观测，而由此产生的观测随机序列，称为观测序列(也就上面例子中的1，6等)。序列的每个位置又可以看作是一个时刻。 隐马尔可夫模型由初始的概率分布、状态转移概率分布以及观测概率分布确定。具体的形式如下，这里设Q是所有可能的状态的集合，V是所有可能的观测的集合，即有： 3 前向算法

## 二、部分源代码

``````function varargout = HMM_VoiceRecognation(varargin)
% HMM_VOICERECOGNATION MATLAB code for HMM_VoiceRecognation.fig
%      HMM_VOICERECOGNATION, by itself, creates a new HMM_VOICERECOGNATION or raises the existing
%      singleton*.
%
%      H = HMM_VOICERECOGNATION returns the handle to a new HMM_VOICERECOGNATION or the handle to
%      the existing singleton*.
%
%      HMM_VOICERECOGNATION('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in HMM_VOICERECOGNATION.M with the given input arguments.
%
%      HMM_VOICERECOGNATION('Property','Value',...) creates a new HMM_VOICERECOGNATION or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before HMM_VoiceRecognation_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to HMM_VoiceRecognation_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%

% Edit the above text to modify the response to help HMM_VoiceRecognation

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
'gui_Singleton',  gui_Singleton, ...
'gui_OpeningFcn', @HMM_VoiceRecognation_OpeningFcn, ...
'gui_OutputFcn',  @HMM_VoiceRecognation_OutputFcn, ...
'gui_LayoutFcn',  [] , ...
'gui_Callback',   []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT

% --- Executes just before HMM_VoiceRecognation is made visible.
function HMM_VoiceRecognation_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to HMM_VoiceRecognation (see VARARGIN)

% Choose default command line output for HMM_VoiceRecognation
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes HMM_VoiceRecognation wait for user response (see UIRESUME)
% uiwait(handles.figure1);

% --- Outputs from this function are returned to the command line.
function varargout = HMM_VoiceRecognation_OutputFcn(hObject, eventdata, handles)
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;

% --- Executes on button press in button_choose.
function button_choose_Callback(hObject, eventdata, handles)
% hObject    handle to button_choose (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% fname: 返回文件名
% panme: 返回文件路径名
% index: 选择的文件类型
global fname
global pname
[fname, pname, index] = uigetfile( { '*.wav', '选择语音文件'} ) ;
set( handles.button_reco, 'Enable', 'on' )

% --- Executes on button press in button_reco.
function button_reco_Callback(hObject, eventdata, handles)
% hObject    handle to button_reco (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global fname
global pname
global str

filename = strcat( pname, '\\', fname ) ;
% 发音
[ y, fs ] = audioread(filename) ;
sound(y, fs) ;
% 识别
m = mfcc(x);
m = m(x1-2:x2-2,:);
for j=1:10
pout(j) = viterbi(hmm{j}, m);
end
[d,result_index] = max(pout);
%在static text里显示结果
if result_index == 10

% --- Executes on button press in button_exit.
function button_exit_Callback(hObject, eventdata, handles)
% hObject    handle to button_exit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
clear all
close

% --- Executes on selection change in listbox1.
function listbox1_Callback(hObject, eventdata, handles)
% hObject    handle to listbox1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns listbox1 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from listbox1

% --- Executes during object creation, after setting all properties.
function listbox1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to listbox1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

function edit1_Callback(hObject, eventdata, handles)
% hObject    handle to edit1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit1 as text
%        str2double(get(hObject,'String')) returns contents of edit1 as a double

% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end

% --- Executes on button press in button_clear.
function button_clear_Callback(hObject, eventdata, handles)
% hObject    handle to button_clear (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
function f=enframe(x,win,inc)
%ENFRAME split signal up into (overlapping) frames: one per row. F=(X,WIN,INC)
%
%	F = ENFRAME(X,LEN) splits the vector X up into
%	frames. Each frame is of length LEN and occupies
%	one row of the output matrix. The last few frames of X
%	will be ignored if its length is not divisible by LEN.
%	It is an error if X is shorter than LEN.
%
%	F = ENFRAME(X,LEN,INC) has frames beginning at increments of INC
%	The centre of frame I is X((I-1)*INC+(LEN+1)/2) for I=1,2,...
%	The number of frames is fix((length(X)-LEN+INC)/INC)
%
%	F = ENFRAME(X,WINDOW) or ENFRAME(X,WINDOW,INC) multiplies
%	each frame by WINDOW(:)

%
%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

nx=length(x);
nwin=length(win);
if (nwin == 1)
len = win;
else
len = nwin;
end
if (nargin < 3)
inc = len;
end
nf = fix((nx-len+inc)/inc);
f=zeros(nf,len);
indf= inc*(0:(nf-1)).';
inds = (1:len);
f(:) = x(indf(:,ones(1,len))+inds(ones(nf,1),:));
if (nwin > 1)
w = win(:)';
f = f .* w(ones(nf,1),:);
end

## 四、matlab版本及参考文献

1 matlab版本 2014a

2 参考文献 [1]韩纪庆,张磊,郑铁然.语音信号处理（第3版）[M].清华大学出版社，2019. [2]柳若边.深度学习:语音识别技术实践[M].清华大学出版社，2019.