Esempio n. 1
0
/**
 * <p>Converts a coefficient vector of a polynomial in z: a(1)z^0+a(2)z^1+...
 * to a coefficient vector of a polynomial in s: b(1)s^0+b(2)s^1+...
 * by a bilinear map z:= (s+1)/(s-1)
 * which converts zeros of z inside the unit circle
 * into zeros of s in the left halfplane,
 * i.e. prepares a polynomial for the Routh/Hurwitz-test.</p>
 *
 * @param poly
 *          Pointer to polynomial coefficients.
 * @param n_order
 *          Number of polynomial coefficients stored in  <CODE>poly</CODE>.
 * @return <code>O_K</code> if successful, a (negative) error code otherwise
 */
INT16 dlm_z2s(FLOAT64* poly, INT16 n_order) {
  INT16 i_order1;
  INT16 i_order2;
  INT32 n_order2 = n_order * n_order;
  FLOAT64* pp = NULL;
  FLOAT64* pm = NULL;
  FLOAT64* ppd = NULL;
  FLOAT64* pmd = NULL;
  FLOAT64* poly_z = NULL;
  FLOAT64* poly_s = NULL;

  pp = (FLOAT64*) dlp_calloc(2*n_order2+4*n_order, sizeof(FLOAT64));
  pm = pp + n_order2;
  ppd = pm + n_order2;
  pmd = ppd + n_order;
  poly_z = pmd + n_order;
  poly_s = poly_z + n_order;

  if (dlm_pascal(pp, n_order) != O_K) return NOT_EXEC;

  dlp_memmove(pm, pp, n_order2 * sizeof(FLOAT64));

  for (i_order1 = 1; i_order1 < n_order; i_order1 += 2) {
    for (i_order2 = 0; i_order2 < n_order; i_order2++) {
      pm[i_order2 * n_order + i_order1] = -pp[i_order2 * n_order + i_order1];
    }
  }

  for (i_order1 = 0; i_order1 < n_order; i_order1++) {
    for (i_order2 = 0; i_order2 < (n_order - i_order1); i_order2++) {
      pmd[i_order2] = pm[i_order2 * n_order + n_order - i_order1 - 1 - i_order2];
    }
    for (i_order2 = n_order - i_order1; i_order2 < n_order; i_order2++) {
      pmd[i_order2] = 0.0;
    }
    for (i_order2 = 0; i_order2 <= i_order1; i_order2++) {
      ppd[i_order2] = pp[i_order2 * n_order + i_order1 - i_order2];
    }
    for (i_order2 = i_order1 + 1; i_order2 < n_order; i_order2++) {
      ppd[i_order2] = 0.0;
    }
    dlm_filter_fir(ppd, n_order, pmd, poly_z, n_order, NULL, 0);
    for (i_order2 = 0; i_order2 < n_order; i_order2++) {
      poly_s[i_order2] += poly_z[i_order2] * poly[n_order - 1 - i_order1];
    }
  }
  dlp_memmove(poly, poly_s, n_order * sizeof(FLOAT64));
  dlp_free(pp);
  return O_K;
}
Esempio n. 2
0
/**
 * Fetches a string from a string table.
 *
 * @param lpST    Pointer to a string table data structure (returned by
 *                {@link Ssr_Init CFst_Ssr_Init})
 * @param nS      Index of string to fetch
 * @param lpBuf   Pointer to a buffer to be filled
 * @param nMaxLen Maximal number of symbols to fetch (including the terminal
 *                symbol <code>-1</code>)
 */
void CGEN_SPROTECTED CFst_Ssr_Fetch(FST_SST_TYPE* lpST, FST_ITYPE nS, FST_ITYPE* lpBuf, INT32 nMaxLen)
{
  INT32 nL = CFst_Ssr_Len(lpST,nS);
  if (nMaxLen<nL) nL = nMaxLen;
  dlp_memmove(lpBuf,ST_LP(lpST,nS),sizeof(FST_STYPE)*nL);
  if (nL==nMaxLen) lpBuf[nL]=-1;
}
Esempio n. 3
0
/**
 * Utility function for CData_PrintVectors; pads a string up to a given length
 * with spaces.
 */
char* __pad
(
  char* sStr,                                                                   /* The string to pad                 */
  INT32  nLen,                                                                   /* The target length                 */
  char  nMode                                                                   /* Alignment: 'l', 'r' or 'c'        */
)                                                                               /* Returns sStr                      */
{
  char* tx = NULL;                                                              /* Current character in string       */
  if ((INT32)dlp_strlen(sStr)==nLen) return sStr;                                /* Nothing to be done                */
  if ((INT32)dlp_strlen(sStr)> nLen)                                             /* String longer than nLen           */
  {                                                                             /* >>                                */
    for (tx=sStr;tx<sStr+nLen;tx++) *tx='#';                                    /*   Make it "#######"               */
    *tx='\0';                                                                   /*   Terminate it                    */
    return sStr;                                                                /*   Return it                       */
  }                                                                             /* <<                                */
  while ((INT32)dlp_strlen(sStr)<nLen)                                           /* While string shorter than required*/
  {                                                                             /* >>                                */
    if (nMode=='c' || nMode=='r')                                               /*   Padding at left side            */
    {                                                                           /*   >>                              */
      dlp_memmove(&sStr[1],sStr,dlp_strlen(sStr)+1);                            /*     Move data                     */
      sStr[0]=' ';                                                              /*     Write heading space           */
      if (nMode=='c') nMode='C';                                                /*     Toggle centering flag         */
    }                                                                           /*   <<                              */
    else if (nMode=='C' || nMode=='l')                                          /*   Paddig at right side            */
    {                                                                           /*   >>                              */
      sStr[dlp_strlen(sStr)+2]='\0';                                            /*     Write second terminal 0       */
      sStr[dlp_strlen(sStr)+1]=' ';                                             /*     Write trailing space          */
      if (nMode=='C') nMode='c';                                                /*     Toggle centering flag         */
    }                                                                           /*   <<                              */
  }                                                                             /* <<                                */
  return sStr;                                                                  /* Return sStr                       */
}
Esempio n. 4
0
/**
 *  Expand/reduce number of pitch markers to fit new target sum of period length.
 *
 * @param idSrc        source object
 * @param idDst        target object
 * @param n            target length
 * @return O_K if sucessfull, not exec otherwise
 */
INT16 CGEN_PUBLIC CPMproc::ExpandPm(CData *idSrc, CData* idDst, INT32 n)
{
  if(CData_IsEmpty(idSrc) == TRUE) return NOT_EXEC;
  if(n <= 0)                       return NOT_EXEC;

  CREATEVIRTUAL(CData,idSrc,idDst);

  DLPASSERT(CData_GetNComps(idSrc) == 2);

  CData_Reset(idDst, TRUE);
  CData_Scopy(idDst,idSrc);

  INT32    nRecsNew = 0;
  INT32    nRecs    = (INT32)CData_GetNRecs(idSrc);
  INT16* pm_new = NULL;

  if(dlm_pm_expand((INT16*)CData_XAddr(idSrc,0,0), nRecs, &pm_new, &nRecsNew, n) != O_K) {
    return IERROR(this,ERR_NULLARG, "", NULL, NULL);
  }
  CData_Allocate(idDst,nRecsNew);
  dlp_memmove(CData_XAddr(idDst,0,0), pm_new, nRecsNew*2*sizeof(INT16));
  dlp_free(pm_new);
  CData_CopyDescr(idDst,idSrc);

  /* clean up */
  DESTROYVIRTUAL(idSrc,idDst)

  return(O_K);
}
Esempio n. 5
0
/**
 * Addition operation of the symbol string semiring (longest common prefix).
 *
 * @param lpST Pointer to a string table data structure (returned by
 *             {@link Ssr_Init CFst_Ssr_Init})
 * @param nS1  Index of first string
 * @param nS2  Index of second string
 * @return Index of result string or -1 if no matching prefixes
 */
FST_ITYPE CGEN_SPROTECTED CFst_Ssr_Add(FST_SST_TYPE* lpST, FST_ITYPE nS1, FST_ITYPE nS2)
{
  INT32 nL1 = 0;
  INT32 nL2 = 0;
  INT32 nLr = 0;

  if (nS1==nS2          ) return nS1;  /* Args are identical                  */
  if (nS1==-2           ) return nS2;  /* Arg. 1 is the infinite string       */
  if (nS2==-2           ) return nS1;  /* Arg. 2 is the infinite string       */
  if (nS1==-1 || nS2==-1) return -1;   /* Either argument is the empty string */

  nL1  = CFst_Ssr_Len(lpST,nS1);
  nL2  = CFst_Ssr_Len(lpST,nS2);

  for (nLr=0; nLr<nL1 && nLr<nL2; nLr++)
  {
    if (ST_LP(lpST,nS1)[nLr]!=ST_LP(lpST,nS2)[nLr]) break;
    if (ST_LP(lpST,nS1)[nLr]==-1 || ST_LP(lpST,nS2)[nLr]==-1) break;
  }

  if (nLr==0) return -1;

  if ((INT32)dlp_size(lpST->lpBuf)<nLr)
    lpST->lpBuf=(FST_STYPE*)__dlp_realloc(lpST->lpBuf,nLr+lpST->nGrany,sizeof(FST_STYPE),__FILE__,__LINE__,"CFst","");

  dlp_memmove(lpST->lpBuf,ST_LP(lpST,nS1),nLr);
  lpST->lpBuf[nLr]=-1;

  return CFst_Ssr_Store(lpST,lpST->lpBuf);
}
Esempio n. 6
0
/**
 *  Convert f0-contour with equal spaced sampling points to pitch markers.
 *
 * @param idSrc        source f0-contour
 * @param idDst        target pitch marker
 * @param n            target sum of pitch marker lengths
 * @param srate        sampling rate
 * @return O_K if sucessfull, not exec otherwise
 */
INT16 CGEN_PUBLIC CPMproc::F02pm(CData *idSrc, CData* idDst, INT32 n, INT32 srate) {

  INT16* p_pm = NULL;
  INT32    n_pm = 0;

  if(CData_IsEmpty(idSrc) == TRUE) return NOT_EXEC;
  if(n <= 0)                       return NOT_EXEC;

  DLPASSERT(CData_GetNComps(idSrc) == 1);

  CREATEVIRTUAL(CData,idSrc,idDst);

  CData_Reset(idDst, TRUE);

  dlm_f02pm((FLOAT64*)idSrc->XAddr(0,0), idSrc->GetNRecs(), &p_pm, &n_pm, n, srate);

  CData_AddComp(idDst, "pm",   T_SHORT);
  CData_AddComp(idDst, "v/uv", T_SHORT);
  CData_Allocate(idDst, n_pm);
  ISETFIELD_RVALUE(idDst, "fsr", 1000.0/srate);

  dlp_memmove(idDst->XAddr(0,0), p_pm, 2*n_pm*sizeof(INT16));

  /* clean up */
  DESTROYVIRTUAL(idSrc,idDst)

  dlp_free(p_pm);

  return(O_K);
}
Esempio n. 7
0
/////////////////////////////////////////////////////////////////////////////////////
//
// ModEx - generates excitation from pitch file
//
// CData *gain       -> gains of excitation per frame
// CData *idPm       -> both components must be type long
// CData *idExcite   -> type FBA_FLOAT
//
INT16 CGEN_PUBLIC CFBAproc::ModEx(CData *idPm, CData *idExcite) {
  // Error handling
  if (idPm == NULL)                return IERROR(this,ERR_NULLINST,0,0,0);
  if (idPm -> IsEmpty() == TRUE)   return IERROR(idPm,DATA_EMPTY,idPm->m_lpInstanceName,0,0);
  if (idExcite == NULL)            return IERROR(this,ERR_NULLINST,idExcite->m_lpInstanceName,0,0);

  DLPASSERT(idPm->GetNComps()>1);

  FLOAT64* exc = NULL;
  INT32    n_exc = 0;

  switch(m_lpsExcType[0]) {
    case 'P':
      if(dlm_pm2exc((INT16*)idPm->XAddr(0,0),(INT32)idPm->GetNRecs(),&exc,&n_exc,m_nSrate, (BOOL)TRUE,DLM_PITCH_PULSE    ) != O_K) return NOT_EXEC;
      break;
    case 'G':
      if(dlm_pm2exc((INT16*)idPm->XAddr(0,0),(INT32)idPm->GetNRecs(),&exc,&n_exc,m_nSrate, (BOOL)TRUE,DLM_PITCH_GLOTT    ) != O_K) return NOT_EXEC;
      break;
    case 'R':
      if(dlm_pm2exc((INT16*)idPm->XAddr(0,0),(INT32)idPm->GetNRecs(),&exc,&n_exc,m_nSrate, (BOOL)TRUE,DLM_PITCH_RANDPHASE) != O_K) return NOT_EXEC;
      break;
    case 'V':
      if(dlm_pm2exc((INT16*)idPm->XAddr(0,0),(INT32)idPm->GetNRecs(),&exc,&n_exc,m_nSrate, (BOOL)TRUE,DLM_PITCH_VOICED   ) != O_K) return NOT_EXEC;
      break;
    case 'U':
      if(dlm_pm2exc((INT16*)idPm->XAddr(0,0),(INT32)idPm->GetNRecs(),&exc,&n_exc,m_nSrate, (BOOL)TRUE,DLM_PITCH_UNVOICED ) != O_K) return NOT_EXEC;
      break;
    case 'C':
      if(m_idExc == NULL) return IERROR(this,ERR_BADPTR,NULL,"of CFBAproc->m_lpExc","(FBAproc.exc)");
      for(INT32 i_pm=0; i_pm < idPm->GetNRecs(); i_pm++) n_exc += (INT16)idPm->Dfetch(i_pm,0);
      if(CData_GetNRecs(m_idExc) < n_exc) return IERROR(this,FBA_BADEXCLEN,0,0,0);
      exc = (FLOAT64*)dlp_malloc(n_exc*sizeof(FLOAT64));
      dlp_memmove(exc,m_idExc->XAddr(0,0),n_exc*sizeof(FLOAT64));
      break;
    default:
      return IERROR(this,FBA_BADARG,m_lpsExcType,"exc_type","P, G, R, V, U or C");
  }

  idExcite->Reset(TRUE);
  idExcite->AddComp("exc", T_DOUBLE);
  idExcite->Allocate(n_exc);
  dlp_memmove(idExcite->XAddr(0,0), exc, n_exc*sizeof(FLOAT64));
  dlp_free(exc);

  return O_K;
}
Esempio n. 8
0
char* CDlpObject_GetFQName(CDlpObject* _this, char* lpName, BOOL bForceArray)
{
  char        lpBuf1[255];
  char        lpBuf2[255];
  SWord*      lpWord;
  CDlpObject* lpInst = _this;

  if (!_this)
  {
    dlp_strcpy(lpName,"(null)");
    return lpName;
  }

  lpBuf2[0]='\0';

  if (!_this->m_lpContainer) dlp_strcpy(lpName,_this->m_lpInstanceName);
  else
  {
    lpWord = _this->m_lpContainer;
    while (TRUE)
    {
      /* Do no include interpreter instance name */
      if (dlp_strcmp(lpWord->lpContainer->m_lpClassName,"itp")==0) break;

      if (lpWord->nWordType==WL_TYPE_FIELD &&
          lpWord->ex.fld.nType==T_INSTANCE &&
          (lpWord->ex.fld.nArrlen!=1 || bForceArray))
      {
        /* Seek instance in array */
        INT32 i = 0;

        for (i=0; i<lpWord->ex.fld.nArrlen; i++)
          if (((CDlpObject**)lpWord->lpData)[i] == lpInst)
            break;

        if (i<lpWord->ex.fld.nArrlen)
          sprintf(lpBuf1,".%s[%ld]%s",lpWord->lpName,(long)i,lpBuf2);
        else
          /* MWX 2002-01-16:
             This is an error actually, but what should be done here ???? --> */
          sprintf(lpBuf1,".%s%s",lpWord->lpName,lpBuf2);
          /* <-- */
      }
      else sprintf(lpBuf1,".%s%s",lpWord->lpName,lpBuf2);
      strcpy(lpBuf2,lpBuf1);

      lpInst = (CDlpObject*)lpWord->lpContainer;
      DLPASSERT(lpInst); /* Word must belong to an instance! */
      if (!lpInst->m_lpContainer) break; /* No parent --> stop */
      lpWord = lpInst->m_lpContainer;
    }
    if (lpBuf2[0] == '.') dlp_memmove(lpBuf2,&lpBuf2[1],dlp_strlen(lpBuf2));
    if (strlen(lpBuf2)>0)  sprintf(lpName,"%s.%s",lpInst->m_lpInstanceName,lpBuf2);
    else                  strcpy(lpName,lpInst->m_lpInstanceName);
  }

  return lpName;
}
Esempio n. 9
0
/**
 * Registers an instanciation function and assosicates it with a dLabPro class
 * name.
 *
 * @param lpClassWord
 *         <code>SWord</code> struct containing dLabPro class info
 * @see CDlpObject_CreateInstanceOf
 * @see CDlpObject_UnregisterAllClasses
 */
void CDlpObject_RegisterClass(const SWord* lpClassWord)
{
  INT32 nXC = 0;

  if (!lpClassWord                           ) return;
  if (lpClassWord->nWordType!=WL_TYPE_FACTORY) return;

  nXC               = __nXClassRegistry++;
  __lpClassRegistry = (SWord*)dlp_realloc(__lpClassRegistry,__nXClassRegistry,sizeof(SWord));
  dlp_memmove(&__lpClassRegistry[nXC],lpClassWord,sizeof(SWord));
}
Esempio n. 10
0
/**
 * Multiplication operation of the symbol string semiring (concatentation).
 *
 * @param lpST Pointer to a string table data structure (returned by
 *             {@link Ssr_Init CFst_Ssr_Init})
 * @param nS1  Index of first (left) string
 * @param nS2  Index of second (right) string
 * @return Index of result string
 */
FST_ITYPE CGEN_SPROTECTED CFst_Ssr_Mult(FST_SST_TYPE* lpST, FST_ITYPE nS1, FST_ITYPE nS2)
{
  INT32 nL1 = 0;
  INT32 nL2 = 0;

  if (nS1<0) return nS2;
  if (nS2<0) return nS1;

  nL1 = CFst_Ssr_Len(lpST,nS1);
  nL2 = CFst_Ssr_Len(lpST,nS2);

  if ((INT32)dlp_size(lpST->lpBuf)<nL1+nL2+1)
    lpST->lpBuf=(FST_STYPE*)__dlp_realloc(lpST->lpBuf,nL1+nL2+lpST->nGrany,sizeof(FST_STYPE),__FILE__,__LINE__,"CFst","");

  dlp_memmove( lpST->lpBuf     ,ST_LP(lpST,nS1),nL1*sizeof(FST_STYPE));
  dlp_memmove(&lpST->lpBuf[nL1],ST_LP(lpST,nS2),nL2*sizeof(FST_STYPE));
  lpST->lpBuf[nL1+nL2]=-1;

  return CFst_Ssr_Store(lpST,lpST->lpBuf);
}
Esempio n. 11
0
/**
 * Prints the instance in text mode (exactly one component of type 1).
 */
INT16 CGEN_PRIVATE CData_PrintText(CData* _this)
{
  INT32  nR   = 0;                                                               /* Current record                    */
  INT32  nXR  = 0;                                                               /* Number of records                 */
  char* tx   = NULL;                                                            /* Auxilary char pointer #1          */
  char* tx0  = NULL;                                                            /* Pointer to string                 */
  char* ty   = NULL;                                                            /* Auxilary char pointer #2          */
  char* ty0  = NULL;                                                            /* Pointer to last white space       */
  INT32  nCtr = 0;                                                               /* Line counter                      */
  char  sBuf[255];                                                              /* Printing buffer                   */

  /* Initialize */                                                              /* --------------------------------- */
  nXR = CData_GetNRecs(_this);                                                  /* Get number of records             */

  /* Print data contents as text */                                             /* --------------------------------- */
  DLPASSERT(FMSG("ALPHA: String mode not tested yet"));                         /* TODO: Remove after debugging      */
  printf("\n  String Mode"); dlp_inc_printlines(1);                             /* Protocol                          */
  tx0=tx=(char*)CData_XAddr(_this,0,0);                                         /* Initialize char pointers          */
  if (!*tx)                                                                     /* Empty string                      */
  {                                                                             /* >>                                */
    printf("\n  [empty]");                                                      /*   Protocol                        */
    dlp_inc_printlines(1);                                                      /*   Adjust no. of printed lines     */
  }                                                                             /* <<                                */
  else while (*tx)                                                              /* Loop over characters              */
  {                                                                             /* >>                                */
    /* Make line breaks */                                                      /*   - - - - - - - - - - - - - - - - */
    for (ty=tx,ty0=NULL; *ty && ty<tx0+nXR; )                                   /*   Do until end of string          */
    {                                                                           /*   >>                              */
      while (*ty && ty<tx0+nXR && !iswspace(*ty)) ty++;                         /*     Seek next white space         */
      while (*ty && ty<tx0+nXR &&  iswspace(*ty)) ty++;                         /*     Skip following white spaces   */
      ty0=ty;                                                                   /*     Remember previous white spc.  */
      if (ty>tx+dlp_maxprintcols()-16) break;                                   /*     Line full                     */
    }                                                                           /*   <<                              */
    if (ty0) ty=ty0;                                                            /*   Go back to last white space     */

    /* Print one line */                                                        /*   - - - - - - - - - - - - - - - - */
    dlp_memset(sBuf,0,255);                                                     /*   Clear printing buffer           */
    dlp_memmove(sBuf,tx,ty-tx);                                                 /*   Copy characters in              */
    printf("\n  %4d(%06d): %s",(int)nCtr,(int)(tx-tx0),sBuf);                   /*   Print 'em                       */
    dlp_inc_printlines(1);                                                      /*   Adjust no. of printed lines     */
    if (dlp_if_printstop()) break;                                              /*   Break listing                   */

    /* End-of-line actions */                                                   /*   - - - - - - - - - - - - - - - - */
    DLPASSERT(ty>tx);                                                           /*   Should have made some progress  */
    tx=ty;                                                                      /*   Move to end of printed line     */
    nCtr++;                                                                     /*   Increment line counter          */
  }                                                                             /* <<                                */
  if (nR>=nXR) printf("\n  No more data - Stop.");                              /* Protocol                          */
  else         printf("\n  Cancelled - Stop.");                                 /* Protocol                          */
  return O_K;                                                                   /* Ok                                */
}
Esempio n. 12
0
 /*   Daubechies D4 transform
 *
 *    FLOAT64* sig         signal array
 *    FLOAT64* trans       transformed signal
 *    INT32    size        signal size
 *    INT16   level       detail level (max. level = -1)  
  */
INT16 CGEN_IGNORE dlm_fwt_d4(FLOAT64* sig, FLOAT64* trans, INT32 size, INT16 level)
{
  register INT32 i;
  register INT32 n;
  register INT32 size2;
  FLOAT64* sigTemp;
  

  if(size < 4)
  {
    return NOT_EXEC;
  } 
  
  sigTemp = (FLOAT64*)dlp_calloc(size, sizeof(FLOAT64));
  dlp_memmove(sigTemp, sig, size * sizeof(FLOAT64));
  
  for (n = size; n >= 4; n >>= 1)
  {
    size2 = n >> 1;
    for (i = 0; i <= size2-2; i++)
    {
      trans[i]       = sigTemp[i*2]*h0 + sigTemp[i*2+1]*h1 + sigTemp[i*2+2]*h2 + sigTemp[i*2+3]*h3; /* scaling function coefficients */
      trans[i+size2] = sigTemp[i*2]*h3 - sigTemp[i*2+1]*h2 + sigTemp[i*2+2]*h1 - sigTemp[i*2+3]*h0; /* wavelet function coefficents */
    }

    trans[i]       = sigTemp[n-2]*h0 + sigTemp[n-1]*h1 + sigTemp[0]*h2 + sigTemp[1]*h3;
    trans[i+size2] = sigTemp[n-2]*h3 - sigTemp[n-1]*h2 + sigTemp[0]*h1 - sigTemp[1]*h0;

    dlp_memmove(sigTemp, trans, n * sizeof(FLOAT64));
    level--;
    if(level == 0) break; /* reduce detail level and exit if reaching 0; if detail level is max. (-1) nothing happens */
  }
  
  dlp_free(sigTemp);
  
  return O_K;
}
Esempio n. 13
0
INT16 CGEN_IGNORE dlm_pam(FLOAT64* X, INT32 nC, INT32 nRX, FLOAT64* Q, INT32 nRQ) {
  INT32* clusters = (INT32*) dlp_calloc(nRQ,sizeof(INT32));
  INT32* classify = (INT32*) dlp_calloc(nRX,sizeof(INT32));
  INT32 iRX1 = 0;
  INT32 iRX2 = 0;
  INT32 iRQ = 0;
  FLOAT64 min = T_DOUBLE_MAX;
  FLOAT64 distance = 0.0;
  FLOAT64 error = T_DOUBLE_MAX;
  FLOAT64 error_old = T_DOUBLE_MAX;
  FLOAT64* pX = X;


  for(iRQ = 0; iRQ < nRQ; iRQ++) {
    clusters[iRQ] = iRQ * nRX / nRQ;
  }
  while(1) {
    error_old = error;
    error = dlm_pam_assign(X,nC,classify,nRX,clusters,nRQ);
    if(error_old <= error) break;
    for(iRQ = 0; iRQ < nRQ; iRQ++) {
      min = T_DOUBLE_MAX;
      for(iRX1 = 0; iRX1 < nRX; iRX1++) {
        if(classify[iRX1] != iRQ) continue;
        pX = X+iRX1*nC;
        distance = 0.0;
        for(iRX2 = 0; iRX2 < nRX; iRX2++) {
          if(classify[iRX2] != iRQ) continue;
          distance += dlm_pam_norm2(pX,X+iRX2*nC,nC);
        }
        if(min > distance) {
          min = distance;
          clusters[iRQ] = iRX1;
        }
      }
    }
  }
  for(iRQ = 0; iRQ < nRQ; iRQ++) {
    dlp_memmove(Q+iRQ*nC,X+clusters[iRQ]*nC,nC*sizeof(FLOAT64));
  }
  dlp_free(clusters);
  dlp_free(classify);

  return O_K;
}
Esempio n. 14
0
/**
 * Stores a string in a string table.
 *
 * @param lpST  Pointer to a string table data structure (returned by
 *              {@link Ssr_Init CFst_Ssr_Init})
 * @param lpBuf Pointer to string to be stored
 * @return The unique string index of the new string
 */
FST_ITYPE CGEN_SPROTECTED CFst_Ssr_Store(FST_SST_TYPE* lpST, FST_ITYPE* lpBuf)
{
  FST_ITYPE nS = 0;
  INT32      nL = 0;

  if (lpBuf[0]                      ==-1) return -1;                           /* epsilon string                    */
  if ((nS=CFst_Ssr_Find(lpST,lpBuf))>= 0) return nS;                           /* String already present in table   */

  nS = CDlpTable_AddRecs(lpST->iST,1,lpST->nGrany);
  for (nL=0; lpBuf[nL]!=-1; nL++) {}
  nL++;

  *(FST_STYPE**)CDlpTable_XAddr(lpST->iST,nS,0) =
    (FST_STYPE*)__dlp_calloc(nL,sizeof(FST_STYPE),__FILE__,__LINE__,"CFst","");

  dlp_memmove(ST_LP(lpST,nS),lpBuf,sizeof(FST_STYPE)*nL);

  return nS;
}
Esempio n. 15
0
 /*   Inverse Daubechies D4 transform
 *
 *    FLOAT64* trans       transformed signal
 *    FLOAT64* sig         signal array 
 *    INT32    size        transformed signal array size
 *    INT16   level       detail level (max. level = -1) that was used to transform signal  
 */
INT16 CGEN_IGNORE dlm_fwt_d4_inv(FLOAT64* trans, FLOAT64* sig, INT32 size, INT16 level)
{
  register INT32 i;
  register INT32 n;
  register INT32 size2;
  FLOAT64*  transTemp;

  if(size < 4)
  {
    return NOT_EXEC;
  } 

  transTemp = (FLOAT64*)dlp_calloc(size, sizeof(FLOAT64));
  dlp_memmove(transTemp, trans, size * sizeof(FLOAT64));

  if(level > 0)
  {
    n = size >> (level-1);
    if(n<4) n=4;
  }
Esempio n. 16
0
/**
 * <p>INTERNAL USE ONLY. This method is called by {@link -pool} to pool the
 * statistics blocks contained in <code>idSrc</code> and store the result in
 * <code>idPool</code>. The operation is controlled through the pooling mode
 * flag <code>nMode</code> as follows:</p>
 *
 * <ul>
 *   <li>nMode=0: Pool sum data, <code>idPool</code> will be overwritten</li>
 *   <li>nMode=1: Pool min data</li>
 *   <li>nMode=2: Pool max data</li>
 * </ul>
 *
 * <h3>Remarks</h3>
 * <ul>
 *   <li>In order to pool raw statistics data, there are three calls
 *     (<code>nMode</code>=0,1 and 2) necessary. The first call of these
 *     <em>must</em> be the one with <code>nMode</code>=0!</li>
 *   <li>There are NO checks performed</li>
 * </ul>
 *
 * @param idPool
 *          Pooled raw statistics data block
 * @param idSrc
 *          Raw statistics data blocks to be pooled
 * @param nMode
 *          Pooling mode, see above
 */
void CGEN_SPRIVATE CStatistics_PoolInt(CData* idPool, CData* idSrc, INT32 nMode)
{
  CData* idAux      = NULL;                                                     /* Auxilary data instance #1         */

  if (nMode==0)                                                                 /* Sum aggregation mode              */
  {                                                                             /* >>                                */
    ISETOPTION(idPool,"/block");                                                /*   Switch target to block mode     */
    CData_Aggregate(idPool,idSrc,NULL,CMPLX(0),"sum");                          /*   Aggregate (sum up)              */
    IRESETOPTIONS(idPool);                                                      /*   Switch target to normal mode    */
  }                                                                             /* <<                                */
  else if (nMode==1 || nMode==2)                                                /* Extrama aggregation modes         */
  {                                                                             /* >>                                */
    ICREATEEX(CData,idAux,"CStatistics_Pool_int.~idAux",NULL);                  /*   Create auxilary data instance #1*/
    ISETOPTION(idAux,"/block");                                                 /*   Switch target to block mode     */
    CData_Aggregate(idAux,idSrc,NULL,CMPLX(0),nMode==1?"min":"max");            /*   Aggregate (minimum or maximum)  */
    dlp_memmove(CData_XAddr(idPool,nMode,0),CData_XAddr(idAux,nMode,0),         /*   Copy aggregated min. or max. ...*/
      CData_GetRecLen(idAux));                                                  /*   | ... to target                 */
    IRESETOPTIONS(idAux);                                                       /*   Switch target to normal mode    */
    IDESTROY(idAux);                                                            /*   Destroy auxilary data inst. #1  */
  }                                                                             /* <<                                */
  else                                                                          /* Unknown mode                      */
    DLPASSERT(FMSG("Invalid internal pooling mode"));                           /*   Not so good ...                 */
}
Esempio n. 17
0
INT16 CGEN_VPROTECTED CLPCproc::SynthesizeFrameImpl(FLOAT64* lpcCoef, INT16 n_lpcCoeff, FLOAT64* exc, INT32 n_exc, FLOAT64 nPfaLambda, FLOAT64 nSynLambda, FLOAT64* syn) {
  INT32    i    = 0;
  FLOAT64  gain = 0.0;
  FLOAT64* lpc  = NULL;

  if(n_lpcCoeff != m_nCoeff) {
    if(m_lpMemLpc != NULL) {
      dlp_free(m_lpMemLpc);
    }
  }
  if(m_lpMemLpc == NULL) {
    m_lpMemLpc = (FLOAT64*)dlp_calloc(n_lpcCoeff - 1, sizeof(FLOAT64));        //  allocate memory
    if(!m_lpMemLpc) return ERR_MEM;
    m_nCoeff = n_lpcCoeff;
  }

  lpc = (FLOAT64*)dlp_calloc(n_lpcCoeff, sizeof(FLOAT64));
  if(!lpc) return IERROR(this, ERR_MEM, "lpc", 0, 0);

  gain = *lpcCoef;
  *lpcCoef = 1.0;
  if(nPfaLambda != nSynLambda) {
    dlm_mlpc2lpc(lpcCoef, n_lpcCoeff, lpc, n_lpcCoeff, (nPfaLambda-nSynLambda)/(1-nPfaLambda*nSynLambda));
  } else {
    dlp_memmove(lpc, lpcCoef, n_lpcCoeff*sizeof(FLOAT64));
  }

  gain = gain/ *lpc;
  *lpc = 1.0;
  dlm_filter_iir(lpc, n_lpcCoeff, exc, syn, n_exc, m_lpMemLpc, n_lpcCoeff - 1);
  for(i = 0; i < n_exc; i++) {
    syn[i] *= gain;
  }

  dlp_free(lpc);
  return O_K;
}
Esempio n. 18
0
/*
 * Manual page at fst_man.def
 */
INT16 CGEN_PUBLIC CFst_Product
(
  CFst* _this,
  CFst* itSrc1,
  CFst* itSrc2,
  INT32  nUnit1,
  INT32  nUnit2
)
{
  INT32      nC           = 0;                                                   /* Current component                  */
  INT32      nFCS2        = 0;                                                   /* 1st data comp. of state tab.itSrc2 */
  INT32      nXCS1        = 0;                                                   /* No. of comps. of state tab. itSrc1 */
  INT32      nXCS2        = 0;                                                   /* No. of comps. of state tab. itSrc2 */
  INT32      nFCT2        = 0;                                                   /* 1st data comp. of trans.tab.itSrc2 */
  INT32      nXCT1        = 0;                                                   /* No. of comps. of trans.tab. itSrc1 */
  INT32      nXCT2        = 0;                                                   /* No. of comps. of trans.tab. itSrc2 */
  INT32      nRls1        = 0;                                                   /* Rec. len. of state table of itSrc1 */
  INT32      nRls2        = 0;                                                   /* Rec. len. of state table of itSrc2 */
  INT32      nRlt1        = 0;                                                   /* Rec. len. of trans.table of itSrc1 */
  INT32      nRlt2        = 0;                                                   /* Rec. len. of trans.table of itSrc2 */
  FST_ITYPE nS1          = 0;
  FST_ITYPE nS2          = 0;
  FST_ITYPE nFS1         = 0;
  FST_ITYPE nFS2         = 0;
  FST_ITYPE nXS1         = 0;
  FST_ITYPE nXS2         = 0;
  FST_ITYPE nT           = 0;
  FST_ITYPE nIni         = 0;
  FST_ITYPE nTer         = 0;
  FST_ITYPE nT1          = 0;
  FST_ITYPE nT2          = 0;
  FST_ITYPE nFT1         = 0;
  FST_ITYPE nFT2         = 0;
  FST_ITYPE nXT1         = 0;
  FST_ITYPE nXT2         = 0;
  char*     lpsBuf       = NULL;                                                /* String buffer                      */
  INT16     nVirt        = 0;                                                   /* Auxiliary: patching ovrl.args. bug */
  BOOL      bNoloopsSave = FALSE;                                               /* Save buffer for /noloops option    */

  /* Validate */
  CHECK_THIS_RV(NOT_EXEC);
  if (itSrc1==NULL || itSrc2==NULL)
  {
    CFst_Reset(BASEINST(_this),TRUE);
    return O_K;
  }
  if (nUnit1<0 || nUnit1>=UD_XXU(itSrc1)) return IERROR(_this,FST_BADID,"unit",nUnit1,0);
  if (nUnit2<0 || nUnit2>=UD_XXU(itSrc2)) return IERROR(_this,FST_BADID,"unit",nUnit2,0);

  /* Initialize */
  /* HACK: Multiple overlapping arguments on _this not handled correctly by
           CREATEVIRTUAL --> */
  if (_this==itSrc1 && _this==itSrc2) return IERROR(_this,ERR_GENERIC,"Invalid arguments",0,0);
  if      (itSrc1==_this) { CREATEVIRTUAL(CFst,itSrc1,_this); nVirt=1; }
  else if (itSrc2==_this) { CREATEVIRTUAL(CFst,itSrc2,_this); nVirt=2; }
  /* <-- */
  bNoloopsSave = _this->m_bNoloops;
  CFst_Reset(BASEINST(_this),TRUE);
  _this->m_bNoloops = bNoloopsSave;

  /* Initialize destination state table */
  nFS1  = UD_FS(itSrc1,nUnit1);
  nXS1  = UD_XS(itSrc1,nUnit1);
  nFS2  = UD_FS(itSrc2,nUnit2);
  nXS2  = UD_XS(itSrc2,nUnit2);
  nRls1 = CData_GetRecLen(AS(CData,itSrc1->sd)) - CData_GetCompOffset(AS(CData,itSrc1->sd),IC_SD_DATA);
  nRls2 = CData_GetRecLen(AS(CData,itSrc2->sd)) - CData_GetCompOffset(AS(CData,itSrc2->sd),IC_SD_DATA);
  nXCS1 = CData_GetNComps(AS(CData,itSrc1->sd)) - IC_SD_DATA;
  nXCS2 = CData_GetNComps(AS(CData,itSrc2->sd)) - IC_SD_DATA;
  nFCS2 = IC_SD_DATA + nXCS1;
  for (nC=IC_SD_DATA; nC<IC_SD_DATA+nXCS1; nC++)
    CData_AddComp
    (
      AS(CData,_this->sd),
      CData_GetCname(AS(CData,itSrc1->sd),nC),
      CData_GetCompType(AS(CData,itSrc1->sd),nC)
    );
  for (nC=IC_SD_DATA; nC<IC_SD_DATA+nXCS2; nC++)
    CData_AddComp
    (
      AS(CData,_this->sd),
      CData_GetCname(AS(CData,itSrc2->sd),nC),
      CData_GetCompType(AS(CData,itSrc2->sd),nC)
    );

  /* Initialize destination transition table */
  nFT1  = UD_FT(itSrc1,nUnit1);
  nXT1  = UD_XT(itSrc1,nUnit1);
  nFT2  = UD_FT(itSrc2,nUnit2);
  nXT2  = UD_XT(itSrc2,nUnit2);
  nRlt1 = CData_GetRecLen(AS(CData,itSrc1->td)) - CData_GetCompOffset(AS(CData,itSrc1->td),IC_TD_DATA);
  nRlt2 = CData_GetRecLen(AS(CData,itSrc2->td)) - CData_GetCompOffset(AS(CData,itSrc2->td),IC_TD_DATA);
  nXCT1 = CData_GetNComps(AS(CData,itSrc1->td)) - IC_TD_DATA;
  nXCT2 = CData_GetNComps(AS(CData,itSrc2->td)) - IC_TD_DATA;
  nFCT2 = IC_TD_DATA + nXCT1;
  for (nC=IC_TD_DATA; nC<IC_TD_DATA+nXCT1; nC++)
    CData_AddComp
    (
      AS(CData,_this->td),
      CData_GetCname(AS(CData,itSrc1->td),nC),
      CData_GetCompType(AS(CData,itSrc1->td),nC)
    );
  for (nC=IC_TD_DATA; nC<IC_TD_DATA+nXCT2; nC++)
    CData_AddComp
    (
      AS(CData,_this->td),
      CData_GetCname(AS(CData,itSrc2->td),nC),
      CData_GetCompType(AS(CData,itSrc2->td),nC)
    );

  /* Copy input and output symbol tables */
  CData_Copy(_this->is,itSrc1->is);
  CData_Copy(_this->os,itSrc2->os);

  /* Initialize destination unit */
  lpsBuf = (char*)dlp_calloc
  (
    CData_GetCompType(AS(CData,itSrc1->ud),IC_UD_NAME) +
    CData_GetCompType(AS(CData,itSrc2->ud),IC_UD_NAME) + 2,
    sizeof(char)
  );
  sprintf
  (
    lpsBuf,"%s.%s",
    (const char*)CData_XAddr(AS(CData,itSrc1->ud),nUnit1,IC_UD_NAME),
    (const char*)CData_XAddr(AS(CData,itSrc2->ud),nUnit2,IC_UD_NAME)
  );
  CFst_Addunit(_this,lpsBuf);
  dlp_free(lpsBuf);

  /* Add product states */
  CFst_Addstates(_this,0,nXS1*nXS2,FALSE);

  /* Copy state qualification (including final state flag) */
  for (nS1=0; nS1<nXS1; nS1++)
    for (nS2=0; nS2<nXS2; nS2++)
    {
      if ((SD_FLG(itSrc1,nS1+nFS1)&0x01)==0x01 && (SD_FLG(itSrc2,nS2+nFS2)&0x01)==0x01)
        SD_FLG(_this,nS1*nXS2+nS2) |= 0x01;
      if (nRls1>0)
        dlp_memmove
        (
          CData_XAddr(AS(CData,_this ->sd),nS1*nXS2+nS2,IC_SD_DATA),
          CData_XAddr(AS(CData,itSrc1->sd),nS1+nFS1    ,IC_SD_DATA),
          nRls1
        );
      if (nRls2>0)
        dlp_memmove
        (
          CData_XAddr(AS(CData,_this ->sd),nS1*nXS2+nS2,nFCS2     ),
          CData_XAddr(AS(CData,itSrc2->sd),nS2+nFS2    ,IC_SD_DATA),
          nRls2
        );
    }

  CData_AddRecs(AS(CData,_this->td),nXT1*nXT2,_this->m_nGrany);
  /* Loop over all transitions of both factors */
  for (nT=0, nT1=nFT1; nT1<nFT1+nXT1; nT1++)
    for (nT2=nFT2; nT2<nFT2+nXT2; nT2++, nT++)
    {
      /* Get product of initial and terminal state */
      nIni = TD_INI(itSrc1,nT1)*nXS2 + TD_INI(itSrc2,nT2);
      nTer = TD_TER(itSrc1,nT1)*nXS2 + TD_TER(itSrc2,nT2);
      if (_this->m_bNoloops && nIni==nTer) continue;

      /* Add product transition */
      *(FST_ITYPE*)CData_XAddr(AS(CData,_this->td),nT,IC_TD_INI) = nIni;
      *(FST_ITYPE*)CData_XAddr(AS(CData,_this->td),nT,IC_TD_TER) = nTer;

      /* Copy transition qualification */
      dlp_memmove
      (
        CData_XAddr(AS(CData,_this ->td),nT ,IC_TD_DATA),
        CData_XAddr(AS(CData,itSrc1->td),nT1,IC_TD_DATA),
        nRlt1
      );
      dlp_memmove
      (
        CData_XAddr(AS(CData,_this ->td),nT ,nFCT2     ),
        CData_XAddr(AS(CData,itSrc2->td),nT2,IC_TD_DATA),
        nRlt2
      );
    }

  /* Finish destination instance */
  CData_SelectRecs(AS(CData,_this->td),AS(CData,_this->td),0,nT);
  UD_XT(_this,0) = nT;
  /* TODO: Trim result !? */

  /* Clean up */
  if (nVirt==1) { DESTROYVIRTUAL(itSrc1,_this); }
  if (nVirt==2) { DESTROYVIRTUAL(itSrc2,_this); }
  return O_K;
}
Esempio n. 19
0
INT16 CGEN_PRIVATE CProsody::EnergyContour(data *dSignal, data *dEnergy)
{
  /* Initialization */
  INT32 i = 0;
  INT32 j = 0;
  INT32 nSrate = 0;             // Sample Rate
  INT32 nSamples = 0;         // Number of samples in WAV-signal (number of records in data instance)
  INT32 nWindowShift = 0;      // Shift of Windows (samples)
  INT32 nWindowLength = 0;      // Length of Window (samples)
  INT32 nShiftNumber = 0;      // Number of Shifts in the signal
  INT32 nWindowNumber = 0;      // Number of windows
  FLOAT64 nAuxEner = 0;      // Auxiliary variable to calculate the energy for one window
  FLOAT64 *samples = NULL;         // WAV Signal in double
  FLOAT64 *dEnergyContour = NULL;  // Short Time Energy Contour
  
  
  /* Validation of data instance dEnergy */
  if (!dEnergy || !dEnergy->IsEmpty())    // If dEnergy not exist or empty then return false
    return NOT_EXEC;                  // NOT_EXEC = -1 (Generic error)
 
  /* Definition of data instance dEnergy */
  // One column is the values of energy contour  
    dEnergy->AddNcomps(T_DOUBLE, 1);
    dEnergy->SetCname(0, "nEnergy");


  
  /* Compute Sample Rate (SR) */
  //nSrate = m_nSrate;
  //nSrate  = (INT32)(1000.0/CData_GetDescr(dSignal,RINC));
  
  //# "\n Distance between two Samples = ${idSig.rinc} [msec] "  -echo;
  //# 1000 idSig.rinc / nSrate =;          # Compute Sample Rate (SR)  
  nSrate = (INT32)((1000.0/CData_GetDescr(dSignal,RINC)) + 0.5);  /* +0.5 forces cast to round up or down */    
  //fprintf(stdout,"Sample Rate:   %d Hz\n\n",nSrate);
  
  
     /* Number of samples in WAV-signal */
  nSamples  = dSignal->GetNRecs();  // GetNRecs returns the number of valid records in the data instance
  //fprintf(stdout,"Number of samples in WAV-signal:   %i\n\n",nSamples);


  /* Copies data from memory area to another memory area */
  samples = (FLOAT64*)dlp_calloc(nSamples, sizeof(FLOAT64));  // dlp_calloc(NUM,SIZE) Allocates zero-initialize memory
    dlp_memmove(samples, dSignal->XAddr(0,0), nSamples * sizeof(FLOAT64));
      
    /*
    fprintf(stdout,"\n The samples of speech signal are: \n");  // show data 
  for(i=0; i<nSamples; i++)               
    fprintf(stdout,"Signal Sample %i = %f\n",i,samples[i]);
  */

  /* Calculate the Window-Length and the Window-Shift */
  nWindowShift = nSrate / 100;      // Shift = 10 msec = 16000 / 100 = 160 samples
  nWindowLength = nWindowShift * 3;  // Window Length = 30 msec = Window Shift * 3
  
  
  nShiftNumber = nSamples / nWindowShift;    // Number of Shifts in the whole signal
  nWindowNumber = nShiftNumber - 2;      // Number of Windows
  
  if (nWindowNumber < 1)            // There is no window (signal is too short)
  {
    nWindowNumber = 1;            // Set number of windows to 1
    nWindowLength = nSamples;
  }
  
  dEnergyContour = (FLOAT64*)dlp_calloc(nWindowNumber, sizeof(FLOAT64));
  
  
  /* Fensterung des Signals und Berechnung der Energy f�r jedes Fensters */
  for (i=0; i<nWindowNumber; i++)
  {
    nAuxEner = 0;
    for (j=0; j<nWindowLength; j++)
    {
      nAuxEner = nAuxEner + samples[i*nWindowShift+j]*samples[i*nWindowShift+j];  
    }
    dEnergyContour[i] = nAuxEner;
  }
    
    

  /* Write Energy values in struct */
    STEnergy_CONTOUR *Energy_contour = NULL;  // (Energy_contour) is an object of struct (STEnergy_CONTOUR)


  Energy_contour = (STEnergy_CONTOUR*)dlp_calloc(nWindowNumber, sizeof(STEnergy_CONTOUR));  // Allocates zero-initialize memory

  for(i = 0; i < nWindowNumber; i++)  
  {
        (Energy_contour + i)->nEnergyValue = dEnergyContour[i];
        //fprintf( stdout,"nEnergyValue  %i = %f\n",i,dEnergyContour[i] );
  }


    
  /* Copy Energy values from (struct Energy_contour) to output data object (dEnergy) */
  dEnergy->AddRecs(nWindowNumber, 1); // AddRecs: Appends (n) records to the end of the table (data object)  
  for( i = 0; i < nWindowNumber; i++ )
  {
      dEnergy->Dstore((FLOAT64)(Energy_contour + i)->nEnergyValue, i, 0);
  }


  /*
  FLOAT64 nAuxCopy = 0;
  
  for (i = 0; i < nWindowNumber; i++)
  {
    nAuxCopy = (FLOAT64)CData_Dfetch(dEnergy,i,0);
    fprintf(stdout,"F0 value %i = %f\n",i,nAuxCopy);  
  }
  */  
  
  dlp_free(samples);    
  dlp_free(dEnergyContour);
  dlp_free(Energy_contour);    
      
  return O_K;
}
Esempio n. 20
0
/**
 * Creates a tree with the best <code>nPaths</code> paths (minimum cost) taken from <code>itSrc</code>. There are no
 * checks performed.
 *
 * @param _this  Pointer to this (destination) automaton instance
 * @param itSrc  Pointer to source automaton instance
 * @param nUnit  Index of unit to process
 * @param nPaths Number of paths to be extracted
 * @param nPathlength Length of paths to be extracted
 * @return O_K if successfull, a (negative) error code otherwise
 * @see BestN CFst_BestN
 */
INT16 CGEN_PROTECTED CFst_BestNUnit(CFst* _this, CFst* itSrc, INT32 nUnit, INT32 nPaths, INT32 nPathlength)
{
  FST_PQU_TYPE* lpPQ      = NULL;                                              /* Priority queue data struct         */
  FST_TID_TYPE* lpTIX     = NULL;                                              /* Source graph iterator data struct  */
  BYTE*         lpTX      = NULL;                                              /* Ptr. to current source transition  */
  FST_ITYPE     nFS       = 0;                                                 /* First state of source unit         */
  INT32          nArrivals = 0;                                                 /* Paths having reached a final state */
  FST_WTYPE     nNeAdd    = 0.;                                                /* Neutral element of addition        */
  FST_WTYPE     nNeMult   = 0.;                                                /* Neutral element of multiplication  */
  FST_ITYPE     nPQsize   = 0;                                                 /* Priority queue size                */
  FST_ITYPE     nT        = 0;                                                 /* Index of current source transition */
  FST_ITYPE     nNewT     = 0;                                                 /* Index of newly inserted dst. trans.*/
  FST_ITYPE     nSini     = 0;                                                 /* Ini. state of curr. dest. trans.   */
  FST_ITYPE     nXini     = 0;                                                 /* Ini. state of curr. source trans.  */
  FST_ITYPE     nYini     = 0;                                                 /* Ini. state of curr. aux. trans.    */
  FST_ITYPE     nSter     = 0;                                                 /* Ter. state of curr. dest. trans.   */
  FST_ITYPE     nXter     = 0;                                                 /* Ter. state of curr. source trans.  */
  FST_ITYPE     nYter     = 0;                                                 /* Ter. state of curr. aux. trans.    */
  FST_ITYPE     nPlen     = 0;                                                 /* length of the path up to here      */
  FST_WTYPE     nWX       = 0.0;                                               /* Weight of current src. transition  */
  FST_WTYPE     nPot      = 0.0;                                               /* Potential of current src. state    */
  FST_WTYPE     nCacc     = 0.0;                                               /* Accumulated cost from start state  */
  FST_WTYPE     nCtot     = 0.0;                                               /* Total cost to final state          */
  FST_WTYPE     nCmax     = 0.0;                                               /* Maximal cost in priority queue     */
  INT32          nNewY     = 0;                                                 /* New auxiliary state                */
  INT32          nIcP      = 0;                                                 /* Comp. index of src. state potential*/
  INT16         nCheck    = 0;                                                 /* Copy buffer for verbose level      */
  BOOL          bPush     = FALSE;                                             /* Copy of m_bPush option             */
  INT32          k         = 0;                                                 /* Auxilary loop counter              */

  /* Validate */
  DLPASSERT(_this!=itSrc);
  DLPASSERT(nUnit>=0 && nUnit<UD_XXU(itSrc));

  /* Initialize destination */
  nCheck = BASEINST(_this)->m_nCheck;
  bPush  = _this->m_bPush;
  CFst_Reset(BASEINST(_this),TRUE);
  BASEINST(_this)->m_nCheck=nCheck;
  CData_Scopy(AS(CData,_this->sd),AS(CData,itSrc->sd));
  CData_Scopy(AS(CData,_this->td),AS(CData,itSrc->td));
  CData_SelectRecs(AS(CData,_this->ud),AS(CData,itSrc->ud),nUnit,1);
  UD_FS(_this,0)  = 0;
  UD_FT(_this,0)  = 0;
  UD_XS(_this,0)  = 0;
  UD_XT(_this,0)  = 0;
  _this->m_nGrany = itSrc->m_nGrany;
  _this->m_nWsr   = CFst_Wsr_GetType(itSrc,&_this->m_nIcW);
  BASEINST(_this)->m_nCheck = BASEINST(_this)->m_nCheck>BASEINST(itSrc)->m_nCheck?BASEINST(_this)->m_nCheck:BASEINST(itSrc)->m_nCheck;

  CFst_Cps_AddSdAux(_this);
  nNeMult = CFst_Wsr_NeMult(_this->m_nWsr);
  nNeAdd  = CFst_Wsr_NeAdd (_this->m_nWsr);

  /* Add in itSrc the component Extracted that stores how many times this node has been visited */
  nIcP = CData_FindComp(AS(CData,itSrc->sd),NC_SD_POT);
  DLPASSERT(nIcP         >=0);
  DLPASSERT(_this->m_nIcW>=0);

  /* Initialize - Create priority queue */
  lpPQ = (FST_PQU_TYPE*)dlp_calloc(nPaths+1,sizeof(FST_PQU_TYPE));
  for (k=0; k<nPaths+1; k++) lpPQ[k].nCtot = nNeAdd;

  /* Initialize - Graph iterator */
  lpTIX = CFst_STI_Init(itSrc,nUnit,FSTI_SORTINI);
  nFS   = UD_FS(itSrc,nUnit);

  /* Create initial state in _this */
  nXter = CFst_Addstates(_this,0,1,SD_FLG(itSrc,nFS)&0x01);
  CFst_Cps_SetSdAux(_this,nXter,0,0,0);

  /* Initialize local variables */
  nXter = 0;
  nYter = 0;
  nCacc = nNeMult;

  /* Best-N computation */
  for(;;)
  {
    /* For all transitions leaving nXter */
    lpTX = NULL;
    while ((lpTX=CFst_STI_TfromS(lpTIX,nXter,lpTX))!=NULL)
    {
      /* Compute nCtot following the current transition */
      nT    = CFst_STI_GetTransId(lpTIX,lpTX);
      nWX   = *CFst_STI_TW(lpTIX,lpTX);
      nPot  = *(FST_WTYPE*)(CData_XAddr(AS(CData,itSrc->sd),TD_TER(itSrc,nT)+lpTIX->nFS,nIcP));
      nCtot = CFst_Wsr_Op(_this,nCacc,CFst_Wsr_Op(_this,nPot,nWX,OP_MULT),OP_MULT); /* nCacc + nWX + nPot; */
      IFCHECKEX(1)
        printf("\n state: %d \t Cacc: %f\t W: %f \t Pot: %f \t Ctot: %f\t\n",
          (int)nXter,(float)nCacc,(float)nWX,(float)nPot,(float)nCtot);

      /* Insert into priority queue? */
      if
      (
        nPQsize < (nPaths - nArrivals)              ||
        (
          nPQsize==(nPaths - nArrivals)             &&
          CFst_Wsr_Op(_this,nCtot,nCmax,OP_GREATER)
        )
      )
      {
        nNewY++;

        /* Push into priority queue */
        lpPQ[nPQsize].nXini = nXter;
        lpPQ[nPQsize].nYini = nYter;
        lpPQ[nPQsize].nXter = *CFst_STI_TTer(lpTIX,lpTX);
        lpPQ[nPQsize].nYter = nNewY;
        lpPQ[nPQsize].nTran = nT;
        lpPQ[nPQsize].nPlen = nPlen+1;
        lpPQ[nPQsize].nCacc = CFst_Wsr_Op(_this,nCacc,nWX,OP_MULT); /* nWX + nCacc; */
        lpPQ[nPQsize].nCtot = nCtot;
        nPQsize++;

        IFCHECKEX(1)
          printf("\n PriQsize:%d PUSH: [X:%d Y:%d] --- Cacc:%f Tran:%d Plen:%d Ctot:%f ----> [X:%d Y:%d]\n",
            (int)nPQsize,(int)nXter,(int)nYter,(float)nCacc,(int)nT,(int)nPlen,
            (float)nCtot,(int)*CFst_STI_TTer(lpTIX,lpTX),(int)nNewY);

        /* Sort priority queue */
        if (_this->m_nWsr == FST_WSR_PROB)
          qsort(lpPQ,nPaths+1,sizeof(FST_PQU_TYPE),CFst_Bsn_CompDown);
        else
          qsort(lpPQ,nPaths+1,sizeof(FST_PQU_TYPE),CFst_Bsn_CompUp);

        /* Delete last element from queue (if neccesary) to keep its size smaller than nPaths+1 */
        if (nPQsize == nPaths+1-nArrivals)
        {
          lpPQ[nPQsize-1].nCtot = nNeAdd;
          nPQsize--;
        }
        nCmax = lpPQ[nPQsize-1].nCtot;

        IFCHECKEX(2)
          for (k=0; k<nPQsize; k++)
            printf("Q %d : [X:%d Y:%d] ---  Tran:%d  ----> [X:%d Y:%d] Plen:%d Cacc:%f ",
              (int)k,(int)lpPQ[k].nXini,(int)lpPQ[k].nYini,(int)lpPQ[k].nTran,
              (int)lpPQ[k].nXter,(int)lpPQ[k].nYter,(int)lpPQ[k].nPlen,(float)lpPQ[k].nCacc),
          printf("Ctot:%f \n",lpPQ[k].nCtot);
      }
    }

    /* Write transition and state in _this */
    DLPASSERT(nPQsize>=0);
    if (nPQsize>0)
    {
      /* Pop first record data from priority queue */
      nXini = lpPQ[0].nXini;
      nYini = lpPQ[0].nYini;
      nXter = lpPQ[0].nXter;
      nYter = lpPQ[0].nYter;
      nT    = lpPQ[0].nTran;
      nPlen = lpPQ[0].nPlen;
      nCacc = lpPQ[0].nCacc;
      nCtot = lpPQ[0].nCtot;

      dlp_memmove(lpPQ,&lpPQ[1],nPaths*sizeof(FST_PQU_TYPE));
      lpPQ[nPQsize-1].nCtot = nNeAdd;
      nPQsize--;
      IFCHECKEX(1) printf(" Pop priority queue\n");
      IFCHECKEX(2)
        for (k=0; k<nPQsize; k++)
          printf("Q %d : [X:%d Y:%d] ---  Tran:%d  ----> [X:%d Y:%d] Plen:%d Cacc:%f ",
            (int)k,(int)lpPQ[k].nXini,(int)lpPQ[k].nYini,(int)lpPQ[k].nTran,
            (int)lpPQ[k].nXter,(int)lpPQ[k].nYter,(int)lpPQ[k].nPlen,(float)lpPQ[k].nCacc),
          printf("Ctot:%f \n",(float)lpPQ[k].nCtot);

      /* Add state and trans in _this */
      nSini = CFst_Cps_FindState(_this,nXini,nYini,0);
      nSter = CFst_Addstates(_this,0,1,(nPathlength<=0||nPlen>=nPathlength)?(SD_FLG(itSrc,nXter+nFS)&0x01):0); /* FS-Flag ist only used if Path is long enough */
      CFst_Cps_SetSdAux(_this,nSter,nXter,nYter,0);

      /* If the path in itSrc is finished and path length exceeded increment nArrivals */
      if ((SD_FLG(itSrc,nXter+nFS)&0x01) && (nPathlength<=0 || nPlen>=nPathlength))
      {
        nArrivals++;
        nWX = nCtot;
      }
      else nWX = nNeMult;

      nNewT = CFst_AddtransCopy(_this,0,nSini,nSter,itSrc,nT);
      if (bPush) *(FST_WTYPE*)CData_XAddr(AS(CData,_this->td),nNewT,_this->m_nIcW) = nWX;

      IFCHECKEX(1)
        printf("\n AddTrans: [X:%d Y:%d] --- Cacc:%f Tran:%d Plen:%d Ctot:%f ----> [X:%d Y:%d]\n",
          (int)nXini,(int)nYini,(float)nCacc,(int)nT,(int)nPlen,(float)nCtot,(int)nXter,(int)nYter);
    }
Esempio n. 21
0
INT16 CGEN_VPROTECTED CFBAproc::SynthesizeUsingInto(data *idFea, data *idInto, data *idSyn) {
  INT32         nSamplingPoints               = idInto->GetNRecs();
  INT32         iSamplingPoints1              = 0;
  INT32         iSamples                      = 0;
  INT32         iSamplesOld                   = 0;
  INT32         nSamples                      = 0;
  INT32         nSamplesOfLastUnits           = 0;
  INT32         nSampleOfLastF0SamplingPoint  = 0;
  INT32         nSampleOfNextF0SamplingPoint  = 0;
  INT32         nPer                          = 0;
  INT32         iFrames                       = 0;
  INT32         iFramesOld                    = 0;
  INT32         nFrames                       = idFea->GetNRecs();
  INT32         iLab                          = 0;
  INT32         nLab                          = 0;
  INT32         iFea                          = 0;
  INT32         nCompVuv                      = idFea->FindComp("v/uv");
  INT32         nFea                          = idFea->GetNNumericComps() - (nCompVuv >= 0 ? 1 : 0);
  const char*   nextF0Pho                     = NULL;
  FLOAT64       lastF0Val                     = 1.0;
  FLOAT64       currF0Val                     = 1.0;
  FLOAT64       nextF0Val                     = 1.0;
  FLOAT64       nextF0Pos                     = 0.0;
  FLOAT64       lastI0Val                     = 1.0;
  FLOAT64       currI0Val                     = 1.0;
  FLOAT64       nextI0Val                     = 1.0;
  FLOAT64*      exc                           = NULL;
  FLOAT64*      fea                           = NULL;
  FLOAT64*      feaBefore                     = NULL;
  FLOAT64*      feaUsed                       = NULL;
  FLOAT64*      feaUsedBefore                 = NULL;
  BOOL          isVoiceless                   = FALSE;
  BOOL          isSmoothable                  = FALSE;
  BOOL          isSmoothableBefore            = FALSE;
  SLAB*         sIntoLab                      = NULL;
  SLAB*         sFeaLab                       = NULL;

  if(nSamplingPoints <= 0) {
    return O_K;
  }

  if(SynthesizeUsingIntoGetFeaIntoLab(idFea, idInto, &sFeaLab, &sIntoLab, &nSamples, &nLab) != O_K) {
    if(sFeaLab != NULL) dlp_free(sFeaLab);
    if(sIntoLab != NULL) dlp_free(sIntoLab);
    return NOT_EXEC;
  }

  exc       = (FLOAT64*)dlp_malloc(nSamples * sizeof(FLOAT64));
  fea       = (FLOAT64*)dlp_malloc(nFea * sizeof(FLOAT64));
  feaBefore = (FLOAT64*)dlp_malloc(nFea * sizeof(FLOAT64));

  idSyn->Reset(TRUE);
  idSyn->AddComp("syn", T_DOUBLE);
  idSyn->Allocate(nSamples);

  iFrames = 0;
  iLab = 0;
  iSamplingPoints1 = 0;
  iSamples = 0;
  iSamplesOld = 0;
  iFramesOld = 0;
  nSamplesOfLastUnits = 0;
  while(iSamples < nSamples) {
    if(iSamples >= nSampleOfNextF0SamplingPoint) {
      lastF0Val = nextF0Val;
      lastI0Val = nextI0Val;
      nSampleOfLastF0SamplingPoint = nSampleOfNextF0SamplingPoint;
      while(iSamples >= nSampleOfNextF0SamplingPoint) {
        for(; iSamplingPoints1 < nSamplingPoints; iSamplingPoints1++) {
          nextF0Pho = idInto->Sfetch(iSamplingPoints1, 2);
          nextF0Val = idInto->Dfetch(iSamplingPoints1, 3);
          nextI0Val = idInto->Dfetch(iSamplingPoints1, 6);
          nextF0Pos = idInto->Dfetch(iSamplingPoints1, 4);
          if(iSamplingPoints1 && dlp_strcmp(idInto->Sfetch(iSamplingPoints1-1, 0), idInto->Sfetch(iSamplingPoints1, 0))) {
            nSamplesOfLastUnits += (INT32)idInto->Dfetch(iSamplingPoints1-1, 1);
          }
          if((nextF0Val > 0.0) && (nextF0Pos >= 0.0) && (nextF0Pho != NULL) && (dlp_strlen(nextF0Pho) > 0)) {
            break;
          } else {
            if(this->m_nCheck > 0) {
              dlp_message(__FILE__,__LINE__,"no next f0 value");
            }
          }

        }
        if(iSamplingPoints1 >= nSamplingPoints) {
          nextF0Val = 1.0;
          nextI0Val = 1.0;
          nSampleOfNextF0SamplingPoint = nSamples;
        } else {
          nSampleOfNextF0SamplingPoint = (INT32)(((FLOAT64)nSamplesOfLastUnits + idInto->Dfetch(iSamplingPoints1, 1) * nextF0Pos) * (FLOAT64)m_nSrate / 1000.0 + 0.5);
          iSamplingPoints1++;
        }
      }
    }

    currF0Val = lastF0Val + (nextF0Val - lastF0Val) * (iSamples - nSampleOfLastF0SamplingPoint) / (nSampleOfNextF0SamplingPoint+1 - nSampleOfLastF0SamplingPoint);
    nPer = (INT32)((FLOAT64)m_nSrate / (currF0Val * (FLOAT64)m_nBaseF0) + 0.5);
    if((iSamples+nPer) > nSamples) nPer = nSamples-iSamples;

    currI0Val = lastI0Val + (nextI0Val - lastI0Val) * (iSamples - nSampleOfLastF0SamplingPoint) / (nSampleOfNextF0SamplingPoint+1 - nSampleOfLastF0SamplingPoint);

    while((iLab < nLab) && (sIntoLab[iLab].pos < iSamples)) {
      iSamplesOld = sIntoLab[iLab].pos;
      iFramesOld = INT32((FLOAT64)sFeaLab[iLab].pos / (FLOAT64)m_nCrate + 0.5);
      iLab++;
    }
    while((iFrames < nFrames) &&
        (((FLOAT64)(((iFrames-iFramesOld+1L))*m_nCrate)/((iLab>0)?(FLOAT64)(sFeaLab[iLab].pos -sFeaLab[iLab-1].pos ):sFeaLab[iLab].pos )) <
          ((FLOAT64)(iSamples-iSamplesOld)              /((iLab>0)?(FLOAT64)(sIntoLab[iLab].pos-sIntoLab[iLab-1].pos):sIntoLab[iLab].pos))))
      iFrames++;

    dlp_memmove(fea, (FLOAT64*)idFea->XAddr(iFrames, 0), nFea*sizeof(FLOAT64));
    if(m_bSynEnhancement) {
      FeaEnhancement(fea, nFea);
    }

    if(nCompVuv>=0) {
      isVoiceless  = (*(INT16*)idFea->XAddr(iFrames, nCompVuv) & 1) == 0;
      isSmoothable = (*(INT16*)idFea->XAddr(iFrames, nCompVuv) & 2) == 0;
    } else {
      isVoiceless = IsFeaVoiceless(fea, nFea);
      isSmoothable = 1;
    }

    if(feaUsed == NULL) {
      feaUsed = (FLOAT64*)dlp_malloc(nFea*sizeof(FLOAT64));
      dlp_memmove(feaUsed, fea, nFea*sizeof(FLOAT64));
    }

    if(m_bSynSmoothFea) {
      if(feaUsedBefore == NULL) {
        feaUsedBefore = (FLOAT64*)dlp_malloc(nFea*sizeof(FLOAT64));
        dlp_memmove(feaUsedBefore, fea, nFea*sizeof(FLOAT64));
      } else {
        if(isSmoothableBefore && isSmoothable) {
          for(iFea = 0; iFea < nFea; iFea++) {
            feaUsed[iFea] = 0.29289322 * (feaBefore[iFea] + fea[iFea]) + 0.41421356 * feaUsedBefore[iFea];
          }
        } else if(isSmoothableBefore && !isSmoothable) {
          for(iFea = 0; iFea < nFea; iFea++) {
            feaUsed[iFea] = 0.75 * feaBefore[iFea] + 0.25 * fea[iFea];
          }
        } else if(!isSmoothableBefore && isSmoothable) {
          for(iFea = 0; iFea < nFea; iFea++) {
            feaUsed[iFea] = 0.25 * feaBefore[iFea] + 0.75 * fea[iFea];
          }
        } else {
          dlp_memmove(feaUsed, fea, nFea*sizeof(FLOAT64));
        }
      }
      isSmoothableBefore = isSmoothable;
      dlp_memmove(feaUsedBefore, feaUsed, nFea*sizeof(FLOAT64));
    } else {
      dlp_memmove(feaUsed, fea, nFea*sizeof(FLOAT64));
    }
    dlp_memmove(feaBefore, fea, nFea*sizeof(FLOAT64));

    FLOAT64 gain = ((m_nWnorm == 0 ) ? 1.0 : (FLOAT64)sqrt((double)nPer)) * exp(m_nBaseI0) * currI0Val;

    switch(m_lpsExcType[0]) {
    case 'P':
      dlm_getExcPeriod(nPer, !isVoiceless, DLM_PITCH_PULSE, 1.0/gain, m_nSrate, exc+iSamples);
      break;
    case 'G':
      dlm_getExcPeriod(nPer, !isVoiceless, DLM_PITCH_GLOTT, 1.0/gain, m_nSrate, exc+iSamples);
      break;
    case 'R':
      dlm_getExcPeriod(nPer, !isVoiceless, DLM_PITCH_RANDPHASE, 1.0/gain, m_nSrate, exc+iSamples);
      break;
    case 'V':
      dlm_getExcPeriod(nPer, !isVoiceless, DLM_PITCH_VOICED, 1.0/gain, m_nSrate, exc+iSamples);
      break;
    case 'U':
      dlm_getExcPeriod(nPer, !isVoiceless, DLM_PITCH_UNVOICED, 1.0/gain, m_nSrate, exc+iSamples);
      break;
    default:
      return IERROR(this,FBA_BADARG,m_lpsExcType,"exc_type","P, G, R, V or U");
    }

    if(this->m_nCheck > 0) {
      dlp_message(__FILE__,__LINE__,"nPer=%5d, uv/v=%1d, s/ns=%ld, pos=%7.1f, F0=%4.2f/%4.2f/%4.2f, idFea (%3d) %5s, Fea (%3d): %5s (%3d), Into (%3d) %5s (%3d)", \
          (int)nPer,
          (int)!isVoiceless,
          (int)isSmoothable,
          (double)((FLOAT64)iSamples*1000.0/(FLOAT64)m_nSrate),
          (double)lastF0Val,
          (double)currF0Val,
          (double)nextF0Val,
          (int)iFrames,
          idFea->Sfetch(iFrames, nFea),
          (int)iLab, sFeaLab[iLab].phoneme,
          (int)sFeaLab[iLab].pos,
          (int)iLab, sIntoLab[iLab].phoneme,
          (int)sIntoLab[iLab].pos);
    }

    if(SynthesizeFrame(feaUsed, nFea, &exc[iSamples], nPer, (FLOAT64*)idSyn->XAddr(iSamples, 0)) != O_K) return IERROR(this,FBA_SYNTHESISE, iFrames, 0,0);

    iSamples += nPer;
  }

  dlp_free(exc);
  dlp_free(sIntoLab);
  dlp_free(sFeaLab);
  dlp_free(fea);
  dlp_free(feaBefore);
  dlp_free(feaUsed);
  if(m_bSynSmoothFea) {
    dlp_free(feaUsedBefore);
  }

  if (m_nMinLog != 0.0) for (INT32 i=0; i<idSyn->GetNRecs(); i++) *((FLOAT64*)idSyn->XAddr(0, 0)+i) *= exp(m_nMinLog);

  return O_K;
}
Esempio n. 22
0
/**
 * Analyse
 *
 * Derived instances of FBAproc should override method
 * Analyse() to add the desired functionality
 *
 * @return O_K if successfull, NOT_EXEC otherwise
 */
INT16 CGEN_PUBLIC CCPproc::AnalyzeFrame() {
  INT16 ret = O_K;

  if(!strcmp(m_lpsType, "MelFilter")) {
    CData* idCep = NULL;
    CData* idMel = NULL;
    ICREATEEX(CData, idCep, "~idCep", NULL);
    ICREATEEX(CData, idMel, "~idMel", NULL);
    if((ret = CMELproc::AnalyzeFrame()) != O_K) return ret;
    idMel->Select(m_idRealFrame, 0, m_nCoeff);
    if((ret = Mf2mcep(idMel, idCep, m_nCoeff)) != O_K) return ret;
    dlp_memmove(m_idRealFrame->XAddr(0, 0), idCep->XAddr(0, 0), m_nCoeff * sizeof(FLOAT64));
    IDESTROY(idMel);
    IDESTROY(idCep);
  } else {
    if(!strcmp(m_lpsWarptype, "time")) {
      if(!strcmp(m_lpsType, "BurgLPC")) {
        dlm_calcmcep((FLOAT64*)m_idRealFrame->XAddr(0, 0), m_nWlen, (FLOAT64*)m_idRealFrame->XAddr(0, 0), m_nCoeff, 0.0, -m_nMinLog, DLM_CALCCEP_METHOD_S_MLPC_BURG_MCEP);
      } else if(!strcmp(m_lpsType, "LevinsonLPC")) {
        dlm_calcmcep((FLOAT64*)m_idRealFrame->XAddr(0, 0), m_nWlen, (FLOAT64*)m_idRealFrame->XAddr(0, 0), m_nCoeff, 0.0, -m_nMinLog, DLM_CALCCEP_METHOD_S_MLPC_LEVI_MCEP);
      } else if(!strcmp(m_lpsType, "Uels")) {
        dlm_calcmcep((FLOAT64*)m_idRealFrame->XAddr(0, 0), m_nLen, (FLOAT64*)m_idRealFrame->XAddr(0, 0), m_nCoeff, 0.0, -m_nMinLog, DLM_CALCCEP_METHOD_S_MCEP_UELS);
      } else if(!strcmp(m_lpsType, "LogFFT")) {
        dlp_memset(m_idImagFrame->XAddr(0, 0), 0L, m_nLen * sizeof(FLOAT64));
        if((ret = dlm_fft((FLOAT64*)m_idRealFrame->XAddr(0, 0),(FLOAT64*)m_idImagFrame->XAddr(0, 0),m_nLen,FALSE)) != O_K) {
          if(ret == ERR_MDIM) IERROR(this, FBA_BADFRAMELEN, m_nLen, "FFT", 0);
          return NOT_EXEC;
        }
        LN();
        if((ret = dlm_fft((FLOAT64*)m_idRealFrame->XAddr(0, 0), (FLOAT64*)m_idImagFrame->XAddr(0, 0), m_nLen, TRUE)) != O_K) {
          if(ret == ERR_MDIM) IERROR(this, FBA_BADFRAMELEN, m_nLen, "FFT", 0);
          return NOT_EXEC;
        }
        for(INT32 i = 1; i < m_nCoeff; i++) {
          ((FLOAT64*)m_idRealFrame->XAddr(0, 0))[i] *= 2.0;
        }
      } else {
        IERROR(this,ERR_NULLINST,0,0,0);
        return NOT_EXEC;
      }
    } else {
      if(!strcmp(m_lpsType, "BurgLPC")) {
        if(!strcmp(m_lpsWarptype, "lpc")) {
          dlm_calcmcep((FLOAT64*)m_idRealFrame->XAddr(0, 0), m_nWlen, (FLOAT64*)m_idRealFrame->XAddr(0, 0),
              m_nCoeff, m_nPfaLambda, -m_nMinLog, DLM_CALCCEP_METHOD_S_LPC_BURG_MLPC_MCEP);
        } else if(!strcmp(m_lpsWarptype, "cepstrum")) {
          dlm_calcmcep((FLOAT64*)m_idRealFrame->XAddr(0, 0), m_nWlen, (FLOAT64*)m_idRealFrame->XAddr(0, 0),
              m_nCoeff, m_nPfaLambda, -m_nMinLog, DLM_CALCCEP_METHOD_S_LPC_BURG_CEP_MCEP);
        } else if(!strcmp(m_lpsWarptype, "none")) {
          dlm_calcmcep((FLOAT64*)m_idRealFrame->XAddr(0, 0), m_nWlen, (FLOAT64*)m_idRealFrame->XAddr(0, 0),
              m_nCoeff, 0.0, -m_nMinLog, DLM_CALCCEP_METHOD_S_LPC_BURG_CEP);
        } else {
          IERROR(this, CP_WARPTYPE, m_lpsWarptype, 0, 0);
          return NOT_EXEC;
        }
      } else if(!strcmp(m_lpsType, "LevinsonLPC")) {
        if(!strcmp(m_lpsWarptype, "lpc")) {
          dlm_calcmcep((FLOAT64*)m_idRealFrame->XAddr(0, 0), m_nWlen, (FLOAT64*)m_idRealFrame->XAddr(0, 0),
              m_nCoeff, m_nPfaLambda, -m_nMinLog, DLM_CALCCEP_METHOD_S_LPC_LEVI_MLPC_MCEP);
        } else if(!strcmp(m_lpsWarptype, "cepstrum")) {
          dlm_calcmcep((FLOAT64*)m_idRealFrame->XAddr(0, 0), m_nWlen, (FLOAT64*)m_idRealFrame->XAddr(0, 0), m_nWlen
              / 2, m_nPfaLambda, -m_nMinLog, DLM_CALCCEP_METHOD_S_LPC_LEVI_CEP_MCEP);
        } else if(!strcmp(m_lpsWarptype, "none")) {
          dlm_calcmcep((FLOAT64*)m_idRealFrame->XAddr(0, 0), m_nWlen, (FLOAT64*)m_idRealFrame->XAddr(0, 0),
              m_nCoeff, 0.0, -m_nMinLog, DLM_CALCCEP_METHOD_S_LPC_LEVI_CEP);
        } else {
          IERROR(this, CP_WARPTYPE, m_lpsWarptype, 0, 0);
          return NOT_EXEC;
        }
      } else if(!strcmp(m_lpsType, "Uels")) {
        if((ret = dlm_calcmcep((FLOAT64*)m_idRealFrame->XAddr(0, 0), m_nLen, (FLOAT64*)m_idRealFrame->XAddr(0, 0), m_nCoeff,
            m_nPfaLambda, exp(-m_nMinLog), DLM_CALCCEP_METHOD_S_MCEP_UELS)) != O_K) {
          return NOT_EXEC;
        }
      } else if(!strcmp(m_lpsType, "LogFFT")) {
        dlp_memset(m_idImagFrame->XAddr(0, 0), 0L, m_nLen * sizeof(FLOAT64));
        if((ret = dlm_fft((FLOAT64*)m_idRealFrame->XAddr(0, 0), (FLOAT64*)m_idImagFrame->XAddr(0, 0), m_nLen, FALSE)) != O_K) {
          if(ret == ERR_MDIM) IERROR(this, FBA_BADFRAMELEN, m_nLen, "FFT", 0);
          return NOT_EXEC;
        }
        LN();
        if(!strcmp(m_lpsWarptype, "lpc")) {
          IERROR(this, CP_WARPTYPE, m_lpsWarptype, 0, 0);
          return NOT_EXEC;
        } else if(!strcmp(m_lpsWarptype, "cepstrum")) {
          dlp_memset(m_idImagFrame->XAddr(0, 0), 0L, m_nLen * sizeof(FLOAT64));
          if((ret = dlm_fft((FLOAT64*)m_idRealFrame->XAddr(0, 0), (FLOAT64*)m_idImagFrame->XAddr(0, 0), m_nLen, TRUE)) != O_K) {
            if(ret == ERR_MDIM) IERROR(this, FBA_BADFRAMELEN, m_nLen, "FFT", 0);
            return NOT_EXEC;
          }
          dlm_cep2mcep((FLOAT64*)m_idRealFrame->XAddr(0, 0), m_nLen, (FLOAT64*)m_idRealFrame->XAddr(0, 0),
              m_nCoeff, m_nPfaLambda, NULL);
        } else if(!strcmp(m_lpsWarptype, "spectrum")) {
          WARP();
          dlp_memset(m_idImagFrame->XAddr(0, 0), 0L, m_nLen * sizeof(FLOAT64));
          if((ret = dlm_fft((FLOAT64*)m_idRealFrame->XAddr(0, 0), (FLOAT64*)m_idImagFrame->XAddr(0, 0), m_nLen, TRUE)) != O_K) {
            if(ret == ERR_MDIM) IERROR(this, FBA_BADFRAMELEN, m_nLen, "FFT", 0);
            return NOT_EXEC;
          }
        } else if(!strcmp(m_lpsWarptype, "none")) {
          dlp_memset(m_idImagFrame->XAddr(0, 0), 0L, m_nLen * sizeof(FLOAT64));
          if((ret = dlm_fft((FLOAT64*)m_idRealFrame->XAddr(0, 0), (FLOAT64*)m_idImagFrame->XAddr(0, 0), m_nLen, TRUE)) != O_K) {
            if(ret == ERR_MDIM) IERROR(this, FBA_BADFRAMELEN, m_nLen, "FFT", 0);
            return NOT_EXEC;
          }
        } else {
          IERROR(this,ERR_NULLINST,0,0,0);
        }
        for(INT32 i = 1; i < m_nCoeff; i++) {
          ((FLOAT64*)m_idRealFrame->XAddr(0, 0))[i] *= 2.0;
        }
      } else if(strcmp(m_lpsWarptype, "none")) {
        IERROR(this,ERR_NULLINST,0,0,0);
      }
    }
  }
  return O_K;
}
Esempio n. 23
0
/**
 * Copies the one field from iSrc to this instance
 *
 * @param _this  This (destination) instance
 * @param lpWord Pointer to a SWord structure identifying the field to copy
 * @param iSrc   The source instance to copy the field from
 * @return O_K if successful, an error code otherwise
 *
 * HACK: This implementation copies simple types and strings only!!!
 * TODO: Implement instance and pointer copying
 */
INT16 CDlpObject_CopyField(CDlpObject* _this, SWord* lpWord, CDlpObject* iSrc)
{
  SWord* lpWordSrc = NULL;

  /* Validate input */
  CHECK_THIS_RV(NOT_EXEC);
  if (!lpWord) return NOT_EXEC;
  if (!iSrc  ) return NOT_EXEC;
  if (lpWord->nFlags & (FF_NONAUTOMATIC|FF_NOSAVE)) return NOT_EXEC;

  /* Get source word */
  lpWordSrc = CDlpObject_FindWord(iSrc,lpWord->lpName,WL_TYPE_FIELD);
  DLPASSERT(lpWordSrc);                                     /* Instance type?     */
  DLPASSERT(lpWord->nWordType   ==lpWordSrc->nWordType   ); /* Field overwritten? */
  DLPASSERT(lpWord->ex.fld.nType==lpWordSrc->ex.fld.nType); /* Field overwritten? */

  /*printf("\n Copy field %s.%s --> %s.%s",BASEINST(iSrc)->m_lpInstanceName,lpWord->lpName,BASEINST(_this)->m_lpInstanceName,lpWord->lpName);*/

  /* Copy data */
  switch (lpWord->ex.fld.nType)
  {
  case T_BOOL   : dlp_memmove(lpWord->lpData,lpWordSrc->lpData,sizeof(     BOOL)); return O_K;
  case T_UCHAR  : dlp_memmove(lpWord->lpData,lpWordSrc->lpData,sizeof(    UINT8)); return O_K;
  case T_CHAR   : dlp_memmove(lpWord->lpData,lpWordSrc->lpData,sizeof(     INT8)); return O_K;
  case T_USHORT : dlp_memmove(lpWord->lpData,lpWordSrc->lpData,sizeof(   UINT16)); return O_K;
  case T_SHORT  : dlp_memmove(lpWord->lpData,lpWordSrc->lpData,sizeof(    INT16)); return O_K;
  case T_UINT   : dlp_memmove(lpWord->lpData,lpWordSrc->lpData,sizeof(   UINT32)); return O_K;
  case T_INT    : dlp_memmove(lpWord->lpData,lpWordSrc->lpData,sizeof(    INT32)); return O_K;
  case T_ULONG  : dlp_memmove(lpWord->lpData,lpWordSrc->lpData,sizeof(   UINT64)); return O_K;
  case T_LONG   : dlp_memmove(lpWord->lpData,lpWordSrc->lpData,sizeof(    INT64)); return O_K;
  case T_FLOAT  : dlp_memmove(lpWord->lpData,lpWordSrc->lpData,sizeof(  FLOAT32)); return O_K;
  case T_DOUBLE : dlp_memmove(lpWord->lpData,lpWordSrc->lpData,sizeof(  FLOAT64)); return O_K;
  case T_COMPLEX: dlp_memmove(lpWord->lpData,lpWordSrc->lpData,sizeof(COMPLEX64)); return O_K;
  case T_STRING :
  case T_CSTRING:
  case T_TEXT   :
    dlp_free(*(char**)lpWord->lpData);
    *(char**)lpWord->lpData = NULL;
    if (*(char**)lpWordSrc->lpData)
    {
      *(char**)lpWord->lpData = (char*)dlp_malloc(dlp_size(*(char**)lpWordSrc->lpData));
      dlp_strcpy(*(char**)lpWord->lpData,*(char**)lpWordSrc->lpData);
    }
    return O_K;
  case T_INSTANCE:
    if (*(CDlpObject**)lpWordSrc->lpData)
    {
      if (*(CDlpObject**)lpWord->lpData==NULL)
      {
      #ifdef __cplusplus
        *(CDlpObject**)lpWord->lpData = CDlpObject_CreateInstanceOf(lpWordSrc->ex.fld.lpType,lpWordSrc->lpName);
      #else
        *(CDlpObject**)lpWord->lpData = *(CDlpObject**)CDlpObject_CreateInstanceOf(lpWordSrc->ex.fld.lpType,lpWordSrc->lpName);
      #endif
        if (!*(CDlpObject**)lpWord->lpData)
          IERROR(_this,ERR_CREATEINSTANCE,lpWord->lpName,0,0);
        else
          (*(CDlpObject**)lpWord->lpData)->m_lpContainer=lpWord;
      }
    #ifdef __cplusplus
      return (*(CDlpObject**)lpWord->lpData)->Copy(*(CDlpObject**)lpWordSrc->lpData);
    #else
      return (*(CDlpObject**)lpWord->lpData)->Copy(*(CDlpObject**)lpWord->lpData,*(CDlpObject**)lpWordSrc->lpData);
    #endif
    }
    else if (*(CDlpObject**)lpWord->lpData)
    {
      IDESTROY((*(CDlpObject**)lpWord->lpData));
    }
  default:
    if (lpWord->ex.fld.nType>0 && lpWord->ex.fld.nType<=255)
    {
      dlp_memmove(lpWord->lpData,lpWordSrc->lpData,lpWord->ex.fld.nType);
      return O_K;
    }
    return NOT_EXEC;
  }
}
Esempio n. 24
0
/**
 * <p>Calculates roots by tracking from known roots using coefficient matching
 * method by Starer, Nehorai, 1991, Transactions - Adaptive Polynomial Factorization
 * By Coefficient Matching.</p>
 *
 * @param poly1
 *          Polynomial of predecessor
 * @param rtr1
 *          Real part of roots of predecessor.
 * @param rti1
 *          Imaginary part of roots of predecessor.
 * @param poly2
 *          Polynomial of successor where the roots are calculated from.
 * @param rtr2
 *          Real part of roots of successor to be calculated.
 * @param rti2
 *          Imaginary part of roots of successor to be calculated.
 * @param m
 *          Number of roots, i.e. size of <CODE>rtr[12]</CODE> and <CODE>rti[12]</CODE>.
 * @param n_iter
 *          Maximum number of iterations of newtons method per interim point
 * @param eps
 *          Maximum error used for breaking condition at newtons method
 * @return <code>O_K</code> if successful, a (negative) error code otherwise
 */
FLOAT64 dlm_roots_track_match_poly(COMPLEX64 poly1[], COMPLEX64 rt1[], COMPLEX64 poly2[], COMPLEX64 rt2[], INT16 m, INT16 n_iter, FLOAT64 eps) {
  INT16 i = 0;
  INT16 j = 0;
  INT16 i_iter = 0;
  FLOAT64 alpha = 1.0;
  FLOAT64 e0 = 0.0;
  FLOAT64 e1 = 0.0;
  FLOAT64 e2 = 0.0;
  COMPLEX64* poly_c = NULL;
  COMPLEX64* T = NULL;
  COMPLEX64* L = NULL;
  COMPLEX64* E = NULL;
  COMPLEX64* D = NULL;
  COMPLEX64* P1 = NULL;
  COMPLEX64* P2 = NULL;

  if (!poly1 || !poly2 || !rt1 || !rt2) return NOT_EXEC;

  poly_c = (COMPLEX64*) dlp_calloc(m+1, sizeof(COMPLEX64));
  P1 = (COMPLEX64*) dlp_calloc(m, sizeof(COMPLEX64));
  P2 = (COMPLEX64*) dlp_calloc(m, sizeof(COMPLEX64));
  E = (COMPLEX64*) dlp_calloc(m, sizeof(COMPLEX64));
  D = (COMPLEX64*) dlp_calloc(m, sizeof(COMPLEX64));
  T = (COMPLEX64*) dlp_calloc(m*m, sizeof(COMPLEX64));
  L = (COMPLEX64*) dlp_calloc(m*m, sizeof(COMPLEX64));

  dlp_memmove(rt2, rt1, m * sizeof(COMPLEX64));
  for (i = 0; i <= m; i++) {
    poly_c[i] = dlp_scalopC(poly1[i], poly1[0], OP_DIV);
  }

  for (e1 = 0.0, i = 0; i < m; i++) {
    P1[i] = rt2[i];
    E[i] =
        dlp_scalopC(dlp_scalopC(poly2[i + 1], poly2[0], OP_DIV), dlp_scalopC(poly1[i + 1], poly1[0], OP_DIV), OP_DIFF);
    e0 += E[i].x * E[i].x;
  }

  e1 = e0;

  while ((i_iter < n_iter) && (sqrt(e1 / (FLOAT64) m) > eps)) {

    for (i = 0; i < m; i++) {
      for (j = 0; j <= i; j++) {
        (*(T + (i) * m + j)).x = poly_c[i - j].x;
        (*(T + (i) * m + j)).y = poly_c[i - j].y;
      }
    }
    for (j = 0; j < m; j++) {
      (*(L + (0) * m + j)).x = 1.0;
      (*(L + (0) * m + j)).y = 0.0;
      (*(L + (1) * m + j)).x = P1[j].x;
      (*(L + (1) * m + j)).y = P1[j].y;
    }
    for (i = 2; i < m; i++) {
      for (j = 0; j < m; j++) {
        (*(L + (i) * m + j)).x = (*(L + (i - 1) * m + j)).x * P1[j].x - (*(L + (i - 1) * m + j)).y * P1[j].y;
        (*(L + (i) * m + j)).y = (*(L + (i - 1) * m + j)).x * P1[j].y + (*(L + (i - 1) * m + j)).y * P1[j].x;
      }
    }

    dlm_solve_ludC(T, m, E, 1);
    dlm_solve_ludC(L, m, E, 1);

    dlp_memmove(D, E, m * sizeof(COMPLEX64));

    alpha = 1.0;

    do {
      for (i = 0; i < m; i++) {
        P2[i].x = P1[i].x - alpha * D[i].x;
        P2[i].y = P1[i].y - alpha * D[i].y;
      }

      dlm_polyC(P2, m, poly_c);

      for (e2 = 0.0, i = 0; i < m; i++) {
        E[i].x = dlp_scalopC(poly2[i + 1], poly2[0], OP_DIV).x - poly_c[i + 1].x;
        E[i].y = -poly_c[i + 1].y;
        e2 += E[i].x * E[i].x + E[i].y * E[i].y;
      }
      alpha /= 2.0;
    } while ((alpha > 1.0e-10) && (e2 / e1 > 1.0));
    dlp_memmove(P1, P2, m * sizeof(COMPLEX64));
    e1 = e2;
    i_iter++;
  }

  for (i = 0; i < m; i++) {
    rt2[i] = P1[i];
  }

  dlp_free(D);
  dlp_free(E);
  dlp_free(P1);
  dlp_free(P2);
  dlp_free(T);
  dlp_free(L);
  dlp_free(poly_c);

  return e2 / e0;
}
Esempio n. 25
0
/**
 * <p>Dynamic time warping.</p>
 * This function calculates the alignment path of the two input vector sequences <code>lpSrc</code> and <code>lpDst</code>
 * by means of the minimum accumulated vector difference norm. The output <code>lpResult</code> consists of the index
 * sequence related to the vectors of <code>lpSrc</code> and the corresponding index sequence related to vectors of
 * <code>lpDst</code>. The output <code>lpError</code> contains the error at the corresponding point of the alignment path.
 *
 * @param lpSrc Pointer to reference (source) input vector sequence.
 * @param nSrcRows Number of vectors given in <code>lpSrc</code>.
 * @param lpDst Pointer to (destination) input vector sequence.
 * @param nDstRows Number of vectors given in <code>lpDst</code>.
 * @param nCols Number of vector elements of <code>lpSrc</code> and <code>lpDst</code>.
 * @param lpResult Pointer to alignment path to be calculated (must be at least <code>2*(nSrcRows+nDstRows)</code>.
 * @param nResult Pointer to the value filled with the actual size of the alignment path, so that <code>lpResult</code> and <code>lpError</code> can be reallocated by <code>nResult</code>.
 * @param lpError Pointer to the error sequence (must be at least <code>nSrcRows+nDstRows</code>.
 */
INT16 dlm_dtwC(COMPLEX64* lpSrc, INT32 nSrcRows, COMPLEX64* lpDst, INT32 nDstRows, INT32 nCols, INT32* lpResult, INT32* nResult, FLOAT64* lpError) {
  INT32 nCol;
  INT32 nSrcRow;
  INT32 nDstRow;
  FLOAT64* lpMatrix = NULL;

  DLPASSERT((lpSrc!=NULL) && (lpDst!=NULL) && (lpResult!=NULL) && (nResult!=NULL) && (lpError!=NULL));

  lpMatrix = (FLOAT64*)dlp_calloc(nSrcRows*nDstRows, sizeof(FLOAT64));

  if(lpDst != NULL) {
    for(nSrcRow = 0; nSrcRow < nSrcRows; nSrcRow++) {
      for(nDstRow = 0; nDstRow < nDstRows; nDstRow++) {
        for(nCol = 0; nCol < nCols; nCol++) {
          lpMatrix[nSrcRow*nDstRows+nDstRow] += dlp_scalopC(lpSrc[nSrcRow*nCols+nCol],lpDst[nDstRow*nCols+nCol], OP_QABSDIFF).x;
        }
        lpMatrix[nSrcRow*nDstRows+nDstRow] = sqrt(lpMatrix[nSrcRow*nDstRows+nDstRow]);
      }
    }
  }

  for(nSrcRow = 0; nSrcRow < nSrcRows; nSrcRow++) {
    for(nDstRow = 0; nDstRow < nDstRows; nDstRow++) {
      if(nSrcRow > 0) {
        if(nDstRow > 0) {
          if(lpMatrix[(nSrcRow)*nDstRows+nDstRow-1] <= lpMatrix[(nSrcRow-1)*nDstRows+nDstRow]) {
            if(lpMatrix[(nSrcRow)*nDstRows+nDstRow-1] < lpMatrix[(nSrcRow-1)*nDstRows+nDstRow-1]) {
              lpMatrix[nSrcRow*nDstRows+nDstRow] += lpMatrix[(nSrcRow)*nDstRows+nDstRow-1];
            } else {
              lpMatrix[nSrcRow*nDstRows+nDstRow] = lpMatrix[nSrcRow*nDstRows+nDstRow] + lpMatrix[(nSrcRow-1)*nDstRows+nDstRow-1];
            }
          } else if(lpMatrix[(nSrcRow-1)*nDstRows+nDstRow] < lpMatrix[(nSrcRow-1)*nDstRows+nDstRow-1]) {
            lpMatrix[nSrcRow*nDstRows+nDstRow] += lpMatrix[(nSrcRow-1)*nDstRows+nDstRow];
          } else {
            lpMatrix[nSrcRow*nDstRows+nDstRow] += lpMatrix[(nSrcRow-1)*nDstRows+nDstRow-1];
          }
        } else {
          lpMatrix[nSrcRow*nDstRows+nDstRow] += lpMatrix[(nSrcRow-1)*nDstRows+nDstRow];
        }
      } else if(nDstRow > 0) {
        lpMatrix[nSrcRow*nDstRows+nDstRow] += lpMatrix[(nSrcRow)*nDstRows+nDstRow-1];
      }
    }
  }

  nSrcRow = nSrcRows-1;
  nDstRow = nDstRows-1;
  *nResult = nSrcRows+nDstRows-1;
  lpResult[*nResult*2] = nSrcRow;
  lpResult[*nResult*2+1] = nDstRow;
  lpError[*nResult] = lpMatrix[nSrcRow*nDstRows+nDstRow];
  while(nSrcRow || nDstRow) {
    if(nSrcRow) {
      if(nDstRow) {
        if(lpMatrix[(nSrcRow-1)*nDstRows+nDstRow-1] <= lpMatrix[(nSrcRow-1)*nDstRows+nDstRow]) {
          if(lpMatrix[(nSrcRow-1)*nDstRows+nDstRow-1] <= lpMatrix[(nSrcRow)*nDstRows+nDstRow-1]) {
            nSrcRow--;
            nDstRow--;
          } else {
            nDstRow--;
          }
        } else if(lpMatrix[(nSrcRow-1)*nDstRows+nDstRow] < lpMatrix[(nSrcRow)*nDstRows+nDstRow-1]) {
          nSrcRow--;
        } else {
          nDstRow--;
        }
      } else {
        nSrcRow--;
      }
    } else if(nDstRow) {
      nDstRow--;
    }
    (*nResult)--;
    lpResult[*nResult*2] = nSrcRow;
    lpResult[*nResult*2+1] = nDstRow;
    lpError[*nResult] = lpMatrix[nSrcRow*nDstRows+nDstRow];
  }

  dlp_memmove(lpResult, lpResult+2**nResult, 2*(nSrcRows+nDstRows-*nResult)*sizeof(INT32));
  dlp_memmove(lpError,  lpError +  *nResult,   (nSrcRows+nDstRows-*nResult)*sizeof(FLOAT64));
  *nResult = nSrcRows+nDstRows - *nResult;

  dlp_free(lpMatrix);

  return O_K;
}