134tmat_init(
char const *file_name, logmath_t *lmath, float64 tpfloor, int32 breport)
137 int32 n_src, n_dst, n_tmat;
139 int32 byteswap, chksum_present;
142 int32 i, j, k, tp_per_tmat;
143 char **argname, **argval;
148 E_INFO(
"Reading HMM transition probability matrices: %s\n",
154 if ((fp = fopen(file_name,
"rb")) == NULL)
155 E_FATAL_SYSTEM(
"Failed to open transition file '%s' for reading", file_name);
158 if (bio_readhdr(fp, &argname, &argval, &byteswap) < 0)
159 E_FATAL(
"Failed to read header from file '%s'\n", file_name);
163 for (i = 0; argname[i]; i++) {
164 if (strcmp(argname[i],
"version") == 0) {
165 if (strcmp(argval[i], TMAT_PARAM_VERSION) != 0)
166 E_WARN(
"Version mismatch(%s): %s, expecting %s\n",
167 file_name, argval[i], TMAT_PARAM_VERSION);
169 else if (strcmp(argname[i],
"chksum0") == 0) {
173 bio_hdrarg_free(argname, argval);
174 argname = argval = NULL;
179 if ((bio_fread(&n_tmat,
sizeof(int32), 1, fp, byteswap, &chksum)
181 || (bio_fread(&n_src,
sizeof(int32), 1, fp, byteswap, &chksum) !=
183 || (bio_fread(&n_dst,
sizeof(int32), 1, fp, byteswap, &chksum) !=
185 || (bio_fread(&i,
sizeof(int32), 1, fp, byteswap, &chksum) != 1)) {
186 E_FATAL(
"Failed to read header from '%s'\n", file_name);
188 if (n_tmat >= MAX_INT16)
189 E_FATAL(
"%s: Number of transition matrices (%d) exceeds limit (%d)\n", file_name,
193 if (n_dst != n_src + 1)
194 E_FATAL(
"%s: Unsupported transition matrix. Number of source states (%d) != number of target states (%d)-1\n", file_name,
198 if (i != t->
n_tmat * n_src * n_dst) {
200 (
"%s: Invalid transitions. Number of coefficients (%d) doesn't match expected array dimension: %d x %d x %d\n",
201 file_name, i, t->
n_tmat, n_src, n_dst);
205 t->
tp = ckd_calloc_3d(t->
n_tmat, n_src, n_dst,
sizeof(***t->
tp));
208 tp = ckd_calloc_2d(n_src, n_dst,
sizeof(**tp));
211 tp_per_tmat = n_src * n_dst;
212 for (i = 0; i < t->
n_tmat; i++) {
213 if (bio_fread(tp[0],
sizeof(float32), tp_per_tmat, fp,
214 byteswap, &chksum) != tp_per_tmat) {
215 E_FATAL(
"Failed to read transition matrix %d from '%s'\n", i, file_name);
219 for (j = 0; j < n_src; j++) {
220 if (vector_sum_norm(tp[j], n_dst) == 0.0)
221 E_WARN(
"Normalization failed for transition matrix %d from state %d\n",
223 vector_nz_floor(tp[j], n_dst, tpfloor);
224 vector_sum_norm(tp[j], n_dst);
227 for (k = 0; k < n_dst; k++) {
232 if (k >= j && k-j < 3 && tp[j][k] == 0.0f)
237 if (ltp > 255) ltp = 255;
238 t->
tp[i][j][k] = (uint8)ltp;
246 bio_verify_chksum(fp, byteswap, chksum);
248 if (fread(&tmp, 1, 1, fp) == 1)
249 E_ERROR(
"Non-empty file beyond end of data\n");
253 if (tmat_chk_uppertri(t, lmath) < 0)
254 E_FATAL(
"Tmat not upper triangular\n");
255 if (tmat_chk_1skip(t, lmath) < 0)
256 E_FATAL(
"Topology not Left-to-Right or Bakis\n");