⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hmodel.c

📁 隐马尔科夫模型工具箱
💻 C
📖 第 1 页 / 共 5 页
字号:
               --repCount;            } else               UnGetCh(c,src);         }      }      tpdf[m] = weight;      /* set it */   }   if(GetToken(src,tok)<SUCCESS){      HMError(src,"GetToken failed");      return(FAIL);   }   return(SUCCESS);}/* GetDiscreteWeights: parse src and get compact mixture weight def */ReturnStatus GetDiscreteWeights(Source *src, Token *tok, int M, ShortVec dpdf){   short weight=0;   short repCount=0;      /* repeat counter for weights */   int c,m;      if (trace&T_PAR) printf("HModel: GetDiscreteWeights: M=%d\n",M);   for (m=1; m<=M; m++) {      if (repCount>0)         /* get mixture weight */         --repCount;      else {         if (tok->binForm) {            if (!ReadShort(src,&weight,1,TRUE)){               HMError(src,"Discrete Weight expected");               return(FAIL);            }            if (weight<0) {               repCount=GetCh(src);               --repCount; weight &= 077777;            }         } else {            if (!ReadShort(src,&weight,1,FALSE)){               HMError(src,"Discrete Weight expected");               return(FAIL);            }            c=GetCh(src);            if (c == '*') {               if (!ReadShort(src,&repCount,1,FALSE)){                  HMError(src,"Discrete Repeat Count expected");                  return(SUCCESS);               }               --repCount;            } else               UnGetCh(c,src);         }      }      dpdf[m] = weight;      /* set it */   }   if(GetToken(src,tok)<SUCCESS){      HMError(src,"GetToken failed");      return(FAIL);   }      return(SUCCESS);}/* InitTMixRecs: create all TMixRecs in given hset */void InitTMixRecs(HMMSet *hset, int s, int M){   TMixRec *p;   MixPDF **mpp;   TMProb *tm;      p = hset->tmRecs+s;   p->mixId = NULL; p->nMix = M;   mpp = (MixPDF **)New(hset->hmem,sizeof(MixPDF *) * M);   p->mixes = mpp-1;   tm = (TMProb *)New(hset->hmem,sizeof(TMProb)*M);   p->probs = tm-1;}/* GetTiedMixtures: parse src and get compact tied mixture def */ReturnStatus GetTiedMixtures(HMMSet *hset, Source *src, Token *tok,                              int M, int s, Vector tpdf){   char tmName[MAXSTRLEN],macName[2*MAXSTRLEN],intstr[20];   LabId id,mid;   Boolean isNew;   int m;   MLink q;      if (trace&T_PAR) printf("HModel: GetTiedMixtures\n");   if (!ReadString(src,tmName)){      /* read generic ~m macro name */      HMError(src,"Tied Mix macro name expected");      return(FAIL);   }   id = GetLabId(tmName,TRUE);   isNew = hset->tmRecs[s].mixId == NULL;   if (isNew) {      InitTMixRecs(hset,s,M);      hset->tmRecs[s].mixId = id;      for (m=1; m<=M; m++){         sprintf(intstr,"%d",m);         strcpy(macName,tmName);         strcat(macName,intstr);         if((mid = GetLabId(macName,FALSE)) == NULL){            HRError(7035,"GetTiedMixtures: Unknown tied mix macro name %s",macName);            return(FAIL);         }         if ((q = FindMacroName(hset,'m',mid))==NULL){            HRError(7035,"GetTiedMixtures: no macro %s in this set",macName);            return(FAIL);         }         hset->tmRecs[s].mixes[m] = (MixPDF *)q->structure;      }   }else {      if (hset->tmRecs[s].mixId != id){         HMError(src,"Bad Generic ~m Macro Name in TMix");         return(FAIL);      }      if (hset->tmRecs[s].nMix != M){         HMError(src,"Inconsistent Num Mixtures in TMix");         return(FAIL);      }   }   if(GetTiedWeights(src,tok,M,tpdf)<SUCCESS){      HMError(src, "GetTiedWeights failed");      return(FAIL);   }   return(SUCCESS);}/* ------------ Standard Macro Definition Input Routines ----------------- *//* GetOptions: read a global options macro, return numStates if set */static ReturnStatus GetOptions(HMMSet *hset, Source *src, Token *tok, int *nState){   int p=0;      *nState=0;    if (trace&T_PAR) printf("HModel: GetOptions\n");   if(GetToken(src,tok)<SUCCESS){      HMError(src,"GetOptions: GetToken failed");      return(FAIL);   }   while (tok->sym == PARMKIND || tok->sym == INVDIAGCOV ||           tok->sym == HMMSETID  || tok->sym == INPUTXFORM ||          (tok->sym >= NUMSTATES && tok->sym <= XFORMCOV)){      if(GetOption(hset,src,tok,&p)<SUCCESS){         HMError(src,"GetOptions: GetOption failed");         return(FAIL);      }      if (p>*nState) *nState = p;   }   FreezeOptions(hset);   return(SUCCESS);}/* GetStructure: next input token is a string containing macro name,                 a pointer to corresponding structure is returned */static Ptr GetStructure(HMMSet *hset, Source *src, char type){   char buf[MAXSTRLEN];   LabId id;   MLink m;   if (!ReadString(src,buf)){      HRError(7013,"GetStructure: cannot read macro name");      return(NULL);   }   id = GetLabId(buf,FALSE);   if (id==NULL){      HRError(7035,"GetStructure: undef macro name %s, type %c",buf,type);      return(NULL);   }   m = FindMacroName(hset,type,id);   if (m==NULL){      HRError(7035,"GetStructure: no macro %s, type %c exists",buf,type);        return(NULL);   }   if (trace&T_MAC)      printf("HModel: getting structure ~%c %s -> %p\n",             type,buf,m->structure);   return m->structure;}/* Find a node in the regression tree given the index and return the node */static RegTree *FindNode(RegTree *t, RegTree *r, int i){   if (t != NULL) {      r = FindNode(t->left, r, i);      if (t->nodeInfo->nodeIndex == i)         return t;      r = FindNode(t->right, r, i);   }   return r;}static RegNode *CreateNodeInfo(MemHeap *m, short nodeId){   RegNode *n;     n  = (RegNode *) New(m, sizeof(RegNode));   n->nodeIndex = nodeId;   n->nodeComps = 0;   n->nodeOcc = 0.0;   n->nBases = 0;   n->WTrans = NULL;   n->HTrans = NULL;   n->bases  = NULL;   n->backTrans = NULL;   return n;}/* GetRegTree: parse src and return the regression tree structure */static RegTree *GetRegTree(HMMSet *hset, Source *src, Token *tok){   RegTree *rtree = NULL, *t;   short size, i;   int comps;   short index;   /* allocate space for the regression tree root node */   rtree = (RegTree *) New(hset->hmem, sizeof(RegTree));   rtree->left = rtree->right = NULL;   rtree->nodeInfo = CreateNodeInfo(hset->hmem, 1);   if (trace&T_PAR) printf("HModel: GetRegTree\n");   if (tok->sym==REGTREE) {            if (!ReadShort(src,&size,1,tok->binForm)){         HMError(src,"Size of the regression tree is expected");         return(NULL);      }      size *= 2; size -= 1;      for (i = 1; i <= size; i++) {               if(GetToken(src,tok)<SUCCESS){            HMError(src,"GetToken failed");            return(NULL);         }         if (!ReadShort(src,&index,1,tok->binForm)){            HMError(src,"Parent node index for regression tree expected");            return(NULL);         }         t = FindNode(rtree, NULL, index);         if (t == NULL){            HRError(7085, "GetRegTree: Can't find node %d in tree", index);            return(NULL);         }         switch(tok->sym) {         case NODE:            /* create children */            t->left  = (RegTree *) New(hset->hmem, sizeof(RegTree));            t->right = (RegTree *) New(hset->hmem, sizeof(RegTree));            t->left->left = t->left->right = NULL;            t->right->left = t->right->right = NULL;            if (!ReadShort(src,&index,1,tok->binForm)){               HMError(src,"Left node index for regression tree expected");               return(NULL);            }            t->left->nodeInfo  = CreateNodeInfo(hset->hmem, index);            if (!ReadShort(src,&index,1,tok->binForm)){               HMError(src,"Right node index for regression tree expected");               return(NULL);            }            t->right->nodeInfo = CreateNodeInfo(hset->hmem, index);            break;         case TNODE:            /* load the number of components */            if (!ReadInt(src,&comps,1,tok->binForm)){               HMError(src,"Number of components for regression base class expected");               return(NULL);            }            t->nodeInfo->nodeComps = comps;            /* if (!ReadFloat(src,&tmp,1,tok->binForm))               HMError(src,"Occ count (training) for regression base class expected");               if (!ReadFloat(src,&tmp,1,tok->binForm))               HMError(src,"Node score for regression base class expected"); */            break;         default:            HRError(7085,"GetRegTree:Unexpected token symbol");            return(NULL);         }      }   }   if(GetToken(src,tok)<SUCCESS){      HMError(src,"GetToken failed");      return(NULL);   }     return rtree; }/* GetMean: parse src and return Mean structure */static SVector GetMean(HMMSet *hset, Source *src, Token *tok){   SVector m = NULL;   short size;      if (trace&T_PAR) printf("HModel: GetMean\n");   if (tok->sym==MEAN) {            if (!ReadShort(src,&size,1,tok->binForm)){         HMError(src,"Size of Mean Vector expected");         return(NULL);      }      m = CreateSVector(hset->hmem,size);      if (!ReadVector(src,m,tok->binForm)){         HMError(src,"Mean Vector expected");         return(NULL);      }   }      else if (tok->sym==MACRO && tok->macroType=='u'){      if((m=(SVector)GetStructure(hset,src,'u'))==NULL){         HMError(src,"GetStructure Failed");         return(NULL);      }      IncUse(m);   } else{      HMError(src,"<Mean> symbol expected in GetMean");      return(NULL);   }   if(GetToken(src,tok)<SUCCESS){      HMError(src,"GetToken failed");      return(NULL);   }   return m;}/* GetVariance: parse src and return Variance structure */static SVector GetVariance(HMMSet *hset, Source *src, Token *tok){   SVector v = NULL;   short size;       if (trace&T_PAR) printf("HModel: GetVariance\n");   if (tok->sym==VARIANCE) {      if (!ReadShort(src,&size,1,tok->binForm)){         HMError(src,"Size of Variance Vector expected");         return(NULL);      }      v = CreateSVector(hset->hmem,size);      if (!ReadVector(src,v,tok->binForm)){         HMError(src,"Variance Vector expected");         return(NULL);      }   }      else if (tok->sym==MACRO && tok->macroType=='v'){      if((v=(SVector)GetStructure(hset,src,'v'))==NULL){         HMError(src,"GetStructure Failed");         return(NULL);      }      IncUse(v);   } else{      HMError(src,"<Variance> symbol expected in GetVariance");      return(NULL);   }   if(GetToken(src,tok)<SUCCESS){      HMError(src,"GetToken failed");      return(NULL);   }   return v;}/* GetCovar: parse src and return Covariance structure */static STriMat GetCovar(HMMSet *hset, Source *src, Token *tok){   STriMat m = NULL;   short swidth;      if (trace&T_PAR) printf("HModel: GetCovar\n");   if (tok->sym==INVCOVAR || tok->sym==LLTCOVAR) {      if (!ReadShort(src,&swidth,1,tok->binForm)){         HMError(src,"Size of Inv Covariance expected");         return(NULL);      }      m = CreateSTriMat(hset->hmem,swidth);      if (!ReadTriMat(src,m,tok->binForm)){         HMError(src,"Inverse/LLT Covariance Matrix expected");         return(NULL);      }   } else if (tok->sym==MACRO &&               (tok->macroType=='i' || tok->macroType=='c') ){      if((m=(STriMat)GetStructure(hset,src,tok->macroType))==NULL){         HMError(src,"GetStructure Failed");         return(NULL);      }      IncUse(m);   } else{      HMError(src,"<InvCovar>/<LLTCovar> symbol expected in GetCovar");      return(NULL);   }   if(GetToken(src,tok)<SUCCESS){      HMError(src,"GetToken failed");      return(NULL);   }   return m;}/* GetTransform: parse src and return Transform structure */static SMatrix GetTransform(HMMSet *hset, Source *src, Token *tok){   SMatrix m = NULL;   MemHeap *hmem;   short xformRows,xformCols;      if (trace&T_PAR) printf("HModel: GetTransform\n");   if (tok->sym==XFORM) {      if (hset==NULL) hmem = &xformStack;      else hmem = hset->hmem;      if (!ReadShort(src,&xformRows,1,tok->binForm)){         HMError(src,"Num Rows in Xform matrix expected");         return(NULL);      }      if (!ReadShort(src,&xformCols,1,tok->binForm)){         HMError(src,"Num Cols in Xform matrix expected");         return(NULL);      }      m = CreateSMatrix(hmem,xformRows,xformCols);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -