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

📄 hutil.c

📁 实现HMM算法
💻 C
📖 第 1 页 / 共 3 页
字号:
   if (hset->hsKind == DISCRETEHS || hset->hsKind == TIEDHS)       return;   NewHMMScan(hset, &hss);   while (GoNextMix(&hss,FALSE)) {      if (hss.mp->ckind == INVDIAGC){         hss.mp->ckind = DIAGC;         v = hss.mp->cov.var;         if (! IsSeenV(v)) {            for (k=1; k<=hset->swidth[hss.s]; k++) {               if (v[k] > MAXVAR) v[k] = MAXVAR;               if (v[k] < MINVAR) v[k] = MINVAR;               v[k] = 1/v[k];            }            TouchV(v);         }      }   }   EndHMMScan(&hss);   ClearSeenFlags(hset,CLR_ALL);}/* ----------------------- LogWt conversions ----------------------------- *//* EXPORT->ConvLogWt Converts all mixture weights into log-weights. */void ConvLogWt(HMMSet *hset){   HMMScanState hss;   if (hset->hsKind == DISCRETEHS || hset->hsKind == TIEDHS || hset->logWt == TRUE)       return;   NewHMMScan(hset, &hss);   while (GoNextMix(&hss,FALSE))      hss.me->weight = MixLogWeight(hset,hss.me->weight);   EndHMMScan(&hss);   hset->logWt = TRUE;}/* EXPORT->ConvExpWt Converts all mixture log-weights into weights. */void ConvExpWt(HMMSet *hset){   HMMScanState hss;   if (hset->hsKind == DISCRETEHS || hset->hsKind == TIEDHS  || hset->logWt == FALSE)       return;   NewHMMScan(hset, &hss);   while (GoNextMix(&hss,FALSE))      hss.me->weight = exp(hss.me->weight);   EndHMMScan(&hss);   hset->logWt = FALSE;}/* ---------------------- HMM Identification ------------------- */char *HMMPhysName(HMMSet *hset,HLink hmm){   MLink ml;   if ((ml=FindMacroStruct(hset,'h',hmm))==NULL)      HError(7270,"HMMPhysName: Cannot find hmm definition");   return(ml->id->name);}/* --------------------- Item List Handling -------------------- *//*    Lists of HMM structures represented by an 'itemlist' are    represented internally by ItemRec's*//* AddItem: create an ItemRec holding x and prepend it to list */void AddItem(HLink owner, Ptr item, ILink *list){   ILink p;      p=(ILink) New(&itemHeap,0);   p->item=item; p->owner=owner; p->next=*list;   *list=p;}/* NumItems: return number of items in given list */int NumItems(ILink list){   int n=0;      while(list!=NULL) {      n++;      list=list->next;   }   return n;}/* FreeItems: return given list of items to free list */void FreeItems(ILink *list){   ILink p,q;      for (p=*list;p!=NULL;p=q) {      q=p->next;      Dispose(&itemHeap,p);   }}/* ----------------------- Integer Sets --------------------- *//* InitSet: create an IntSet of given size */IntSet CreateSet(int size){   int i;   Boolean *b;   IntSet s;      s.nMembers = size;   b = (Boolean*) New(&setHeap, sizeof(Boolean)*size);   s.set = b-1;   for (i=1;i<=size;i++) *b++ = FALSE;   return s;}/* FreeSet: free space occupied by given set */void FreeSet(IntSet s){   Dispose(&setHeap,s.set+1);}/* IsMember: return TRUE if x is a member of s */Boolean IsMember(IntSet s, int x){   if (x<1 || x>s.nMembers)      HError(7271,"IsMember: Illegal set member test");   return s.set[x];}/* AddMember: add the given member to set s */void AddMember(IntSet s, int x){   if (x<1 || x>s.nMembers)      HError(7271,"AddMember: Illegal set member addition");   s.set[x] = TRUE;}/* IsFullSet: return TRUE if given set is full */Boolean IsFullSet(IntSet s){   int i;      for (i=1;i<=s.nMembers;i++)       if (!s.set[i]) return FALSE;   return TRUE;}/* ClearSet: clear the given set */void ClearSet(IntSet s){   int i;      for (i=1;i<=s.nMembers;i++)       s.set[i] = FALSE;}/* SetSet: set the given set */void SetSet(IntSet s){   int i;      for (i=1;i<=s.nMembers;i++)       s.set[i] = TRUE;}/* -------------------- Item List Parser -------------------- */#define PAT_LEN 1024static Source *source;         /* Current source for item list */static int ch;                 /* Current character from source */static char pattern[PAT_LEN];  /* A copy of the last pattern parsed */static char *position;         /*  and the current position in copy */static int maxStates;          /*  and its max number of states */static int maxMixes;           /*  and max number of mix comps *//* EdError: report a syntax error in input command */static void EdError(char *s){   char *where,*p;   int i;      where=position;   for (i=1;i<20;i++) if (ch=='}' || ch=='\n' || ch==EOF) break;   fprintf(stderr,"%s\n",pattern);   for (p=pattern; p<where; p++) fputc(' ',stderr);   fprintf(stderr,"^\n");   fprintf(stderr,"Error %s\n",s);   HError(7230,"EdError: item list parse error");}/* ReadCh: get next character from f into ch */static void ReadCh(void){   ch = GetCh(source);   if (position < (pattern+PAT_LEN-1))      *position++ = ch, *position = 0;}/* SkipSpaces: skip white space */static void SkipSpaces(void){   while (isspace(ch)) ReadCh();}/* GetAlpha: get an alphanumeric string *//*  This is a copy of ReadString but allows additional terminating *//*  characters for unquoted strings */static char *GetAlpha(char *s){   static char term[]=".,)}";   Boolean wasQuoted;   int i,n,q=0;   wasQuoted=FALSE;   SkipSpaces();   if (ch == DBL_QUOTE || ch == SING_QUOTE){      wasQuoted = TRUE; q = ch;      ReadCh();   }   for (i=0; i<MAXSTRLEN ; i++){      if (wasQuoted){         if (ch == q) {            ReadCh();            s[i] = '\0';            return s;         }      } else {         if (isspace(ch) || strchr(term,ch)){            s[i] = '\0';            return s;         }      }      if (ch==ESCAPE_CHAR) {         ReadCh();         if (ch<'0' || ch>'7') {            n = ch - '0'; ReadCh();            if (ch<'0' || ch>'7') EdError("Octal digit expected");            n = n*8 + ch - '0'; ReadCh();            if (ch<'0' || ch>'7') EdError("Octal digit expected");            ch += n*8 - '0';         }      }      s[i] = ch; ReadCh();   }        EdError("String too long");   return NULL;         /* never reached -- make compiler happy */}/* GetInt: read integer coerced given range */static int GetInt(int lo, int hi){   char buf[20];   int i = 0, num;      SkipSpaces();   if (!isdigit(ch))      EdError("Int expected");   while (isdigit(ch)) {      if (i==19)         EdError("Integer too long!");      buf[i++] = ch; ReadCh();   }   buf[i] = '\0';   num = atoi(buf);   if (num<lo) num=lo;   if (num>hi) num=hi;   return num;}typedef enum {   TRANSP_KEY, STATE_KEY, DUR_KEY, WEIGHTS_KEY,   MIX_KEY, MEAN_KEY, STREAM_KEY, COV_KEY} Keyword;static char *keymap[] = {   "TRANSP", "STATE", "DUR", "WEIGHTS",   "MIX", "MEAN", "STREAM", "COV"};/* GetKey: get a keyword */static Keyword GetKey(void){   char buf[20];   int i = 0;   Keyword k;      SkipSpaces();   if (!isalpha(ch))      EdError("Keyword expected");   while (isalpha(ch)) {      buf[i++] = toupper(ch); ReadCh();      if (i==20)         EdError("Keyword too long!");   }   buf[i] = '\0';   for (k=TRANSP_KEY; k<=COV_KEY; k=(Keyword) (k+1))      if (strcmp(keymap[k],buf) == 0 )         return k;   EdError("Unknown Keyword");    return COV_KEY;  /* never reached -- make compiler happy */}static void ChkType(char newtype, char *type){   if (*type == ' ' || (*type =='a' &&                         (newtype=='v' || newtype=='i' || newtype=='c' || newtype=='x')))      *type = newtype;   else      if (newtype != *type)         HError(7231,"ChkType: Attempt to form list from different types %c and %c",                newtype,*type);}/* AddTransP: add all transP's of models to ilist */static void AddTransP(ILink models, ILink *ilist, char *type){   ILink h;   HMMDef *hmm;      ChkType('t',type);   for (h=models; h!=NULL; h=h->next) {      hmm = h->owner;      AddItem(hmm,hmm,ilist);   /* tie ->transP */   }}/* PIntRange: parse an integer range and add its members to s */static void PIntRange(IntSet s){   int i,i1,i2;      i1 = GetInt(1,s.nMembers);   SkipSpaces();   if (ch == '-') {      ReadCh();      i2 = GetInt(1,s.nMembers);   }   else      i2=i1;   for (i=i1; i<=i2; i++)      AddMember(s,i);}/* PIndex: parse and index and return in s */static void PIndex(IntSet s){   SkipSpaces();   if (ch != '[')      EdError("[ expected");   ReadCh();   PIntRange(s);   SkipSpaces();   while (ch==',') {      ReadCh();      PIntRange(s);      SkipSpaces();   }   if (ch != ']')      EdError("] expected");   ReadCh();}/* PMix: parse a mixture spec */static void PMix(ILink models, ILink *ilist, char *type,                 IntSet states, IntSet streams,HMMSet *hset){   IntSet mixes;   HMMDef *hmm;   ILink h;   int s,j,m;   MixtureElem *me;   StreamElem *ste;   enum {TMIX, TMEAN, TCOV} what;      mixes = CreateSet(maxMixes);   PIndex(mixes);   SkipSpaces();   what = TMIX;   if (ch == '.') {      ReadCh();      switch(GetKey()) {      case MEAN_KEY:         what = TMEAN; ChkType('u',type); break;      case COV_KEY:         what = TCOV;         ChkType('a',type);         break;      default:         EdError("Mean or Cov expected");      }   } else      ChkType('m',type);   for (h=models; h!=NULL; h=h->next) {      hmm = h->owner;      for (j=2; j<hmm->numStates; j++)          if (IsMember(states,j)) {            ste = hmm->svec[j].info->pdf+1;            for (s=1; s<=hset->swidth[0]; s++,ste++)               if (IsMember(streams,s)) {                  me = ste->spdf.cpdf+1;                  for (m=1; m<=ste->nMix; m++,me++)                      if ((MixWeight(hset,me->weight) > MINMIX) && IsMember(mixes,m)) {                        switch (what) {                        case TMIX: /* tie ->mpdf */                           if (trace & T_ITM)                              printf(" %12s.state[%d].stream[%d].mix[%d]\n",                                     HMMPhysName(hset,hmm),j,s,m);                           AddItem(hmm,me,ilist);                            break;                        case TMEAN: /* tie ->mean */                           ChkType('u',type);                            if (trace & T_ITM)                              printf(" %12s.state[%d].stream[%d].mix[%d].mean\n",                                     HMMPhysName(hset,hmm),j,s,m);                           AddItem(hmm,me->mpdf,ilist); break;                        case TCOV: /* tie ->cov  */                           switch (me->mpdf->ckind) {                           case INVDIAGC:                           case DIAGC:                              ChkType('v',type); break;                           case FULLC:                              ChkType('i',type); break;                           case LLTC:                              ChkType('c',type); break;                           case XFORMC:                              ChkType('x',type); break;                           }                           if (trace & T_ITM)                              printf(" %12s.state[%d].stream[%d].mix[%d].%c\n",                                     HMMPhysName(hset,hmm),j,s,m,*type);                           AddItem(hmm,me->mpdf,ilist);                            break;                        }                     }               }         }   }   FreeSet(mixes);}/* PStatecomp: parse a statecomp */static void PStatecomp(ILink models, ILink *ilist, char *type,                        IntSet states, HMMSet *hset)

⌨️ 快捷键说明

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