예제 #1
0
파일: sta_reo.c 프로젝트: gitgun/dLabPro
/*
 * Manual page at statistics.def
 */
INT16 CGEN_PUBLIC CStatistics_Merge(CStatistics* _this, CStatistics* iSrc)
{
	INT32   nB    = 0;                                                            /* Current block                     */
	INT32   nC    = 0;                                                            /* Current component                 */
	INT32   nR    = 0;                                                            /* Current record                    */
	INT32   nXB   = 0;                                                            /* Number of statistics blocks       */
	INT32   nXC   = 0;                                                            /* Number of components              */
	INT32   nXR   = 0;                                                            /* Number of records per block       */
	FLOAT64 nVal  = 0.;                                                           /* Merged statistics value           */
	FLOAT64 nVal1 = 0.;                                                           /* Statistics value of this instance */
	FLOAT64 nVal2 = 0.;                                                           /* Statistics value of source inst.  */

  /* Initialize */                                                              /* --------------------------------- */
  CHECK_THIS_RV(NOT_EXEC);                                                      /* Check this instance               */
  if (_this==iSrc) return O_K;                                                  /* Nothing to be done                */
  IF_NOK(CStatistics_Check(iSrc))                                               /* Check source statistics           */
    return IERROR(_this,ERR_INVALARG,"iSrc",0,0);                               /* ...                               */

  /* Check-up */                                                                /* --------------------------------- */
  nXB = CData_GetNBlocks      (AS(CData,_this->m_idDat));                       /* Get number of statistics blocks   */
  nXC = CData_GetNComps       (AS(CData,_this->m_idDat));                       /* Get number of components          */
  nXR = CData_GetNRecsPerBlock(AS(CData,_this->m_idDat));                       /* Get number of records per block   */
  if                                                                            /* Instances incompatible if ...     */
  (                                                                             /* |                                 */
    nXB != CData_GetNBlocks      (AS(CData,iSrc->m_idDat)) ||                   /* | ... no. of blocks mismatch   OR */
    nXC != CData_GetNComps       (AS(CData,iSrc->m_idDat)) ||                   /* | ... no. of comps. mismatch   OR */
    nXR != CData_GetNRecsPerBlock(AS(CData,iSrc->m_idDat))                      /* | ... no. of records mismatch     */
  )                                                                             /* |                                 */
  {                                                                             /* >>                                */
  	return IERROR(_this,STA_INCOMPAT,0,0,0);                                    /*   So sorry ...                    */
  }                                                                             /* <<                                */

  /* Do merge */                                                                /* --------------------------------- */
  for (nB=0; nB<nXB; nB++)                                                      /* Loop over blocks                  */
  	for (nR=0; nR<nXR; nR++)                                                    /*   Loop over records of block      */
  		for (nC=0; nC<nXC; nC++)                                                  /*     Loop over components          */
			{                                                                         /*     >>                            */
				nVal1 = CData_Dfetch(AS(CData,_this->m_idDat),nB*nXR+nR,nC);            /*       Get value from this         */
				nVal2 = CData_Dfetch(AS(CData,iSrc ->m_idDat),nB*nXR+nR,nC);            /*       Get value from source       */
				switch (nR)                                                             /*       Depending on value type     */
				{                                                                       /*       >>                          */
				case STA_DAI_MIN: nVal = MIN(nVal1,nVal2); break;                       /*         Aggregate minimums        */
				case STA_DAI_MAX: nVal = MAX(nVal1,nVal2); break;                       /*         Aggregate maximums        */
				default         : nVal = nVal1+nVal2;      break;                       /*         Aggregate all other       */
				}                                                                       /*       <<                          */
				CData_Dstore(AS(CData,_this->m_idDat),nVal,nB*nXR+nR,nC);               /*       Store merged value          */
			}                                                                         /*     <<                            */

  /* Aftermath */                                                               /* --------------------------------- */
  return O_K;                                                                   /* Everything's ok                   */
}
예제 #2
0
파일: fnc_iam.cpp 프로젝트: gitgun/dLabPro
/*
 * Manual page at function.def
 */
const char* CGEN_PROTECTED CFunction::Argv(INT32 nArg)
{
  CData* idArg = NULL;
  idArg = GetRootFnc() ? GetRootFnc()->m_idArg : m_idArg;
  if (nArg<1 || nArg>CData_GetNRecs(idArg)) return NULL;
  if (CData_Dfetch(idArg,nArg-1,FNC_ALIC_TYPE)!=T_STRING) return NULL;
  return *(const char**)CData_Pfetch(idArg,nArg-1,FNC_ALIC_PTR);
}
예제 #3
0
파일: fst_ixp.c 프로젝트: gitgun/dLabPro
/*
 * Manual page at fst_man.def
 */
INT16 CGEN_PUBLIC CFst_CopyUi(CFst* _this, CFst* itSrc, CData* idIndex, INT32 nPar)
{
  INT32 i  = 0;
  INT32 nU = 0;

  /* Validate */
  CHECK_THIS_RV(NOT_EXEC);
  CFst_Check(_this);
  CFst_Check(itSrc);

  if (idIndex)
  {
    if (CData_IsEmpty(idIndex)) return NOT_EXEC;
    if (nPar<0)
      for (i=0; i<CData_GetNComps(idIndex); i++)
        if (dlp_is_numeric_type_code(CData_GetCompType(idIndex,i)))
          { nPar=i; break; }

    if
    (
      nPar<0 || nPar>=CData_GetNComps(idIndex) ||
      !dlp_is_numeric_type_code(CData_GetCompType(idIndex,nPar))
    )
    {
      return IERROR(_this,FST_BADID,"component",nPar,0);
    }
  }

  /* Initialize */
  CREATEVIRTUAL(CFst,itSrc,_this);
  CFst_Reset(BASEINST(_this),TRUE);
  
  if (idIndex)
  {
    /* Loop over records of idIndex */
    for (i=0; i<CData_GetNRecs(idIndex); i++)
    {
      nU = (INT32)CData_Dfetch(idIndex,i,nPar);
      if (nU>=0 && nU<UD_XXU(itSrc)) {
        DLPASSERT(OK(CFst_CatEx(_this,itSrc,nU,1)))
      } else IERROR(_this,FST_BADID2,"unit",nU,0);
    }
  }
  else if (nPar<0)
예제 #4
0
파일: file_midi.c 프로젝트: thias42/dLabPro
/**
 * Import midi notes of a midifile into data, needs external program midiconvert
 *
 * @param lpsFilename Name of file to import
 * @param iDst        Pointer to instance to import
 * @param lpsFiletype Type of file to import
 * @return <code>O_K</code> if successful, a (negative) error code otherwise
 */
INT16 CGEN_PROTECTED CDlpFile_Midi_ImportMidi
(
  CDlpFile*   _this,
  const char* lpsFilename,
  CDlpObject* iDst,
  const char* lpsFiletype
)
{
   char  lpsTempFile[L_PATH];
   char  lpsCmdline    [3*L_PATH] = "";
   INT16 nErr                     =O_K;

   strcpy(lpsTempFile,dlp_tempnam(NULL,"dlabpro_midi_import"));
   sprintf(lpsCmdline,"midiconvert %s %s", lpsFilename, lpsTempFile);

   if (system(lpsCmdline)!=0) { nErr=IERROR(_this,FIL_EXEC,lpsCmdline,0,0); }
   else {
      CData *idDst = AS(CData,iDst);
      /*Prepare data*/
      CData_Reset(iDst,TRUE);
      CData_AddComp(idDst,"CHAN",T_UCHAR);
      CData_AddComp(idDst,"VOL",T_UCHAR);
      CData_AddComp(idDst,"INST",T_UCHAR);
      CData_AddComp(idDst,"NOTE",T_UCHAR);
      CData_AddComp(idDst,"TIME",T_UINT);
      CData_AddComp(idDst,"LGTH",T_UINT);
      CData_AddComp(idDst,"VEL",T_UCHAR);
      /*import*/
      IF_NOK(CDlpFile_ImportAsciiToData(_this,lpsTempFile,iDst,"csv"))
        nErr = IERROR(iDst,FIL_IMPORT,lpsTempFile,"csv",0);
      /*Set midifilter specific data descriptions and clean data*/
      CData_SetDescr(idDst, DESCR0, CData_Dfetch(idDst,0,5));
      CData_DeleteRecs(idDst,0,1);
   }
   if (remove(lpsTempFile)==-1) nErr=IERROR(_this,FIL_REMOVE,"temporary ",lpsTempFile,0);
   /* Clean up */
   return nErr;
}
예제 #5
0
파일: sta_reo.c 프로젝트: gitgun/dLabPro
/*
 * Manual page at statistics.def
 */
INT16 CGEN_PUBLIC CStatistics_Pool
(
  CStatistics* _this,
  CStatistics* iSrc,
  CData*       idMap
)
{
  INT32   i          = 0;                                                        /* Current component index           */
  INT32   nC         = 0;                                                        /* Current pooled statistics class   */
  INT32   nCs        = 0;                                                        /* Current source class index        */
  INT32   nXC        = 0;                                                        /* Number of pooled classes          */
  INT32   nRpb       = 0;                                                        /* Statistics raw data block size    */
  INT16  nCheckSave = 0;                                                        /* Saved check level                 */
  CData* idAux      = NULL;                                                     /* Auxilary data instance #1         */
  CData* idPmp      = NULL;                                                     /* Pooling map                       */
  CData* idPcd      = NULL;                                                     /* Pooled class raw data buffer      */

  /* Initialize */                                                              /* --------------------------------- */
  CHECK_THIS_RV(0);                                                             /* Check this instance               */
  IF_NOK(CStatistics_Check(iSrc))                                               /* Check source statistics           */
    return IERROR(_this,ERR_INVALARG,"iSrc",0,0);                               /* ...                               */
  nCheckSave = _this->m_nCheck;                                                 /* Save check level                  */
  CStatistics_Reset(BASEINST(_this),TRUE);                                      /* Reset destination                 */
  _this->m_nCheck = nCheckSave;                                                 /* Restore check level               */
  IFIELD_RESET(CData,"dat");                                                    /* Create pool raw stats. data inst. */

  /* Protocol */                                                                /* --------------------------------- */
  IFCHECK                                                                       /* On verbose level 1                */
  {                                                                             /* >>                                */
    printf("\n"); dlp_fprint_x_line(stdout,'-',dlp_maxprintcols());             /*   Print protocol header           */
    printf("\n   statistics -pool");                                            /*   ...                             */
    printf("\n"); dlp_fprint_x_line(stdout,'-',dlp_maxprintcols());printf("\n");/*   ...                             */
  }                                                                             /* <<                                */

  /* No map --> pool all classes */                                             /* --------------------------------- */
  if (CData_IsEmpty(idMap))                                                     /* NULL or empty map instance        */
  {                                                                             /* >>                                */
    IFCHECK printf("\n   Empty pooling map --> pool all classes");              /*   Protocol (verbose level 1)      */
    CStatistics_PoolInt(_this->m_idDat,iSrc->m_idDat,0);                        /*   Pool sum data                   */
    CStatistics_PoolInt(_this->m_idDat,iSrc->m_idDat,1);                        /*   Pool min data                   */
    CStatistics_PoolInt(_this->m_idDat,iSrc->m_idDat,2);                        /*   Pool max data                   */
    STA_PROTOCOL_FOOTER(1,"done");                                              /*   Print protocol footer           */
    return O_K;                                                                 /*   That's it                       */
  }

  IFCHECK printf("\n   Pooling by map");                                        /* Protocol (verbose level 1)        */
  ICREATEEX(CData,idAux,"CStatistics_Pool.~idAux",NULL);                        /* Create auxilary data instance #1  */
  ICREATEEX(CData,idPmp,"CStatistics_Pool.~idPmp",NULL);                        /* Create pooling map                */
  ICREATEEX(CData,idPcd,"CStatistics_Pool.~idPcs",NULL);                        /* Create pooled raw stats.data inst.*/

  /* Find and copy map component (pooled class) */                              /* --------------------------------- */
  for (i=0; i<CData_GetNComps(idMap); i++)                                      /* Loop over components of idMap     */
    if (dlp_is_numeric_type_code(CData_GetCompType(idMap,i)))                   /*   Is current component numeric?   */
    {                                                                           /*   >> (Yes)                        */
      CData_SelectComps(idPmp,idMap,i,1);                                       /*     Copy component                */
      break;                                                                    /*     Have ready :)                 */
    }                                                                           /*   <<                              */
  if (CData_IsEmpty(idPmp))                                                     /* Have not got map component        */
  {                                                                             /* >>                                */
    IERROR(_this,STA_BADCOMP,"map",BASEINST(idMap)->m_lpInstanceName,"numeric");/*   Error message                   */
    DLPTHROW(STA_BADCOMP);                                                      /*   Throw exception                 */
  }                                                                             /* <<                                */

  /* Create source class component */                                           /* --------------------------------- */
  CData_AddComp(idPmp,"srcc",T_LONG);                                           /* Add source class index component  */
  for (i=0; i<CData_GetNRecs(idPmp); i++) CData_Dstore(idPmp,i,i,1);            /* Fill it                           */

  /* Finish pooling map and initialize pooling */                               /* --------------------------------- */
  CData_Sortup(idPmp,idPmp,0);                                                  /* Sort map by pooled class index    */
  nXC = (INT32)CData_Dfetch(idPmp,CData_GetNRecs(idPmp)-1,0)+1;                 /* Get greatest pooled class index   */
  IFCHECK printf("\n   Pooling %ld statistics classes",(long)nXC);              /* Protocol (verbose level 1)        */
  IFCHECKEX(3) CData_Print(idPmp);                                              /* Print pooling map (verbose lvl.3) */
  nRpb = CData_GetNRecsPerBlock(iSrc->m_idDat);                                 /* Get block size                    */

  /* Prepare pooled statistics */                                               /* --------------------------------- */
  CData_Scopy(_this->m_idDat,iSrc->m_idDat);                                    /* Create target raw data components */
  CData_Allocate(_this->m_idDat,nRpb*nXC);                                      /* Allocate target raw data          */
  CData_SetNBlocks(_this->m_idDat,nXC);                                         /* Set target statistics block number*/

  /* Pooling loop */                                                            /* --------------------------------- */
  for (i=0; i<CData_GetNRecs(idPmp); )                                          /* Loop over pooling map             */
  {                                                                             /* >>                                */
    /* - Copy raw statistics data of one pooled class */                        /*   - - - - - - - - - - - - - - - - */
    nC = (INT32)CData_Dfetch(idPmp,0,0);                                         /*   Get pooled class index          */
    IFCHECK printf("\n     Pooled class %3ld");                                 /*   Protocol (verbose level 1)      */
    for (i=0; i<CData_GetNRecs(idPmp); i++)                                     /*   Loop over partition of pool.map */
    {                                                                           /*   >>                              */
      if (nC != (INT32)CData_Dfetch(idPmp,0,0)) break;                           /*     Not the current class anymore */
      nCs = (INT32)CData_Dfetch(idPmp,i,1);                                      /*     Get source class index        */
      IFCHECK printf("\n     - Source class %3ld",nCs);                         /*     Protocol (verbose level 1)    */
      CData_SelectBlocks(idAux,iSrc->m_idDat,nCs,1);                            /*     Copy raw stats. data block    */
      CData_Cat(idPcd,idAux);                                                   /*     Append to buffer              */
    }                                                                           /*   <<                              */

    /* - Pool data */                                                           /*   - - - - - - - - - - - - - - - - */
    CData_SetNBlocks(idPcd,CData_GetNRecs(idPcd)/nRpb);                         /*   Set block count of aggr. buffer */
    IFCHECK                                                                     /*   Protocol (verbose level 1)      */
      printf("\n     - Aggregating %ld statistics classes",                     /*   |                               */
      (long)CData_GetNBlocks(idPcd));                                           /*   |                               */
    CStatistics_PoolInt(idAux,idPcd,0);                                         /*   Pool sum data                   */
    CStatistics_PoolInt(idAux,idPcd,1);                                         /*   Pool min data                   */
    CStatistics_PoolInt(idAux,idPcd,2);                                         /*   Pool max data                   */

    /* - Store pooled raw statistics data block */                              /*   - - - - - - - - - - - - - - - - */
    dlp_memmove                                                                 /*   Copy pooled raw stats. data     */
    (                                                                           /*   |                               */
      CData_XAddr(_this->m_idDat,nC*nRpb,0),                                    /*   | To target statistics block    */
      CData_XAddr(idAux,0,0),                                                   /*   | From aggregation buffer       */
      CData_GetNRecs(idAux)*CData_GetRecLen(idAux)                              /*   | Length of aggregation buffer  */
    );                                                                          /*   |                               */

    /* - Clean up auxilary instances */                                         /*   - - - - - - - - - - - - - - - - */
    CData_Reset(idPcd,TRUE);                                                    /*   Clear aggregation buffer        */
  }

  /* Clean up */                                                                /* --------------------------------- */
  IDESTROY(idAux);                                                              /* Destroy auxilary data instance #1 */
  IDESTROY(idPmp);                                                              /* Destroy pooling map               */
  IDESTROY(idPcd);                                                              /* Destroy pooled cls. raw data inst.*/
  STA_PROTOCOL_FOOTER(1,"done");                                                /* Print protocol footer             */
  return O_K;                                                                   /* Ok                                */

DLPCATCH(STA_BADCOMP)                                                           /* == Catch STA_BADCOMP exception    */
  IDESTROY(idAux);                                                              /* Destroy auxilary data instance #1 */
  IDESTROY(idPmp);                                                              /* Destroy pooling map               */
  IDESTROY(idPcd);                                                              /* Destroy pooled cls. raw data inst.*/
  STA_PROTOCOL_FOOTER(1,"FAILED");                                              /* Print protocol footer             */
  return NOT_EXEC;                                                              /* Not ok                            */
}
예제 #6
0
파일: gmm_core.c 프로젝트: gitgun/dLabPro
INT16 CGmm_PrecalcD(CGmm* _this, BOOL bCleanup)
#endif
{
  INT32      i       = 0;                                                        /* Triangular inv. cov. matrix cntr. */
  INT32      c       = 0;                                                        /* Index of inv. covariance matrix   */
  INT32      k       = 0;                                                        /* Gaussian loop counter             */
  INT32      n       = 0;                                                        /* Dimension loop counter            */
  INT32      m       = 0;                                                        /* Dimension loop counter            */
  INT32      K       = 0;                                                        /* Number of single Gaussians        */
  INT32      N       = 0;                                                        /* Feature space dimensionality      */
  GMM_FTYPE nDelta1 = 0.;                                                       /* Class indep. term of delta const. */
#ifdef __TMS
  GMM_FTYPE *mean = (GMM_FTYPE*)CData_XAddr(AS(CData,_this->m_idMean),0,0);
#endif


  /* Clean up precalculated data */                                             /* --------------------------------- */
  dlp_free(_this->m_lpAlpha);                                                   /* Clear alpha vector                */
  dlp_free(_this->m_lpBeta );                                                   /* Clear beta vectors                */
  dlp_free(_this->m_lpGamma);                                                   /* Clear gamma vector                */
  dlp_free(_this->m_lpDelta);                                                   /* Clear delta vector                */
  dlp_free(_this->m_lpI    );                                                   /* Clear inv. cov. pointer array     */
  dlp_free(_this->m_lpV    );                                                   /* Clear inverse variance array      */
  if(_this->m_idSse2Icov){
    CData *idSse2Icov=AS(CData,_this->m_idSse2Icov);
    IDESTROY(idSse2Icov);                                                     /* Destroy inv.covs. for SSE2 opt.   */
    _this->m_idSse2Icov=NULL;
  }
  dlp_free(_this->m_lpSse2Buf);                                                 /* Clear aux. buffer for SSE2 opt.   */
  dlp_free(_this->m_lpLdlL);                                                    /* Clear L-matrix buffer for LDL     */
  dlp_free(_this->m_lpLdlD);                                                    /* Clear D-vector buffer for LDL     */
  _this->m_nN = 0;                                                              /* Clear feature space dimensionality*/
  _this->m_nK = 0;                                                              /* Clear number of single Gaussians  */
  if (bCleanup) return O_K;                                                     /* When cleaning up that's it        */

  /* Initialize */                                                              /* --------------------------------- */
  K       = CGmm_GetNGauss(_this);                                              /* Get nuumber of single Gaussians   */
  N       = CGmm_GetDim(_this);                                                 /* Get feature space dimensionality  */
  nDelta1 = -N/2*log(2*F_PI);                                                   /* Compute part of delta term        */

  /* Basic validation */                                                        /* --------------------------------- */
  IF_NOK(CGmm_CheckMean(_this)) DLPTHROW(GMM_NOTSETUP);                         /* Check mean vectors                */
  IF_NOK(CGmm_CheckIvar(_this)) DLPTHROW(GMM_NOTSETUP);                         /* Check inverse variance vectors    */
  IF_NOK(CGmm_CheckCdet(_this)) DLPTHROW(GMM_NOTSETUP);                         /* Check (co-)variance determinants  */

  /* Basic dimensions */                                                        /* --------------------------------- */
  _this->m_nN = N;                                                              /* Feature space dimensionality      */
  _this->m_nK = K;                                                              /* Number of single Gaussians        */

  /* Create inverse covariance matrix map */                                    /* --------------------------------- */
  if (_this->m_idIcov)                                                          /* If using covariances              */
  {                                                                             /* >>                                */
    IF_NOK(CGmm_CheckIcov(_this)) DLPTHROW(GMM_NOTSETUP);                       /*   Inverse covariance data corrupt */
    _this->m_lpI = dlp_calloc(K,sizeof(GMM_FTYPE*));                            /*   Allocate map                    */
    if (!_this->m_lpI) DLPTHROW(ERR_NOMEM);                                     /*   Out of memory                   */
    for (k=0; k<K; k++)                                                         /*   Loop over Gaussians             */
    {                                                                           /*   >>                              */
      c=_this->m_idCmap ? (INT32)CData_Dfetch(AS(CData,_this->m_idCmap),k,0) : k;/*     Cov. mat. idx. for Gaussian k */
      I[k] = (GMM_FTYPE*)CData_XAddr(AS(CData,_this->m_idIcov),c,0);            /*     Store ptr. to inv. cov. matrix*/
    }                                                                           /*   <<                              */
  }                                                                             /* <<                                */

  /* Create inverse variance vector map */                                      /* --------------------------------- */
  IF_NOK(CGmm_CheckIvar(_this)) DLPTHROW(GMM_NOTSETUP);                         /*   Inverse covariance data corrupt */
  _this->m_lpV = dlp_calloc(K,sizeof(GMM_FTYPE*));                              /*   Allocate map                    */
  if (!_this->m_lpV) DLPTHROW(ERR_NOMEM);                                       /*   Out of memory                   */
  for (k=0; k<K; k++)                                                           /*   Loop over Gaussians             */
    V[k] = (GMM_FTYPE*)CData_XAddr(AS(CData,_this->m_idIvar),k,0);              /*     Store ptr. to inv. cov. matrix*/

  /* LDL-factorization */                                                       /* --------------------------------- */
  if(_this->m_nLDL)                                                             /* LDL factorization used            */
  {                                                                             /* >>                                */
    /* TODO: GMM_TYPE is float => dlm_factldl in float */                       /*   ------------------------------- */
    _this->m_lpLdlL = dlp_malloc(K*N*(N-1)/2*sizeof(GMM_FTYPE));                /*   Alloc L matrix                  */
    _this->m_lpLdlD = dlp_malloc(K*N*sizeof(GMM_FTYPE));                        /*   Alloc D vector                  */
    if (!_this->m_lpLdlL || !_this->m_lpLdlD) DLPTHROW(ERR_NOMEM);              /*   Out of memory                   */
    {
    FLOAT64 *lpAux1 = (FLOAT64 *)dlp_malloc(N*N*sizeof(FLOAT64));                  /*   Alloc auxilary matrix #1        */
    FLOAT64 *lpAux2 = (FLOAT64 *)dlp_malloc(N*N*sizeof(FLOAT64));                  /*   Alloc auxilary matrix #2        */
    if (!lpAux1 || !lpAux2) DLPTHROW(ERR_NOMEM);                                /*   Out of memory                   */
    for(k=0;k<K;k++)                                                            /*   Loop over all Gaussians         */
    {                                                                           /*   >>                              */
      for(n=0;n<N;n++)                                                          /*     Loop over Gaussian dimension  */
      {                                                                         /*     >>                            */
        lpAux1[n*N+n] = V[k][n];                                                /*       Copy inverse variance values*/
        for(m=0;m<n;m++) lpAux1[n*N+m] = lpAux1[m*N+n] =                        /*       Copy inverse covariance val.*/
          I?I[k][m*N-2*m-m*(m-1)/2+n-1]:0.;                                     /*       |                           */
      }                                                                         /*     <<                            */
      dlm_factldl(lpAux1,lpAux2,N);                                             /*     Factorize matrix              */
      for(n=0;n<N;n++)                                                          /*     Loop over Gaussian dimension  */
      {                                                                         /*     >>                            */
        for(m=n+1;m<N;m++) Li(N,k,n,m) = lpAux1[n*N+m];                         /*       Store L matrix values       */
        D(N,k,n) = lpAux2[n*N+n];                                               /*       Store D vector values       */
      }                                                                         /*     <<                            */
      if(_this->m_nLdlCoef>=0 && _this->m_nLdlCoef<N*(N-1)/2)
      {
        FLOAT64 nMinAbs;
        memcpy(lpAux1,&L(N,k,0),N*(N-1)/2);
#if GMM_FTYPE_CODE == T_FLOAT
        qsort(lpAux1,N*(N-1)/2,sizeof(FLOAT64),cf_absfloat_down);
#else
        qsort(lpAux1,N*(N-1)/2,sizeof(FLOAT64),cf_absdouble_down);
#endif
        nMinAbs=fabs(lpAux1[_this->m_nLdlCoef]);
        for(n=0;n<N*(N-1)/2;n++) if(fabs(L(N,k,n))<=nMinAbs) L(N,k,n)=0.;
      }
    }                                                                           /*   <<                              */
    dlp_free(lpAux1);                                                           /*   Free auxilary matrix #1         */
    dlp_free(lpAux2);                                                           /*   Free auxilary matrix #2         */
    }
  }                                                                             /* <<                                */

  /* Precalculate alpha and beta vectors */                                     /* --------------------------------- */
  if(!_this->m_nLDL)                                                            /* No LDL factorization used         */
  {                                                                             /* >>                                */
    _this->m_lpAlpha = dlp_calloc(K,sizeof(GMM_FTYPE));                         /*   Allocate buffer                 */
    _this->m_lpBeta  = dlp_calloc(K*N,sizeof(GMM_FTYPE));                       /*   Allocate buffer                 */
    if (!_this->m_lpAlpha || !_this->m_lpBeta) DLPTHROW(ERR_NOMEM);             /*   Out of memory                   */
    for (k=0; k<K; k++)                                                         /*   Loop over Gaussians             */
      for (n=0; n<N; n++)                                                       /*     Loop over dimensions          */
        for (m=0; m<N; m++)                                                     /*       Loop over dimensions        */
          if (m==n)                                                             /*         Main diagonal (variances)?*/
          {                                                                     /*         >>                        */
            alpha[k]     += V[k][n]*mu(k,m)*mu(k,n);                            /*           Sum up alpha val. (n==m)*/
            beta [k*N+n] += V[k][n]*mu(k,m);                                    /*           Sum up beta value (n==m)*/
          }                                                                     /*         <<                        */
          else if (_this->m_lpI)                                                /*         Otherwise cov.(if present)*/
          {                                                                     /*         >>                        */
            if (m>n) i = n*N - 2*n - n*(n-1)/2 + m - 1;                         /*           Calc index if I[n,m] in */
            else     i = m*N - 2*m - m*(m-1)/2 + n - 1;                         /*           | triangular icov. mat. */
            alpha[k]     += I[k][i]*mu(k,m)*mu(k,n);                            /*           Sum up alpha val. (n!=m)*/
            beta [k*N+n] += I[k][i]*mu(k,m);                                    /*           Sum up beta value (n!=m)*/
          }                                                                     /*         <<                        */
  }                                                                             /* <<                                */
  else                                                                          /* LDL factorization used            */
  {                                                                             /* >>                                */
    _this->m_lpAlpha = NULL;                                                    /*   No alpha needed here            */
    _this->m_lpBeta  = dlp_calloc(K*N,sizeof(GMM_FTYPE));                       /*   Allocate buffer                 */
    if (!_this->m_lpBeta) DLPTHROW(ERR_NOMEM);                                  /*   Out of memory                   */
    for(k=0;k<K;k++) for(n=0;n<N;n++)                                           /*   Loop over Gaussians & dimensions*/
    {                                                                           /*   >>                              */
      beta[k*N+n] = mu(k,n);                                                    /*     Init beta with mean value     */
      for(m=n+1;m<N;m++) beta[k*N+n] += mu(k,m)*Li(N,k,n,m);                    /*     Calculate beta from L*mu      */
    }                                                                           /*   <<                              */
  }                                                                             /* <<                                */

  /* Allocate gamma vector */                                                   /* --------------------------------- */
  /* NOTE: If this fails there will just be no lazy computation -> no big deal*//*                                   */
  if (_this->m_idCmap && _this->m_idIcov && _this->m_lpI)                       /* Wanna do lazy computation?        */
    if (CData_GetDescr(AS(CData,_this->m_idIvar),0)==0.)                        /*   Only if variances are tied too  */
      _this->m_lpGamma = dlp_calloc(K,sizeof(GMM_FTYPE));                       /*     Allocate buffer               */

  /* Precalculate delta vector */                                               /* --------------------------------- */
  _this->m_lpDelta = dlp_calloc(K,sizeof(GMM_FTYPE));                           /* Allocate buffer                   */
  if (!_this->m_lpDelta) DLPTHROW(ERR_NOMEM);                                   /* Out of memory                     */
  for (k=0; k<K; k++)                                                           /* Loop over Gaussians               */
    delta[k] = nDelta1-(GMM_FTYPE)(0.5*log(                                     /*   Calculate delta[k]              */
      CData_Dfetch(AS(CData,_this->m_idCdet),k,0)));                            /*   |                               */

  /* SSE2 precalculated objects */                                              /* --------------------------------- */
#ifdef GMM_SSE2                                                                 /* Use SSE2?                         */
  IFIELD_RESET(CData,"sse2_icov");                                              /* Create/reset SSE2 inv.cov. matrs. */
  CGmm_Extract(_this,NULL,_this->m_idSse2Icov);                                 /* ... and go get 'em                */
  _this->m_lpSse2Buf = dlp_calloc(2*N,sizeof(GMM_FTYPE));                       /* Allocate auxilary buffer          */
  if (!_this->m_lpSse2Buf) DLPTHROW(ERR_NOMEM);                                 /* Out of memory                     */
#endif                                                                          /* #ifdef GMM_SSE2                   */

  /* Final checkup */                                                           /* --------------------------------- */
#ifndef __NOXALLOC
  IF_NOK(CGmm_CheckPrecalc(_this)) DLPTHROW(GMM_NOTSETUP);                      /* Check precalculated data          */
#endif
  return O_K;                                                                   /* That's it folks ...               */

DLPCATCH(GMM_NOTSETUP)                                                          /* On NOT_EXEC exception             */
DLPCATCH(ERR_NOMEM)                                                             /* On ERR_NOMEM exception            */
#if GMM_FTYPE_CODE == T_FLOAT
  CGmm_PrecalcF(_this,TRUE);                                                    /* - Free memory of precalc'd. data  */
#else
  CGmm_PrecalcD(_this,TRUE);                                                    /* - Free memory of precalc'd. data  */
#endif
  return NOT_EXEC;                                                              /* - Indicate failure                */
}
예제 #7
0
파일: pm2f0.cpp 프로젝트: thias42/dLabPro
INT16 CGEN_PRIVATE CProsody::PmFo(data *dPM, INT32 nSrate, INT32 nSrateF0, data *dF0)    
{
  /* Initialization */
  INT32 i = 0;
  INT32 ii = 0;
  INT32 j = 0;  
  INT32 nPitchMark = 0;         // Number of Pitch Marks  
  FLOAT64 nAuxValue = 0;         // Auxiliary value
  INT32 nSumSamplePM = 0;         // Sum of PM from (0) to last PM in (samples)
  INT32 nSrate_Ratio = 0;        // Sample Rate Ratio = nSrate / nSrateF0


  
  /* Validation of data instance dF0 */
  if (!dF0 || !dF0->IsEmpty())    // If dF0 not exist or empty then return false
    return NOT_EXEC;            // NOT_EXEC = -1 (Generic error)
 
 
  /* Definition of data instance dF0 */
  // One column for F0 values (Hz)  
    dF0->AddNcomps(T_DOUBLE, 1);
    dF0->SetCname(0, "nF0");

  
     /* Number of Pitch Marks */
  nPitchMark  = dPM->GetNRecs();  // GetNRecs returns the number of valid records in the data object 
  //fprintf(stdout,"\nNumber of Pitch Marks:   %i\n",nPitchMark);

  /* Validation of data instance dPM */
  if ( nPitchMark == 0 )    // If there is no Pitch mark
  {
    //fprintf(stdout,"\nError: There is no Pitch Marks in the speech signal.");
    //return NOT_EXEC;            // NOT_EXEC = -1 (Generic error)
    return IERROR(this,NO_PM,0,0,0);
  }  

    
  /* Validation of sample rate */
    if(nSrate == 0)
    {
      //fprintf(stdout, "\nError: Sample rate for speech signal (PM file) is not specified");
      //return NOT_EXEC;
      return IERROR(this,NO_SRate,0,0,0);
    }


  /* Convert PM data object (2 columns [nPer][anreg]) to two separate columns */
  /*----------------------------------------------------------------------- 
     Important Information:
     The output of the method (-analyze) for PMA is a PM data object of type short.
     This data object consist of two separate columns:
     nPer  : is the distance between PM in samples
       anreg : is the Anregung [ (0) for (voiceless or stimmlos) and (1) for (voiced or stimmhaft)] 
                       *** Important ***
       * The data object of PMproc class or data objects with more than one component
       * were stored in memory (record after record) (the first line, second line, third line, ...)
       * i.e. [r,c] = {[0,0],[0,1],[1,0],[1,1],[2,0],[2,1],....}
       ---------------------------------------------------------------------*/
  INT16 *idPMnPer = NULL;  // PMA [nPer] = column 1 
  INT16 *idPManreg = NULL; // PMA [anreg] = column 2
  
  idPMnPer = (INT16*)dlp_calloc(nPitchMark, sizeof(INT16));  // Allocates zero-initialize memory
  idPManreg = (INT16*)dlp_calloc(nPitchMark, sizeof(INT16));  // Allocates zero-initialize memory
  
  

  for (i = 0; i < nPitchMark; i++)
  {
    nAuxValue = CData_Dfetch(dPM,i,0);
    idPMnPer[i] = (INT16)nAuxValue;
    //fprintf(stdout,"PMA-nPer %i = %i",i,idPMnPer[i]);
      
    nAuxValue = CData_Dfetch(dPM,i,1);
    idPManreg[i] = (INT16)nAuxValue;
    //fprintf(stdout,"\t\tPMA-Anreg %i = %i\n",i,idPManreg[i]);  
  }
  
  /*
  fprintf(stdout,"\n\nThe nPer and Anreg of PM: \n");  // show data 
  //for(i=0; i<nPitchMark; i++)
  for(i=0; i<100; i++)  
    fprintf(stdout,"nPer %i = %i \t\t Anreg %i = %i \n",i,idPMnPer[i],i,idPManreg[i]);
  */


  /* Calculate the length of PM from (0) to last PM in (samples) 
     and allocate a vector with this length */
  for (i=0; i<nPitchMark; i++)
  {
    nSumSamplePM = nSumSamplePM + idPMnPer[i];
  }
  //fprintf(stdout,"\n\nNumber of samples from (0) to last PM:   %i\n",nSumSamplePM);

  
  // Allocate memory
  INT32 *idExpandPM = NULL; 
  idExpandPM = (INT32*)dlp_calloc(nSumSamplePM, sizeof(INT32));  // Allocates zero-initialize memory

  /*----- Produce Zahlenvektor for PMs -----*/
  INT16 nCurrentPMnPer = 0;    // Length of current PM in samples
  INT16 nCurrentPManreg = 0;    // Anregung of current PM
  INT16 nNextPManreg = 0;      // Anregung of next PM


  for (i = 0; i < nPitchMark-1; i++)
  {
    nCurrentPMnPer = idPMnPer[i];    // Length of current PM in samples
    nCurrentPManreg = idPManreg[i];    // Anregung of current PM
    nNextPManreg = idPManreg[i+1];    // Anregung of next PM
    
     /*----- Produce Zahlenvektor for (nPitchMark-1) PMs -----*/
     // PM current is unvoiced && PM next is unvoiced  =>  Values = 0
     if ( nCurrentPManreg == 0 && nNextPManreg == 0 )
     {
       for( ii = 0; ii < nCurrentPMnPer ; ii++ )
       {
         idExpandPM[j] = (INT32) 0;
         j++;
       }
     }
     // PM current is unvoiced && PM next is voiced  =>  Values = 0     
     else if ( nCurrentPManreg == 0 && nNextPManreg == 1 )
     {
       for( ii = 0; ii < nCurrentPMnPer ; ii++ )
       {
         idExpandPM[j] = (INT32) 0;
         j++;
       }
     }     
     // PM current is voiced && PM next is unvoiced  =>  Values = nCurrentPMnPer
     else if ( nCurrentPManreg == 1 && nNextPManreg == 0 )
     {
       for( ii = 0; ii < nCurrentPMnPer ; ii++ )
       {
         idExpandPM[j] = (INT32) nCurrentPMnPer;
         //idExpandPM[j] = (INT32) 0;
         j++;
       }
     }     
     // PM current is voiced && PM next is voiced  =>  Values = nCurrentPMnPer
     else   // else if( nCurrentPManreg == 1 && nNextPManreg == 1 )
     {
       for( ii = 0; ii < nCurrentPMnPer ; ii++ )
       {
         idExpandPM[j] = (INT32) nCurrentPMnPer;
         j++;
       }
     }     
  }



    /*----- Produce Zahlenvektor for last PM -----*/
  nCurrentPMnPer = idPMnPer[nPitchMark-1];    // Length of last PM in samples
  nCurrentPManreg = idPManreg[nPitchMark-1];    // Anregung of last PM
  
  if ( nCurrentPManreg == 1 )
  {
     for( ii = 0; ii < nCurrentPMnPer ; ii++ )
     {
       idExpandPM[j] = (INT32) nCurrentPMnPer;
       j++;
     }
  }
  else 
  {
     for( ii = 0; ii < nCurrentPMnPer ; ii++ )
     {
       idExpandPM[j] = (INT32) 0;
       j++;
     }    
  }  
  
  /*  
  fprintf(stdout,"\n\nThe expand values of PM: \n");  // show data 
  //for(i=0; i<nPitchMark; i++)
  for(i=0; i<300; i++)  
    fprintf(stdout,"Expand values of PM %i = %i \n",i,idExpandPM[i]);
  */
  
  

  /*---------- Calculate F0 values ----------*/
  // Calculate Sample Rate Ratio = nSrate / nSrateF0;
  if ( nSrateF0 == 0 )      // Divide by zero (false !!!)
  {
      //fprintf(stdout, "\nError: Divide by zero.");
      //return NOT_EXEC;
      return IERROR(this,Div_Zero,0,0,0);
  }
  else
  {
    nSrate_Ratio = (INT32) nSrate / nSrateF0;
  }
  //fprintf(stdout,"\nSample Rate Ratio:   %i\n",nSrate_Ratio);



  // Allocate memory for F0 values
  FLOAT64 *idContourF0 = NULL;

  INT32 nNumberF0 = 0;  // Number of F0 values in file

  for ( i = 0; i < (nSumSamplePM-nSrate_Ratio); i+=nSrate_Ratio )
  {
    if( idExpandPM[i+nSrate_Ratio] == 0 )
    {
      idContourF0 = (FLOAT64*)dlp_realloc(idContourF0, (nNumberF0+1), sizeof(FLOAT64));
      idContourF0[nNumberF0] = (FLOAT64) 0;
      nNumberF0 = nNumberF0 + 1;
    }
    else
    {
      idContourF0 = (FLOAT64*)dlp_realloc(idContourF0, (nNumberF0+1), sizeof(FLOAT64));
      //idContourF0[nNumberF0] = (FLOAT64) nSrate / idExpandPM[i+nSrate_Ratio];
      nAuxValue =  (FLOAT64) nSrate / idExpandPM[i+nSrate_Ratio];
      idContourF0[nNumberF0] = nAuxValue;
      nNumberF0 = nNumberF0 + 1;
    }    
    
  }

  // Delete single F0 value
  for ( i=1; i<nNumberF0-2; i++)
  {
    if(idContourF0[i]!=0 && idContourF0[i-1]==0 && idContourF0[i+1]==0)
    {
      idContourF0[i]=0;
    }
  }
  

  /* Write F0 values in struct */
    F0_CONTOUR *F0_contour = NULL;  // (F0_contour) is an object of struct (F0_CONTOUR)


  F0_contour = (F0_CONTOUR*)dlp_calloc(nNumberF0, sizeof(F0_CONTOUR));  // Allocates zero-initialize memory

  for(i = 0; i < nNumberF0; i++)  
  {
        (F0_contour + i)->nF0value = idContourF0[i];
        //fprintf( stdout,"nF0value  %i = %f\n",i,idContourF0[i] );
  }


    
  /* Copy F0 values from (struct F0_contour) to output data object (dF0) */
  dF0->AddRecs(nNumberF0, 1); // AddRecs: Appends (n) records to the end of the table (data object)  
  for( i = 0; i < nNumberF0; i++ )
  {
      dF0->Dstore((FLOAT64)(F0_contour + i)->nF0value, i, 0);
  }


  /*
  FLOAT64 nAuxCopy = 0;
  
  for (i = 0; i < nNumberF0; i++)
  {
    nAuxCopy = (FLOAT64)CData_Dfetch(dF0,i,0);
    fprintf(stdout,"F0 value %i = %f\n",i,nAuxCopy);  
  }
  */  
  
  dlp_free(idPMnPer);
  dlp_free(idPManreg);  
  dlp_free(idExpandPM);    
  dlp_free(idContourF0);
  dlp_free(F0_contour);    
      
  return O_K;
}
예제 #8
0
파일: fst_xsr.c 프로젝트: gitgun/dLabPro
/*
 * Manual page at fst_man.def
 */
INT16 CGEN_PUBLIC CFst_Rcs(CFst* _this, INT32 nUnit, FLOAT64 nSeed)
{
#ifdef __UNENTANGLE_FST

  return IERROR(_this,FST_INTERNAL,__FILE__,__LINE__,0);
  /* NOTE: __UNENTANGLE_FST was defined (probably in dlp_config.h or fst.def).
   *       Undefine it to use this feature!
   */

#else /* #ifdef __UNENTANGLE_FST */

  INT32      nU     = 0;                                                        /* Current unit                       */
  FST_ITYPE nS     = 0;                                                        /* Current state                      */
  FST_ITYPE nS2    = 0;                                                        /* Current state                      */
  FST_ITYPE nFS    = 0;                                                        /* First state of current unit        */
  FST_ITYPE nXS    = 0;                                                        /* Number of states of current unit   */
  FST_ITYPE nT     = 0;                                                        /* Current transition                 */
  FST_ITYPE nFT    = 0;                                                        /* First transition of current unit   */
  FST_ITYPE nXT    = 0;                                                        /* Number of tran. of current unit    */
  CData*    idP    = NULL;                                                     /* Incidence matrix                   */
  CData*    idB    = NULL;                                                     /* Constanct vector of (idP-E)idX=idB */
  CData*    idI    = NULL;                                                     /* idI = (idP-E)^-1                   */
  CData*    idX    = NULL;                                                     /* Solution of (idP-E)idX=idB         */
  FST_WTYPE nPSum  = 0.;                                                       /* Probability sum/state              */
  INT32      nIcW   = -1;                                                       /* Index of probability comp. in td   */
  INT32      nIcRcs = -1;                                                       /* Index of ref. counter comp. in sd  */
  INT32      nIcRct = -1;                                                       /* Index of ref. counter comp. in td  */

  /* Validation */
  CHECK_THIS_RV(NOT_EXEC);
  CFst_Check(_this);

  if (CFst_Wsr_GetType(_this,&nIcW)!=FST_WSR_PROB)
    return IERROR(_this,FST_MISS,"transition probability component",NC_TD_PSR,"transition table");

  /* Initialize - Find or add reference counters */
  nIcRcs = CData_FindComp(AS(CData,_this->sd),NC_SD_RC);
  nIcRct = CData_FindComp(AS(CData,_this->td),NC_TD_RC);
  if (nIcRcs<0)
  {
    CData_AddComp(AS(CData,_this->sd),NC_SD_RC,DLP_TYPE(FST_WTYPE));
    nIcRcs = CData_GetNComps(AS(CData,_this->sd))-1;
  }
  if (nIcRct<0)
  {
    CData_AddComp(AS(CData,_this->td),NC_TD_RC,DLP_TYPE(FST_WTYPE));
    nIcRct = CData_GetNComps(AS(CData,_this->td))-1;
  }

  /* Initialize - Create auxilary instances */
  ICREATEEX(CData,idP,"~CFst_Reverse.idP",NULL);
  ICREATEEX(CData,idB,"~CFst_Reverse.idB",NULL);
  ICREATEEX(CData,idI,"~CFst_Reverse.idI",NULL);
  ICREATEEX(CData,idX,"~CFst_Reverse.idX",NULL);

  /* Loop over units */
  for (nU=nUnit<0?0:nUnit; nU<UD_XXU(_this); nU++)
  {
    CData_Reset(BASEINST(idP),TRUE);
    CData_Reset(BASEINST(idB),TRUE);
    CData_Reset(BASEINST(idI),TRUE);
    CData_Reset(BASEINST(idX),TRUE);
    nFS = UD_FS(_this,nU);
    nXS = UD_XS(_this,nU);
    nFT = UD_FT(_this,nU);
    nXT = UD_XT(_this,nU);

    /* Export transposed ergodic incidence matrix */
    IF_NOK(CData_AddNcomps(idP,DLP_TYPE(FST_WTYPE),UD_XS(_this,nU)+1)) break;
    IF_NOK(CData_Allocate (idP,UD_XS(_this,nU)+1)                    ) break;
    IF_NOK(CData_AddNcomps(idB,DLP_TYPE(FST_WTYPE),1                )) break;
    IF_NOK(CData_Allocate (idB,UD_XS(_this,nU)+1)                    ) break;

    /* Fill transposed incidence matrix
       (summing up probabilities of parallel transitions) */
    for (nT=nFT; nT<nFT+nXT; nT++)
      *(FST_WTYPE*)CData_XAddr(idP,TD_TER(_this,nT),TD_INI(_this,nT)) +=
        *(FST_WTYPE*)CData_XAddr(AS(CData,_this->td),nT,nIcW);

    for (nS=0; nS<nXS; nS++)
    {
      if ((SD_FLG(_this,nS+nFS)&0x01)==0x01)     /* Connect final states with start state */
      {
        for (nS2=1, nPSum=0.; nS2<nXS; nS2++)
          nPSum += *(FST_WTYPE*)CData_XAddr(idP,nS2,nS);

        *(FST_WTYPE*)CData_XAddr(idP,0,nS) = 1.-nPSum;
      }

      *(FST_WTYPE*)CData_XAddr(idP,nS,nS)-=1.;   /* Subtract eigenvalue 1 from main diagonal */
      *(FST_WTYPE*)CData_XAddr(idP,nXS,nS)=1.;   /* Additional equation implementing constraint sum(P_state)=1 */
      *(FST_WTYPE*)CData_XAddr(idP,nS,nXS)=1.;   /* Additional variable making incidence matrix quadratic */
    }

    /* Fill constant vector */
    CData_Fill(idB,CMPLX(1.),CMPLX(0.));

    /* Calculate eigenvector of length 1 and eigenvalue 1
       --> stationary state probabilities */
    CMatrix_Op(idI,idP,T_INSTANCE,NULL,T_IGNORE,OP_INVT);
    CMatrix_Op(idX,idB,T_INSTANCE,idI,T_INSTANCE,OP_MULT);

    /* Fill in state reference counters */
    if (nSeed>0.) nSeed /= CData_Dfetch(idX,0,0); else nSeed = 1.;
    for (nS=0; nS<nXS; nS++)
      CData_Dstore(AS(CData,_this->sd),nSeed*CData_Dfetch(idX,nS,0),nS+nFS,nIcRcs);

    /* Calculate stationary transition probabilities */
    for (nT=nFT; nT<nFT+nXT; nT++)
      CData_Dstore
      (
        AS(CData,_this->td),
        CData_Dfetch(AS(CData,_this->sd),TD_INI(_this,nT)+nFS,nIcRcs) *
          CData_Dfetch(AS(CData,_this->td),nT,nIcW),
        nT,nIcRct
      );

    /* Clean up */
    IDESTROY(idP);
    IDESTROY(idB);
    IDESTROY(idI);
    IDESTROY(idX);

    /* Stop in single unit mode */
    if (nUnit>=0) break;
  }

  return O_K;

#endif /* #ifdef __UNENTANGLE_FST */
}
예제 #9
0
파일: fst_xsr.c 프로젝트: gitgun/dLabPro
/*
 * Manual page at fst_man.def
 */
INT16 CGEN_PUBLIC CFst_Probs(CFst* _this, INT32 nUnit)
{
  INT32          nU     = 0;                                                     /* Current unit                      */
  FST_ITYPE     nS     = 0;                                                     /* Current state                     */
  FST_ITYPE     nT     = 0;                                                     /* Current transition                */
  FST_ITYPE     nXS    = 0;                                                     /* Number of states in current unit  */
  INT16         nWsrt  = 0;                                                     /* Weight semiring type              */
  INT32          nIcW   = -1;                                                    /* Weight component in td            */
  INT32          nIcPfl = -1;                                                    /* Floor probability component in sd */
  INT32          nIcRct = -1;                                                    /* Reference counter component in td */
  INT32          nTrCnt = 0;                                                     /* Number of outgoing trans./state   */
  FST_WTYPE     nW     = 0.;                                                    /* Current weight                    */
  FST_WTYPE     nRc    = 0.;                                                    /* Current reference counter/prob.   */
  FST_WTYPE     nRcSum = 0.;                                                    /* Reference counter sum/state       */
  FST_WTYPE     nTW    = 0.;                                                    /* trans.weight                      */
  FST_WTYPE     nTWAgg = 0.;                                                    /* trans.weight aggregator for MAP   */
  CData*        idTd   = NULL;                                                  /* Pointer to transition table       */
  FST_TID_TYPE* lpTI   = NULL;                                                  /* Automaton iterator data structure */
  BYTE*         lpT    = NULL;                                                  /* Current transition (iteration)    */
  FST_WTYPE     nMAP   = _this->m_bUsemap ? _this->m_nMapexp : 0.;              /* MAP exponent (0:off,1:min,-1:max) */
  INT32          nMAPi  = 0;                                                     /* MAP iteration index               */

  /* Validation */                                                              /* --------------------------------- */
  CHECK_THIS_RV(NOT_EXEC);                                                      /* Check this pointer                */
  CFst_Check(_this);                                                            /* Check FST(s)                      */

  /* Initialize */                                                              /* --------------------------------- */
  idTd   = AS(CData,_this->td);                                                 /* Get pointer to transition table   */
  nIcRct = CData_FindComp(AS(CData,_this->td),NC_TD_RC);                        /* Find reference counters           */
  nWsrt  = CFst_Wsr_GetType(_this,&nIcW);                                       /* Find weights and get type         */
  switch (nWsrt)                                                                /* branch for weight semiring type   */
  {                                                                             /* >>                                */
    case FST_WSR_NONE:                                                          /*   No weights                      */
      CData_AddComp(idTd,NC_TD_PSR,DLP_TYPE(FST_WTYPE));                        /*     Add some                      */
      nIcW = CData_GetNComps(idTd)-1;                                           /*     Get index of new weight comp. */
      for (nT=0; nT<UD_XXT(_this); nT++)                                        /*     Initialize probabilities      */
        CData_Dstore(idTd,1.,nT,nIcW);                                          /*     ...                           */
      break;                                                                    /*     *                             */
    case FST_WSR_PROB:                                                          /*   Probabilities                   */
      break;                                                                    /*     * (nothing to be done)        */
    case FST_WSR_TROP: /* fall through */                                       /*   Tropical weights                */
    case FST_WSR_LOG:                                                           /*   Logarithmic weights             */
      for (nT=0; nT<UD_XXT(_this); nT++)                                        /*     Loop over all transitions     */
        CData_Dstore(idTd,                                                      /*       Convert to probabilities    */
          exp(-1.*CData_Dfetch(idTd,nT,nIcW)),nT,nIcW);                         /*       |                           */
      break;                                                                    /*     *                             */
    default:                                                                    /*   Unknown weight semiring         */
      DLPASSERT(FMSG("Unknown weight semiring"));                               /*     New weight semiring type?     */
      CData_SetCname(idTd,nIcW,NC_TD_PSR);                                      /*     Rename to probabilities       */
      for (nT=0; nT<UD_XXT(_this); nT++)                                        /*     Initialize probabilities      */
        CData_Dstore(idTd,1.,nT,nIcW);                                          /*     ...                           */
  }                                                                             /* <<                                */
  if (_this->m_nSymbols>0 && _this->m_nRcfloor>0.)                              /* Smoothing enabled                 */
  {                                                                             /* >>                                */
    if (CData_FindComp(AS(CData,_this->sd),"~PFL")<0)                           /*   No floor prob. comp. at states  */
      CData_AddComp(AS(CData,_this->sd),"~PFL",DLP_TYPE(FST_WTYPE));            /*     Add it                        */
    nIcPfl = CData_FindComp(AS(CData,_this->sd),"~PFL");                        /*   Get index of floor prob. comp.  */
  }                                                                             /* <<                                */

  /* Loop over units */                                                         /* --------------------------------- */
  for (nU=nUnit<0?0:nUnit; nU<UD_XXU(_this); nU++)                              /* For all units ...                 */
  {                                                                             /* >>                                */
    lpTI = CFst_STI_Init(_this,nU,FSTI_SORTINI);                                /*   Get sorted transition iterator  */

    /* Loop over states */                                                      /*   - - - - - - - - - - - - - - - - */
    for (nS=0,nXS=UD_XS(_this,nU); nS<nXS; nS++)                                /*   For all states of the unit ...  */
    /* Loop over MAP-Iterations if MAP enabled */                               /*   - - - - - - - - - - - - - - - - */
    for(nMAPi=0;nMAPi<(nMAP!=0.?10:1);nMAPi++)                                  /*   For all MAP-iterations ...      */
    {                                                                           /*   >>                              */
      /* Pass 1: Count outgoing transitions and sum up reference counters */    /*                                   */
      nRcSum = 0.;                                                              /*     Reset ref. ctr. accumulator   */
      nTWAgg = 0.;                                                              /*     Reset MAP TW accumulator      */
      nTrCnt = 0;                                                               /*     Reset transition counter      */
      lpT    = NULL;                                                            /*     Initialize transition pointer */
      while ((lpT=CFst_STI_TfromS(lpTI,nS,lpT))!=NULL)                          /*     Enumerate transitions at nS   */
      {                                                                         /*     >>                            */
        nRc = nIcRct>=0                                                         /*       Get ref. ctr. or prob.      */
            ? CData_Dfetch(idTd,CFst_STI_GetTransId(lpTI,lpT),nIcRct)           /*       |                           */
            : *CFst_STI_TW(lpTI,lpT);                                           /*       |                           */
        if (nRc>=0.) nRcSum+=nRc;                                               /*       Accumulate non-neg. values  */
        if (nMAP!=0. && nRc>0.)                                                 /*       if MAP enabled and T. used  */
        {                                                                       /*       >>                          */
          nTW = *CFst_STI_TW(lpTI,lpT);                                         /*         get trans.weight          */
          if(nTW<=0.) nTW=0.0001;                                               /*         solve prob. with 0 TW's   */
          nTWAgg += nTW*log(nTW);                                               /*         acc. TW's                 */
        }                                                                       /*       <<                          */
        nTrCnt++;                                                               /*       Count transitions           */
      }                                                                         /*     <<                            */
      if (nIcPfl)                                                               /*     Smoothing eneabled            */
        CData_Dstore(AS(CData,_this->sd),                                       /*       Store floor probability     */
          _this->m_nRcfloor / (nRcSum + _this->m_nSymbols*_this->m_nRcfloor),   /*       |                           */
          nS,nIcPfl);                                                           /*       |                           */
                                                                                /*                                   */
      /* Pass 2: Calculate probabilities */                                     /*                                   */
      if (nTrCnt)                                                               /*     If there were trans. at nS    */
      {                                                                         /*     >>                            */
        lpT = NULL;                                                             /*       Initialize transition ptr.  */
        while ((lpT=CFst_STI_TfromS(lpTI,nS,lpT))!=NULL)                        /*       Enumerate transitions at nS */
        {                                                                       /*       >>                          */
          nRc = nIcRct>=0                                                       /*         Get ref. ctr. or prob.    */
              ? CData_Dfetch(idTd,CFst_STI_GetTransId(lpTI,lpT),nIcRct)         /*         |                         */
              : *CFst_STI_TW(lpTI,lpT);                                         /*         |                         */
          if (nMAP!=0.)                                                         /*         if MAP enabled            */
          {                                                                     /*         <<                        */
            nTW = *CFst_STI_TW(lpTI,lpT);                                       /*           get trans.weight        */
            if(nTW<=0.) nTW=0.0001;                                             /*           solve prob. with 0 TW's */
            if(nIcRct>=0 && _this->m_nSymbols>0 && _this->m_nRcfloor>0)         /*           do jeffrey smooth?      */
              nRc = nRcSum * (nRc + (FST_WTYPE)_this->m_nRcfloor) /             /*             smooth nRc            */
                (nRcSum + ((FST_WTYPE)_this->m_nSymbols*_this->m_nRcfloor));    /*             |                     */
            nTW = (nRc<=0. ? 0. : (nRc+nMAP*nTW*log(nTW))/(nRcSum+nMAP*nTWAgg));/*           do MAP-iteration        */
            *CFst_STI_TW(lpTI,lpT) = nTW;                                       /*           write trans.weight      */
          }                                                                     /*         >>                        */
          else                                                                  /*         else                      */
            if (nIcRct<0 || _this->m_nSymbols<=0 || _this->m_nRcfloor<=0.)      /*         No ref.ctrs. or smoothing */
              *CFst_STI_TW(lpTI,lpT) =                                          /*           Store equally distrib.  */
                nRcSum==0. ? 1./(FST_WTYPE)nTrCnt : nRc/nRcSum;                 /*           | or renormalized probs.*/
            else                                                                /*         Ref. ctrs. or smoothing   */
              *CFst_STI_TW(lpTI,lpT) =                                          /*           Store Jeffrey smoothed  */
                (nRc + (FST_WTYPE)_this->m_nRcfloor) /                          /*           | probabilities         */
                (nRcSum + ((FST_WTYPE)_this->m_nSymbols*_this->m_nRcfloor));    /*           |                       */
        }                                                                       /*       <<                          */
      }                                                                         /*     <<                            */
    }                                                                           /*   <<                              */
    CFst_STI_Done(lpTI);                                                        /*   Destroy iterator                */
    if (nUnit>=0) break;                                                        /*   Stop in single unit mode        */
  }                                                                             /* <<                                */

  /* Convert back to logarithmic/tropical weights */                            /* --------------------------------- */
  if (nWsrt==FST_WSR_LOG || nWsrt==FST_WSR_TROP)                                /* Loarithmic or tropical weights    */
    for (nT=0; nT<UD_XXT(_this); nT++)                                          /*   Loop over all transitions       */
    {                                                                           /*   >>                              */
      nW = CData_Dfetch(idTd,nT,nIcW);                                          /*     Get probability               */
      nW = nW>0. ? -log(nW) : _this->m_nWceil;                                  /*     Convert to log./trop. weight  */
      CData_Dstore(idTd,nW,nT,nIcW);                                            /*     Store weight                  */
    }                                                                           /*   <<                              */

  /* Clean up */                                                                /* --------------------------------- */
  return O_K;                                                                   /* The end                           */
}
예제 #10
0
파일: sta_upd.c 프로젝트: thias42/dLabPro
/*
 * Manual page at statistics.def
 */
INT16 CGEN_PUBLIC CStatistics_Update
(
  CStatistics* _this,
  CData*       idVec,
  INT32         nIcLab,
  CData*       idW
)
{
  INT32        i           = 0;                                                  /* Update vector loop counter        */
  INT32        I           = 0;                                                  /* Number of update vectors          */
  INT32        c           = 0;                                                  /* Class of current update vector    */
  INT32        C           = 0;                                                  /* Number of classes                 */
  INT32        n           = 0;                                                  /* Dimension loop counter            */
  INT32        N           = 0;                                                  /* Statistics' dimensionality        */
  FLOAT64      w           = 0.;                                                 /* Weight of current update vector   */
  char*          lpsLab      = NULL;                                               /* Symbolic label of curr. upd. vec. */
  FLOAT64*     lpX         = NULL;                                               /* Vector copy buffer                */
  INT32        nVecIgnored = 0;                                                  /* Number of ignored vectors         */

  /* Validate */                                                                /* --------------------------------- */
  CHECK_THIS_RV(NOT_EXEC);                                                      /* Check this pointer                */
  IF_NOK(CStatistics_Check(_this))                                              /* Check instance data               */
    return IERROR(_this,STA_NOTSETUP," ( use -status for details)",0,0);        /* ...                               */
  if (CData_IsEmpty(idVec)) return O_K;                                         /* No input vector(s), no service!   */
  I = CData_GetNRecs(idVec);                                                    /* Get number of update vectors      */
  C = CStatistics_GetNClasses(_this);                                           /* Get number of statitistics classes*/
  N = CStatistics_GetDim(_this);                                                /* Get statistics vector dimension   */
  if (C>1)                                                                      /* Multiclass statistics needs labels*/
  {                                                                             /* >>                                */
    if (_this->m_idLtb)                                                         /*   Need symbolic labels            */
    {                                                                           /*   >>                              */
      if ((nIcLab<0 || nIcLab>=CData_GetNComps(idVec)))                         /*     Symbolic label comp. not spec.*/
        for (nIcLab=0; nIcLab<CData_GetNComps(idVec); nIcLab++)                 /*       Seek label component        */
          if (dlp_is_symbolic_type_code(CData_GetCompType(idVec,nIcLab)))       /*       ...                         */
            break;                                                              /*       ...                         */
      if (!dlp_is_symbolic_type_code(CData_GetCompType(idVec,nIcLab)))          /*     Symbolic label comp. not found*/
        return                                                                  /*       -> Error                    */
          IERROR(_this,STA_BADCOMP,"Label",idVec->m_lpInstanceName,"symbolic"); /*       |                           */
    }                                                                           /*   <<                              */
    else                                                                        /*   Need numeric labels             */
    {                                                                           /*   >>                              */
      if (!dlp_is_numeric_type_code(CData_GetCompType(idVec,nIcLab)) &&         /*     Numeric label comp. not found */
          (nIcLab>=0 || CData_GetNComps(idW)!=C))                               /*     |                             */
        return                                                                  /*       -> Error                    */
          IERROR(_this,STA_BADCOMP,"Label",idVec->m_lpInstanceName,"numeric");  /*       |                           */
    }                                                                           /*   <<                              */
  }                                                                             /* <<                                */
  /*else if (nIcLab>=0) IERROR(_this,STA_IGNORE,"label component",0,0);        / * Only one class  -> ignore labels  */
  if (dlp_is_numeric_type_code(CData_GetCompType(idVec,nIcLab)))                /* Check no. of comps. in idVec ...  */
  {                                                                             /* >>                                */
    if (CData_GetNNumericComps(idVec)!=N+1)                                     /*   Wrong number of numeric comps.  */
      IERROR(_this,STA_DIM,idVec->m_lpInstanceName,"numeric components",N+1);   /*     -> Warning                    */
  }                                                                             /* <<                                */
  else if (CData_GetNNumericComps(idVec)!=N)                                    /* Wrong number of numeric comps.    */
    IERROR(_this,STA_DIM,idVec->m_lpInstanceName,"numeric components",N);       /*   -> Warning                      */

  if (idW)                                                                      /* Weigths passed -> check 'em       */
  {                                                                             /* >>                                */
    if (!dlp_is_numeric_type_code(CData_GetCompType(idW,0)))                    /*   Component 0 not numeric         */
      return                                                                    /*   -> Error                        */
        IERROR(_this,STA_BADCOMP,"Weight",idW->m_lpInstanceName,"numeric");     /*   |                               */
    if (CData_GetNComps(idW)!=1 && CData_GetNComps(idW)!=C)                     /*   More than one component         */
      IERROR(_this,STA_IGNORE,"components in weight sequence",0,0);             /*   -> Warning                      */
    if (CData_GetNRecs(idW)!=I)                                                 /*   Not exactly one weight per vec. */
      IERROR(_this,STA_DIM,idW->m_lpInstanceName,"records",I);                  /*   -> Warning                      */
  }                                                                             /* <<                                */

  /* Initialize - NO RETURNS BEYOND THIS POINT! - */                            /* --------------------------------- */
  lpX = (FLOAT64*)dlp_calloc(N,sizeof(FLOAT64));                                  /* Allocate vector copy buffer       */

  /* Update statistics */                                                       /* --------------------------------- */
  for (i=0; i<I; i++)                                                           /* Loop over update vectors          */
  {                                                                             /* >>                                */
    /* Get vector label */                                                      /*   - - - - - - - - - - - - - - - - */
    if (C>1)                                                                    /*   Multiclass stats. needs labels  */
    {                                                                           /*   >>                              */
      if (_this->m_idLtb)                                                       /*     idVec contains symbolic labs. */
      {                                                                         /*     >>                            */
        INT32 nLIdx = 0;
        DLPASSERT(dlp_is_symbolic_type_code(CData_GetCompType(idVec,nIcLab)));  /*       Must be checked before!     */
        lpsLab = (char*)CData_XAddr(idVec,i,nIcLab);                            /*       Get string ptr. to label    */
        if(_this->m_bLabel){
          nLIdx=strlen(lpsLab)-1;
          if(nLIdx && lpsLab[nLIdx]==']') nLIdx--; else nLIdx=0;
          if(nLIdx && lpsLab[nLIdx]>='0' && lpsLab[nLIdx]<='9') nLIdx--; else nLIdx=0;
          while(nLIdx && lpsLab[nLIdx]>='0' && lpsLab[nLIdx]<='9') nLIdx--;
          if(nLIdx && lpsLab[nLIdx]=='[') lpsLab[nLIdx]='\0'; else nLIdx=0;
        }
        c      = CData_Find(_this->m_idLtb,0,C,1,0,lpsLab);                     /*       Look up label -> class idx. */
        if(nLIdx) lpsLab[nLIdx]='[';
        if (c<0)                                                                /*       Label invalid               */
        {                                                                       /*       >>                          */
          IERROR(_this,STA_SLAB,i,lpsLab?lpsLab:"(null)",0);                    /*         Warning                   */
          continue;                                                             /*         Ignore record             */
        }                                                                       /*       <<                          */
      }                                                                         /*     <<                            */
      else if(nIcLab>=0)                                                        /*     idVec contains numeric labs.  */
      {                                                                         /*     >>                            */
        c = (INT32)CData_Dfetch(idVec,i,nIcLab);                                 /*       Fetch label                 */
        if (c<0 || c>=C)                                                        /*       Label invalid               */
        {                                                                       /*       >>                          */
          IERROR(_this,STA_NLAB,i,c,0);                                         /*         Warning                   */
          continue;                                                             /*         Ignore record             */
        }                                                                       /*       <<                          */
      }                                                                         /*     <<                            */
      else c = 0;                                                               /*     Default class is 0            */
    }                                                                           /*   <<                              */
    else c = 0;                                                                 /*   Default class is 0              */

    do                                                                          /*   Loop over classes               */
    {                                                                           /*   >>                              */

    /* Get (weighted) update vector and update statistics */                    /*   - - - - - - - - - - - - - - - - */
    if (idW)                                                                    /*   Using weights                   */
    {                                                                           /*   >>                              */
      w = CData_Dfetch(idW,i,_this->m_idLtb || nIcLab>=0 ? 0 : c);              /*     Fetch weight for this vector  */
      if(w==0.) continue;                                                       /*     Nothing to do if no weight    */
      _this->m_bWeighted=TRUE;
    } else w=1.;                                                                /*   <<                              */
    CData_DrecFetch(idVec,lpX,i,N,nIcLab);                                      /*   Fetch update vector             */
    fpclassify(0.);
    for (n=0; n<N; n++)                                                         /*   Loop over vector components     */
      if (fabs(lpX[n])>1E100)                                                   /*     Check value                   */
        break;                                                                  /*       There's something wrong ... */
    if (n<N) { nVecIgnored++; continue; }                                       /*   Ignore this vector              */
    CStatistics_UpdateVector(_this,lpX,c,w);                                    /*   Update statistics with vector   */

    }                                                                           /*   <<                              */
    while(!_this->m_idLtb && nIcLab<0 && ++c<C);                                /*   Next class if there is one      */
  }                                                                             /* <<                                */

  /* Clean up */                                                                /* --------------------------------- */
  if (nVecIgnored>0) IERROR(_this,STA_VECIGNORED,nVecIgnored,0,0);              /* Error: some vectors ignored       */
  dlp_free(lpX);                                                                /* Free vector copy buffer           */
  return O_K;                                                                   /* Done                              */
}