利用HMM的孤立字词语音识别系统_第1页
利用HMM的孤立字词语音识别系统_第2页
利用HMM的孤立字词语音识别系统_第3页
利用HMM的孤立字词语音识别系统_第4页
利用HMM的孤立字词语音识别系统_第5页
已阅读5页,还剩7页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、文档来源为 :从网络收集整理.word 版本可编辑.欢迎下载支持利用HMM的孤立字(词)语音识别系统/*/*/文件名称:recog.cpp(主程序)*/12/*/#include"emhmm.hintBR_DispMAX_ARCS;/*Displaymenttosavemultiplies(branch_prob)*/intME_DispMAX_ARCS;/*Displaymenttosavemnltiplies(mean)*/intCO_DispMAX_ARCS;/*Displaymenttosavemultiplies(covariance)*/intTrain_DispMAX_

2、ARCS+MAX_STATES;/*reallyonlyneedmax(ma,ms)*/imNum_Dimensions;intNum_Train_Sets;/*Sizeofdimensionoftrainingdata*/intTotal_Frames;intLongest_Train;/*thelengthofthelongesttrainingset*/TRAININGTrainMAX-_TRAIN_SIZE;HMMHmmMAX_REF;main(argc,argv)intargc;char*argv;inti,num_ref,result;intcntMAX_REF;stringhmm

3、_file;char*infile=NULL,*ref_list=NULL;charch_rMAX_REF8;FILE*fp;/*命令行参数设置*/if(argc<5)usage(argv0);for(i=1;i<argc;i+)if(argvi0!='-')fprinff(stderr,"Illegalswitch%sn",argvi);usage(amv0);switch(argvi)1);case'h':ref_list=argv+i;break;case'i':in_file=argv+i;break;de

4、fault:fprimf(stderr,"Illegalswitch%sn',argvi);usage(argv0);break;/*读取参考模型的数量,即词表的大小*/if(fp=fopen(ref_list,"r")=NULL)fprinff(stderr,"%s:can'topen%storead.n',ref_list);exit(0);fscanf(fp,'%d',&num_ref);/*读取模型的参数*/for(i=0;i<num_ref;i+)cnti=0;fscanf(fp,"

5、;%s%s",hmm_file,ch_ri);read_hmm(hmm_file,i);cal_invdet(i);fclose(fp);pfintf("Categorysize:%dn',hum_ref);/*读取待识别语音的数据*/if(Num_Train_Sets=read_training(in_file)>MAX_TRAIN_SIZE)fprintf(stderr,'%s:toomanytestpatternsin%s.n",argv0,in_file);exit(0);/*识别*/for(i=0;i<Num_Train_Se

6、ts;+i)result=dp(Train+i,numref);+cntresult;#ifTRACEprintf("%3d%3d:%sn",i+1,Traini.length,ch_rresult);fflush(stdout);#endiffree(Traini.data);printf("#识别结果#");for(i=0;i<num_ref;i+)printf("#/%4s/=%6.2f%n",ch_ri,cnti*100.0/Num_Train_Sets);/*/*函数名称:usage*/*/*函数功能:提示信息,并退出

7、程序/*输入函数:obj-指向提示信息的指针/*/usage(obj)char*obj;fprintf(stderr,"Useage:%s-hHMM-list-iinput-filen",obj);exit(0);文件名称:(CMHMM的输入输出函数)#include"cmhmm.h"#include<string.h>externintBR_Disp,ME_Disp,CO_Disp,Train_Disp;externintTotal_Frames;externintNum_Dimensions,Longest_Train;externTRA

8、ININGTrain;externHMMHmm;voidread_hmm(char*file,intrnum)FILE*fp;inttemp,i,j,k,num,mat_num,from,to,bdisp,mdisp,cdisp;floattrans_prob;if(fp=fopen(file,"r")=NULL)exit(0);if(fscanf(fp,"%d",&Num_Dimensions)=EOF)exit(0);if(fscanf(fp,"%d",&temp)=EOF)exit(0);Hmmrnum.num_

9、omatrix=temp;if(fscanf(fp,"%d",&temp)=EOF)exit(0);Hmmrnum.num_mixture=temp;for(i=0;i<Hmmrnum.num_omatrix;+i)temp=i*Hmmrnum.num_mixture;BR_Dispi=temp;temp*=Num_Dimensions;ME_Dispi=temp;temp*=Num_Dimensions;CO_Dispi=temp;temp=FLOATSIZE*Hmmrnum.num_omatrix*Hmmrnum.num_mixture;if(Hmmrnu

10、m.branch_prob=(float*)malloc(temp)=NULL)exit(0);temp*=Num_Dimensions;if(Hmmrnum.mean=(float*)malloc(temp)=NULL)exit(0);temp*=Num_Dimensions;if(Hmmrnum.covariance=(float*)malloc(temp)=NULL)exit(0);for(i=0;i<Hmmrnum.num_omatrix;i+)bdisp=BR_Dispi;for(j=0;j<Hmmrnum.num_mixture;j+)if(fscanf(fp,&quo

11、t;%f",Hmmrnum.branch_prob+bdisp+j)=EOF)exit(0);for(temp=0,j=0;j<Hmmrnum.num_mixture;+j,temp+=Num_Dimensions)mdisp=ME_Dispi+temp;cdisp=CO_Dispi+temp*Num_Dimensions;for(k=0;k<Num_Dimensions;k+)if(fscanf(fp,"%f",Hmmrnum.mean+mdisp+k)=EOF)exit(0);for(k=0;k<Num_Dimensions*Num_Dime

12、nsions;k+)if(fscanf(fp,"%f",Hmmrnum.covariance+cdisp+k)=EOF)exit(0);if(fscanf(fp,"%d",&temp)=EOF)exit(0);Hmmrnum.num_states=temp;if(Hmmrnum.states=(STATE*)malloc(sizeof(STATE)*Hmmrnum.num_states)=NULL)exit(0);for(i=0;i<Hmmrnum.num_states;i+)Hmmrnum.statesi.label=i;Hmmrnum.

13、statesi.num_from=Hmmrnum.statesi.num_to=0;Hmmrnum.statesi.is_initial=Hmmrnum.statesi.is_final=FALSE;if(fscanf(fp,"%d",&temp)=EOF)exit(0);Hmmrnum.num_initial=temp;for(i=0;i<Hmmrnum.num_initial;i+)if(fscanf(fp,"%d",&num)=EOF)exit(0);Hmmrnum.statesnum.is_initial=TRUE;if(f

14、scanf(fp,"%d",&temp)=EOF)exit(0);Hmmrnum.num_final=temp;for(i=0;i<Hmmrnum.num_final;i+)if(fscanf(fp,"%d",&num)=EOF)exit(0);Hmmrnum.statesnum.is_final=TRUE;if(fscanf(fp,"%d",&temp)=EOF)exit(0);Hmmrnum.num_arcs=temp;if(Hmmrnum.transitions=(TRANSITION*)mallo

15、c(sizeof(TRANSITION)*Hmmrnum.num_arcs)=NULL)exit(0);for(i=0;i<Hmmrnum.num_arcs;i+)if(fscanf(fp,"%d%d%e%d",&from,&to,&trans_prob,&mat_num)=EOF)exit(0);if(mat_num>=Hmmrnum.num_omatrix)|(mat_num<0)&&(mat_num!=NULL_TRANSITION)exit(0);if(from>Hmmrnum.num_stat

16、es-1)|(from<0)|(to>Hmmrnum.num_states-1)|(to<0)fprintf(stderr,"read_hmm:Illegalfrom(%d)-to(%d)n",from,to);exit(0);if(trans_prob<=0.0)exit(0);Hmmrnum.transitionsi.trans_prob=log(trans_prob);Hmmrnum.transitionsi.origin=from;Hmmrnum.transitionsi.destination=to;Hmmrnum.transitionsi

17、.out_prob_index=mat_num;Hmmrnum.statesfrom.trans_fromHmmrnum.statesfrom.num_from+=i;Hmmrnum.statesto.trans_toHmmrnum.statesto.num_to+=i;if(fscanf(fp,"%*d")!=EOF)exit(0);fclose(fp);intread_training(char*f_list)FILE*fp,*fs;intcnt,i,j,k,length,total_length=0,max=0;intm;stringf_name;floatbuff1

18、00;if(Num_Dimensions>100)exit(0);if(fs=fopen(f_list,"r")=NULL)fprintf(stderr,"red_data:can'topen%storeadn",f_list);exit(0);for(cnt=0;fscanf(fs,"%s",f_name)!=EOF;+cnt)if(cnt=MAX_TRAIN_SIZE)printf("Warninginread_data:numberofdatasets(%s)isforcedinto%d.n",

19、f_list,MAX_TRAIN_SIZE);fflush(stdout);break;if(fp=fopen(f_name,"rb")=NULL)exit(0);for(length=0;(m=fread(buff,FLOATSIZE,(Num_Dimensions+1),fp)=(Num_Dimensions+1);+length);if(Traincnt.data=(float*)malloc(FLOATSIZE*Num_Dimensions*length)=NULL)fprintf(stderr,"read_data:notenoughspacefor%s

20、n",f_list);exit(EOF);rewind(fp);for(i=j=0;(m=fread(buff,FLOATSIZE,(Num_Dimensions+1),fp)=(Num_Dimensions+1);+i)for(k=1;k<(Num_Dimensions+1);)Traincnt.dataj+=100.0*buffk+;fclose(fp);if(length!=1)fprintf(stderr,"read_data:sizemissmatch(%d!=%d)n",length,i);exit(EOF);Traincnt.length=le

21、ngth;total_length+=length;if(length>max)max=length;fclose(fs);Total_Frames=total_length;Longest_Train=max;for(i=j=0;j<MAX_ARCS+MAX_STATES;+j,i+=Longest_Train)Train_Dispj=i;printf("Trainingdatalist:%s%ddatasetsn",f_list,cnt);returncnt;文件名称:cmhmmdp.cpp#include"cmhmm.h"externi

22、ntBR_Disp,ME_Disp,CO_Dsip,Train_Disp;externintNum_Dimensions,Num_Train_Sets,Longest_Train;externTRAININGTrain;externHMMHmm;externdouble*Inv_Covariance,*Det_Covariance;float*date;/*/*函数名称:dp*/*/*函数功能:计算任一特征向量序列输入到各个HMM时的输出频率,并返回有最大输出概率的/*HMM的序列*/*输入参数:train-输入的特征响亮序列;num-ref词表的大小*/*返回值:返回有最大输出概率的HMM的

23、序号。*/*/intdp(TRAINING*train,intnum_ref)char*stop,*cptr;inti,j,k,l,m,n,t;intlength,index,x;intout,lx,nhat;intOutprob_DispMAX_STATES;double*outprob_buff,oprob;doubleout_prob,banch_prob,sum_prob;double*pn,*prev_pn,px,pMAX_REF;TRAINSITION*tptr;length=train->length;data=train->data;/*memoryallocati

24、onforwork*/if(pn=(double*)malloc(Longest_Train*MAX_STATES*DOUBLEESIZE)=NULL)exit(0);if(outprob_buff=(double*)malloc(Longest_Train)*MAX_ARCS*DOUBLESIZE)=NULL)exit(0);for(s=i=0;s<MAX_STATES;i+=Longest_Train)Outprob_Disps+=i;for(n=0;n<num_ref;n+)/*step1*/s=MAX_STATES*Longest_Train;for(i=0;i<s;

25、i+)pni=INFINT;/*clearpn*/for(s=0;s<Hummn.num_omatrix;+s)if(Hmmn.statess.is_initial)pnTrain_Disps=0.0;for(i=0;i<MAX_ARCA*Longest_Train;outprob_buffi+=INFINIT);for(s=0;s<Hummn.num_omatrix;s+)for(i=0;i<length;i+)sum_prob=0.0;for(j=0;j<Hummn.num_mixture;j+)branch_prob=Hummn.branch_probBR_

26、disps+j;if(branch_prob=0.0)continue;sum_prob+=pow(branch_prob,5.0)*cal_outprob(&(Hummn),n,s,i,j);outprob_buffOutprob_Disps+i=log(sum_prob);for(i=0;i<length;i+)/*step2*/for(s=0;s<Hummn.num_states;s+)/*step3*/index=Train_Disps+i;stop=&(Hummn.statess.trans_toHummn.statess.num_to);for(cptr

27、=&(Hummn.statess.trans_to0);cptr<stop;cptr+)/*step4*/tptr=&(Hummn.transitions*cptr);px=INFINIT;if(tptr->out_prob_index=NULL_TRANSITION)prev_pn=pn+(Train_Disptptr->origin+i);if(*prev_pn>INFINIT)px=*prev_pn+tptr->trans_prob;if(px>pnindex)pnindex=px;elseif(i>0)prev_pn=pn+(T

28、rain_Disaptptr->origin+i-1);if(*prev_pn>INFINIE)out_prob=*(outprob_buff+Outprob_Disptptr->out_prob_index+i);if(out_prob>INFINIT)px=*prev_pn+(tptr->trans_prob)+out_prob;if(px>pnindex)pnindex=px;pn=pnTrain_Disp(Hummn.num_states)-1+length-1;free(pn);free(outprob_buff);nhat=0;for(n=1;n

29、<num_ref;n+)if(pn>pnhat)nhat=n;returnnhat;/*/*函数名称:cal-outprob*/*/*/*函数功能:计算特征向量序列输入到HUMM室的输出概率的大小/*输入参数:humm-HUMM的参数;ref-num-HUMM的序号;onum-具有输出字符的/*的状态的序号;inum-输入特征向量的个数;mnum-混合概率密度的序号*/*返回值:HMM的输出概率*/*/doublecal_outprob(HUMM*hmm,intref_num,intonum,intinum,intmnum)doublea100,b,out_prob;inti,j,

30、mdisp,cdsp,ddisp;if(Num_Dismensions>100)exit(0);i=mnum;ddisp=BR_Disaponum+i;i*=Num_Dimensions;mdisp=ME_Disponum+i;i*=Num_Dimensions;cdisp=CO_Disponum+i;out_prob=0.0;for(i=0;i<=Num_Dimensions;i+)ai=datainum*Num_Dimensions+i-hmm->meanmdisp+i;for(i=0;i<=Num_Dimensions;i+)b=0.0;for(i=0;j<

31、=Num_Dimensions;j+)b+=Inv_Covarianceref_numcdisp+i*Num_Dimensions+j*aj;out_prob+=ai*b;out_prob/=-2.0;out_prob=exp(out_prob)/Det_Covarianceref_numddsip;return(out_prob>MIN_OUTPROB)?out_prob;MIN_OUTPROB;文件名称:cmhmm.h#include"cmhmm.h"#defineM_PI3.846externintNum_Dimensions;externHMMHmm;exte

32、rnintCO_Disp,BR_Disp;double*Inv_CovarianceMAX_REF;double*Det_CovarianceMAX_REF;voidcal_invdet(intrnum)inti,j,k,l,m;intwork100;intlr,iw,cdisp,ddisp;doubleeps=1.0e-20,w,wmax,pivot,api;if(Num_Dimensions>100)exit(0);/*memoryallocation*/i=Hmmrnum.num_omatrix*Hmmrnum.num_mixture;if(Det_Covariancernum=(

33、double*)malloc(i*DOUBLESIZE)=NULL)exit(0);for(m=0;m<Hmmrnum.num_omatrix;m+)for(l=0;l<Hmmrnum.num_mixture;l+)cdisp=CO_Dispm+Num_Dimensions*Num_Dimensions*l;ddisp=BR_Dispm+1;for(i=0;i<Num_Dimensions;i+)for(j=0;j<Num_Dimensions;j+)Inv_Covariancernumcdisp+i*Num_Dimensions+j=Hmmrnum.covarianc

34、ecdisp+i*Num_Dimensions+j;Det_Covariancernumddisp=1.0;for(i=0;i<Num_Dimensions;i+)worki=i;for(k=0;k<Num_Dimensions;k+)wmax=0.0;for(i=k;i<Num_Dimensions;i+)w=fabs(Inv_Covariancernumcdisp+k*Num_Dimensions+i);if(w<wmax)continue;wmax=w;lr=i;pivot=Inv_Covariancernumcdisp+k*Num_Dimensions+i;api=fabs(pivot);if(api<=eps)exit(0);Det_Covariancernumddisp*=pivot;if(Det_Covariancernumddisp=0.0)printf("cal_invdet:Det_Covariance%d%disunderflow.n",rnum,ddisp);exit(0);if(lr!=k)Det_Covariancernumddisp*=-1.0;iw=workk

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论