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

📄 hparm.c

📁 隐马尔科夫模型工具箱
💻 C
📖 第 1 页 / 共 5 页
字号:
      { 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0},    /* src = MELSPEC */      { 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0},    /* src = USER */      { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0},    /* src = DISCRETE */      { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0},    /* src = PLP */      { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},    /* src = ANON */   };   if (src == tgt) return TRUE;   if (xmap[src&BASEMASK][tgt&BASEMASK] == 0 ) return FALSE;   if ((tgt&BASEMASK) == DISCRETE){      if ((tgt&~(BASEMASK|HASCRCC)) != 0) return FALSE;   } else {      if ((tgt&HASENERGY) && !(src&HASENERGY) )          return FALSE;      if ((tgt&HASZEROC)  && !(src&HASZEROC) )          return FALSE;      if ((tgt&HASENERGY) && (tgt&HASZEROC) && (tgt&HASDELTA))          return FALSE;      if ((tgt&HASENERGY) && (tgt&HASZEROC) && (tgt&HASNULLE))          return FALSE;      if (!(tgt&HASDELTA) && (tgt&HASACCS)) return FALSE;      if ((tgt&HASNULLE) && !((tgt&HASENERGY) || (tgt&HASZEROC)) )          return FALSE;      if ((tgt&HASNULLE) && !(tgt&HASDELTA) ) return FALSE;   }   return TRUE;}/* FindSpans: Finds the positions of the subcomponents of a parameter    vector with size components as follows (span values are -ve if    subcomponent does not exist):   span: [0] .. [1]  [2]    [3]  [4] .. [5]  [6] .. [7] [8] .. [9]           statics   cep0 energy   deltas       accs      third   */static void FindSpans(short span[12], ParmKind k, int size){   int i,stat,en=0,del=0,acc=0,c0=0,third=0,fourth=0;      for (i=0; i<10; i++) span[i] = -1;   /*       if having higher order differentials and the precondition       is there is third oder differentials    */   if (k&HASTHIRD && highDiff == TRUE)      fourth = third = acc = del = size/5;   else if (k&HASTHIRD)      third = acc = del = size/4;   else if (k&HASACCS)      acc = del = size/3;    else if (k&HASDELTA)       del = size/2;   if (k&HASENERGY) ++en;  if (k&HASZEROC) ++c0;   stat = size - c0 - en - del - acc - third - fourth;   if (stat>0) { span[0] = 0;              span[1] = stat-1;}   if (c0>0)     span[2] = stat; if (en>0) span[3] = stat+c0;   if (del>0)  { span[4] = stat+c0+en;     span[5] = stat+c0+en+del-1;}   if (acc>0)  { span[6] = stat+c0+en+del; span[7] = stat+c0+en+del+acc-1;}   if (third>0){ span[8] = stat+c0+en+del+acc; span[9] = stat+c0+en+del+acc+third-1;}   if (fourth>0){ span[10] = stat+c0+en+del+acc+third; span[11] = stat+c0+en+del+acc+third+fourth-1;}}/* TotalComps: return the total number of components in a parameter vector   with nStatic components and ParmKind pk */static int TotalComps(int nStatic, ParmKind pk){   int n,x;      n = nStatic;   if (pk&HASENERGY) ++n;   if (pk&HASZEROC)  ++n;   x = n;   if (pk&HASDELTA){      n += x;      if (pk&HASACCS) {        n += x;        if (pk&HASTHIRD) {           n += x;           if (highDiff == TRUE){              n += x;           }        }      }   }   return n;   }/* NumStatic: return the number of static components in a parameter vector   with nTotal components and ParmKind pk */static int NumStatic(int nTotal, ParmKind pk){   short span[12];      FindSpans(span,pk,nTotal);   return span[1]-span[0]+1;  }/* NumEnergy: return the number of energy components in a parameter vector   with nTotal components and ParmKind pk */static int NumEnergy(ParmKind pk){   int e;      if (!(pk&(HASENERGY|HASZEROC))) return 0;   e = 1;   if (pk&HASDELTA){      ++e;      if (pk&HASACCS) {        ++e;        if (pk&HASTHIRD){           ++e;           if (highDiff == TRUE){              ++e;           }        }      }   }   return e;   }/* EqualKind: return true if kinds are internally compatible */static Boolean EqualKind(ParmKind a, ParmKind b){   /* Energy suppression only occurs at observation level */   a = a&(~HASNULLE); b = b&(~HASNULLE);   return a==b;}/* --------------- Parameter Conversion Routines ----------------- *//* All of the routines in this section operate on a table of floats.     Typically the same operation is applied to each row.  Source slice   is indexed by si relative to start of that row, target slice is   indexed by ti relative to start of that row   data   v   ................................... ^   si              ti             |   .....xxxxxxxxxxx.....xxxxxxxxxxx... nRows   <--- d --->     <--- d --->    |   ................................... v   <--------------nCols-------------->*//* AddDiffs: target slice of each row are regression coeffs corresponding   to source slice.  Regression is calculated over rows   -winSize to +winSize. This assumes that hdMargin valid rows are stored   before the parameter block, and tlMargin valid rows are stored after   the parameter block.  When applied to tables these will be both zero.   For buffers, they may be positive. */      /* Note that unlike before this function really will process nRows of data *//* And will not automagically do extra (which was a necessary and probably *//* unintended side effect of the way the function worked previously) */static void AddDiffs(float *data, int nRows, int nCols, int si, int ti, int d,                      int winSize, int hdMargin, int tlMargin, Boolean v1Compat, Boolean simpDiffs){   float *p;   int n,offset = ti-si;   int head,tail;   if (hdMargin<0) hdMargin=0;   if (hdMargin>winSize) head = 0;   else head = winSize - hdMargin;   if (tlMargin<0) tlMargin=0;   if (tlMargin>winSize) tail = 0;   else tail = winSize - (tlMargin>0?tlMargin:0);   p = data+si; n=nRows-(head+tail);   if (n<=0) {      if (head>0 && tail>0) {         /* Special case to cope with ultra short buffers */         AddRegression(p,d,nRows,nCols,offset,winSize,                       hdMargin,tlMargin,simpDiffs);         head=tail=n=0; return;      }      else if (tail==0) head=nRows,n=0; /* Just do head rows */      else if (head==0) tail=nRows,n=0; /* Just do tail rows */   }   /* Make sure have winSize before and after columns to qualify */   if (head>0) {      if (v1Compat)         AddHeadRegress(p,d,head,nCols,offset,0,simpDiffs);      else         AddRegression(p,d,head,nCols,offset,winSize,                       hdMargin,winSize,simpDiffs);      p += head*nCols;   }   if (n>0) {      AddRegression(p,d,n,nCols,offset,winSize,winSize,winSize,simpDiffs);      p += n*nCols;   }   if (tail>0) {      if (v1Compat)         AddTailRegress(p,d,tail,nCols,offset,0,simpDiffs);      else         AddRegression(p,d,tail,nCols,offset,winSize,                       winSize,tlMargin,simpDiffs);   }}/* DeleteColumns: delete source slice (NB: nCols is not changed ) */static void DeleteColumn(float *data, int nUsed, int si, int d){   int rest;   char *p1,*p2;      p1 = (char *)data + si*sizeof(float);   p2 = p1 + d*sizeof(float);   rest = (nUsed - (si+d))*sizeof(float);   memmove(p1,p2,rest);}/* AddQualifiers: add quals needed to get from cf->curPK to cf->tgtPK. *//*  Ensures that nRows of data are valid and fully qualified. *//*  This means that delta coefs may be calculated beyond this range *//*  Values of hd/tlValid indicate that margin of static data exists at *//*  start/end of nRows */static void AddQualifiers(ParmBuf pbuf,float *data, int nRows, IOConfig cf,                           int hdValid, int tlValid){   char buf[100],buff1[256],buff2[256];   int si,ti,d,ds,de, i, j, step, size;   short span[12];   float *fp, mean, scale;   ParmKind tgtBase;   if ((cf->curPK == cf->tgtPK) && (cf->MatTranFN == NULL)) return;   if (trace&T_QUA)      printf("HParm:  adding Qualifiers to %s ...",ParmKind2Str(cf->curPK,buf));   if (cf->MatTranFN != NULL) { /* Do the generic checks that the matrix is appropriate */      if (((cf->matPK&BASEMASK)&(cf->curPK&BASEMASK)) != (cf->matPK&BASEMASK))         HError(999,"Incorrect source parameter type (%s %s)",ParmKind2Str(cf->curPK,buff1),                ParmKind2Str(cf->matPK,buff2));      if ((HasEnergy(cf->matPK) && !(HasEnergy(cf->curPK))) || (!(HasEnergy(cf->matPK)) && (HasEnergy(cf->curPK))) ||          (HasZeroc(cf->matPK) && !(HasZeroc(cf->curPK))) || (HasZeroc(cf->curPK) && !(HasZeroc(cf->matPK))))         HError(999,"Incorrect qualifiers in parameter type (%s %s)",ParmKind2Str(cf->curPK,buff1),ParmKind2Str(cf->matPK,buff2));   }   if ((cf->MatTranFN != NULL) && (cf->preQual)) {      if ((HasZerom(cf->matPK) && !(HasZerom(cf->curPK))) || (HasZerom(cf->curPK) && !(HasZerom(cf->matPK))))         HError(999,"Incorrect qualifiers in parameter type (%s %s)",ParmKind2Str(cf->curPK,buff1),ParmKind2Str(cf->matPK,buff2));      ApplyStaticMat(cf,data,cf->MatTran,cf->nCols,nRows,0,0);      pbuf->main.nRows -= (cf->postFrames + cf->preFrames);      nRows = pbuf->main.nRows;      cf->nSamples = pbuf->main.nRows;   }   if ((cf->MatTranFN != NULL) && (!cf->preQual)) {       tgtBase = cf->tgtPK&BASEMASK;      if ((cf->curPK&BASEMASK)!=tgtBase && (tgtBase==LPCEPSTRA || tgtBase==MFCC))         size = TotalComps(cf->numCepCoef,cf->tgtPK);      else          size = TotalComps(NumStatic(cf->srcUsed,cf->srcPK),cf->tgtPK);      FindSpans(span,cf->tgtPK,size);       } else       FindSpans(span,cf->tgtPK,cf->tgtUsed);       /* Add any required difference coefficients */   if ((cf->tgtPK&HASDELTA) && !(cf->curPK&HASDELTA)){         d = span[5]-span[4]+1; si = span[0]; ti = span[4];      if (trace&T_QUA)         printf("\nHParm:  adding %d deltas to %d rows",d,nRows);      /* Deltas need to preceed everything a little when they can */      if (hdValid>0) ds=pbuf->qwin-cf->delWin; else ds=0;      if (tlValid>0) de=nRows+pbuf->qwin-cf->delWin-1; else de=nRows-1;      AddDiffs(data+cf->nCols*ds,de-ds+1,cf->nCols,si,ti,d,cf->delWin,               hdValid+ds,tlValid-ds,cf->v1Compat,cf->simpleDiffs);      cf->curPK |= HASDELTA; cf->nUsed += d;   }   if ((cf->tgtPK&HASACCS) && !(cf->curPK&HASACCS)) {      d = span[7]-span[6]+1; si = span[4]; ti = span[6];      if (trace&T_QUA)         printf("\nHParm:  adding %d accs to %d rows",d,nRows);      /* Treatment of deltas ensures that whole margin is valid */      AddDiffs(data,nRows,cf->nCols,si,ti,d,cf->accWin,               hdValid,tlValid,cf->v1Compat,cf->simpleDiffs);      cf->curPK |= HASACCS;  cf->nUsed += d;      if ((cf->tgtPK&HASTHIRD) && !(cf->curPK&HASTHIRD)) {         d = span[9]-span[8]+1; si = span[6]; ti = span[8];         if (trace&T_QUA)            printf("\nHParm:  adding %d thirds to %d rows",d,nRows);         AddDiffs(data,nRows,cf->nCols,si,ti,d,cf->thirdWin,                  hdValid,tlValid,cf->v1Compat,cf->simpleDiffs);         cf->curPK |= HASTHIRD;  cf->nUsed += d;         /* Adding fourth order differentials */         if (highDiff == TRUE) {            d = span[11]-span[10]+1; si = span[8]; ti = span[10];            if (trace&T_QUA)               printf("\nHParm:  adding %d fourths to %d rows\n",d,nRows);            AddDiffs(data,nRows,cf->nCols,si,ti,d,cf->fourthWin,                     hdValid,tlValid,cf->v1Compat,cf->simpleDiffs);            cf->nUsed += d;         }      }   }   /* Zero Mean the static coefficients if required */   if ((cf->tgtPK&HASZEROM) && !(cf->curPK&HASZEROM)) {      /* if a global mean vector is not available  */      if (cf->cMeanVector ==  0) {         if (cf->MatTranFN == NULL || (!cf->preQual)) {            d = span[1]-span[0]+1;            if (cf->tgtPK&HASZEROC && !(cf->curPK&HASNULLE))  /* zero mean c0 too */               ++d;           } else { /* No idea where the statics are so do everything .... */            d = span[1]-span[0]+1;            if (cf->tgtPK&HASZEROC && !(cf->curPK&HASNULLE)) d++;            if (cf->tgtPK&HASENERGY && !(cf->curPK&HASNULLE)) d++;         }         if (trace&T_QUA)            printf("\nHParm:  zero-meaning first %d cols from %d rows",d,nRows);         FZeroMean(data,d,nRows,cf->nCols);                  cf->curPK |= HASZEROM;      }      /* if a global cepstral mean file is available */      else {         /* subtract the mean vector from cf */         d = VectorSize(cf->cMeanVector);         step = cf->nCols;         for ( i=0; i<d ; i++){            /* subtract mean from i'th components */            fp = data+i;            

⌨️ 快捷键说明

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