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 @ 0:1df4a6fb0d22

History | View | Annotate | Download (1.85 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
    deltaSum = sum(d.delta);
41

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

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