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

📄 feat.c

📁 CMU大名鼎鼎的SPHINX-3大词汇量连续语音识别系统
💻 C
📖 第 1 页 / 共 3 页
字号:
    /* Allocate feature data array so that data is in one block from feat[0][0][0] */    feat = (float32 ***) ckd_calloc_2d (nfr, feat_n_stream(fcb), sizeof(float32 *));    data = (float32 *) ckd_calloc (nfr*k, sizeof(float32));        for (i = 0; i < nfr; i++) {	for (j = 0; j < feat_n_stream(fcb); j++) {	    feat[i][j] = data;	    data += feat_stream_len (fcb, j);	}    }        return feat;}static void feat_s2_4x_cep2feat (feat_t *fcb, float32 **mfc, float32 **feat){    float32 *f;    float32 *w, *_w;    float32 *w1, *w_1, *_w1, *_w_1;    float32 d1, d2;    int32 i, j;        assert (fcb);    assert (feat_cepsize (fcb) == 13);    assert (feat_cepsize_used (fcb) == 13);    assert (feat_n_stream (fcb) == 4);    assert (feat_stream_len (fcb, 0) == 12);    assert (feat_stream_len (fcb, 1) == 24);    assert (feat_stream_len (fcb, 2) == 3);    assert (feat_stream_len (fcb, 3) == 12);    assert (feat_window_size (fcb) == 4);        /* CEP; skip C0 */    memcpy (feat[0], mfc[0]+1, (feat_cepsize(fcb)-1) * sizeof(float32));        /*     * DCEP(SHORT): mfc[2] - mfc[-2]     * DCEP(LONG):  mfc[4] - mfc[-4]     */    w  = mfc[2] + 1;	/* +1 to skip C0 */    _w = mfc[-2] + 1;    f = feat[1];    for (i = 0; i < feat_cepsize(fcb)-1; i++)	/* Short-term */	f[i] = w[i] - _w[i];    w  = mfc[4] + 1;	/* +1 to skip C0 */    _w = mfc[-4] + 1;    for (j = 0; j < feat_cepsize(fcb)-1; i++, j++)	/* Long-term */	f[i] = w[j] - _w[j];    /* D2CEP: (mfc[3] - mfc[-1]) - (mfc[1] - mfc[-3]) */    w1   = mfc[3] + 1;	/* Final +1 to skip C0 */    _w1  = mfc[-1] + 1;    w_1  = mfc[1] + 1;    _w_1 = mfc[-3] + 1;    f = feat[3];    for (i = 0; i < feat_cepsize(fcb)-1; i++) {	d1 =  w1[i] -  _w1[i];	d2 = w_1[i] - _w_1[i];	f[i] = d1 - d2;    }        /* POW: C0, DC0, D2C0; differences computed as above for rest of cep */    f = feat[2];    f[0] = mfc[0][0];    f[1] = mfc[2][0] - mfc[-2][0];    d1 = mfc[3][0] - mfc[-1][0];    d2 = mfc[1][0] - mfc[-3][0];    f[2] = d1 - d2;}static void feat_s3_1x39_cep2feat (feat_t *fcb, float32 **mfc, float32 **feat){    float32 *f;    float32 *w, *_w;    float32 *w1, *w_1, *_w1, *_w_1;    float32 d1, d2;    int32 i;        assert (fcb);    assert (feat_cepsize (fcb) == 13);    assert (feat_cepsize_used (fcb) == 13);    assert (feat_n_stream (fcb) == 1);    assert (feat_stream_len (fcb, 0) == 39);    assert (feat_window_size (fcb) == 3);        /* CEP; skip C0 */    memcpy (feat[0], mfc[0]+1, (feat_cepsize(fcb)-1) * sizeof(float32));    /*     * DCEP: mfc[2] - mfc[-2];     */    f = feat[0] + feat_cepsize(fcb)-1;    w  = mfc[2] + 1;	/* +1 to skip C0 */    _w = mfc[-2] + 1;    for (i = 0; i < feat_cepsize(fcb)-1; i++)	f[i] = w[i] - _w[i];        /* POW: C0, DC0, D2C0 */    f += feat_cepsize(fcb)-1;    f[0] = mfc[0][0];    f[1] = mfc[2][0] - mfc[-2][0];    d1 = mfc[3][0] - mfc[-1][0];    d2 = mfc[1][0] - mfc[-3][0];    f[2] = d1 - d2;    /* D2CEP: (mfc[3] - mfc[-1]) - (mfc[1] - mfc[-3]) */    f += 3;        w1   = mfc[3] + 1;	/* Final +1 to skip C0 */    _w1  = mfc[-1] + 1;    w_1  = mfc[1] + 1;    _w_1 = mfc[-3] + 1;    for (i = 0; i < feat_cepsize(fcb)-1; i++) {	d1 =  w1[i] -  _w1[i];	d2 = w_1[i] - _w_1[i];	f[i] = d1 - d2;    }}static void feat_s3_cep (feat_t *fcb, float32 **mfc, float32 **feat){    assert (fcb);    assert (feat_cepsize (fcb) == 13);    assert ((feat_cepsize_used (fcb) <= 13) && (feat_cepsize_used(fcb) > 0));    assert (feat_n_stream (fcb) == 1);    assert (feat_stream_len (fcb, 0) == feat_cepsize_used(fcb));    assert (feat_window_size (fcb) == 0);        /* CEP */    memcpy (feat[0], mfc[0], feat_cepsize_used(fcb) * sizeof(float32));}static void feat_s3_cep_dcep (feat_t *fcb, float32 **mfc, float32 **feat){    float32 *f;    float32 *w, *_w;    int32 i;        assert (fcb);    assert (feat_cepsize (fcb) == 13);    assert ((feat_cepsize_used (fcb) <= 13) && (feat_cepsize_used(fcb) > 0));    assert (feat_n_stream (fcb) == 1);    assert (feat_stream_len (fcb, 0) == (feat_cepsize_used(fcb) * 2));    assert (feat_window_size (fcb) == 2);        /* CEP */    memcpy (feat[0], mfc[0], feat_cepsize_used(fcb) * sizeof(float32));        /*     * DCEP: mfc[2] - mfc[-2];     */    f = feat[0] + feat_cepsize_used(fcb);    w  = mfc[2];    _w = mfc[-2];    for (i = 0; i < feat_cepsize_used(fcb); i++)	f[i] = w[i] - _w[i];}void feat_1s_c_d_dd_cep2feat (feat_t *fcb, float32 **mfc, float32 **feat){    float32 *f;    float32 *w, *_w;    float32 *w1, *w_1, *_w1, *_w_1;    float32 d1, d2;    int32 i;        assert (fcb);    assert (feat_cepsize (fcb) == 13);    assert (feat_cepsize_used (fcb) == 13);    assert (feat_n_stream (fcb) == 1);    assert (feat_stream_len (fcb, 0) == 39);    assert (feat_window_size (fcb) == 3);    /* CEP */    memcpy (feat[0], mfc[0], feat_cepsize(fcb) * sizeof(float32));        /*     * DCEP: mfc[w] - mfc[-w], where w = FEAT_DCEP_WIN;     */    f = feat[0] + feat_cepsize(fcb);    w  = mfc[ FEAT_DCEP_WIN];    _w = mfc[-FEAT_DCEP_WIN];    for (i = 0; i < feat_cepsize(fcb); i++)	f[i] = w[i] - _w[i];        /*      * D2CEP: (mfc[w+1] - mfc[-w+1]) - (mfc[w-1] - mfc[-w-1]),      * where w = FEAT_DCEP_WIN      */    f += feat_cepsize(fcb);    w1   = mfc[ FEAT_DCEP_WIN+1];    _w1  = mfc[-FEAT_DCEP_WIN+1];    w_1  = mfc[ FEAT_DCEP_WIN-1];    _w_1 = mfc[-FEAT_DCEP_WIN-1];    for (i = 0; i < feat_cepsize(fcb); i++) {	d1 =  w1[i] -  _w1[i];	d2 = w_1[i] - _w_1[i];	f[i] = d1 - d2;    }}feat_t *feat_init (char *type, char *cmn, char *varnorm, char *agc){    feat_t *fcb;    int32 i, l, k;    char wd[16384], *strp;        E_INFO("Initializing feature stream to type: '%s', CMN='%s', VARNORM='%s', AGC='%s'\n",	   type, cmn, varnorm, agc);        fcb = (feat_t *) ckd_calloc (1, sizeof(feat_t));    fcb->name = (char *) ckd_salloc (type);    if (strcmp (type, "s2_4x") == 0) {	/* Sphinx-II format 4-stream feature (Hack!! hardwired constants below) */	fcb->cepsize = 13;	fcb->cepsize_used = 13;	fcb->n_stream = 4;	fcb->stream_len = (int32 *) ckd_calloc (4, sizeof(int32));	fcb->stream_len[0] = 12;	fcb->stream_len[1] = 24;	fcb->stream_len[2] = 3;	fcb->stream_len[3] = 12;	fcb->window_size = 4;	fcb->compute_feat = feat_s2_4x_cep2feat;    } else if (strcmp (type, "s3_1x39") == 0) {	/* 1-stream cep/dcep/pow/ddcep (Hack!! hardwired constants below) */	fcb->cepsize = 13;	fcb->cepsize_used = 13;	fcb->n_stream = 1;	fcb->stream_len = (int32 *) ckd_calloc (1, sizeof(int32));	fcb->stream_len[0] = 39;	fcb->window_size = 3;	fcb->compute_feat = feat_s3_1x39_cep2feat;    } else if (strcmp (type, "1s_c_d_dd") == 0) {        fcb->cepsize = 13;        fcb->cepsize_used = 13;	fcb->n_stream = 1;	fcb->stream_len = (int32 *) ckd_calloc (1, sizeof(int32));	fcb->stream_len[0] = 39;	fcb->window_size = 3; /* FEAT_DCEP_WIN + 1 */	fcb->compute_feat = feat_1s_c_d_dd_cep2feat;    } else if (strncmp (type, "cep_dcep", 8) == 0) {	/* 1-stream cep/dcep (Hack!! hardwired constants below) */	fcb->cepsize = 13;	/* Check if using only a portion of cep dimensions */	if (type[8] == ',') {	    if ((sscanf (type+9, "%d%n", &(fcb->cepsize_used), &l) != 1) ||		(type[l+9] != '\0') ||		(feat_cepsize_used(fcb) <= 0) ||		(feat_cepsize_used(fcb) > feat_cepsize(fcb)))		E_FATAL("Bad feature type argument: '%s'\n", type);	} else	    fcb->cepsize_used = 13;	fcb->n_stream = 1;	fcb->stream_len = (int32 *) ckd_calloc (1, sizeof(int32));	fcb->stream_len[0] = feat_cepsize_used(fcb) * 2;	fcb->window_size = 2;	fcb->compute_feat = feat_s3_cep_dcep;    } else if (strncmp (type, "cep", 3) == 0) {	/* 1-stream cep (Hack!! hardwired constants below) */	fcb->cepsize = 13;	/* Check if using only a portion of cep dimensions */	if (type[3] == ',') {	    if ((sscanf (type+4, "%d%n", &(fcb->cepsize_used), &l) != 1) ||		(type[l+4] != '\0') ||		(feat_cepsize_used(fcb) <= 0) ||		(feat_cepsize_used(fcb) > feat_cepsize(fcb)))		E_FATAL("Bad feature type argument: '%s'\n", type);	} else	    fcb->cepsize_used = 13;	fcb->n_stream = 1;	fcb->stream_len = (int32 *) ckd_calloc (1, sizeof(int32));	fcb->stream_len[0] = feat_cepsize_used(fcb);	fcb->window_size = 0;	fcb->compute_feat = feat_s3_cep;    } else {	/*	 * Generic definition: Format should be %d,%d,%d,...,%d (i.e., comma separated	 * list of feature stream widths; #items = #streams).	 */	l = strlen(type);	k = 0;	for (i = 1; i < l-1; i++)	    if (type[i] == ',') {		type[i] = ' ';		k++;	    }	k++;	/* Presumably there are (#commas+1) streams */	fcb->n_stream = k;	fcb->stream_len = (int32 *) ckd_calloc (k, sizeof(int32));	/* Scan individual feature stream lengths */	strp = type;	i = 0;	while (sscanf (strp, "%s%n", wd, &l) == 1) {	    strp += l;	    if ((i >= fcb->n_stream) || (sscanf (wd, "%d", &(fcb->stream_len[i])) != 1) ||		(fcb->stream_len[i] <= 0))		E_FATAL("Bad feature type argument\n");	    i++;	}	if (i != fcb->n_stream)	    E_FATAL("Bad feature type argument\n");		/* Input is already the feature stream */	fcb->cepsize = -1;	fcb->cepsize_used = -1;	fcb->window_size = 0;	fcb->compute_feat = NULL;    }    fcb->cmn_struct=cmn_init();    if (strcmp (cmn, "current") == 0)	fcb->cmn = 1;    else if (strcmp (cmn, "none") == 0)	fcb->cmn = 0;    else	E_FATAL("Unsupported CMN type '%s'\n", cmn);        if (strcmp (varnorm, "yes") == 0)	fcb->varnorm = 1;    else if (strcmp (varnorm, "no") == 0)	fcb->varnorm = 0;    else	E_FATAL("Unsupported VARNORM type '%s'\n", varnorm);        if (strcmp (agc, "max") == 0)	fcb->agc = 1;    else if (strcmp (agc, "none") == 0)	fcb->agc = 0;    else	E_FATAL("Unsupported AGC type '%s'\n", agc);    fcb->cepbuf=NULL;    fcb->tmpcepbuf=NULL;    fcb->cepbuf = (float32 **)ckd_calloc_2d(LIVEBUFBLOCKSIZE,					    feat_cepsize(fcb), sizeof(float32));    if (!fcb-> cepbuf)       E_FATAL("Unable to allocate cepbuf ckd_calloc_2d(%ld,%d,%d)\n",LIVEBUFBLOCKSIZE,feat_cepsize(fcb),sizeof(float32));    fcb->tmpcepbuf=(float32 **) ckd_calloc_2d(2*feat_window_size(fcb)+1,					      feat_cepsize(fcb), sizeof(float32));    if(!fcb->tmpcepbuf)       E_FATAL("Unable to allocate tmpcepbuf ckd_calloc_2d(%ld,%d,%d)\n",	      2*feat_window_size(fcb)+1,feat_cepsize(fcb),sizeof(float32));    return fcb;}void feat_print (feat_t *fcb, float32 ***feat, int32 nfr, FILE *fp){    int32 i, j, k;        for (i = 0; i < nfr; i++) {	fprintf (fp, "%8d:", i);	for (j = 0; j < feat_n_stream(fcb); j++) {	    fprintf (fp, "\t%2d:", j);	    for (k = 0; k < feat_stream_len(fcb, j); k++)		fprintf (fp, " %8.4f", feat[i][j][k]);	    fprintf (fp, "\n");	}    }        fflush (fp);}int32 feat_s2mfc2feat (feat_t *fcb, char *file, char *dir, char *cepext, int32 sf, int32 ef, float32 ***feat,		       int32 maxfr){    char path[16384];    int32 win, nfr;    int32 i, k;    float32 **mfc;        if (fcb->cepsize <= 0) {	E_ERROR("Bad cepsize: %d\n", fcb->cepsize);	return -1;    }        /* Create mfc filename, combining file, dir and extension (.mfc) if necessary */    k = strlen(file);#if 0    if ((k > 4) && (strcmp (file+k-4, ".mfc") == 0)) {	/* Hack!! Hardwired .mfc extension */#endif      /*20041212: ARCHAN fixed the mfc hacks. */    if ((k > 4) && (strcmp (file+k-4, ".mfc") == 0)) { 	if (dir && (file[0] != '/'))	    sprintf (path, "%s/%s", dir, file);

⌨️ 快捷键说明

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