annotate viterbi_path.m @ 48:6e76c7710fa1 matthiasm-plugin

removed subtraction in chroma dictionary, added to-the-power-of-1.5 in chordino
author matthiasm
date Mon, 25 Oct 2010 16:58:32 +0900
parents 131801714118
children
rev   line source
matthiasm@43 1 function path = viterbi_path(prior, transmat, obslik)
matthiasm@43 2 % VITERBI Find the most-probable (Viterbi) path through the HMM state trellis.
matthiasm@43 3 % path = viterbi(prior, transmat, obslik)
matthiasm@43 4 %
matthiasm@43 5 % Inputs:
matthiasm@43 6 % prior(i) = Pr(Q(1) = i)
matthiasm@43 7 % transmat(i,j) = Pr(Q(t+1)=j | Q(t)=i)
matthiasm@43 8 % obslik(i,t) = Pr(y(t) | Q(t)=i)
matthiasm@43 9 %
matthiasm@43 10 % Outputs:
matthiasm@43 11 % path(t) = q(t), where q1 ... qT is the argmax of the above expression.
matthiasm@43 12
matthiasm@43 13
matthiasm@43 14 % delta(j,t) = prob. of the best sequence of length t-1 and then going to state j, and O(1:t)
matthiasm@43 15 % psi(j,t) = the best predecessor state, given that we ended up in state j at t
matthiasm@43 16
matthiasm@43 17 scaled = 1;
matthiasm@43 18
matthiasm@43 19 T = size(obslik, 2);
matthiasm@43 20 prior = prior(:);
matthiasm@43 21 Q = length(prior);
matthiasm@43 22
matthiasm@43 23 delta = zeros(Q,T);
matthiasm@43 24 psi = zeros(Q,T);
matthiasm@43 25 path = zeros(1,T);
matthiasm@43 26 scale = ones(1,T);
matthiasm@43 27
matthiasm@43 28
matthiasm@43 29 t=1;
matthiasm@43 30 delta(:,t) = prior .* obslik(:,t);
matthiasm@43 31 if scaled
matthiasm@43 32 [delta(:,t), n] = normalise(delta(:,t));
matthiasm@43 33 scale(t) = 1/n;
matthiasm@43 34 end
matthiasm@43 35 psi(:,t) = 0; % arbitrary value, since there is no predecessor to t=1
matthiasm@43 36 for t=2:T
matthiasm@43 37 for j=1:Q
matthiasm@43 38 [delta(j,t), psi(j,t)] = max(delta(:,t-1) .* transmat(:,j));
matthiasm@43 39 delta(j,t) = delta(j,t) * obslik(j,t);
matthiasm@43 40 end
matthiasm@43 41 if scaled
matthiasm@43 42 [delta(:,t), n] = normalise(delta(:,t));
matthiasm@43 43 scale(t) = 1/n;
matthiasm@43 44 end
matthiasm@43 45 end
matthiasm@43 46 [p, path(T)] = max(delta(:,T));
matthiasm@43 47 for t=T-1:-1:1
matthiasm@43 48 path(t) = psi(path(t+1),t+1);
matthiasm@43 49 end
matthiasm@43 50
matthiasm@43 51 % If scaled==0, p = prob_path(best_path)
matthiasm@43 52 % If scaled==1, p = Pr(replace sum with max and proceed as in the scaled forwards algo)
matthiasm@43 53 % Both are different from p(data) as computed using the sum-product (forwards) algorithm
matthiasm@43 54
matthiasm@43 55 if 0
matthiasm@43 56 if scaled
matthiasm@43 57 loglik = -sum(log(scale));
matthiasm@43 58 %loglik = prob_path(prior, transmat, obslik, path);
matthiasm@43 59 else
matthiasm@43 60 loglik = log(p);
matthiasm@43 61 end
matthiasm@43 62 end