pyACA: Documentation 0.3.1
Source Code for Audio Content Analysis
Loading...
Searching...
No Matches
Functions
pyACA.ToolViterbi Namespace Reference

Functions

 ToolViterbi (P_E, P_T, p_s, bUseLogLikelihood=False)
 helper function: viterbi algorithm
 

Function Documentation

◆ ToolViterbi()

ToolViterbi ( P_E,
P_T,
p_s,
bUseLogLikelihood = False )

helper function: viterbi algorithm

Parameters
P_Eemmission probability matrix (states X observations)
P_Ttransition probability matrix (states X states)
p_sstart probability vector (states X 1)
bUseLogLikelihoodflag (default: false)
Returns
p: path with matrix row indices (length: observations)
P_res probability matrix

Definition at line 15 of file ToolViterbi.py.

15def ToolViterbi(P_E, P_T, p_s, bUseLogLikelihood=False):
16
17 if not bUseLogLikelihood:
18 # initialization
19 I = np.zeros(P_E.shape).astype(int)
20 P_res = np.zeros(P_E.shape)
21
22 P_res[:, 0] = P_E[:, 0] * p_s
23
24 # recursion
25 for n in np.arange(1, P_E.shape[1]):
26 for s in range(P_E.shape[0]):
27 # find max of preceding times trans prob
28 p_max = np.max(P_res[:, n-1] * P_T[:, s])
29 I[s, n] = np.argmax(P_res[:, n-1] * P_T[:, s]).astype(int)
30 P_res[s, n] = P_E[s, n] * p_max
31 else:
32 # initialization
33 P_E = np.log(P_E) # hope for non-zero entries
34 P_T = np.log(P_T) # hope for non-zero entries
35 p_s = np.log(p_s) # hope for non-zero entries
36 I = np.zeros(P_E.shape).astype(int)
37 P_res = np.zeros(P_E.shape)
38
39 P_res[:, 0] = P_E[:, 0] + p_s
40
41 # recursion
42 for n in np.arange(1, P_E.shape[1]):
43 for s in range(P_E.shape[0]):
44 # find max of preceding times trans prob
45 p_max = np.max(P_res[:, n-1] + P_T[:, s])
46 I[s, n] = np.argmax(P_res[:, n-1] + P_T[:, s]).astype(int)
47 P_res[s, n] = P_E[s, n] + p_max
48
49 # traceback
50 p = np.zeros(P_E.shape[1]).astype(int)
51 # start with the last element, then count down
52 p[-1] = np.argmax(P_res[:, -1]).astype(int)
53 for n in range(P_E.shape[1]-2, -1, -1):
54 p[n] = I[p[n+1], n+1]
55
56 return p, P_res