To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Revision:

root / matlab / flstViterbiUpdate.m

History | View | Annotate | Download (1.89 KB)

1
function d = flstViterbiUpdate(obsLik, d, isFinal)
2

    
3
if nargin < 3
4
    isFinal = 0;
5
end
6

    
7
% d.psi(:,1:(d.memory-1)) = d.psi(:,2:d.memory);
8
d.psi = circshift(d.psi, -1, 2);
9
% d.scale(1:(d.memory-1)) = d.scale(2:d.memory);
10
d.scale = circshift(d.scale, -1, 2);
11

    
12
if d.updateCount == 0
13
     % initialise first frame
14
    d.oldDelta = d.init .* obsLik;
15
    deltaSum = sum(d.oldDelta);
16
    d.oldDelta = d.oldDelta / deltaSum;
17
    d.scale(d.memory) = 1.0/deltaSum;
18
else
19
    tempPsi = ones(d.nState, 1);
20

    
21
    % calculate best previous state for every current state
22
    % this would be the "sparse" loop in C++
23

    
24
%     for jState = 1:d.nState
25
%         temp = d.oldDelta(d.from) .* d.prob';
26
%         [d.delta(jState), tempPsi(jState)] = max(temp(d.to==jState));
27
%     end
28

    
29
    for iTrans = 1:d.nTrans
30
        transProb = d.prob(iTrans);
31
        fromState = d.from(iTrans);
32
        toState = d.to(iTrans);
33
        currentValue = d.oldDelta(fromState) * transProb;
34
        if (currentValue > d.delta(toState))
35
            d.delta(toState) = currentValue; % will be multiplied by the right obs later!
36
            tempPsi(toState) = fromState;
37
        end
38
    end
39
%     d.delta = d.delta .* obsLik;
40
    d.delta = d.delta .* obsLik + eps;
41
    deltaSum = sum(d.delta);
42

    
43
    if deltaSum > 0
44
        d.oldDelta = d.delta / deltaSum; % normalise (scale)
45
        d.scale(d.memory) = 1.0/deltaSum;
46
        d.delta = zeros(size(d.delta));
47
    else
48
        warning('Viterbi has been fed some zero probabilities (update %d),\nat least they become zero in combination with the model.', d.updateCount);
49
        d.oldDelta = ones(d.nState,1)/d.nState;
50
        d.scale(d.memory) = 1.0;
51
        d.delta = zeros(size(d.delta));
52
    end
53
    d.psi(:,d.memory) = tempPsi;
54
end
55
d.updateCount = d.updateCount + 1;
56

    
57
if isFinal
58
    temp = flstDecode(d);
59
    d.path = [d.path, temp];
60
elseif d.updateCount > (d.memory-1)
61
    temp = flstDecode(d);
62
    d.path = [d.path, temp(1)];
63
end