00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096 #include <string.h>
00097
00098
00099 #include <logmath.h>
00100 #include <err.h>
00101 #include <ckd_alloc.h>
00102
00103
00104 #include "tmat.h"
00105 #include "hmm.h"
00106 #include "bio.h"
00107 #include "vector.h"
00108
00109 #define TMAT_PARAM_VERSION "1.0"
00110
00111
00116 static int32 tmat_chk_uppertri(tmat_t *tmat, logmath_t *lmath);
00117
00118
00125 static int32 tmat_chk_1skip(tmat_t *tmat, logmath_t *lmath);
00126
00127
00128 void
00129 tmat_dump(tmat_t * tmat, FILE * fp)
00130 {
00131 int32 i, src, dst;
00132
00133 for (i = 0; i < tmat->n_tmat; i++) {
00134 fprintf(fp, "TMAT %d = %d x %d\n", i, tmat->n_state,
00135 tmat->n_state + 1);
00136 for (src = 0; src < tmat->n_state; src++) {
00137 for (dst = 0; dst <= tmat->n_state; dst++)
00138 fprintf(fp, " %12d", tmat->tp[i][src][dst]);
00139 fprintf(fp, "\n");
00140 }
00141 fprintf(fp, "\n");
00142 }
00143 fflush(fp);
00144 }
00145
00146
00147
00148
00149
00150
00151 int32
00152 tmat_chk_uppertri(tmat_t * tmat, logmath_t *lmath)
00153 {
00154 int32 i, src, dst;
00155
00156
00157 for (i = 0; i < tmat->n_tmat; i++) {
00158 for (dst = 0; dst < tmat->n_state; dst++)
00159 for (src = dst + 1; src < tmat->n_state; src++)
00160 if (tmat->tp[i][src][dst] < 255) {
00161 E_ERROR("tmat[%d][%d][%d] = %d\n",
00162 i, src, dst, tmat->tp[i][src][dst]);
00163 return -1;
00164 }
00165 }
00166
00167 return 0;
00168 }
00169
00170
00171 int32
00172 tmat_chk_1skip(tmat_t * tmat, logmath_t *lmath)
00173 {
00174 int32 i, src, dst;
00175
00176 for (i = 0; i < tmat->n_tmat; i++) {
00177 for (src = 0; src < tmat->n_state; src++)
00178 for (dst = src + 3; dst <= tmat->n_state; dst++)
00179 if (tmat->tp[i][src][dst] < 255) {
00180 E_ERROR("tmat[%d][%d][%d] = %d\n",
00181 i, src, dst, tmat->tp[i][src][dst]);
00182 return -1;
00183 }
00184 }
00185
00186 return 0;
00187 }
00188
00189
00190 tmat_t *
00191 tmat_init(char const *file_name, logmath_t *lmath, float64 tpfloor, int32 breport)
00192 {
00193 char tmp;
00194 int32 n_src, n_dst;
00195 FILE *fp;
00196 int32 byteswap, chksum_present;
00197 uint32 chksum;
00198 float32 **tp;
00199 int32 i, j, k, tp_per_tmat;
00200 char **argname, **argval;
00201 tmat_t *t;
00202
00203
00204 if (breport) {
00205 E_INFO("Reading HMM transition probability matrices: %s\n",
00206 file_name);
00207 }
00208
00209 t = (tmat_t *) ckd_calloc(1, sizeof(tmat_t));
00210
00211 if ((fp = fopen(file_name, "rb")) == NULL)
00212 E_FATAL_SYSTEM("fopen(%s,rb) failed\n", file_name);
00213
00214
00215 if (bio_readhdr(fp, &argname, &argval, &byteswap) < 0)
00216 E_FATAL("bio_readhdr(%s) failed\n", file_name);
00217
00218
00219 chksum_present = 0;
00220 for (i = 0; argname[i]; i++) {
00221 if (strcmp(argname[i], "version") == 0) {
00222 if (strcmp(argval[i], TMAT_PARAM_VERSION) != 0)
00223 E_WARN("Version mismatch(%s): %s, expecting %s\n",
00224 file_name, argval[i], TMAT_PARAM_VERSION);
00225 }
00226 else if (strcmp(argname[i], "chksum0") == 0) {
00227 chksum_present = 1;
00228 }
00229 }
00230 bio_hdrarg_free(argname, argval);
00231 argname = argval = NULL;
00232
00233 chksum = 0;
00234
00235
00236 if ((bio_fread(&(t->n_tmat), sizeof(int32), 1, fp, byteswap, &chksum)
00237 != 1)
00238 || (bio_fread(&n_src, sizeof(int32), 1, fp, byteswap, &chksum) !=
00239 1)
00240 || (bio_fread(&n_dst, sizeof(int32), 1, fp, byteswap, &chksum) !=
00241 1)
00242 || (bio_fread(&i, sizeof(int32), 1, fp, byteswap, &chksum) != 1)) {
00243 E_FATAL("bio_fread(%s) (arraysize) failed\n", file_name);
00244 }
00245 if (t->n_tmat >= MAX_INT16)
00246 E_FATAL("%s: #tmat (%d) exceeds limit (%d)\n", file_name,
00247 t->n_tmat, MAX_INT16);
00248 if (n_dst != n_src + 1)
00249 E_FATAL("%s: #from-states(%d) != #to-states(%d)-1\n", file_name,
00250 n_src, n_dst);
00251 t->n_state = n_src;
00252
00253 if (i != t->n_tmat * n_src * n_dst) {
00254 E_FATAL
00255 ("%s: #float32s(%d) doesn't match dimensions: %d x %d x %d\n",
00256 file_name, i, t->n_tmat, n_src, n_dst);
00257 }
00258
00259
00260 t->tp = ckd_calloc_3d(t->n_tmat, n_src, n_dst, sizeof(***t->tp));
00261
00262
00263 tp = ckd_calloc_2d(n_src, n_dst, sizeof(**tp));
00264
00265
00266 tp_per_tmat = n_src * n_dst;
00267 for (i = 0; i < t->n_tmat; i++) {
00268 if (bio_fread(tp[0], sizeof(float32), tp_per_tmat, fp,
00269 byteswap, &chksum) != tp_per_tmat) {
00270 E_FATAL("fread(%s) (arraydata) failed\n", file_name);
00271 }
00272
00273
00274 for (j = 0; j < n_src; j++) {
00275 if (vector_sum_norm(tp[j], n_dst) == 0.0)
00276 E_WARN("Normalization failed for tmat %d from state %d\n",
00277 i, j);
00278 vector_nz_floor(tp[j], n_dst, tpfloor);
00279 vector_sum_norm(tp[j], n_dst);
00280
00281
00282 for (k = 0; k < n_dst; k++) {
00283 int ltp;
00284 #if 0
00285
00286
00287 if (k >= j && k-j < 3 && tp[j][k] == 0.0f)
00288 tp[j][k] = tpfloor;
00289 #endif
00290
00291 ltp = -logmath_log(lmath, tp[j][k]) >> SENSCR_SHIFT;
00292 if (ltp > 255) ltp = 255;
00293 t->tp[i][j][k] = (uint8)ltp;
00294 }
00295 }
00296 }
00297
00298 ckd_free_2d(tp);
00299
00300 if (chksum_present)
00301 bio_verify_chksum(fp, byteswap, chksum);
00302
00303 if (fread(&tmp, 1, 1, fp) == 1)
00304 E_ERROR("Non-empty file beyond end of data\n");
00305
00306 fclose(fp);
00307
00308 if (tmat_chk_uppertri(t, lmath) < 0)
00309 E_FATAL("Tmat not upper triangular\n");
00310 if (tmat_chk_1skip(t, lmath) < 0)
00311 E_FATAL("Topology not Left-to-Right or Bakis\n");
00312
00313 return t;
00314 }
00315
00316 void
00317 tmat_report(tmat_t * t)
00318 {
00319 E_INFO_NOFN("Initialization of tmat_t, report:\n");
00320 E_INFO_NOFN("Read %d transition matrices of size %dx%d\n",
00321 t->n_tmat, t->n_state, t->n_state + 1);
00322 E_INFO_NOFN("\n");
00323
00324 }
00325
00326
00327
00328
00329 void
00330 tmat_free(tmat_t * t)
00331 {
00332 if (t) {
00333 if (t->tp)
00334 ckd_free_3d(t->tp);
00335 ckd_free(t);
00336 }
00337 }