annotate matlab/flstViterbiUpdate.m @ 1:4283604499f8 tip

added mini probability to delta (not sure if this is a good idea, but I did it)
author matthiasm
date Mon, 10 Nov 2014 19:38:49 +0000
parents 1df4a6fb0d22
children
rev   line source
matthiasm@0 1 function d = flstViterbiUpdate(obsLik, d, isFinal)
matthiasm@0 2
matthiasm@0 3 if nargin < 3
matthiasm@0 4 isFinal = 0;
matthiasm@0 5 end
matthiasm@0 6
matthiasm@0 7 % d.psi(:,1:(d.memory-1)) = d.psi(:,2:d.memory);
matthiasm@0 8 d.psi = circshift(d.psi, -1, 2);
matthiasm@0 9 % d.scale(1:(d.memory-1)) = d.scale(2:d.memory);
matthiasm@0 10 d.scale = circshift(d.scale, -1, 2);
matthiasm@0 11
matthiasm@0 12 if d.updateCount == 0
matthiasm@0 13 % initialise first frame
matthiasm@0 14 d.oldDelta = d.init .* obsLik;
matthiasm@0 15 deltaSum = sum(d.oldDelta);
matthiasm@0 16 d.oldDelta = d.oldDelta / deltaSum;
matthiasm@0 17 d.scale(d.memory) = 1.0/deltaSum;
matthiasm@0 18 else
matthiasm@0 19 tempPsi = ones(d.nState, 1);
matthiasm@0 20
matthiasm@0 21 % calculate best previous state for every current state
matthiasm@0 22 % this would be the "sparse" loop in C++
matthiasm@0 23
matthiasm@0 24 % for jState = 1:d.nState
matthiasm@0 25 % temp = d.oldDelta(d.from) .* d.prob';
matthiasm@0 26 % [d.delta(jState), tempPsi(jState)] = max(temp(d.to==jState));
matthiasm@0 27 % end
matthiasm@0 28
matthiasm@0 29 for iTrans = 1:d.nTrans
matthiasm@0 30 transProb = d.prob(iTrans);
matthiasm@0 31 fromState = d.from(iTrans);
matthiasm@0 32 toState = d.to(iTrans);
matthiasm@0 33 currentValue = d.oldDelta(fromState) * transProb;
matthiasm@0 34 if (currentValue > d.delta(toState))
matthiasm@0 35 d.delta(toState) = currentValue; % will be multiplied by the right obs later!
matthiasm@0 36 tempPsi(toState) = fromState;
matthiasm@0 37 end
matthiasm@0 38 end
matthiasm@1 39 % d.delta = d.delta .* obsLik;
matthiasm@1 40 d.delta = d.delta .* obsLik + eps;
matthiasm@0 41 deltaSum = sum(d.delta);
matthiasm@0 42
matthiasm@0 43 if deltaSum > 0
matthiasm@0 44 d.oldDelta = d.delta / deltaSum; % normalise (scale)
matthiasm@0 45 d.scale(d.memory) = 1.0/deltaSum;
matthiasm@0 46 d.delta = zeros(size(d.delta));
matthiasm@0 47 else
matthiasm@0 48 warning('Viterbi has been fed some zero probabilities (update %d),\nat least they become zero in combination with the model.', d.updateCount);
matthiasm@0 49 d.oldDelta = ones(d.nState,1)/d.nState;
matthiasm@0 50 d.scale(d.memory) = 1.0;
matthiasm@0 51 d.delta = zeros(size(d.delta));
matthiasm@0 52 end
matthiasm@0 53 d.psi(:,d.memory) = tempPsi;
matthiasm@0 54 end
matthiasm@0 55 d.updateCount = d.updateCount + 1;
matthiasm@0 56
matthiasm@0 57 if isFinal
matthiasm@0 58 temp = flstDecode(d);
matthiasm@0 59 d.path = [d.path, temp];
matthiasm@0 60 elseif d.updateCount > (d.memory-1)
matthiasm@0 61 temp = flstDecode(d);
matthiasm@0 62 d.path = [d.path, temp(1)];
matthiasm@0 63 end