1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | |
14 | |
15 | |
16 | |
17 | |
18 | |
19 | |
20 | |
21 | |
22 | |
23 | |
24 | |
25 | |
26 | |
27 | |
28 | |
29 | |
30 | |
31 | |
32 | |
33 | |
34 | |
35 | |
36 | |
37 | |
38 | |
39 | |
40 | |
41 | |
42 | |
43 | |
44 | |
45 | #ifndef HTS_PSTREAM_C |
46 | #define HTS_PSTREAM_C |
47 | |
48 | #ifdef __cplusplus |
49 | #define HTS_PSTREAM_C_START extern "C" { |
50 | #define HTS_PSTREAM_C_END } |
51 | #else |
52 | #define HTS_PSTREAM_C_START |
53 | #define HTS_PSTREAM_C_END |
54 | #endif /* __CPLUSPLUS */ |
55 | |
56 | HTS_PSTREAM_C_START; |
57 | |
58 | #include <math.h> /* for sqrt() */ |
59 | |
60 | |
61 | #include "HTS_hidden.h" |
62 | |
63 | |
64 | static double HTS_finv(const double x) |
65 | { |
66 | if (x >= INFTY2((double) 1.0e+19)) |
67 | return 0.0; |
68 | if (x <= -INFTY2((double) 1.0e+19)) |
69 | return 0.0; |
70 | if (x <= INVINF2((double) 1.0e-19) && x >= 0) |
71 | return INFTY((double) 1.0e+38); |
72 | if (x >= -INVINF2((double) 1.0e-19) && x < 0) |
73 | return -INFTY((double) 1.0e+38); |
74 | |
75 | return (1.0 / x); |
76 | } |
77 | |
78 | |
79 | static void HTS_PStream_calc_wuw_and_wum(HTS_PStream * pst, const int m) |
80 | { |
81 | int t, i, j, k; |
82 | double wu; |
83 | |
84 | for (t = 0; t < pst->length; t++) { |
85 | |
86 | pst->sm.wum[t] = 0.0; |
87 | for (i = 0; i < pst->width; i++) |
88 | pst->sm.wuw[t][i] = 0.0; |
89 | |
90 | |
91 | for (i = 0; i < pst->win_size; i++) |
92 | for (j = pst->win_l_width[i]; j <= pst->win_r_width[i]; j++) |
93 | if ((t + j >= 0) && (t + j < pst->length) |
94 | && (pst->win_coefficient[i][-j] != 0.0)) { |
95 | wu = pst->win_coefficient[i][-j] * |
96 | pst->sm.ivar[t + j][i * pst->static_length + m]; |
97 | pst->sm.wum[t] += |
98 | wu * pst->sm.mean[t + j][i * pst->static_length + m]; |
99 | for (k = 0; (k < pst->width) && (t + k < pst->length); k++) |
100 | if ((k - j <= pst->win_r_width[i]) |
101 | && (pst->win_coefficient[i][k - j] != 0.0)) |
102 | pst->sm.wuw[t][k] += wu * pst->win_coefficient[i][k - j]; |
103 | } |
104 | } |
105 | } |
106 | |
107 | |
108 | |
109 | static void HTS_PStream_ldl_factorization(HTS_PStream * pst) |
110 | { |
111 | int t, i, j; |
112 | |
113 | for (t = 0; t < pst->length; t++) { |
114 | for (i = 1; (i < pst->width) && (t >= i); i++) |
115 | pst->sm.wuw[t][0] -= pst->sm.wuw[t - i][i] * |
116 | pst->sm.wuw[t - i][i] * pst->sm.wuw[t - i][0]; |
117 | |
118 | for (i = 1; i < pst->width; i++) { |
119 | for (j = 1; (i + j < pst->width) && (t >= j); j++) |
120 | pst->sm.wuw[t][i] -= pst->sm.wuw[t - j][j] * |
121 | pst->sm.wuw[t - j][i + j] * pst->sm.wuw[t - j][0]; |
122 | pst->sm.wuw[t][i] /= pst->sm.wuw[t][0]; |
123 | } |
124 | } |
125 | } |
126 | |
127 | |
128 | static void HTS_PStream_forward_substitution(HTS_PStream * pst) |
129 | { |
130 | int t, i; |
131 | |
132 | for (t = 0; t < pst->length; t++) { |
133 | pst->sm.g[t] = pst->sm.wum[t]; |
134 | for (i = 1; (i < pst->width) && (t >= i); i++) |
135 | pst->sm.g[t] -= pst->sm.wuw[t - i][i] * pst->sm.g[t - i]; |
136 | } |
137 | } |
138 | |
139 | |
140 | static void HTS_PStream_backward_substitution(HTS_PStream * pst, const int m) |
141 | { |
142 | int t, i; |
143 | |
144 | for (t = pst->length - 1; t >= 0; t--) { |
145 | pst->par[t][m] = pst->sm.g[t] / pst->sm.wuw[t][0]; |
146 | for (i = 1; (i < pst->width) && (t + i < pst->length); i++) |
147 | pst->par[t][m] -= pst->sm.wuw[t][i] * pst->par[t + i][m]; |
148 | } |
149 | } |
150 | |
151 | |
152 | static void HTS_PStream_calc_gv(HTS_PStream * pst, const int m, double *mean, |
153 | double *vari) |
154 | { |
155 | int t; |
156 | |
157 | *mean = 0.0; |
158 | for (t = 0; t < pst->length; t++) |
159 | if (pst->gv_switch[t]) |
160 | *mean += pst->par[t][m]; |
161 | *mean /= pst->gv_length; |
162 | *vari = 0.0; |
163 | for (t = 0; t < pst->length; t++) |
164 | if (pst->gv_switch[t]) |
165 | *vari += (pst->par[t][m] - *mean) * (pst->par[t][m] - *mean); |
166 | *vari /= pst->gv_length; |
167 | } |
168 | |
169 | |
170 | static void HTS_PStream_conv_gv(HTS_PStream * pst, const int m) |
171 | { |
172 | int t; |
173 | double ratio; |
174 | double mean; |
175 | double vari; |
176 | |
177 | HTS_PStream_calc_gv(pst, m, &mean, &vari); |
178 | ratio = sqrt(pst->gv_mean[m] / vari); |
179 | for (t = 0; t < pst->length; t++) |
180 | if (pst->gv_switch[t]) |
181 | pst->par[t][m] = ratio * (pst->par[t][m] - mean) + mean; |
182 | } |
183 | |
184 | |
185 | static double HTS_PStream_calc_derivative(HTS_PStream * pst, const int m) |
186 | { |
187 | int t, i; |
188 | double mean; |
189 | double vari; |
190 | double dv; |
191 | double h; |
192 | double gvobj; |
193 | double hmmobj; |
194 | const double w = 1.0 / (pst->win_size * pst->length); |
195 | |
196 | HTS_PStream_calc_gv(pst, m, &mean, &vari); |
197 | gvobj = -0.5 * W21.0 * vari * pst->gv_vari[m] * (vari - 2.0 * pst->gv_mean[m]); |
198 | dv = -2.0 * pst->gv_vari[m] * (vari - pst->gv_mean[m]) / pst->length; |
199 | |
200 | for (t = 0; t < pst->length; t++) { |
201 | pst->sm.g[t] = pst->sm.wuw[t][0] * pst->par[t][m]; |
202 | for (i = 1; i < pst->width; i++) { |
203 | if (t + i < pst->length) |
204 | pst->sm.g[t] += pst->sm.wuw[t][i] * pst->par[t + i][m]; |
205 | if (t + 1 > i) |
206 | pst->sm.g[t] += pst->sm.wuw[t - i][i] * pst->par[t - i][m]; |
207 | } |
208 | } |
209 | |
210 | for (t = 0, hmmobj = 0.0; t < pst->length; t++) { |
211 | hmmobj += W11.0 * w * pst->par[t][m] * (pst->sm.wum[t] - 0.5 * pst->sm.g[t]); |
212 | h = -W11.0 * w * pst->sm.wuw[t][1 - 1] |
213 | - W21.0 * 2.0 / (pst->length * pst->length) * |
214 | ((pst->length - 1) * pst->gv_vari[m] * (vari - pst->gv_mean[m]) |
215 | + 2.0 * pst->gv_vari[m] * (pst->par[t][m] - mean) * (pst->par[t][m] - |
216 | mean)); |
217 | if (pst->gv_switch[t]) |
218 | pst->sm.g[t] = |
219 | 1.0 / h * (W11.0 * w * (-pst->sm.g[t] + pst->sm.wum[t]) + |
220 | W21.0 * dv * (pst->par[t][m] - mean)); |
221 | else |
222 | pst->sm.g[t] = 1.0 / h * (W11.0 * w * (-pst->sm.g[t] + pst->sm.wum[t])); |
223 | } |
224 | |
225 | return (-(hmmobj + gvobj)); |
226 | } |
227 | |
228 | |
229 | static void HTS_PStream_gv_parmgen(HTS_PStream * pst, const int m) |
230 | { |
231 | int t, i; |
232 | double step = STEPINIT0.1; |
233 | double prev = -LZERO(-1.0e+10); |
234 | double obj; |
235 | |
236 | if (pst->gv_length == 0) |
237 | return; |
238 | |
239 | HTS_PStream_conv_gv(pst, m); |
240 | if (GV_MAX_ITERATION5 > 0) { |
241 | HTS_PStream_calc_wuw_and_wum(pst, m); |
242 | for (i = 1; i <= GV_MAX_ITERATION5; i++) { |
243 | obj = HTS_PStream_calc_derivative(pst, m); |
244 | if (obj > prev) |
245 | step *= STEPDEC0.5; |
246 | if (obj < prev) |
247 | step *= STEPINC1.2; |
248 | for (t = 0; t < pst->length; t++) |
249 | pst->par[t][m] += step * pst->sm.g[t]; |
250 | prev = obj; |
251 | } |
252 | } |
253 | } |
254 | |
255 | |
256 | static void HTS_PStream_mlpg(HTS_PStream * pst) |
257 | { |
258 | int m; |
259 | |
260 | if (pst->length == 0) |
261 | return; |
262 | |
263 | for (m = 0; m < pst->static_length; m++) { |
264 | HTS_PStream_calc_wuw_and_wum(pst, m); |
265 | HTS_PStream_ldl_factorization(pst); |
266 | HTS_PStream_forward_substitution(pst); |
267 | HTS_PStream_backward_substitution(pst, m); |
268 | if (pst->gv_length > 0) |
269 | HTS_PStream_gv_parmgen(pst, m); |
270 | } |
271 | } |
272 | |
273 | |
274 | void HTS_PStreamSet_initialize(HTS_PStreamSet * pss) |
275 | { |
276 | pss->pstream = NULL((void*)0); |
277 | pss->nstream = 0; |
278 | pss->total_frame = 0; |
279 | } |
280 | |
281 | |
282 | void HTS_PStreamSet_create(HTS_PStreamSet * pss, HTS_SStreamSet * sss, |
283 | double *msd_threshold, double *gv_weight) |
284 | { |
285 | int i, j, k, l, m; |
286 | int frame, msd_frame, state; |
287 | |
288 | HTS_PStream *pst; |
289 | HTS_Boolean not_bound; |
290 | |
291 | if (pss->nstream) |
| |
292 | HTS_error(1, "HTS_PstreamSet_create: HTS_PStreamSet should be clear.\n"); |
293 | |
294 | |
295 | pss->nstream = HTS_SStreamSet_get_nstream(sss); |
296 | pss->pstream = (HTS_PStream *) HTS_calloc(pss->nstream, sizeof(HTS_PStream)); |
297 | pss->total_frame = HTS_SStreamSet_get_total_frame(sss); |
298 | |
299 | |
300 | for (i = 0; i < pss->nstream; i++) { |
| 2 | | Loop condition is true. Entering loop body | |
|
| 8 | | Loop condition is true. Entering loop body | |
|
| 14 | | Loop condition is true. Entering loop body | |
|
| 20 | | Loop condition is true. Entering loop body | |
|
301 | pst = &pss->pstream[i]; |
302 | if (HTS_SStreamSet_is_msd(sss, i)) { |
| |
| |
| |
| |
303 | pst->length = 0; |
304 | for (state = 0; state < HTS_SStreamSet_get_total_state(sss); state++) |
305 | if (HTS_SStreamSet_get_msd(sss, i, state) > msd_threshold[i]) |
306 | pst->length += HTS_SStreamSet_get_duration(sss, state); |
307 | pst->msd_flag = |
308 | (HTS_Boolean *) HTS_calloc(pss->total_frame, sizeof(HTS_Boolean)); |
309 | for (state = 0, frame = 0; state < HTS_SStreamSet_get_total_state(sss); |
310 | state++) |
311 | if (HTS_SStreamSet_get_msd(sss, i, state) > msd_threshold[i]) |
312 | for (j = 0; j < HTS_SStreamSet_get_duration(sss, state); j++) { |
313 | pst->msd_flag[frame] = TRUE1; |
314 | frame++; |
315 | } else |
316 | for (j = 0; j < HTS_SStreamSet_get_duration(sss, state); j++) { |
317 | pst->msd_flag[frame] = FALSE0; |
318 | frame++; |
319 | } |
320 | } else { |
321 | pst->length = pss->total_frame; |
322 | pst->msd_flag = NULL((void*)0); |
| 22 | | Null pointer value stored to field 'msd_flag' | |
|
323 | } |
324 | pst->vector_length = HTS_SStreamSet_get_vector_length(sss, i); |
325 | pst->width = HTS_SStreamSet_get_window_max_width(sss, i) * 2 + 1; |
326 | pst->win_size = HTS_SStreamSet_get_window_size(sss, i); |
327 | pst->static_length = pst->vector_length / pst->win_size; |
328 | pst->sm.mean = HTS_alloc_matrix(pst->length, pst->vector_length); |
329 | pst->sm.ivar = HTS_alloc_matrix(pst->length, pst->vector_length); |
330 | pst->sm.wum = (double *) HTS_calloc(pst->length, sizeof(double)); |
331 | pst->sm.wuw = HTS_alloc_matrix(pst->length, pst->width); |
332 | pst->sm.g = (double *) HTS_calloc(pst->length, sizeof(double)); |
333 | pst->par = HTS_alloc_matrix(pst->length, pst->static_length); |
334 | |
335 | pst->win_l_width = (int *) HTS_calloc(pst->win_size, sizeof(int)); |
336 | pst->win_r_width = (int *) HTS_calloc(pst->win_size, sizeof(int)); |
337 | pst->win_coefficient = |
338 | (double **) HTS_calloc(pst->win_size, sizeof(double *)); |
339 | for (j = 0; j < pst->win_size; j++) { |
| 4 | | Loop condition is false. Execution continues on line 354 | |
|
| 10 | | Loop condition is false. Execution continues on line 354 | |
|
| 16 | | Loop condition is false. Execution continues on line 354 | |
|
| 23 | | Loop condition is false. Execution continues on line 354 | |
|
340 | pst->win_l_width[j] = HTS_SStreamSet_get_window_left_width(sss, i, j); |
341 | pst->win_r_width[j] = HTS_SStreamSet_get_window_right_width(sss, i, j); |
342 | if (pst->win_l_width[j] + pst->win_r_width[j] == 0) |
343 | pst->win_coefficient[j] = (double *) |
344 | HTS_calloc(-2 * pst->win_l_width[j] + 1, sizeof(double)); |
345 | else |
346 | pst->win_coefficient[j] = (double *) |
347 | HTS_calloc(-2 * pst->win_l_width[j], sizeof(double)); |
348 | pst->win_coefficient[j] -= pst->win_l_width[j]; |
349 | for (k = pst->win_l_width[j]; k <= pst->win_r_width[j]; k++) |
350 | pst->win_coefficient[j][k] = |
351 | HTS_SStreamSet_get_window_coefficient(sss, i, j, k); |
352 | } |
353 | |
354 | if (HTS_SStreamSet_use_gv(sss, i)) { |
| |
| |
| |
| |
355 | pst->gv_mean = |
356 | (double *) HTS_calloc(pst->static_length, sizeof(double)); |
357 | pst->gv_vari = |
358 | (double *) HTS_calloc(pst->static_length, sizeof(double)); |
359 | for (j = 0; j < pst->static_length; j++) { |
360 | pst->gv_mean[j] = |
361 | HTS_SStreamSet_get_gv_mean(sss, i, j) * gv_weight[i]; |
362 | pst->gv_vari[j] = HTS_SStreamSet_get_gv_vari(sss, i, j); |
363 | } |
364 | pst->gv_switch = |
365 | (HTS_Boolean *) HTS_calloc(pst->length, sizeof(HTS_Boolean)); |
366 | if (HTS_SStreamSet_is_msd(sss, i)) { |
367 | for (state = 0, frame = 0, msd_frame = 0; |
368 | state < HTS_SStreamSet_get_total_state(sss); state++) |
369 | for (j = 0; j < HTS_SStreamSet_get_duration(sss, state); |
370 | j++, frame++) |
371 | if (pst->msd_flag[frame]) |
372 | pst->gv_switch[msd_frame++] = |
373 | HTS_SStreamSet_get_gv_switch(sss, i, state); |
374 | } else { |
375 | for (state = 0, frame = 0; |
376 | state < HTS_SStreamSet_get_total_state(sss); state++) |
377 | for (j = 0; j < HTS_SStreamSet_get_duration(sss, state); j++) |
378 | pst->gv_switch[frame++] = |
379 | HTS_SStreamSet_get_gv_switch(sss, i, state); |
380 | } |
381 | for (j = 0, pst->gv_length = 0; j < pst->length; j++) |
382 | if (pst->gv_switch[j]) |
383 | pst->gv_length++; |
384 | } else { |
385 | pst->gv_switch = NULL((void*)0); |
386 | pst->gv_length = 0; |
387 | pst->gv_mean = NULL((void*)0); |
388 | pst->gv_vari = NULL((void*)0); |
389 | } |
390 | |
391 | if (HTS_SStreamSet_is_msd(sss, i)) { |
| |
| |
| |
| |
392 | for (state = 0, frame = 0, msd_frame = 0; |
| 26 | | Loop condition is true. Entering loop body | |
|
393 | state < HTS_SStreamSet_get_total_state(sss); state++) |
394 | for (j = 0; j < HTS_SStreamSet_get_duration(sss, state); j++) { |
| 27 | | Loop condition is true. Entering loop body | |
|
395 | if (pst->msd_flag[frame]) { |
| 28 | | Array access (via field 'msd_flag') results in a null pointer dereference |
|
396 | |
397 | for (k = 0; k < pst->win_size; k++) { |
398 | not_bound = TRUE1; |
399 | for (l = pst->win_l_width[k]; l <= pst->win_r_width[k]; |
400 | l++) |
401 | if (frame + l < 0 || pss->total_frame <= frame + l |
402 | || !pst->msd_flag[frame + l]) { |
403 | not_bound = FALSE0; |
404 | break; |
405 | } |
406 | for (l = 0; l < pst->static_length; l++) { |
407 | m = pst->static_length * k + l; |
408 | pst->sm.mean[msd_frame][m] = |
409 | HTS_SStreamSet_get_mean(sss, i, state, m); |
410 | if (not_bound || k == 0) |
411 | pst->sm.ivar[msd_frame][m] = |
412 | HTS_finv(HTS_SStreamSet_get_vari |
413 | (sss, i, state, m)); |
414 | else |
415 | pst->sm.ivar[msd_frame][m] = 0.0; |
416 | } |
417 | } |
418 | msd_frame++; |
419 | } |
420 | frame++; |
421 | } |
422 | } else { |
423 | for (state = 0, frame = 0; |
| 7 | | Loop condition is false. Execution continues on line 449 | |
|
| 13 | | Loop condition is false. Execution continues on line 449 | |
|
| 19 | | Loop condition is false. Execution continues on line 449 | |
|
424 | state < HTS_SStreamSet_get_total_state(sss); state++) { |
425 | for (j = 0; j < HTS_SStreamSet_get_duration(sss, state); j++) { |
426 | for (k = 0; k < pst->win_size; k++) { |
427 | not_bound = TRUE1; |
428 | for (l = pst->win_l_width[k]; l <= pst->win_r_width[k]; l++) |
429 | if (frame + l < 0 || pss->total_frame <= frame + l) { |
430 | not_bound = FALSE0; |
431 | break; |
432 | } |
433 | for (l = 0; l < pst->static_length; l++) { |
434 | m = pst->static_length * k + l; |
435 | pst->sm.mean[frame][m] = |
436 | HTS_SStreamSet_get_mean(sss, i, state, m); |
437 | if (not_bound || k == 0) |
438 | pst->sm.ivar[frame][m] = |
439 | HTS_finv(HTS_SStreamSet_get_vari(sss, i, state, m)); |
440 | else |
441 | pst->sm.ivar[frame][m] = 0.0; |
442 | } |
443 | } |
444 | frame++; |
445 | } |
446 | } |
447 | } |
448 | |
449 | HTS_PStream_mlpg(pst); |
450 | } |
451 | } |
452 | |
453 | |
454 | int HTS_PStreamSet_get_nstream(HTS_PStreamSet * pss) |
455 | { |
456 | return pss->nstream; |
457 | } |
458 | |
459 | |
460 | int HTS_PStreamSet_get_static_length(HTS_PStreamSet * pss, int stream_index) |
461 | { |
462 | return pss->pstream[stream_index].static_length; |
463 | } |
464 | |
465 | |
466 | int HTS_PStreamSet_get_total_frame(HTS_PStreamSet * pss) |
467 | { |
468 | return pss->total_frame; |
469 | } |
470 | |
471 | |
472 | double HTS_PStreamSet_get_parameter(HTS_PStreamSet * pss, |
473 | int stream_index, int frame_index, |
474 | int vector_index) |
475 | { |
476 | return pss->pstream[stream_index].par[frame_index][vector_index]; |
477 | } |
478 | |
479 | |
480 | double *HTS_PStreamSet_get_parameter_vector(HTS_PStreamSet * pss, |
481 | int stream_index, int frame_index) |
482 | { |
483 | return pss->pstream[stream_index].par[frame_index]; |
484 | } |
485 | |
486 | |
487 | HTS_Boolean HTS_PStreamSet_get_msd_flag(HTS_PStreamSet * pss, |
488 | int stream_index, int frame_index) |
489 | { |
490 | return pss->pstream[stream_index].msd_flag[frame_index]; |
491 | } |
492 | |
493 | |
494 | HTS_Boolean HTS_PStreamSet_is_msd(HTS_PStreamSet * pss, int stream_index) |
495 | { |
496 | return pss->pstream[stream_index].msd_flag ? TRUE1 : FALSE0; |
497 | } |
498 | |
499 | |
500 | void HTS_PStreamSet_clear(HTS_PStreamSet * pss) |
501 | { |
502 | int i, j; |
503 | HTS_PStream *pstream; |
504 | |
505 | if (pss->pstream) { |
506 | for (i = 0; i < pss->nstream; i++) { |
507 | pstream = &pss->pstream[i]; |
508 | HTS_free(pstream->sm.wum); |
509 | HTS_free(pstream->sm.g); |
510 | HTS_free_matrix(pstream->sm.wuw, pstream->length); |
511 | HTS_free_matrix(pstream->sm.ivar, pstream->length); |
512 | HTS_free_matrix(pstream->sm.mean, pstream->length); |
513 | HTS_free_matrix(pstream->par, pstream->length); |
514 | if (pstream->msd_flag) |
515 | HTS_free(pstream->msd_flag); |
516 | for (j = pstream->win_size - 1; j >= 0; j--) { |
517 | pstream->win_coefficient[j] += pstream->win_l_width[j]; |
518 | HTS_free(pstream->win_coefficient[j]); |
519 | } |
520 | if (pstream->gv_mean) |
521 | HTS_free(pstream->gv_mean); |
522 | if (pstream->gv_vari) |
523 | HTS_free(pstream->gv_vari); |
524 | HTS_free(pstream->win_coefficient); |
525 | HTS_free(pstream->win_l_width); |
526 | HTS_free(pstream->win_r_width); |
527 | if (pstream->gv_switch) |
528 | HTS_free(pstream->gv_switch); |
529 | } |
530 | HTS_free(pss->pstream); |
531 | } |
532 | HTS_PStreamSet_initialize(pss); |
533 | } |
534 | |
535 | HTS_PSTREAM_C_END; |
536 | |
537 | #endif /* !HTS_PSTREAM_C */ |