-
Notifications
You must be signed in to change notification settings - Fork 0
/
HiddenMarkovModel.m
119 lines (106 loc) · 4.53 KB
/
HiddenMarkovModel.m
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
classdef HiddenMarkovModel < handle
% HiddenMarkovModel class
% Covers all HMM related functionality
properties
trans_model
obs_model
init_distribution
n_states
end
methods
function obj = HiddenMarkovModel(transition_model, observation_model, ...
initial_distribution)
obj.trans_model = transition_model;
obj.obs_model = observation_model;
obj.n_states = obj.trans_model.state_space.n_states;
if exist('initial_distribution', 'var')
obj.init_distribution = initial_distribution;
else
% assume uniform distribution
obj.init_distribution = ones(obj.n_states, 1) / obj.n_states;
end
end
function path = viterbi(obj, observations, use_mex_viterbi)
if ~exist('use_mex_viterbi', 'var')
use_mex_viterbi = 1;
end
% compute observation likelihoods
obs_lik = obj.obs_model.compute_obs_lik(observations);
if use_mex_viterbi
try
path = obj.viterbi_mex(obs_lik);
catch
fprintf(['\n WARNING: viterbi.cpp has to be', ...
'compiled, using the pure MATLAB version', ...
'instead\n']);
path = obj.viterbi_matlab(obs_lik);
end
else
path = obj.viterbi_matlab(obs_lik);
end
end
end
methods (Access = protected)
function path = viterbi_matlab(obj, obs_lik)
% [ bestpath, delta, loglik ] = viterbi_cont_int( A, obslik, y,
% init_distribution)
% Implementation of the Viterbi algorithm
% ----------------------------------------------------------------------
%INPUT parameter:
% obj.trans_model.A : transition matrix
% obslik : observation likelihood [R x nBarGridSize x nFrames]
% obj.init_distribution : initial state probabilities
%
%OUTPUT parameter:
% bestpath : MAP state sequence
%
% 26.7.2012 by Florian Krebs
% ------------------------------------------------------------------
n_state_ids = ...
length(obj.trans_model.state_space.position_from_state);
n_frames = size(obs_lik, 2);
delta = obj.init_distribution;
A = obj.trans_model.A;
if length(delta) > 65535
% store psi as 32 bit unsigned integer
psi_mat = zeros(n_state_ids, n_frames, 'uint32');
else
% store psi as 16 bit unsigned integer
psi_mat = zeros(n_state_ids, n_frames, 'uint16');
end
i_row = 1:n_state_ids;
j_col = 1:n_state_ids;
for i_frame = 1:n_frames
% create a matrix that has the same value of delta for all
% entries with the same state i (row)
% (same as repmat(delta, 1, col), but faster)
D = sparse(i_row, j_col, delta(:), n_state_ids, n_state_ids);
[delta, psi_mat(:, i_frame)] = max(D * A);
% compute likelihood p(yt|x1:t)
delta = obs_lik(obj.obs_model.gmm_from_state, ...
i_frame) .* delta';
% normalize
delta = delta / sum(delta);
end
% Backtracing
path = zeros(n_frames, 1);
[m, path(n_frames)] = max(delta);
maxIndex = find(delta == m);
path(n_frames) = round(median(maxIndex));
for i_frame=n_frames-1:-1:1
path(i_frame) = psi_mat(path(i_frame+1), i_frame+1);
end
end
function path = viterbi_mex(obj, obs_lik)
% convert transition matrix to three vectors containing the
% from_state, to_state and the transition probability
[state_ids_i, state_ids_j, trans_prob_ij] = find(obj.trans_model.A);
path = obj.viterbi_cpp(state_ids_i, state_ids_j, trans_prob_ij, ...
obj.init_distribution, obs_lik, obj.obs_model.gmm_from_state);
end
end
methods (Static)
[path] = viterbi_cpp(state_ids_i, state_ids_j, trans_prob_ij, ...
initial_prob, obs_lik, gmm_from_state);
end
end