版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領
文檔簡介
1、文檔來源為 :從網(wǎng)絡收集整理.word 版本可編輯.歡迎下載支持利用HMM的孤立字(詞)語音識別系統(tǒng)/*/*/文件名稱: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;/*命令行參數(shù)設置*/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;/*讀取參考模型的數(shù)量,即詞表的大小*/if(fp=fopen(ref_list,"r")=NULL)fprinff(stderr,"%s:can'topen%storead.n',ref_list);exit(0);fscanf(fp,'%d',&num_ref);/*讀取模型的參數(shù)*/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);/*讀取待識別語音的數(shù)據(jù)*/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);/*/*函數(shù)名稱:usage*/*/*函數(shù)功能:提示信息,并退出
7、程序/*輸入函數(shù):obj-指向提示信息的指針/*/usage(obj)char*obj;fprintf(stderr,"Useage:%s-hHMM-list-iinput-filen",obj);exit(0);文件名稱:(CMHMM的輸入輸出函數(shù))#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;/*/*函數(shù)名稱:dp*/*/*函數(shù)功能:計算任一特征向量序列輸入到各個HMM時的輸出頻率,并返回有最大輸出概率的/*HMM的序列*/*輸入?yún)?shù):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;/*/*函數(shù)名稱:cal-outprob*/*/*/*函數(shù)功能:計算特征向量序列輸入到HUMM室的輸出概率的大小/*輸入?yún)?shù):humm-HUMM的參數(shù);ref-num-HUMM的序號;onum-具有輸出字符的/*的狀態(tài)的序號;inum-輸入特征向量的個數(shù);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. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2024年度企業(yè)員工全面保障標準勞動合同封面3篇
- 2024年建筑用鋼筋加工與銷售一體化服務合同3篇
- 2024版ISO9000質(zhì)量管理體系認證與風險評估咨詢合同2篇
- 2024年度設備采購合同:某工廠生產(chǎn)線設備采購合同2篇
- 2024年人防工程合同范本:人防疏散設施建設及管理協(xié)議3篇
- 2024年度商務咨詢合同:市場調(diào)查與分析合同(2024版)2篇
- 2024年度河南事業(yè)單位100人招聘勞動合同范本下載
- 2024年度物流倉儲服務深度合作合同6篇
- 2024年度委托合同:市場調(diào)查與分析3篇
- 2024上海建筑勞務分包合同范本模板(含安全責任險)3篇
- 福建省廈門市2023-2024學年高二上學期期考化學試題(含答案)
- 廣東省六校聯(lián)考2024-2025學年高二上學期12月月考試題 英語 含答案
- 2024-2025學年北師大版九年級上冊數(shù)學期末測試綜合練習題(原卷版)-A4
- 2024年碳排放管理員(中級)職業(yè)鑒定考試題庫及答案
- 2024年干水泥罐出售合同范本
- 公司業(yè)務轉移協(xié)議書(2篇)
- 村集體資產(chǎn)盤活方案
- 強制性條文監(jiān)理檢查記錄(電氣)
- 2024年輔導員年終總結
- GB/T 44811-2024物聯(lián)網(wǎng)數(shù)據(jù)質(zhì)量評價方法
- 2024年國家開放大學本科《知識產(chǎn)權法》第一至四次形考任務試題及答案
評論
0/150
提交評論