[求助]viterbi译码源程序
各位高手,谁提供一下实现(3,1,3)viterbi译码的源程序,不胜感激!!!回复:(zw123)[求助]viterbi译码源程序
<P>/*<BR>** File:Viterbi.cpp<BR>** 功能:给定HMM和观察序列,求最可能的状态<BR>*/</P><P>//#include "StdAfx.h"<BR>#include <math.h><BR>#include "hmm.h"<BR>#include "nrutil.h"</P>
<P>#define VITHUGE100000000000.0</P>
<P>/**************************************************************************<BR>** 函数名称:Viterbi<BR>** 功能:Viterbi算法<BR>** 参数:phmm:HMM结构指针<BR>** T:观察值的个数<BR>** O:观察序列<BR>** delta,psi为中间变量<BR>** q:求得的最佳状态序列<BR>** pprob:概率<BR>**/<BR>void Viterbi(HMM *phmm, int T, int *O, double **delta, int **psi, <BR> int *q, double *pprob)<BR>{<BR> inti, j; /* 状态下标 */<BR> int t; /* 时间下标 */ </P>
<P> int maxvalind;<BR> double maxval, val;</P>
<P> /* 1. 初始化*/<BR> <BR> for (i = 1; i <= phmm->N; i++) <BR> {<BR>delta = phmm->pi * (phmm->B]);<BR>psi = 0;<BR> } </P>
<P> /* 2. 递归 */<BR> <BR> for (t = 2; t <= T; t++) <BR> {<BR>for (j = 1; j <= phmm->N; j++) <BR>{<BR> maxval = 0.0;<BR> maxvalind = 1; <BR> for (i = 1; i <= phmm->N; i++) <BR> {<BR> val = delta*(phmm->A);<BR> if (val > maxval) {<BR> maxval = val; <BR> maxvalind = i; <BR> }<BR> }<BR> <BR> delta = maxval*(phmm->B]);<BR> psi = maxvalind; </P>
<P>}<BR> }</P>
<P> /* 3. 终止 */</P>
<P> *pprob = 0.0;<BR> q = 1;<BR> for (i = 1; i <= phmm->N; i++) <BR> {<BR>if (delta > *pprob) <BR>{<BR> *pprob = delta; <BR> q = i;<BR>}<BR> }</P>
<P> /* 4. Path (state sequence) backtracking */</P>
<P> for (t = T - 1; t >= 1; t--)<BR>q = psi];</P>
<P>}</P>
<P>/**************************************************************************<BR>** 函数名称:ViterbiLog<BR>** 功能:Viterbi算法<BR>** 参数:phmm:HMM结构指针<BR>** T:观察值的个数<BR>** O:观察序列<BR>** delta,psi为中间变量<BR>** q:求得的最佳状态序列<BR>** pprob:概率<BR>**/<BR>void ViterbiLog(HMM *phmm, int T, int *O, double **delta, int **psi,<BR> int *q, double *pprob)<BR>{<BR> int i, j; /* 状态下标 */<BR> int t; /* 时间下标 */<BR> <BR> int maxvalind;<BR> doublemaxval, val;<BR> double**biot;</P>
<P> /* 0. 预处理 */</P>
<P> for (i = 1; i <= phmm->N; i++) <BR>phmm->pi = log(phmm->pi);<BR> for (i = 1; i <= phmm->N; i++) <BR>for (j = 1; j <= phmm->N; j++) <BR>{<BR> phmm->A = log(phmm->A);<BR>}</P>
<P> biot = dmatrix(1, phmm->N, 1, T);<BR> for (i = 1; i <= phmm->N; i++) <BR>for (t = 1; t <= T; t++) <BR>{<BR> biot = log(phmm->B]);<BR>}<BR> <BR> /* 1. 初始化*/<BR> <BR> for (i = 1; i <= phmm->N; i++) <BR> {<BR>delta = phmm->pi + biot;<BR>psi = 0;<BR> }<BR> <BR> /* 2. 递归 */<BR> <BR> for (t = 2; t <= T; t++) <BR> {<BR>for (j = 1; j <= phmm->N; j++) <BR>{<BR> maxval = -VITHUGE;<BR> maxvalind = 1;<BR> for (i = 1; i <= phmm->N; i++) <BR> {<BR> val = delta + (phmm->A);<BR> if (val > maxval) <BR> {<BR> maxval = val;<BR> maxvalind = i;<BR> }<BR> }<BR> <BR> delta = maxval + biot; <BR> psi = maxvalind;<BR> <BR>}<BR> }<BR> <BR> /* 3. 终止 */<BR> <BR> *pprob = -VITHUGE;<BR> q = 1;<BR> for (i = 1; i <= phmm->N; i++) <BR> {<BR>if (delta > *pprob) <BR>{<BR> *pprob = delta;<BR> q = i;<BR>}<BR> }<BR> <BR> <BR> /* 4. 回溯 */</P>
<P> for (t = T - 1; t >= 1; t--)<BR>q = psi];</P>
<P>}<BR></P>
没有头文件
太感谢了!!<BR>能否再提供一下hmm.h和nrutil.h这两个头文件?<BR>谢谢!!
页:
[1]