9 #include "test_macros.h"
12 main(int argc, char *argv[])
14 static const arg_t fe_args[] = {
15 waveform_to_cepstral_command_line_macro(),
16 { NULL, 0, NULL, NULL }
23 mfcc_t **cepbuf, **cptr;
24 mfcc_t ***featbuf1, ***featbuf2, ***fptr;
26 int32 total_frames, ncep, nfr, i;
28 if ((raw = fopen(TESTDATADIR "/chan3.raw", "rb")) == NULL) {
29 perror(TESTDATADIR "/chan3.raw");
33 config = cmd_ln_parse_r(NULL, fe_args, argc, argv, FALSE);
34 fe = fe_init_auto_r(config);
35 fcb = feat_init("1s_c_d_dd", CMN_NONE, FALSE, AGC_NONE,
36 TRUE, fe_get_output_size(fe));
38 /* Determine how much data and how many MFCC frames we need. */
39 fseek(raw, 0, SEEK_END);
40 nsamp = ftell(raw) / sizeof(int16);
41 fe_process_frames(fe, NULL, &nsamp, NULL, &total_frames);
42 printf("%d samples, %d + 1 frames\n", nsamp, total_frames);
43 total_frames++; /* For the possible fe_end_utt() frame */
44 cepbuf = ckd_calloc_2d(total_frames + 1, fe_get_output_size(fe), sizeof(**cepbuf));
45 fseek(raw, 0, SEEK_SET);
47 /* Pay close attention, kids. This is how you use fe_process_frames(). */
51 while ((nsamp = fread(buf, sizeof(int16), 2048, raw)) > 0) {
52 int16 const *bptr = buf;
55 fe_process_frames(fe, &bptr, &nsamp, cptr, &ncep);
60 fe_end_utt(fe, *cptr, &nfr);
62 /* Now test some feature extraction problems. */
63 featbuf1 = feat_array_alloc(fcb, total_frames);
64 featbuf2 = feat_array_alloc(fcb, total_frames);
66 /* Whole utterance: canonical, assumed to be correct. */
68 TEST_EQUAL(total_frames,
69 feat_s2mfc2feat_live(fcb, cepbuf,
72 TEST_EQUAL(ncep, total_frames);
74 /* Process one frame at a time. */
78 nfr = feat_s2mfc2feat_live(fcb, cptr, &ncep, TRUE, FALSE, fptr);
79 TEST_EQUAL(nfr, 0); /* Not possible to make any frames yet. */
80 TEST_EQUAL(ncep, 1); /* But we shold have consumed one. */
82 for (i = 1; i < total_frames - 1; ++i) {
84 nfr = feat_s2mfc2feat_live(fcb, cptr, &ncep, FALSE, FALSE, fptr);
88 nfr = feat_s2mfc2feat_live(fcb, cptr, &ncep, FALSE, TRUE, fptr);
89 TEST_EQUAL(nfr, 4); /* This should have dumped the trailing window. */
90 TEST_EQUAL(ncep, 1); /* And only consumed one frame of MFCCs. */
93 /* Verify that we actually got the correct number of frames. */
94 TEST_EQUAL(cptr - cepbuf, total_frames);
95 TEST_EQUAL(fptr - featbuf2, total_frames);
97 /* Now verify that the results are equal. */
98 for (i = 0; i < total_frames; ++i) {
101 for (j = 0; j < feat_dimension(fcb); ++j) {
102 TEST_EQUAL_FLOAT(featbuf1[i][0][j], featbuf2[i][0][j]);
109 /* Process large chunks of frames at once, so as to exceed the
110 * internal ringbuffer size in feat_s2mfc2feat_live(). */
114 nfr = feat_s2mfc2feat_live(fcb, cptr, &ncep, TRUE, FALSE, fptr);
115 TEST_ASSERT(ncep != nfr);
118 ncep = total_frames - ncep;
122 nfr = feat_s2mfc2feat_live(fcb, cptr, &tmp_ncep, FALSE, FALSE, fptr);
127 nfr = feat_s2mfc2feat_live(fcb, cptr, &ncep, FALSE, TRUE, fptr);
130 TEST_EQUAL(cptr - cepbuf, total_frames);
131 TEST_EQUAL(fptr - featbuf2, total_frames);
133 /* Now verify that the results are equal. */
134 for (i = 0; i < total_frames; ++i) {
137 for (j = 0; j < feat_dimension(fcb); ++j)
138 TEST_EQUAL_FLOAT(featbuf1[i][0][j], featbuf2[i][0][j]);
146 feat_array_free(featbuf1);
147 feat_array_free(featbuf2);