예제 #1
0
static void
cmn_prior_shiftwin(cmn_t *cmn)
{
    mfcc_t sf;
    int32 i;

    E_INFO("cmn_prior_update: from < ");
    for (i = 0; i < cmn->veclen; i++)
        E_INFOCONT("%5.2f ", MFCC2FLOAT(cmn->cmn_mean[i]));
    E_INFOCONT(">\n");

    sf = FLOAT2MFCC(1.0) / cmn->nframe;
    for (i = 0; i < cmn->veclen; i++)
        cmn->cmn_mean[i] = cmn->sum[i] / cmn->nframe; /* sum[i] * sf */

    /* Make the accumulation decay exponentially */
    if (cmn->nframe >= CMN_WIN_HWM) {
        sf = CMN_WIN * sf;
        for (i = 0; i < cmn->veclen; i++)
            cmn->sum[i] = MFCCMUL(cmn->sum[i], sf);
        cmn->nframe = CMN_WIN;
    }

    E_INFO("cmn_prior_update: to   < ");
    for (i = 0; i < cmn->veclen; i++)
        E_INFOCONT("%5.2f ", MFCC2FLOAT(cmn->cmn_mean[i]));
    E_INFOCONT(">\n");
}
예제 #2
0
파일: agc.c 프로젝트: 006/ios_lab
void
agc_noise(agc_t *agc,
          mfcc_t **cep,
          int32 nfr)
{
    mfcc_t min_energy; /* Minimum log-energy */
    mfcc_t noise_level;        /* Average noise_level */
    int32 i;           /* frame index */
    int32 noise_frames;        /* Number of noise frames */

    /* Determine minimum log-energy in utterance */
    min_energy = cep[0][0];
    for (i = 0; i < nfr; ++i) {
        if (cep[i][0] < min_energy)
            min_energy = cep[i][0];
    }

    /* Average all frames between min_energy and min_energy + agc->noise_thresh */
    noise_frames = 0;
    noise_level = 0;
    min_energy += agc->noise_thresh;
    for (i = 0; i < nfr; ++i) {
        if (cep[i][0] < min_energy) {
            noise_level += cep[i][0];
            noise_frames++;
        }
    }
    noise_level /= noise_frames;

    E_INFO("AGC NOISE: max= %6.3f\n", MFCC2FLOAT(noise_level));

    /* Subtract noise_level from all log_energy values */
    for (i = 0; i < nfr; ++i)
        cep[i][0] -= noise_level;
}
예제 #3
0
void
gauden_dump_ind(const gauden_t * g, int senidx)
{
#ifndef POCKETSPHINX_NET
    int32 f, d, i;

    for (f = 0; f < g->n_feat; f++) {
        E_INFO("Codebook %d, Feature %d (%dx%d):\n",
               senidx, f, g->n_density, g->featlen[f]);

        for (d = 0; d < g->n_density; d++) {
            printf("m[%3d]", d);
            for (i = 0; i < g->featlen[f]; i++)
		printf(" %7.4f", MFCC2FLOAT(g->mean[senidx][f][d][i]));
            printf("\n");
        }
        printf("\n");

        for (d = 0; d < g->n_density; d++) {
            printf("v[%3d]", d);
            for (i = 0; i < g->featlen[f]; i++)
                printf(" %d", (int)g->var[senidx][f][d][i]);
            printf("\n");
        }
        printf("\n");

        for (d = 0; d < g->n_density; d++)
            printf("d[%3d] %d\n", d, (int)g->det[senidx][f][d]);
    }
	fflush(stderr);
#endif
}
예제 #4
0
void
cmn_prior_set(cmn_t *cmn, mfcc_t const * vec)
{
    int32 i;

    E_INFO("cmn_prior_set: from < ");
    for (i = 0; i < cmn->veclen; i++)
        E_INFOCONT("%5.2f ", MFCC2FLOAT(cmn->cmn_mean[i]));
    E_INFOCONT(">\n");

    for (i = 0; i < cmn->veclen; i++) {
        cmn->cmn_mean[i] = vec[i];
        cmn->sum[i] = vec[i] * CMN_WIN;
    }
    cmn->nframe = CMN_WIN;

    E_INFO("cmn_prior_set: to   < ");
    for (i = 0; i < cmn->veclen; i++)
        E_INFOCONT("%5.2f ", MFCC2FLOAT(cmn->cmn_mean[i]));
    E_INFOCONT(">\n");
}
예제 #5
0
static void
cep_dump_dbg(feat_t *fcb, mfcc_t **mfc, int32 nfr, const char *text)
{
    int32 i, j;

    E_INFO("%s\n", text);
    for (i = 0; i < nfr; i++) {
        for (j = 0; j < fcb->cepsize; j++) {
            fprintf(stderr, "%f ", MFCC2FLOAT(mfc[i][j]));
        }
        fprintf(stderr, "\n");
    }
}
예제 #6
0
void
feat_print(feat_t * fcb, mfcc_t *** feat, int32 nfr, FILE * fp)
{
    int32 i, j, k;
    for (i = 0; i < nfr; i++) {
#ifndef POCKETSPHINX_NET
        fprintf(fp, "%8d:\n", i);
#else
		net_fprintf(fp, "{0:8}:\n", i);
#endif
        for (j = 0; j < feat_dimension1(fcb); j++) {
#ifndef POCKETSPHINX_NET
            fprintf(fp, "\t%2d:", j);
#else
			net_fprintf(fp, "\t{0:2}:\n", j);
#endif
            for (k = 0; k < (int32)feat_dimension2(fcb, j); k++)
			{
#ifndef POCKETSPHINX_NET
                fprintf(fp, " %8.4f", MFCC2FLOAT(feat[i][j][k]));
#else
				//need check
				net_fprintf(fp, " {0:8.4}", MFCC2FLOAT(feat[i][j][k]));
#endif
			}
#ifndef POCKETSPHINX_NET
            fprintf(fp, "\n");
#else
			net_fprintf(fp, "\n");
#endif

        }
    }


    fflush(fp);
}
예제 #7
0
/**
 * Convert a block of mfcc_t to float32 (can be done in-place)
 **/
int32
fe_mfcc_to_float(fe_t * FE,
                 mfcc_t ** input, float32 ** output, int32 nframes)
{
    int32 i;

#ifndef FIXED_POINT
    if ((void *) input == (void *) output)
        return nframes * FE->FEATURE_DIMENSION;
#endif
    for (i = 0; i < nframes * FE->FEATURE_DIMENSION; ++i)
        output[0][i] = MFCC2FLOAT(input[0][i]);

    return i;
}
예제 #8
0
/**
 * Convert a block of mfcc_t to float32 (can be done in-place)
 **/
int32
fe_mfcc_to_float(fe_t * fe,
                 mfcc_t ** input, float32 ** output, int32 nframes)
{
    int32 i;

#ifndef FIXED_POINT
    if ((void *) input == (void *) output)
        return nframes * fe->feature_dimension;
#endif
    for (i = 0; i < nframes * fe->feature_dimension; ++i)
        output[0][i] = MFCC2FLOAT(input[0][i]);

    return i;
}
예제 #9
0
파일: cmn.c 프로젝트: QuinnEbert/zedom8or
cmn_t *
cmn_init(int32 veclen)
{
    cmn_t *cmn;
    cmn = (cmn_t *) ckd_calloc(1, sizeof(cmn_t));
    cmn->veclen = veclen;
    cmn->cmn_mean = (mfcc_t *) ckd_calloc(veclen, sizeof(mfcc_t));
    cmn->cmn_var = (mfcc_t *) ckd_calloc(veclen, sizeof(mfcc_t));
    cmn->sum = (mfcc_t *) ckd_calloc(veclen, sizeof(mfcc_t));
    /* A front-end dependent magic number */
    cmn->cmn_mean[0] = FLOAT2MFCC(12.0);
    cmn->nframe = 0;
    E_INFO("mean[0]= %.2f, mean[1..%d]= 0.0\n",
           MFCC2FLOAT(cmn->cmn_mean[0]), veclen - 1);

    return cmn;
}
예제 #10
0
/**
 * Output frames in text format.
 */
static int
output_frames_text(sphinx_wave2feat_t *wtf, mfcc_t **frames, int nfr)
{
    int i, j, nfloat = 0;

    fe_mfcc_to_float(wtf->fe, frames, (float32 **)frames, nfr);
    for (i = 0; i < nfr; ++i) {
        for (j = 0; j < wtf->veclen; ++j) {
            fprintf(wtf->outfh, "%.5g", MFCC2FLOAT(frames[i][j]));
            if (j == wtf->veclen - 1)
                fprintf(wtf->outfh, "\n");
            else
                fprintf(wtf->outfh, " ");
        }
        nfloat += wtf->veclen;
    }
    return nfloat;
}
예제 #11
0
void
feat_print(feat_t * fcb, mfcc_t *** feat, int32 nfr, FILE * fp)
{
    uint32 i, j, k;

    for (i = 0; i < nfr; i++) {
        fprintf(fp, "%8d:\n", i);

        for (j = 0; j < feat_dimension1(fcb); j++) {
            fprintf(fp, "\t%2d:", j);

            for (k = 0; k < feat_dimension2(fcb, j); k++)
                fprintf(fp, " %8.4f", MFCC2FLOAT(feat[i][j][k]));
            fprintf(fp, "\n");
        }
    }

    fflush(fp);
}
예제 #12
0
int
main(int argc, char *argv[])
{
    feat_t *fcb;
    mfcc_t **in_feats, ***out_feats;
    int32 i, j, ncep;

    /* Test "raw" features without concatenation */
    fcb = feat_init("13", CMN_NONE, 0, AGC_NONE, 1, 13);

    in_feats = (mfcc_t **)ckd_alloc_2d_ptr(6, 13, data, sizeof(mfcc_t));
    out_feats = (mfcc_t ***)ckd_calloc_3d(6, 1, 13, sizeof(mfcc_t));
    ncep = 6;
    feat_s2mfc2feat_live(fcb, in_feats, &ncep, 1, 1, out_feats);

    for (i = 0; i < 6; ++i) {
        for (j = 0; j < 13; ++j) {
            printf("%.3f ", MFCC2FLOAT(out_feats[i][0][j]));
        }
        printf("\n");
    }
    feat_free(fcb);
    ckd_free(in_feats);
    ckd_free_3d(out_feats);

    /* Test "raw" features with concatenation */
    fcb = feat_init("13:1", CMN_NONE, 0, AGC_NONE, 1, 13);

    in_feats = (mfcc_t **)ckd_alloc_2d_ptr(6, 13, data, sizeof(mfcc_t));
    out_feats = (mfcc_t ***)ckd_calloc_3d(8, 1, 39, sizeof(mfcc_t));
    ncep = 6;
    feat_s2mfc2feat_live(fcb, in_feats, &ncep, 1, 1, out_feats);

    for (i = 0; i < 6; ++i) {
        for (j = 0; j < 39; ++j) {
            printf("%.3f ", MFCC2FLOAT(out_feats[i][0][j]));
        }
        printf("\n");
    }
    feat_free(fcb);

    /* Test 1s_c_d_dd features */
    fcb = feat_init("1s_c_d_dd", CMN_NONE, 0, AGC_NONE, 1, 13);
    ncep = 6;
    feat_s2mfc2feat_live(fcb, in_feats, &ncep, 1, 1, out_feats);

    for (i = 0; i < 6; ++i) {
        for (j = 0; j < 39; ++j) {
            printf("%.3f ", MFCC2FLOAT(out_feats[i][0][j]));
        }
        printf("\n");
    }

    /* Verify that the deltas are correct. */
    for (i = 2; i < 4; ++i) {
        for (j = 0; j < 13; ++j) {
            if (fabs(MFCC2FLOAT(out_feats[i][0][13+j] -
                                (out_feats[i+2][0][j]
                                 - out_feats[i-2][0][j]))) > 0.01) {
                printf("Delta mismatch in [%d][%d]\n", i, j);
                return 1;
            }
        }
    }
    feat_free(fcb);
    ckd_free(in_feats);
    ckd_free_3d(out_feats);

    return 0;
}
예제 #13
0
파일: cmn.c 프로젝트: QuinnEbert/zedom8or
void
cmn(cmn_t *cmn, mfcc_t ** mfc, int32 varnorm, int32 n_frame)
{
    mfcc_t *mfcp;
    mfcc_t t;
    int32 i, f;

    oe_assert(mfc != NULL);

    if (n_frame <= 0)
        return;

    /* If cmn->cmn_mean wasn't NULL, we need to zero the contents */
    memset(cmn->cmn_mean, 0, cmn->veclen * sizeof(mfcc_t));

    /* Find mean cep vector for this utterance */
    for (f = 0; f < n_frame; f++) {
        mfcp = mfc[f];
        for (i = 0; i < cmn->veclen; i++) {
            cmn->cmn_mean[i] += mfcp[i];
        }
    }

    for (i = 0; i < cmn->veclen; i++)
        cmn->cmn_mean[i] /= n_frame;

    E_INFO("CMN: ");
    for (i = 0; i < cmn->veclen; i++)
        E_INFOCONT("%5.2f ", MFCC2FLOAT(cmn->cmn_mean[i]));
    E_INFOCONT("\n");
    if (!varnorm) {
        /* Subtract mean from each cep vector */
        for (f = 0; f < n_frame; f++) {
            mfcp = mfc[f];
            for (i = 0; i < cmn->veclen; i++)
                mfcp[i] -= cmn->cmn_mean[i];
        }
    }
    else {
        /* Scale cep vectors to have unit variance along each dimension, and subtract means */
        /* If cmn->cmn_var wasn't NULL, we need to zero the contents */
        memset(cmn->cmn_var, 0, cmn->veclen * sizeof(mfcc_t));

        for (f = 0; f < n_frame; f++) {
            mfcp = mfc[f];

            for (i = 0; i < cmn->veclen; i++) {
                t = mfcp[i] - cmn->cmn_mean[i];
                cmn->cmn_var[i] += MFCCMUL(t, t);
            }
        }
        for (i = 0; i < cmn->veclen; i++)
            /* Inverse Std. Dev, RAH added type case from sqrt */
            cmn->cmn_var[i] = FLOAT2MFCC(sqrt((float64)n_frame / MFCC2FLOAT(cmn->cmn_var[i])));

        for (f = 0; f < n_frame; f++) {
            mfcp = mfc[f];
            for (i = 0; i < cmn->veclen; i++)
                mfcp[i] = MFCCMUL((mfcp[i] - cmn->cmn_mean[i]), cmn->cmn_var[i]);
        }
    }
}
예제 #14
0
파일: agc.c 프로젝트: 006/ios_lab
float32
agc_emax_get(agc_t *agc)
{
    return MFCC2FLOAT(agc->max);
}
예제 #15
0
int
main(int argc, char *argv[])
{
	feat_t *fcb;
	mfcc_t **in_feats, ***out_feats, ***out_feats2, ***optr;
	int32 i, j, ncep, nfr, nfr1, nfr2;

	in_feats = (mfcc_t **)ckd_alloc_2d_ptr(6, 13, data, sizeof(mfcc_t));
	out_feats = (mfcc_t ***)ckd_calloc_3d(8, 1, 39, sizeof(mfcc_t));
	/* Test 1s_c_d_dd features */
	fcb = feat_init("1s_c_d_dd", CMN_NONE, 0, AGC_NONE, 1, 13);
	ncep = 6;
	nfr1 = feat_s2mfc2feat_live(fcb, in_feats, &ncep, 1, 1, out_feats);
	printf("Processed %d input %d output frames\n", ncep, nfr1);
	for (i = 0; i < nfr1; ++i) {
		printf("%d: ", i);
		for (j = 0; j < 39; ++j) {
			printf("%.3f ", MFCC2FLOAT(out_feats[i][0][j]));
		}
		printf("\n");
	}
	feat_free(fcb);

	/* Test in "live" mode. */
	fcb = feat_init("1s_c_d_dd", CMN_NONE, 0, AGC_NONE, 1, 13);
	optr = out_feats2 = (mfcc_t ***)ckd_calloc_3d(8, 1, 39, sizeof(mfcc_t));
	nfr2 = 0;
	ncep = 2;
	nfr = feat_s2mfc2feat_live(fcb, in_feats, &ncep, TRUE, FALSE, optr);
	printf("Processed %d input %d output frames\n", ncep, nfr);
	nfr2 += nfr;
	for (i = 0; i < nfr; ++i) {
		printf("%d: ", i);
		for (j = 0; j < 39; ++j) {
			printf("%.3f ", MFCC2FLOAT(optr[i][0][j]));
		}
		printf("\n");
	}
	optr += nfr;

	ncep = 2;
	nfr = feat_s2mfc2feat_live(fcb, in_feats + 2, &ncep, FALSE, FALSE, optr);
	nfr2 += nfr;
	printf("Processed %d input %d output frames\n", ncep, nfr);
	for (i = 0; i < nfr; ++i) {
		printf("%d: ", i);
		for (j = 0; j < 39; ++j) {
			printf("%.3f ", MFCC2FLOAT(optr[i][0][j]));
		}
		printf("\n");
	}
	optr += nfr;

	ncep = 2;
	nfr = feat_s2mfc2feat_live(fcb, in_feats + 4, &ncep, FALSE, TRUE, optr);
	nfr2 += nfr;
	printf("Processed %d input %d output frames\n", ncep, nfr);
	for (i = 0; i < nfr; ++i) {
		printf("%d: ", i);
		for (j = 0; j < 39; ++j) {
			printf("%.3f ", MFCC2FLOAT(optr[i][0][j]));
		}
		printf("\n");
	}
	optr += nfr;
	feat_free(fcb);

	TEST_EQUAL(nfr1, nfr2);
	for (i = 0; i < nfr1; ++i) {
		for (j = 0; j < 39; ++j) {
			TEST_EQUAL(out_feats[i][0][j], out_feats2[i][0][j]);
		}
	}
	ckd_free_3d(out_feats2);
	ckd_free_3d(out_feats);
	ckd_free(in_feats);

	return 0;
}
예제 #16
0
파일: test_fe.c 프로젝트: 006/ios_lab
int
main(int argc, char *argv[])
{
	static const arg_t fe_args[] = {
		waveform_to_cepstral_command_line_macro(),
		{ NULL, 0, NULL, NULL }
	};
	FILE *raw;
	cmd_ln_t *config;
	fe_t *fe;
	int16 buf[1024];
	int16 const *inptr;
	int32 frame_shift, frame_size;
	mfcc_t **cepbuf1, **cepbuf2, **cptr;
	int32 nfr, i;
	size_t nsamp;

	TEST_ASSERT(config = cmd_ln_parse_r(NULL, fe_args, argc, argv, FALSE));
	TEST_ASSERT(fe = fe_init_auto_r(config));

	TEST_EQUAL(fe_get_output_size(fe), DEFAULT_NUM_CEPSTRA);

	fe_get_input_size(fe, &frame_shift, &frame_size);
	TEST_EQUAL(frame_shift, DEFAULT_FRAME_SHIFT);
	TEST_EQUAL(frame_size, (int)(DEFAULT_WINDOW_LENGTH*DEFAULT_SAMPLING_RATE));

	TEST_ASSERT(raw = fopen(TESTDATADIR "/chan3.raw", "rb"));

	TEST_EQUAL(0, fe_start_utt(fe));
	TEST_EQUAL(1024, fread(buf, sizeof(int16), 1024, raw));

	nsamp = 1024;
	TEST_ASSERT(fe_process_frames(fe, NULL, &nsamp, NULL, &nfr) >= 0);
	TEST_EQUAL(1024, nsamp);
	TEST_EQUAL(4, nfr);

	cepbuf1 = ckd_calloc_2d(5, DEFAULT_NUM_CEPSTRA, sizeof(**cepbuf1));
	inptr = &buf[0];
	nfr = 1;

	printf("frame_size %d frame_shift %d\n", frame_size, frame_shift);
	/* Process the first frame. */
	TEST_ASSERT(fe_process_frames(fe, &inptr, &nsamp, &cepbuf1[0], &nfr) >= 0);
	printf("inptr %d nsamp %d nfr %d\n", inptr - buf, nsamp, nfr);
	TEST_EQUAL(nfr, 1);

	/* Note that this next one won't actually consume any frames
	 * of input, because it already got sufficient overflow
	 * samples last time around.  This is implementation-dependent
	 * so we shouldn't actually test for it. */
	TEST_ASSERT(fe_process_frames(fe, &inptr, &nsamp, &cepbuf1[1], &nfr) >= 0);
	printf("inptr %d nsamp %d nfr %d\n", inptr - buf, nsamp, nfr);
	TEST_EQUAL(nfr, 1);

	TEST_ASSERT(fe_process_frames(fe, &inptr, &nsamp, &cepbuf1[2], &nfr) >= 0);
	printf("inptr %d nsamp %d nfr %d\n", inptr - buf, nsamp, nfr);
	TEST_EQUAL(nfr, 1);

	TEST_ASSERT(fe_process_frames(fe, &inptr, &nsamp, &cepbuf1[3], &nfr) >= 0);
	printf("inptr %d nsamp %d nfr %d\n", inptr - buf, nsamp, nfr);
	TEST_EQUAL(nfr, 1);

	TEST_ASSERT(fe_end_utt(fe, cepbuf1[4], &nfr) >= 0);
	printf("nfr %d\n", nfr);
	TEST_EQUAL(nfr, 1);

	/* What we *should* test is that the output we get by
	 * processing one frame at a time is exactly the same as what
	 * we get from doing them all at once.  So let's do that */
	cepbuf2 = ckd_calloc_2d(5, DEFAULT_NUM_CEPSTRA, sizeof(**cepbuf2));
	inptr = &buf[0];
	nfr = 5;
	nsamp = 1024;
	TEST_EQUAL(0, fe_start_utt(fe));
	TEST_ASSERT(fe_process_frames(fe, &inptr, &nsamp, cepbuf2, &nfr) >= 0);
	printf("nfr %d\n", nfr);
	TEST_EQUAL(nfr, 4);
	nfr = 1;
	TEST_ASSERT(fe_end_utt(fe, cepbuf2[4], &nfr) >= 0);
	printf("nfr %d\n", nfr);
	TEST_EQUAL(nfr, 1);

	for (i = 0; i < 5; ++i) {
		int j;
		printf("%d: ", i);
		for (j = 0; j < DEFAULT_NUM_CEPSTRA; ++j) {
			printf("%.2f,%.2f ",
			       MFCC2FLOAT(cepbuf1[i][j]),
			       MFCC2FLOAT(cepbuf2[i][j]));
			TEST_EQUAL_FLOAT(cepbuf1[i][j], cepbuf2[i][j]);
		}
		printf("\n");
	}

	/* Now, also test to make sure that even if we feed data in
	 * little tiny bits we can still make things work. */
	memset(cepbuf2[0], 0, 5 * DEFAULT_NUM_CEPSTRA * sizeof(**cepbuf2));
	inptr = &buf[0];
	cptr = &cepbuf2[0];
	nfr = 5;
	i = 5;
	nsamp = 256;
	TEST_EQUAL(0, fe_start_utt(fe));
	TEST_ASSERT(fe_process_frames(fe, &inptr, &nsamp, cptr, &i) >= 0);
	printf("inptr %d nsamp %d nfr %d\n", inptr - buf, nsamp, i);
	cptr += i;
	nfr -= i;
	i = nfr;
	nsamp = 256;
	TEST_ASSERT(fe_process_frames(fe, &inptr, &nsamp, cptr, &i) >= 0);
	printf("inptr %d nsamp %d nfr %d\n", inptr - buf, nsamp, i);
	cptr += i;
	nfr -= i;
	i = nfr;
	nsamp = 256;
	TEST_ASSERT(fe_process_frames(fe, &inptr, &nsamp, cptr, &i) >= 0);
	printf("inptr %d nsamp %d nfr %d\n", inptr - buf, nsamp, i);
	cptr += i;
	nfr -= i;
	i = nfr;
	nsamp = 256;
	TEST_ASSERT(fe_process_frames(fe, &inptr, &nsamp, cptr, &i) >= 0);
	printf("inptr %d nsamp %d nfr %d\n", inptr - buf, nsamp, i);
	cptr += i;
	nfr -= i;
	TEST_ASSERT(fe_end_utt(fe, *cptr, &nfr) >= 0);
	printf("nfr %d\n", nfr);
	TEST_EQUAL(nfr, 1);

	for (i = 0; i < 5; ++i) {
		int j;
		printf("%d: ", i);
		for (j = 0; j < DEFAULT_NUM_CEPSTRA; ++j) {
			printf("%.2f,%.2f ",
			       MFCC2FLOAT(cepbuf1[i][j]),
			       MFCC2FLOAT(cepbuf2[i][j]));
			TEST_EQUAL_FLOAT(cepbuf1[i][j], cepbuf2[i][j]);
		}
		printf("\n");
	}

	/* And now, finally, test fe_process_utt() */
	inptr = &buf[0];
	i = 0;
	TEST_EQUAL(0, fe_start_utt(fe));
	TEST_ASSERT(fe_process_utt(fe, inptr, 256, &cptr, &nfr) >= 0);
	printf("i %d nfr %d\n", i, nfr);
	if (nfr)
		memcpy(cepbuf2[i], cptr[0], nfr * DEFAULT_NUM_CEPSTRA * sizeof(**cptr));
	ckd_free_2d(cptr);
	i += nfr;
	inptr += 256;
	TEST_ASSERT(fe_process_utt(fe, inptr, 256, &cptr, &nfr) >= 0);
	printf("i %d nfr %d\n", i, nfr);
	if (nfr)
		memcpy(cepbuf2[i], cptr[0], nfr * DEFAULT_NUM_CEPSTRA * sizeof(**cptr));
	ckd_free_2d(cptr);
	i += nfr;
	inptr += 256;
	TEST_ASSERT(fe_process_utt(fe, inptr, 256, &cptr, &nfr) >= 0);
	printf("i %d nfr %d\n", i, nfr);
	if (nfr)
		memcpy(cepbuf2[i], cptr[0], nfr * DEFAULT_NUM_CEPSTRA * sizeof(**cptr));
	ckd_free_2d(cptr);
	i += nfr;
	inptr += 256;
	TEST_ASSERT(fe_process_utt(fe, inptr, 256, &cptr, &nfr) >= 0);
	printf("i %d nfr %d\n", i, nfr);
	if (nfr)
		memcpy(cepbuf2[i], cptr[0], nfr * DEFAULT_NUM_CEPSTRA * sizeof(**cptr));
	ckd_free_2d(cptr);
	i += nfr;
	inptr += 256;
	TEST_ASSERT(fe_end_utt(fe, cepbuf2[i], &nfr) >= 0);
	printf("i %d nfr %d\n", i, nfr);
	TEST_EQUAL(nfr, 1);

	for (i = 0; i < 5; ++i) {
		int j;
		printf("%d: ", i);
		for (j = 0; j < DEFAULT_NUM_CEPSTRA; ++j) {
			printf("%.2f,%.2f ",
			       MFCC2FLOAT(cepbuf1[i][j]),
			       MFCC2FLOAT(cepbuf2[i][j]));
			TEST_EQUAL_FLOAT(cepbuf1[i][j], cepbuf2[i][j]);
		}
		printf("\n");
	}

	ckd_free_2d(cepbuf1);
	ckd_free_2d(cepbuf2);
	fclose(raw);
	fe_free(fe);

	return 0;
}
예제 #17
0
/**
 * For fixed point we are doing the computation in a fixlog domain,
 * so we have to add many processing cases.
 */
void
fe_track_snr(fe_t * fe, int32 *in_speech)
{
    powspec_t *signal;
    powspec_t *gain;
    noise_stats_t *noise_stats;
    powspec_t *mfspec;
    int32 i, num_filts;
    powspec_t lrt, snr;

    if (!(fe->remove_noise || fe->remove_silence)) {
        *in_speech = TRUE;
        return;
    }

    noise_stats = fe->noise_stats;
    mfspec = fe->mfspec;
    num_filts = noise_stats->num_filters;

    signal = (powspec_t *) ckd_calloc(num_filts, sizeof(powspec_t));

    if (noise_stats->undefined) {
        for (i = 0; i < num_filts; i++) {
            noise_stats->power[i] = mfspec[i];
            noise_stats->noise[i] = mfspec[i];
#ifndef FIXED_POINT
            noise_stats->floor[i] = mfspec[i] / noise_stats->max_gain;
            noise_stats->peak[i] = 0.0;       
#else
            noise_stats->floor[i] = mfspec[i] - noise_stats->max_gain;
            noise_stats->peak[i] = MIN_FIXLOG;
#endif
        }
        noise_stats->undefined = FALSE;
    }

    /* Calculate smoothed power */
    for (i = 0; i < num_filts; i++) {
#ifndef FIXED_POINT
        noise_stats->power[i] =
            noise_stats->lambda_power * noise_stats->power[i] + noise_stats->comp_lambda_power * mfspec[i];   
#else
        noise_stats->power[i] = fe_log_add(noise_stats->lambda_power + noise_stats->power[i],
            noise_stats->comp_lambda_power + mfspec[i]);
#endif
    }

    /* Noise estimation and vad decision */
    fe_lower_envelope(noise_stats, noise_stats->power, noise_stats->noise, num_filts);

    lrt = FLOAT2MFCC(-10.0);
    for (i = 0; i < num_filts; i++) {
#ifndef FIXED_POINT
        signal[i] = noise_stats->power[i] - noise_stats->noise[i];
            if (signal[i] < 0)
                signal[i] = 0;
        snr = log(noise_stats->power[i] / noise_stats->noise[i]);

#else
        signal[i] = fe_log_sub(noise_stats->power[i], noise_stats->noise[i]);
        snr = MFCC2FLOAT(noise_stats->power[i] - noise_stats->noise[i]);
#endif    
        if (snr > lrt)
            lrt = snr;
    }

    if (fe->remove_silence && (lrt < fe->vad_threshold))
        *in_speech = FALSE;
    else
	*in_speech = TRUE;

    fe_lower_envelope(noise_stats, signal, noise_stats->floor, num_filts);

    fe_temp_masking(noise_stats, signal, noise_stats->peak, num_filts);

    if (!fe->remove_noise) {
        //no need for further calculations if noise cancellation disabled
        ckd_free(signal);
        return;
    }

    for (i = 0; i < num_filts; i++) {
        if (signal[i] < noise_stats->floor[i])
            signal[i] = noise_stats->floor[i];
    }

    gain = (powspec_t *) ckd_calloc(num_filts, sizeof(powspec_t));
#ifndef FIXED_POINT
    for (i = 0; i < num_filts; i++) {
        if (signal[i] < noise_stats->max_gain * noise_stats->power[i])
            gain[i] = signal[i] / noise_stats->power[i];
        else
            gain[i] = noise_stats->max_gain;
        if (gain[i] < noise_stats->inv_max_gain)
            gain[i] = noise_stats->inv_max_gain;
    }
#else
    for (i = 0; i < num_filts; i++) {
        gain[i] = signal[i] - noise_stats->power[i];
        if (gain[i] > noise_stats->max_gain)
            gain[i] = noise_stats->max_gain;
        if (gain[i] < noise_stats->inv_max_gain)
            gain[i] = noise_stats->inv_max_gain;
    }
#endif

    /* Weight smoothing and time frequency normalization */
    fe_weight_smooth(noise_stats, mfspec, gain, num_filts);

    ckd_free(gain);
    ckd_free(signal);
}