gusucode.com > 用matlab仿真0到9十个数字的语音识别源码程序 > Speech-Recogenition/trainmfcc.m

    function trainmfcc(file)
% 对0到9十个数字训练出HMM模型参数
for biaoshi=1:10 
fid = fopen([file char(biaoshi+47) '.mat'],'r');%依次打开存储了0到9十个数字的语音文件
%定义一个临时的矩阵ku,用于存储20个相应数字的语音波形序列,序列长度不超过3000
ku=zeros(20,3000);
for i=1:20
   ku(i,:)=fread(fid,3000,'double');%ku(i,:)中,第一个数存贮了该序列的长度
end
fclose(fid);
%对20个语音波形文件,分别计算出其MFCC参数,MFCC参数为13维*帧长,帧语音波形的长短而不同,最后截取前13*15的部分,存入datacell中
for i=1:20
    input=ku(i,2:ku(i,1));
    [ceps,freqresp,fb,fbrecon,freqrecon] = 	mfcc(input, 8000, 160);
    datacell(:,:,i)=ceps(:,1:15);%datacell存贮格式为datacell(mfcc参数长度13,帧长15,序列个数20)
end    
  O = 13;         %Number of coefficients in a vector (mfcc参数长度)
  M = 1;          %Number of mixtures (混合高斯分布的数目)
  Q = 6;          %Number of states (状态数)
  cov_type = 'full';
% 连续HMM模型参数训练
prior0 = normalise(rand(Q,1));%随机生成初始状态
transmat0 = mk_stochastic(rand(Q,Q));%随机生成状态转移矩阵
  [mu0, Sigma0] = mixgauss_init(Q*M, datacell, cov_type);%生成高斯分布的均值和方差
  mu0 = reshape(mu0, [O Q M]);
  Sigma0 = reshape(Sigma0, [O O Q M]);
  mixmat0 = mk_stochastic(rand(Q,M));
%HMM模参数训练函数
[LL, prior1, transmat1, mu1, Sigma1, mixmat1] = mhmm_em(datacell, prior0, transmat0, mu0, Sigma0, mixmat0, 'max_iter', 5);
%将训练得到的相应数字的HMM参数prior1, transmat1, mu1, Sigma1, mixmat1分别存入到shuzi0,shuzi1.....shuzi9,文件中
fid = fopen(['d:\shuzi' char(biaoshi+47) '.mat'],'wb');
fwrite(fid,prior1,'double');
fwrite(fid,transmat1,'double');
fwrite(fid,mu1,'double');
fwrite(fid,Sigma1,'double');
fwrite(fid,mixmat1,'double');
fclose(fid);
end