//#include "cmhmm.h"
int BR_Di/p[MAX_ARCS];
int ME_Disp[MAX_ARCS];
int CO_Disp[MAX_ARCS];
int Train_Disp[MAX_ARCS+MAX_STATES];
int Num_Dimensions;
int Num_Train_Sets;
int Total_Frames;
int Longest_Train;
TRAINING Train[MAX_TRAIN_SIZE];
HMM Hmm[MAS_REF];
main(argc,argv)
int argc;
char *argv[];
{
int i,num_ref,result;
int cnt[MAX_REF];
string hmm_file;
char *in_file=NULL,*ref_list=NULL;
char ch_r[MAX_REF][8];
FILE *fp;
****************
*命令行参数设置*
****************
if(argc<5)
{
usage(argv[0]);
}
for(i=1;i<argc;i++)
{
if(argv[i][0]!='-')
{
fprintf(stderr,"Illegal switch %s\n",argv[i]);
usage(argv[0]);
}
}
switch(argv[i][1])
{
case'h':
ref_list=argv[++i];
break;
case'i':
in_file=argv[++i];
break;
default:
fprint(stderr,"Illegal switch %s\n",argv[i]);
usage(argv[0]);
break;
}
**********************************
*读取参考模型的数量,即词表的大小*
**********************************
if(fp=fopen(ref_list,"r"))==NULL)
{
fprint(stedrr,"%s:can't open %s to read.\n",ref_list);
exit(0);
}
fscanf(fp,"%d",&num_ref);
****************
*读取模型的参数*
**************
for(i=0;i<num_ref;i++)
{
cnt[i]=0;
fscanf(fp,"%s%s",hmm_file,ch_r[i]);
read_hmm(hmm_file,i);
cal_invdet(i);
}
fclose(fp);
print("Category size:%d\n",num_ref);
*********************
*读取待识别的语音数据*
*********************
if((Num_Train_Sets=read_training(in_file))>MAX_TRAIN_SIZE)
{
fprintf(stderr,"%s:too many test patterns in %s.\n",argv[0],in_file);
exit(0);
}
*******
*识别**
******
for(i=0;i<Num_Train_Sets;++i)
{
result=dp(Train+i,num_ref);
++cnt[result];
#if TRACE
printf("[%3d]%3d:%s\n",i+1,Train[i].length,ch_r[result]);
fflush(stdout);
#endif
free(Train[i].data);
}
printf("###识别结果###");
for(i=0;i<num_ref;i++)
{
printf("###/%4s/=%6.2f%%\n",ch_r[i],cnt[i]*100/Num_Train_Sets);
}
usage(obj)
char *obj;
{
fprint(stderr,"Usage:%s-h HMM-list -i input-file\n",obj);
exit(0);
}
#include "cmhmm.h"
extern int BR_Disp[],ME_Disp[],CO_Disp[],Train_Disp[];
extern int Total_Frames;
extern int Num_Dimensions,Longest_Train;
extern TRAINING Train[];
extern HMM Hmm[];
void read_hmm(char *file,int rnum)
{
FILE *fp;
int temp,i,j,k,num,mat_num,from,to,bdisp,mdisp,cdisp;
float trans_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);
Hmm[rnum].num_omatrix=temp;
if(fscanf(fp,"%d",&temp)==EOF)
exit(0);
Hmm[rnum].num_mixture=temp;
for(i=0;i<Hmm[rnum].num_omatrix;++i)
{
temp=i*Hmm[rnum].num_mixture;
BR_Disp[i]=temp;
temp*=Num_Dimensions;
ME_Disp[i]=temp;
temp*=Num_Dimensions;
CO_Disp=temp;
}
temp=FLOATSIZE*Hmm[rnum].num_omatrix*Hmm[rnum].num_mixture;
if((Hmm[rnum].branch_prob=(float*)malloc(temp))==NULL)
exit(0);
temp*=Num_Dimensions;
if(Hmm[rnum].mean=(float*)malloc(temp))==NULL)
exit(0);
if(Hmm[rnum].covariance=(float*)malloc(temp)==NULL)
exit(0);
for(i=0;i<Hmm[rnum].num_omatrix;i++)
{
bdisp=BR_Disp[i];
for(j=0;j<Hmm[rnum].num_mixture;j++)
{
if(scanf(fp,"%f",Hmm[rnum].branch_prob+bdisp+j)==EOF)
exit(0);
}
}
for(temp=0,j=0;j<Hmm[rnum].num_mixture;++j,temp+=Num_Dimensions)
{
mdisp=ME_Disp[i]+temp;
cdisp=CO_Disp[i]+temp*Num_Dimensions;
for(k=0;k<Num_Dimensions;k++)
if(fscanf(fp,"%f",Hmm[rnum].mean+mdisp+k)==EOF)
exit(0);
for(k=0;k<Num_Dimensions*Num_Dimensions;k++)
if(fscanf(fp,"%f",Hmm[rnum].covariance+cdisp+k)==EOF)
exit(0);
}
if(fscanf(fp,"%d",&temp)==EOF)
exit(0);
Hmm[rnum].num_states=temp;
if((Hmm[rnum].states=(STATE*)malloc(sizeof(STATE)*Hmm[rnum].num_sates))==NULL)
exit(0);
for(i=0;i<Hmm[rnum].num_states;i++)
{
Hmm[rnum].states[i].label=i;
Hmm[rnum].states[i].num_from=Hmm[rnum].states[i].num_to=0;
Hmm[rnum].states[i].is_initial=Hmm[rnum].states[i].is_final=FALSE;
}
if(fscanf(fp,"%d",&temp)==EOF)
exit(0);
Hmm[rnum].num_initial=temp;
for(i=0;i<Hmm[rnum].num_initial;i++)
{
if(fscanf(fp,"%d",&num)==EOF)
exit(0);
Hmm[rnum].states[num].is_initial=TRUE;
}
if(fscanf(fp,"%d",&temp)==EOF)
exit(0);
Hmm[rnum].num_final=temp;
for(i=0;i<Hmm[rnum].num_final;i++)
{
if(fscanf(fp,"%d",&num)==EOF)
exit(0);
Hmm[rnum].states[num].is_final=TRUE;
}
if(fscanf(fp,"%d",&temp)==EOF)
exit(0);
Hmm[rnum].num_arcs=temp;
if((Hmm[rnum].transitions=(TRANSITION*)malloc(sizeof(TRANSITION)*Hmm[rnum].num_arcs))==NULL)
exit(0);
for(i=0;i<Hmm[rnum].num_arcs;i++)
{
if(fscanf(fp,"%d%d%e%d",&from,&to,&trans_prob,&mat_num)==EOF)
exit(0);
if((mat_num>=Hmm[rnum].num_omatrix)||(mat_num<0)&&(mat_num!=NULL_TRANSITION)))
exit(0);
if((from>Hmm[rnum].num_states-1)||(from<0)||(to>Hmm[rnum].num_states-1)||(to<0))
{
fprintf(stderr,"read_hmm;Illegal from(%d)-to(%d)\n",from,to);
exit(0);
}
if(trans_prob<=0.0)
exit(0);
Hmm[rnum].transitions[i].trans_prob=log(trans_prob);
Hmm[rnum].transitions[i].origin=from;
Hmm[rnum].transitions[i].destination=to;
Hmm[rnum].transitions[i].out_prob_index=mat_num;
Hmm[rnum].states[from].trans_from[Hmm[rnum].states[from].num_from++]=i;
Hmm[rnum].states[to].trans_to[Hmm[rnum].states[to].num_to++]=i;
if(fscanf(fp,"%*d")!=EOF)
exit(0);
fclose(fp);
}
int read_training(char *f_list)
{
FILE *fp,*fs;
int cnt,i,j,k,length,total_length=0,max+0;
sring f_name;
float buff[100];
if(Num_Dimensions>100)
exit(0);
if((fs=fopen(f_list,"r"))==NULL)
{
fprint(stderr,"read_data:can't open %s to read\n",f_list);
exit(0);
}
for(cnt=0;fscanf(fs,"%s",f_name)!=EOF;++cnt)
{
if(cnt==MAX_TRAIN_SIZE)
{
printf("Warning in read_data;number of datasets(%s) is forced inte %d.\n",f_list,MAX_TRAIN_SIZE);
fflush(stdout);
break;
}
if((fp=fopen(f_name,"rb"))==NULL)
exit(0);
for(length=0;fread(buff,FLOATSIZE,Num_Dimensions+1,fp)==(Num_Dimensions+1);++length);
if((Train[cnt].data=(float*)malloc(FLOATSIZE*Num_Dimensions*length))==NULL)
{
fprintf(stderr,"read_data:not enough space for %s\n",f_list);
exit(EOF);
}
rewind(fp);
for(i=j=0;fread(buff,FLOATSIZE,Num_Dimensions+1,fp)==(Num_Dimensions+1);++i)
for(k=1;k<(Num_Dimensions+1);)
Train[cnt].data[j++]=100.0*buff[k++];
fclose(fp);
if(length!=i)
{
fprintf(stderr,"read_data:size missmatch(%d!=%d)\n",length,i);
exit(EOF);
}
Train[cnt].length=length;
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_Disp[j]=i;
printf("Training data list:%s[%d data sets]\n",f_list,cnt);
return cnt;
}
/*文件名称cmhmmdp.cpp*/
#include "cmhmm.h"
extern int BR_Disp[],ME_Disp[],CO_Disp[],Train_Disp[];
extern int Num_Dimensions,Num_Train_Sets,Longest_Train;
extern TRAINING Train[];
extern HMM Hmm[];
extern double *Inv_Covariance[],*Det_Covariance[];
float *data;
//dp函数
int dp(TRAINING *train,int num_ref)
{
char *stop,*cptr;
int i,j,k,l,m,s,n,t;
int length,index,x;
int out,lx,nhat;
int Outprob_Disp[MAX_STATES];
double *outprob_buff,oprob;
double out_prob,branch_prob,sum_prob;
double *pn,*prev_pn,px,p[MAX_REF];
TRANSITION *tptr;
length=train->length;
data=train->data;
//memory allocation for work
if((pn=(double *)malloc(Longest_Train*MAX_STATES*DOUBLESIZE))==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_Disp[s++]=i;
for(n=0;n<num_ref;n++)
{
s=MAX_STATES*Longest_Train;
for(i=0;i<s;i++)
pn[i]=INFINIT;
for(s=0;s<Hmm[n].num_omatrix;++s)
if(Hmm[n].states[s].is_initial)
pn[Train_Disp[s]]=0.0;
}
for(i=0;i<MAX_ARCS*Longest_Train;outprob_buff[i++]=INFINIT);
for(s=0;s<Hmm[n].num_omatrix;s++)
{
for(i=0;i<leng