-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforward_func.m
54 lines (45 loc) · 1.49 KB
/
forward_func.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
function [alpha,scaling_coefficient] = forward_func(P_init,Pt,likelihood,scaling)
%%% To conduct forward process
%%% input: init_pt pt likelihood
%%% input: scaling to control if the forward process in sclaed or not
%%% referenced from Rabiner 1989 implementation part
%%% to deal with the underflow of forward variable (alpha) as it is getting smaller
%%% with time
%%% output: alpha forward variable
%%% scaling_coefficient scaling factor for each time index t
T = size(likelihood,2);
region_num = length(P_init);
scaling_coefficient = zeros(1,T);
if scaling
%%% -------- scaling
% initialization
alpha_init = P_init'.*likelihood(:,1);
scaling_coefficient(1) = 1/sum(alpha_init);
alpha(:,1) = alpha_init.*scaling_coefficient(1);
% induction
for t=2:T
for j=1:region_num
temp = 0;
for i=1:region_num
temp = temp + Pt(i,j)*alpha(i,t-1);
end
alpha(j,t) = temp*likelihood(j,t);
end
% scaling
scaling_coefficient(t) = 1/sum(alpha(:,t));
alpha(:,t) = alpha(:,t).*scaling_coefficient(t);
end
else
%%% -------- non-scaling
alpha(:,1) = P_init'.*likelihood(:,1);
for t=2:T
for j=1:region_num
temp = 0;
for i=1:region_num
temp = temp + Pt(i,j)*alpha(i,t-1);
end
alpha(j,t) = temp*likelihood(j,t);
end
end
end
end