Initial import to Tizen
[profile/ivi/sphinxbase.git] / test / unit / test_fe / test_fe.c
1 #include <stdio.h>
2 #include <string.h>
3
4 #include "fe.h"
5 #include "cmd_ln.h"
6 #include "ckd_alloc.h"
7
8 #include "test_macros.h"
9
10 int
11 main(int argc, char *argv[])
12 {
13         static const arg_t fe_args[] = {
14                 waveform_to_cepstral_command_line_macro(),
15                 { NULL, 0, NULL, NULL }
16         };
17         FILE *raw;
18         cmd_ln_t *config;
19         fe_t *fe;
20         int16 buf[1024];
21         int16 const *inptr;
22         int32 frame_shift, frame_size;
23         mfcc_t **cepbuf1, **cepbuf2, **cptr;
24         int32 nfr, i;
25         size_t nsamp;
26
27         TEST_ASSERT(config = cmd_ln_parse_r(NULL, fe_args, argc, argv, FALSE));
28         TEST_ASSERT(fe = fe_init_auto_r(config));
29
30         TEST_EQUAL(fe_get_output_size(fe), DEFAULT_NUM_CEPSTRA);
31
32         fe_get_input_size(fe, &frame_shift, &frame_size);
33         TEST_EQUAL(frame_shift, DEFAULT_FRAME_SHIFT);
34         TEST_EQUAL(frame_size, (int)(DEFAULT_WINDOW_LENGTH*DEFAULT_SAMPLING_RATE));
35
36         TEST_ASSERT(raw = fopen(TESTDATADIR "/chan3.raw", "rb"));
37
38         TEST_EQUAL(0, fe_start_utt(fe));
39         TEST_EQUAL(1024, fread(buf, sizeof(int16), 1024, raw));
40
41         nsamp = 1024;
42         TEST_ASSERT(fe_process_frames(fe, NULL, &nsamp, NULL, &nfr) >= 0);
43         TEST_EQUAL(1024, nsamp);
44         TEST_EQUAL(4, nfr);
45
46         cepbuf1 = ckd_calloc_2d(5, DEFAULT_NUM_CEPSTRA, sizeof(**cepbuf1));
47         inptr = &buf[0];
48         nfr = 1;
49
50         printf("frame_size %d frame_shift %d\n", frame_size, frame_shift);
51         /* Process the first frame. */
52         TEST_ASSERT(fe_process_frames(fe, &inptr, &nsamp, &cepbuf1[0], &nfr) >= 0);
53         printf("inptr %d nsamp %d nfr %d\n", inptr - buf, nsamp, nfr);
54         TEST_EQUAL(nfr, 1);
55
56         /* Note that this next one won't actually consume any frames
57          * of input, because it already got sufficient overflow
58          * samples last time around.  This is implementation-dependent
59          * so we shouldn't actually test for it. */
60         TEST_ASSERT(fe_process_frames(fe, &inptr, &nsamp, &cepbuf1[1], &nfr) >= 0);
61         printf("inptr %d nsamp %d nfr %d\n", inptr - buf, nsamp, nfr);
62         TEST_EQUAL(nfr, 1);
63
64         TEST_ASSERT(fe_process_frames(fe, &inptr, &nsamp, &cepbuf1[2], &nfr) >= 0);
65         printf("inptr %d nsamp %d nfr %d\n", inptr - buf, nsamp, nfr);
66         TEST_EQUAL(nfr, 1);
67
68         TEST_ASSERT(fe_process_frames(fe, &inptr, &nsamp, &cepbuf1[3], &nfr) >= 0);
69         printf("inptr %d nsamp %d nfr %d\n", inptr - buf, nsamp, nfr);
70         TEST_EQUAL(nfr, 1);
71
72         TEST_ASSERT(fe_end_utt(fe, cepbuf1[4], &nfr) >= 0);
73         printf("nfr %d\n", nfr);
74         TEST_EQUAL(nfr, 1);
75
76         /* What we *should* test is that the output we get by
77          * processing one frame at a time is exactly the same as what
78          * we get from doing them all at once.  So let's do that */
79         cepbuf2 = ckd_calloc_2d(5, DEFAULT_NUM_CEPSTRA, sizeof(**cepbuf2));
80         inptr = &buf[0];
81         nfr = 5;
82         nsamp = 1024;
83         TEST_EQUAL(0, fe_start_utt(fe));
84         TEST_ASSERT(fe_process_frames(fe, &inptr, &nsamp, cepbuf2, &nfr) >= 0);
85         printf("nfr %d\n", nfr);
86         TEST_EQUAL(nfr, 4);
87         nfr = 1;
88         TEST_ASSERT(fe_end_utt(fe, cepbuf2[4], &nfr) >= 0);
89         printf("nfr %d\n", nfr);
90         TEST_EQUAL(nfr, 1);
91
92         for (i = 0; i < 5; ++i) {
93                 int j;
94                 printf("%d: ", i);
95                 for (j = 0; j < DEFAULT_NUM_CEPSTRA; ++j) {
96                         printf("%.2f,%.2f ",
97                                MFCC2FLOAT(cepbuf1[i][j]),
98                                MFCC2FLOAT(cepbuf2[i][j]));
99                         TEST_EQUAL_FLOAT(cepbuf1[i][j], cepbuf2[i][j]);
100                 }
101                 printf("\n");
102         }
103
104         /* Now, also test to make sure that even if we feed data in
105          * little tiny bits we can still make things work. */
106         memset(cepbuf2[0], 0, 5 * DEFAULT_NUM_CEPSTRA * sizeof(**cepbuf2));
107         inptr = &buf[0];
108         cptr = &cepbuf2[0];
109         nfr = 5;
110         i = 5;
111         nsamp = 256;
112         TEST_EQUAL(0, fe_start_utt(fe));
113         TEST_ASSERT(fe_process_frames(fe, &inptr, &nsamp, cptr, &i) >= 0);
114         printf("inptr %d nsamp %d nfr %d\n", inptr - buf, nsamp, i);
115         cptr += i;
116         nfr -= i;
117         i = nfr;
118         nsamp = 256;
119         TEST_ASSERT(fe_process_frames(fe, &inptr, &nsamp, cptr, &i) >= 0);
120         printf("inptr %d nsamp %d nfr %d\n", inptr - buf, nsamp, i);
121         cptr += i;
122         nfr -= i;
123         i = nfr;
124         nsamp = 256;
125         TEST_ASSERT(fe_process_frames(fe, &inptr, &nsamp, cptr, &i) >= 0);
126         printf("inptr %d nsamp %d nfr %d\n", inptr - buf, nsamp, i);
127         cptr += i;
128         nfr -= i;
129         i = nfr;
130         nsamp = 256;
131         TEST_ASSERT(fe_process_frames(fe, &inptr, &nsamp, cptr, &i) >= 0);
132         printf("inptr %d nsamp %d nfr %d\n", inptr - buf, nsamp, i);
133         cptr += i;
134         nfr -= i;
135         TEST_ASSERT(fe_end_utt(fe, *cptr, &nfr) >= 0);
136         printf("nfr %d\n", nfr);
137         TEST_EQUAL(nfr, 1);
138
139         for (i = 0; i < 5; ++i) {
140                 int j;
141                 printf("%d: ", i);
142                 for (j = 0; j < DEFAULT_NUM_CEPSTRA; ++j) {
143                         printf("%.2f,%.2f ",
144                                MFCC2FLOAT(cepbuf1[i][j]),
145                                MFCC2FLOAT(cepbuf2[i][j]));
146                         TEST_EQUAL_FLOAT(cepbuf1[i][j], cepbuf2[i][j]);
147                 }
148                 printf("\n");
149         }
150
151         /* And now, finally, test fe_process_utt() */
152         inptr = &buf[0];
153         i = 0;
154         TEST_EQUAL(0, fe_start_utt(fe));
155         TEST_ASSERT(fe_process_utt(fe, inptr, 256, &cptr, &nfr) >= 0);
156         printf("i %d nfr %d\n", i, nfr);
157         if (nfr)
158                 memcpy(cepbuf2[i], cptr[0], nfr * DEFAULT_NUM_CEPSTRA * sizeof(**cptr));
159         ckd_free_2d(cptr);
160         i += nfr;
161         inptr += 256;
162         TEST_ASSERT(fe_process_utt(fe, inptr, 256, &cptr, &nfr) >= 0);
163         printf("i %d nfr %d\n", i, nfr);
164         if (nfr)
165                 memcpy(cepbuf2[i], cptr[0], nfr * DEFAULT_NUM_CEPSTRA * sizeof(**cptr));
166         ckd_free_2d(cptr);
167         i += nfr;
168         inptr += 256;
169         TEST_ASSERT(fe_process_utt(fe, inptr, 256, &cptr, &nfr) >= 0);
170         printf("i %d nfr %d\n", i, nfr);
171         if (nfr)
172                 memcpy(cepbuf2[i], cptr[0], nfr * DEFAULT_NUM_CEPSTRA * sizeof(**cptr));
173         ckd_free_2d(cptr);
174         i += nfr;
175         inptr += 256;
176         TEST_ASSERT(fe_process_utt(fe, inptr, 256, &cptr, &nfr) >= 0);
177         printf("i %d nfr %d\n", i, nfr);
178         if (nfr)
179                 memcpy(cepbuf2[i], cptr[0], nfr * DEFAULT_NUM_CEPSTRA * sizeof(**cptr));
180         ckd_free_2d(cptr);
181         i += nfr;
182         inptr += 256;
183         TEST_ASSERT(fe_end_utt(fe, cepbuf2[i], &nfr) >= 0);
184         printf("i %d nfr %d\n", i, nfr);
185         TEST_EQUAL(nfr, 1);
186
187         for (i = 0; i < 5; ++i) {
188                 int j;
189                 printf("%d: ", i);
190                 for (j = 0; j < DEFAULT_NUM_CEPSTRA; ++j) {
191                         printf("%.2f,%.2f ",
192                                MFCC2FLOAT(cepbuf1[i][j]),
193                                MFCC2FLOAT(cepbuf2[i][j]));
194                         TEST_EQUAL_FLOAT(cepbuf1[i][j], cepbuf2[i][j]);
195                 }
196                 printf("\n");
197         }
198
199         ckd_free_2d(cepbuf1);
200         ckd_free_2d(cepbuf2);
201         fclose(raw);
202         fe_free(fe);
203
204         return 0;
205 }