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 "bio.h"
00106 #include "vector.h"
00107
00108 #define TMAT_PARAM_VERSION "1.0"
00109
00110
00115 static int32 tmat_chk_uppertri(tmat_t *tmat, logmath_t *lmath);
00116
00117
00124 static int32 tmat_chk_1skip(tmat_t *tmat, logmath_t *lmath);
00125
00126
00127 void
00128 tmat_dump(tmat_t * tmat, FILE * fp)
00129 {
00130 int32 i, src, dst;
00131
00132 for (i = 0; i < tmat->n_tmat; i++) {
00133 fprintf(fp, "TMAT %d = %d x %d\n", i, tmat->n_state,
00134 tmat->n_state + 1);
00135 for (src = 0; src < tmat->n_state; src++) {
00136 for (dst = 0; dst <= tmat->n_state; dst++)
00137 fprintf(fp, " %12d", tmat->tp[i][src][dst]);
00138 fprintf(fp, "\n");
00139 }
00140 fprintf(fp, "\n");
00141 }
00142 fflush(fp);
00143 }
00144
00145
00146
00147
00148
00149
00150 int32
00151 tmat_chk_uppertri(tmat_t * tmat, logmath_t *lmath)
00152 {
00153 int32 i, src, dst;
00154
00155
00156 for (i = 0; i < tmat->n_tmat; i++) {
00157 for (dst = 0; dst < tmat->n_state; dst++)
00158 for (src = dst + 1; src < tmat->n_state; src++)
00159 if (tmat->tp[i][src][dst] > logmath_get_zero(lmath)) {
00160 E_ERROR("tmat[%d][%d][%d] = %d\n",
00161 i, src, dst, tmat->tp[i][src][dst]);
00162 return -1;
00163 }
00164 }
00165
00166 return 0;
00167 }
00168
00169
00170 int32
00171 tmat_chk_1skip(tmat_t * tmat, logmath_t *lmath)
00172 {
00173 int32 i, src, dst;
00174
00175 for (i = 0; i < tmat->n_tmat; i++) {
00176 for (src = 0; src < tmat->n_state; src++)
00177 for (dst = src + 3; dst <= tmat->n_state; dst++)
00178 if (tmat->tp[i][src][dst] > logmath_get_zero(lmath)) {
00179 E_ERROR("tmat[%d][%d][%d] = %d\n",
00180 i, src, dst, tmat->tp[i][src][dst]);
00181 return -1;
00182 }
00183 }
00184
00185 return 0;
00186 }
00187
00188
00189 tmat_t *
00190 tmat_init(char const *file_name, logmath_t *lmath, float64 tpfloor, int32 breport)
00191 {
00192 char tmp;
00193 int32 n_src, n_dst;
00194 FILE *fp;
00195 int32 byteswap, chksum_present;
00196 uint32 chksum;
00197 float32 **tp;
00198 int32 i, j, k, tp_per_tmat;
00199 char **argname, **argval;
00200 tmat_t *t;
00201
00202
00203 if (breport) {
00204 E_INFO("Reading HMM transition probability matrices: %s\n",
00205 file_name);
00206 }
00207
00208 t = (tmat_t *) ckd_calloc(1, sizeof(tmat_t));
00209
00210 if ((fp = fopen(file_name, "rb")) == NULL)
00211 E_FATAL_SYSTEM("fopen(%s,rb) failed\n", file_name);
00212
00213
00214 if (bio_readhdr(fp, &argname, &argval, &byteswap) < 0)
00215 E_FATAL("bio_readhdr(%s) failed\n", file_name);
00216
00217
00218 chksum_present = 0;
00219 for (i = 0; argname[i]; i++) {
00220 if (strcmp(argname[i], "version") == 0) {
00221 if (strcmp(argval[i], TMAT_PARAM_VERSION) != 0)
00222 E_WARN("Version mismatch(%s): %s, expecting %s\n",
00223 file_name, argval[i], TMAT_PARAM_VERSION);
00224 }
00225 else if (strcmp(argname[i], "chksum0") == 0) {
00226 chksum_present = 1;
00227 }
00228 }
00229 bio_hdrarg_free(argname, argval);
00230 argname = argval = NULL;
00231
00232 chksum = 0;
00233
00234
00235 if ((bio_fread(&(t->n_tmat), sizeof(int32), 1, fp, byteswap, &chksum)
00236 != 1)
00237 || (bio_fread(&n_src, sizeof(int32), 1, fp, byteswap, &chksum) !=
00238 1)
00239 || (bio_fread(&n_dst, sizeof(int32), 1, fp, byteswap, &chksum) !=
00240 1)
00241 || (bio_fread(&i, sizeof(int32), 1, fp, byteswap, &chksum) != 1)) {
00242 E_FATAL("bio_fread(%s) (arraysize) failed\n", file_name);
00243 }
00244 if (t->n_tmat >= MAX_INT32)
00245 E_FATAL("%s: #tmat (%d) exceeds limit (%d)\n", file_name,
00246 t->n_tmat, MAX_INT32);
00247 if (n_dst != n_src + 1)
00248 E_FATAL("%s: #from-states(%d) != #to-states(%d)-1\n", file_name,
00249 n_src, n_dst);
00250 t->n_state = n_src;
00251
00252 if (i != t->n_tmat * n_src * n_dst) {
00253 E_FATAL
00254 ("%s: #float32s(%d) doesn't match dimensions: %d x %d x %d\n",
00255 file_name, i, t->n_tmat, n_src, n_dst);
00256 }
00257
00258
00259 t->tp =
00260 (int32 ***) ckd_calloc_3d(t->n_tmat, n_src, n_dst, sizeof(int32));
00261
00262
00263 tp = (float32 **) ckd_calloc_2d(n_src, n_dst, sizeof(float32));
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
00284
00285 if (k >= j && k-j < 3 && tp[j][k] == 0.0f)
00286 tp[j][k] = (float32)tpfloor;
00287 t->tp[i][j][k] = logmath_log(lmath, tp[j][k]);
00288 }
00289 }
00290 }
00291
00292 ckd_free_2d((void **) tp);
00293
00294 if (chksum_present)
00295 bio_verify_chksum(fp, byteswap, chksum);
00296
00297 if (fread(&tmp, 1, 1, fp) == 1)
00298 E_ERROR("Non-empty file beyond end of data\n");
00299
00300 fclose(fp);
00301
00302
00303 if (tmat_chk_uppertri(t, lmath) < 0)
00304 E_FATAL("Tmat not upper triangular\n");
00305 if (tmat_chk_1skip(t, lmath) < 0)
00306 E_FATAL("Topology not Left-to-Right or Bakis\n");
00307
00308 return t;
00309 }
00310
00311 void
00312 tmat_report(tmat_t * t)
00313 {
00314 E_INFO_NOFN("Initialization of tmat_t, report:\n");
00315 E_INFO_NOFN("Read %d transition matrices of size %dx%d\n",
00316 t->n_tmat, t->n_state, t->n_state + 1);
00317 E_INFO_NOFN("\n");
00318
00319 }
00320
00321
00322
00323
00324 void
00325 tmat_free(tmat_t * t)
00326 {
00327 if (t) {
00328 if (t->tp)
00329 ckd_free_3d((void ***) t->tp);
00330 ckd_free((void *) t);
00331 }
00332 }