VC++写了一个BP自适应噪声对消器
利用VC++写了一个BP神经网络噪声对消器:/网络初始化配置
//权值存储到"d:\w.txt"文件
void CAnnFilter::InitNet(UINT un_In, UINT un_hide,
UINT un_out, double rate,double zengyi)
{
in = un_In;
hide = un_hide;
out = un_out;
ratio = rate;
UINT i,j;
m_lfZengyi = zengyi;
srand((unsigned)time(NULL));//种子发生器
w.InLinkHide = new double* ;
w.hideLinkOut = new double ;
m_pHideIn = new double ;
m_pHideOut = new double ;
m_pHideErr = new double ;
CStdioFile f1;
CString strdata;
if(!f1.Open("d:\\w.txt",CFile::modeCreate | CFile::modeWrite))
{
AfxMessageBox("w file create failed!");
return;
}
f1.WriteString("初始权系数:\n");
f1.WriteString("输入-隐层:\n");
for(i = 0; i < hide; i++)
{
w.InLinkHide = new double;
for (j = 0; j < in; j++)
{
w.InLinkHide = (double)((rand() / 32767.0) * 2.0 - 1.0) / 10.0;
strdata.Format("%lf\t",w.InLinkHide);
f1.WriteString(strdata);
}
f1.WriteString("\n");
}
f1.WriteString("隐层-输出层:\n");
for(i = 0; i < hide; i++)
{
w.hideLinkOut = (double)((rand() / 32767.0) * 2.0 - 1.0) / 10.0;
strdata.Format("%lf\n",w.hideLinkOut);
f1.WriteString(strdata);
}
m_bRecord = TRUE;
hh = 0;
f1.Close();
}
//网络训练函数
void CAnnFilter::NetTrain(double *pInput, double lfExOut,
UINT nTimes)
{
if (m_bRecord == TRUE)
{
m_pNetOut = new double ;
Err = new double ;
m_bRecord = FALSE;
}
UINT i,j;
double sigma;
//计算隐层输入,输出
for (j = 0; j < hide; j++)
{
sigma = 0.0;
for (i = 0; i < in; i++)
{
sigma += w.InLinkHide * pInput;
}
m_pHideIn = sigma;
m_pHideOut = Trans(sigma);
}
//计算输出层输入
sigma = 0.0;
for (j = 0; j < hide; j++)
{
sigma += w.hideLinkOut * m_pHideOut;
}
m_pOutIn = sigma;
m_pNetOut = Trans(sigma);
//外层输出误差,为滤波后信号
Err = lfExOut - m_pNetOut;
//外层一般化误差,用来调节网络权重
m_pOutErr = ITrans(m_pNetOut)*Err;
//隐层的一般化误差,用来调节网络权重
for (j = 0; j < hide; j++)
{
m_pHideErr = m_pOutErr * w.hideLinkOut * ITrans(m_pHideOut);
}
//输出至隐层的权重调整
for (j = 0; j < hide; j++)
{
w.hideLinkOut += ratio * m_pOutErr * m_pHideOut;
}
//隐层至输入层的权值调整
for (j = 0; j < hide; j++)
{
for (i = 0; i < in; i++)
{
w.InLinkHide += ratio * m_pHideErr * pInput;
}
}
hh++;
}
这是程序的主体部分,用了三层BP网络,可以拓展到多层
页:
[1]