Hmm.cpp ( File view )

Description:...
  • By duandian 2013-10-03
  • View(s):442
  • Download(s):14
  • Point(s): 4
			// Hmm.cpp: implementation of the CHmm class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "hmm_1.h"
#include "Hmm.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CHmm::CHmm()
{

	m_pA = NULL;
	m_pB = NULL;
	m_pPI = NULL;

	m_pInput = NULL;
	m_pRltMatrix = NULL;
	m_plamda = NULL;

	m_iDimA = 0;
	m_iDimB = 0;
	m_iInputLen = 0;

}

CHmm::~CHmm()
{


}

/*
	int iDimA		转换矩阵的维数
	int iDimB		输出符号数
	double **pA		状态转换矩阵iDimA*iDimA
	double **pB		输出符号的概率 iDimA*iDimB
	double *pPI		初始状态概率
*/
void CHmm::SetHmmParam(int iDimA, int iDimB, double **pA, double **pB, double *pPI)
{

	m_pA = pA;
	m_iDimA = iDimA;

	m_pB = pB;
	m_iDimB = iDimB;
	
	m_pPI = pPI;

}

/*
 *	向前算法
 *	int *pInput			检测序列
 *	int iInputLen		序列长度
 *	double **pRltMatrix	中间结果
 */
double CHmm::Forward(int *pInput, int iInputLen, double **pRltMatrix)
{

	for(int i=0; i<iInputLen; i++)
	{

		if(pInput[i]>=m_iDimB)
		{

			AfxMessageBox("输入的数据超界");
			return 0.0;
		
}
	
}


	m_pRltMatrix = pRltMatrix;

	double rlt = 0;
	m_pInput = pInput;
	m_iInputLen = iInputLen;

//	NewMatrix();

	double *alfa;
	alfa = new double[m_iDimA];

	for(i=0; i<m_iDimA; i++)
	{

		alfa[i] = forward_recursion(iInputLen-1, i);
		//m_pRltMatrix[i][0] = alfa[i];
		rlt+=alfa[i];
	
}



	delete [] alfa;
	return rlt;

}

double CHmm::forward_recursion(int iIndex, int j)
{

	double rlt= 0;
	double *alfa_last;
	alfa_last = new double[m_iDimA];

	if(iIndex==1)
	{


		for(int i=0; i<m_iDimA; i++)
		{

			alfa_last[i] = m_pPI[i]*m_pB[i][m_pInput[0]];
			m_pRltMatrix[i][0] = alfa_last[i];
		
}
	
}
	else
	{

		for(int i=0; i<m_iDimA; i++)
		{

			alfa_last[i] = forward_recursion(iIndex-1, i);
		
}
	
}

	for(int i=0; i<m_iDimA; i++)
	{

		rlt+=alfa_last[i]*m_pA[i][j];
	
}

	rlt *= m_pB[j][m_pInput[iIndex]];

	m_pRltMatrix[j][iIndex] = rlt;
	//m_pRltMatrix[j][iIndex+1] = rlt;
	delete [] alfa_last;
	return rlt;

}


/*
 *	向后算法
 *	int *pInput			检测序列
 *	int iInputLen		序列长度
 *	double **pRltMatrix	中间结果
 */
double CHmm::Backward(int *pInput, int iInputLen, double **pRltMatrix)
{

	for(int i=0; i<iInputLen; i++)
	{

		if(pInput[i]>=m_iDimB)
		{

			AfxMessageBox("输入的数据超界");
			return 0.0;
		
}
	
}


	m_pRltMatrix = pRltMatrix;

	double rlt = 0;
	m_pInput = pInput;
	m_iInputLen = iInputLen;

//	NewMatrix();

	double *beta;
	beta = new double[m_iDimA];

	for(i=0; i<m_iDimA; i++)
	{

		beta[i] = backward_recursion(0, i)*m_pPI[i]*m_pB[i][m_pInput[0]];
		m_pRltMatrix[i][0] = beta[i];
		rlt+=beta[i];
	
}


	return rlt;

}
double CHmm::backward_recursion(int iIndex, int j)
{

	double rlt = 0.0;
	double *beta_last;
	beta_last = new double[m_iDimA];

	if(iIndex==m_iInputLen-1)
	{

		rlt = 1.0;
		//rlt = 2.0;

	
}
	else
	{

		for(int i=0; i<m_iDimA; i++)
		{

			beta_last[i] = backward_recursion(iIndex+1, i);

			rlt+=beta_last[i]*m_pA[j][i]*m_pB[i][m_pInput[iIndex+1]];
		
}
	
}


	//保存中间结果
	m_pRltMatrix[j][iIndex+1] = rlt;


	return rlt;

}


/*
 *	韦特比算法
 *	int *pInput			检测序列
 *	int iInputLen		序列长度
 *	int *pBestPath		结果最佳序列
 *	double **pRltMatrix	中间结果
 */
double CHmm::Viterbi(int *pInput, int iInputLen, int *pBestPath, double **pRltMatrix, int **plamda)
{


	for(int i=0; i<iInputLen; i++)
	{

		if(pInput[i]>=m_iDimB)
		{

			AfxMessageBox("输入的数据超界");
			return 0.0;
		
}
	
}

	m_pRltMatrix = pRltMatrix;
	m_plamda = plamda;


	double rlt = 0.0;

	m_pInput = pInput;
	m_iInputLen = iInputLen;


	double *deta;
	deta = new double[m_iDimA];

	for(i=0; i<m_iDimA; i++)
	{

		deta[i] = viterbi_recursion(iInputLen-1, i);
		if(rlt<deta[i])
		{

			rlt=deta[i];
			pBestPath[iInputLen-1] = i+1;
		
}
	
}

	
	for(i=iInputLen-2; i>=0; i--)
	{

		pBestPath[i] = m_plamda[pBestPath[i+1]-1][i+1];
	
}

	delete [] deta;
	return rlt;

}
//韦特比算法 的递推函数
double CHmm::viterbi_recursion(int iIndex, int j)
{

	double rlt= 0;
	double *deta_last;
	deta_last = new double[m_iDimA];

	if(iIndex==1)
	{


		for(int i=0; i<m_iDimA; i++)
		{

			deta_last[i] = m_pPI[i]*m_pB[i][m_pInput[0]];

			//保存中间结果
			m_pRltMatrix[i][0] = deta_last[i];
			
		
}
	
}
	else
	{

		for(int i=0; i<m_iDimA; i++)
		{

			deta_last[i] = viterbi_recursion(iIndex-1, i);
		
}
	
}

	double temp;
	for(int i=0; i<m_iDimA; i++)
	{

		temp = deta_last[i]*m_pA[i][j];
		if(rlt<temp)
		{

			rlt=temp;

			//保存最佳路径
			m_plamda[j][iIndex] = i+1;
		
}
	
}

	rlt *= m_pB[j][m_pInput[iIndex]];

	//保存中间结果
	//m_pRltMatrix[iIndex][j] = rlt;
	m_pRltMatrix[j][iIndex] = rlt;

	delete [] deta_last;
	return rlt;

}

			
...
Expand> <Close

Want complete source code? Download it here

Point(s): 4

Download
0 lines left, continue to read
Sponsored links

File list

Tips: You can preview the content of files by clicking file names^_^
Name Size Date
Hmm.cpp4.95 kB21-10-06 21:35
Hmm.h1.08 kB21-10-06 21:24
hmm_1.aps19.91 kB11-09-07 11:30
hmm_1.clw928.00 B21-09-07 15:59
hmm_1.cpp2.00 kB20-10-06 09:22
hmm_1.dsp4.16 kB21-10-06 21:36
hmm_1.dsw516.00 B20-10-06 09:22
hmm_1.h1.28 kB20-10-06 09:22
hmm_1.ncb57.00 kB21-09-07 16:02
hmm_1.opt52.50 kB21-09-07 16:02
hmm_1.plg697.00 B20-08-07 09:53
hmm_1.rc4.64 kB21-10-06 21:40
hmm_1Dlg.cpp6.37 kB29-07-07 15:18
hmm_1Dlg.h1.54 kB21-10-06 21:08
ReadMe.txt3.48 kB20-10-06 09:22
hmm_1.ico1.05 kB20-10-06 09:22
hmm_1.rc2397.00 B20-10-06 09:22
Thumbs.db3.00 kB11-09-07 12:11
resource.h775.00 B21-10-06 21:09
StdAfx.cpp207.00 B20-10-06 09:22
StdAfx.h1.03 kB20-10-06 09:22
<Debug>0.00 B08-11-07 10:03
<res>0.00 B11-09-07 12:11
<hmm源码-c>0.00 B21-09-07 16:02
...
Sponsored links

Hmm.cpp (27.65 kB)

Need 4 point
Your Point(s)

Your Point isn't enough.

Get point immediately by PayPal

More(Debit card / Credit card / PayPal Credit / Online Banking)

Submit your source codes. Get more point

LOGIN

Don't have an account? Register now
Need any help?
Mail to: support@codeforge.com

切换到中文版?

CodeForge Chinese Version
CodeForge English Version

Where are you going?

^_^"Oops ...

Sorry!This guy is mysterious, its blog hasn't been opened, try another, please!
OK

Warm tip!

CodeForge to FavoriteFavorite by Ctrl+D