📄 feat.c
字号:
else strcpy (path, file); } else { if (dir && (file[0] != '/')){ if(cepext!=NULL) sprintf (path, "%s/%s%s", dir, file,cepext); else sprintf (path, "%s/%s.%s", dir, file,"mfc"); }else{ if(cepext!=NULL) sprintf (path, "%s%s", file,cepext); else sprintf (path, "%s.%s", file,"mfc"); } } win = feat_window_size(fcb); /* Adjust boundaries to include padding for feature computation */ if (ef < 0) ef = (int32)0x7fff0000 - win; /* Hack!! Hardwired constant */ sf -= win; ef += win; /* Read mfc file */ mfc = (float32 **) ckd_calloc_2d (S3_MAX_FRAMES, fcb->cepsize, sizeof(float32)); if (sf < 0) nfr = feat_s2mfc_read (path, 0, ef, mfc-sf, S3_MAX_FRAMES+sf-win); else nfr = feat_s2mfc_read (path, sf, ef, mfc, S3_MAX_FRAMES-win); if (nfr < 0) { ckd_free_2d((void **) mfc); return -1; }#if 0 for(i=0;i<nfr;i++){ for(j=0 ; j< fcb->cepsize ; j++){ fprintf(stderr,"%f ",mfc[i][j]); } fprintf(stderr,"\n"); }#endif if (nfr < 2*win+1) { E_ERROR("%s: MFC file/segment too short to compute features: %d frames\n", file, nfr); ckd_free_2d((void **) mfc); return -1; } /* Add padding at the beginning by replicating input data, if necessary */ if (sf < 0) { for (i = 0; i < -sf; i++) memcpy (mfc[i], mfc[i-sf+1], fcb->cepsize * sizeof(float32)); nfr -= sf; }#if 0 for(i=0;i<nfr;i++){ for(j=0 ; j< fcb->cepsize ; j++){ fprintf(stderr,"%f ",mfc[i][j]); } fprintf(stderr,"\n"); }#endif /* Add padding at the end by replicating input data, if necessary */ k = ef - sf + 1; if (nfr < k) { k -= nfr; /* Extra frames padding needed at the end */ if (k > win) k = win; /* Limit feature frames to extent of file, not to unbounded ef */ for (i = 0; i < k; i++) memcpy (mfc[nfr+i], mfc[nfr+i-1-k], fcb->cepsize * sizeof(float32)); nfr += k; } /* At this point, nfr includes complete padded cepstrum frames */ if (nfr - win*2 > maxfr) { E_ERROR("%s: Feature buffer size(%d frames) < required(%d)\n", maxfr, nfr - win*2); ckd_free_2d((void **) mfc); return -1; } if (fcb->cmn){ E_INFO("CMN\n"); cmn (mfc, fcb->varnorm, nfr, fcb->cepsize,fcb->cmn_struct); }#if 0 E_INFO("Before CMN. \n"); for(i=0;i<nfr;i++){ for(j=0 ; j< fcb->cepsize ; j++){ fprintf(stderr,"%f ",mfc[i][j]); } fprintf(stderr,"\n"); }#endif if (fcb->agc){ E_INFO("AGC\n"); agc_max (mfc, nfr); }#if 0 for(i=0;i<nfr;i++){ for(j=0 ; j< fcb->cepsize ; j++){ fprintf(stderr,"%f ",mfc[i][j]); } fprintf(stderr,"\n"); }#endif /* Create feature vectors */ for (i = win; i < nfr-win; i++) fcb->compute_feat (fcb, mfc+i, feat[i-win]);#if 0 E_INFO("After dynamic coefficients computation. \n"); feat_print(fcb,feat,nfr,stderr);#endif ckd_free_2d((void **) mfc); return (nfr - win*2);}/* 20041111: ARCHAN: Nearly rewrite the whole thing because some variables are STATIC. bufpos and curpos were orignially static function, I decided to eliminate by putting all of them into the structure feat. This is slower, but we were not optimizing the front-end yet. */int32 feat_s2mfc2feat_block(feat_t *fcb, float32 **uttcep, int32 nfr, int32 beginutt, int32 endutt, float32 ***ofeat){ float32 **cepbuf=NULL; float32 **tmpcepbuf=NULL; int32 win, cepsize; int32 i, j, nfeatvec, residualvecs; int32 tmppos; cepbuf=fcb->cepbuf; tmpcepbuf=fcb->tmpcepbuf; assert(nfr < LIVEBUFBLOCKSIZE); assert(fcb->cepsize >0); assert(cepbuf); assert(tmpcepbuf); win = feat_window_size(fcb); cepsize = feat_cepsize(fcb); if (cepbuf == NULL){ beginutt = 1; /* If no buffer was present we are beginning an utt */ E_INFO("Feature buffers initialized to %d vectors\n",LIVEBUFBLOCKSIZE); } if (fcb->cmn) /* Only cmn_prior in block computation mode */ cmn_prior (uttcep, fcb->varnorm, nfr, fcb->cepsize, endutt); /* cmn_prior (uttcep, fcb->varnorm, nfr, fcb->cepsize, endutt,fcb->cmn_struct);*/ residualvecs = 0; if (beginutt){ /* Replicate first frame into the first win frames */ for (i=0;i<win;i++) { if(nfr>=win+1) memcpy(cepbuf[i],uttcep[0+i+1],cepsize*sizeof(float32)); else memcpy(cepbuf[i],uttcep[0],cepsize*sizeof(float32)); } fcb->bufpos = win; fcb->bufpos %= LIVEBUFBLOCKSIZE; fcb->curpos = fcb->bufpos; residualvecs -= win; } for (i=0;i<nfr;i++){ assert(fcb->bufpos < LIVEBUFBLOCKSIZE); memcpy(cepbuf[fcb->bufpos++],uttcep[i],cepsize*sizeof(float32)); fcb->bufpos %= LIVEBUFBLOCKSIZE; } if (endutt){ /* Replicate last frame into the last win frames */ if (nfr > 0) { for (i=0;i<win;i++) { assert(fcb->bufpos < LIVEBUFBLOCKSIZE); memcpy(cepbuf[fcb->bufpos++],uttcep[nfr-1],cepsize*sizeof(float32)); fcb->bufpos %= LIVEBUFBLOCKSIZE; } } else { int16 tpos = fcb->bufpos-1; tpos %= LIVEBUFBLOCKSIZE; for (i=0;i<win;i++) { assert(fcb->bufpos < LIVEBUFBLOCKSIZE); memcpy(cepbuf[fcb->bufpos++],cepbuf[tpos],cepsize*sizeof(float32)); fcb->bufpos %= LIVEBUFBLOCKSIZE; } } residualvecs += win; } nfeatvec = 0; nfr += residualvecs; for (i=0; i < nfr; i++,nfeatvec++){ if(fcb->curpos <win || fcb->curpos > LIVEBUFBLOCKSIZE -win-1){ /* HACK! Just copy the frames and read them to compute_feat */ for(j=-win;j<=win;j++){ tmppos= (j+ fcb->curpos + LIVEBUFBLOCKSIZE)% LIVEBUFBLOCKSIZE; memcpy(tmpcepbuf[win+j],cepbuf[tmppos],cepsize*sizeof(float32)); } fcb->compute_feat(fcb, tmpcepbuf+win,ofeat[i]); }else{ fcb->compute_feat(fcb, cepbuf+fcb->curpos, ofeat[i]); } fcb->curpos++; fcb->curpos %= LIVEBUFBLOCKSIZE; } return(nfeatvec);}/* * RAH, remove memory allocated by feat_init * What is going on? feat_vector_alloc doesn't appear to be called */void feat_free (feat_t *f){ if (f) { // if (f->stream_len) // ckd_free ((void *) f->stream_len); // ckd_free ((void *) f); }}/* ARCHAN: Unused code in the past. I keep them to show my respect to the original author*//* This part of the code is contaminated by static-style programming. */#if 0 /*static*/ float32 **feat=NULL; /*static*/ float32 **cepbuf=NULL; /*static*/ int32 bufpos; /* RAH 4.15.01 upgraded unsigned char variables to int32*/ /*static*/ int32 curpos; /* RAH 4.15.01 upgraded unsigned char variables to int32*/ /*static*/ int32 jp1, jp2, jp3, jf1, jf2, jf3; /* RAH 4.15.01 upgraded unsigned char variables to int32 */ int32 win, cepsize; int32 i, j, nfeatvec, residualvecs; float32 *w, *_w, *f; float32 *w1, *w_1, *_w1, *_w_1; float32 d1, d2; /* If this assert fails, you're risking overwriting elements * in the buffer. -EBG */ assert(nfr < LIVEBUFBLOCKSIZE); win = feat_window_size(fcb); if (fcb->cepsize <= 0) E_FATAL("Bad cepsize: %d\n", fcb->cepsize); cepsize = feat_cepsize(fcb); if (feat == NULL) feat = (float32 **)ckd_calloc_2d(LIVEBUFBLOCKSIZE, feat_stream_len(fcb,0), sizeof(float32)); if (cepbuf == NULL){ cepbuf = (float32 **)ckd_calloc_2d(LIVEBUFBLOCKSIZE, cepsize, sizeof(float32)); beginutt = 1; /* If no buffer was present we are beginning an utt */ if (! feat) E_FATAL("Unable to allocate feat ckd_calloc_2d(%ld,%d,%d)\n",LIVEBUFBLOCKSIZE,feat_stream_len(fcb,0),sizeof(float32)); if (! cepbuf) E_FATAL("Unable to allocate cepbuf ckd_calloc_2d(%ld,%d,%d)\n",LIVEBUFBLOCKSIZE,cepsize,sizeof(float32)); E_INFO("Feature buffers initialized to %d vectors\n",LIVEBUFBLOCKSIZE); } if (fcb->cmn) /* Only cmn_prior in block computation mode */ cmn_prior (uttcep, fcb->varnorm, nfr, fcb->cepsize, endutt); /*cmn_prior (uttcep, fcb->varnorm, nfr, fcb->cepsize, endutt,fcb->cmn_struct);*/ residualvecs = 0; if (beginutt){ /* Replicate first frame into the first win frames */ for (i=0;i<win;i++) memcpy(cepbuf[i],uttcep[0],cepsize*sizeof(float32)); /* beginutt = 0; */ /* Removed by Rita Singh around 02-Jan-2001 */ /* See History at the top of this file */ bufpos = win; bufpos %= LIVEBUFBLOCKSIZE; curpos = bufpos; jp1 = curpos - 1; jp1 %= LIVEBUFBLOCKSIZE; jp2 = curpos - 2; jp2 %= LIVEBUFBLOCKSIZE; jp3 = curpos - 3; jp3 %= LIVEBUFBLOCKSIZE; jf1 = curpos + 1; jf1 %= LIVEBUFBLOCKSIZE; jf2 = curpos + 2; jf2 %= LIVEBUFBLOCKSIZE; jf3 = curpos + 3; jf3 %= LIVEBUFBLOCKSIZE; residualvecs -= win; } for (i=0;i<nfr;i++){ assert(bufpos < LIVEBUFBLOCKSIZE); memcpy(cepbuf[bufpos++],uttcep[i],cepsize*sizeof(float32)); bufpos %= LIVEBUFBLOCKSIZE; } if (endutt){ /* Replicate last frame into the last win frames */ if (nfr > 0) { for (i=0;i<win;i++) { assert(bufpos < LIVEBUFBLOCKSIZE); memcpy(cepbuf[bufpos++],uttcep[nfr-1],cepsize*sizeof(float32)); bufpos %= LIVEBUFBLOCKSIZE; } } else { int16 tpos = bufpos-1; tpos %= LIVEBUFBLOCKSIZE; for (i=0;i<win;i++) { assert(bufpos < LIVEBUFBLOCKSIZE); memcpy(cepbuf[bufpos++],cepbuf[tpos],cepsize*sizeof(float32)); bufpos %= LIVEBUFBLOCKSIZE; } } residualvecs += win; } /* Create feature vectors */ nfeatvec = 0; nfr += residualvecs; /* ARCHAN : "Polluted code", reference to s3.3 to know the original */ for (i = 0; i < nfr; i++,nfeatvec++){ /* CEP */ /* memcpy (feat[i], cepbuf[curpos], (cepsize) * sizeof(float32));*/ memcpy (feat[i], cepbuf[curpos]+1, (cepsize-1) * sizeof(float32)); /* * DCEP: mfc[2] - mfc[-2]; */ /*f = feat[i] + cepsize ;*/ f=feat[i]+ cepsize -1; /* w = cepbuf[jf2]; */ /* +1 to skip C0 */ /*_w = cepbuf[jp2];*/ w = cepbuf[jf2] +1 ; _w = cepbuf[jp2] +1 ; /* for (j = 0; j < cepsize; j++)*/ for (j = 0; j < cepsize-1; j++) f[j] = w[j] - _w[j]; f += cepsize-1; f[0]=cepbuf[curpos][0]; f[1]=cepbuf[jf2][0]-cepbuf[jp2][0]; d1 = cepbuf[jf3][0]-cepbuf[jp1][0]; d2 = cepbuf[jf1][0]-cepbuf[jp3][0]; f[2] = d1 -d2; /* D2CEP: (mfc[3] - mfc[-1]) - (mfc[1] - mfc[-3]) */ /*f += cepsize;*/ f+=3; /* w1 = cepbuf[jf3];*/ /* Final +1 to skip C0 */ /*_w1 = cepbuf[jp1]; w_1 = cepbuf[jf1]; _w_1 = cepbuf[jp3];*/ w1 = cepbuf[jf3] +1; _w1 = cepbuf[jp1] +1; w_1 = cepbuf[jf1] +1; _w_1 = cepbuf[jp3] +1; /* for (j = 0; j < cepsize; j++) {*/ for (j = 0; j < cepsize -1; j++) { d1 = w1[j] - _w1[j]; d2 = w_1[j] - _w_1[j]; f[j] = d1 - d2; } jf1++; jf2++; jf3++; jp1++; jp2++; jp3++; curpos++; jf1 %= LIVEBUFBLOCKSIZE; jf2 %= LIVEBUFBLOCKSIZE; jf3 %= LIVEBUFBLOCKSIZE; jp1 %= LIVEBUFBLOCKSIZE; jp2 %= LIVEBUFBLOCKSIZE; jp3 %= LIVEBUFBLOCKSIZE; curpos %= LIVEBUFBLOCKSIZE; } *ofeat = feat; return(nfeatvec);#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -