马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?我要加入
x
利用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* [hide];
w.hideLinkOut = new double [hide];
m_pHideIn = new double [hide];
m_pHideOut = new double [hide];
m_pHideErr = new double [hide];
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[in];
for (j = 0; j < in; j++)
{
w.InLinkHide[j] = (double)((rand() / 32767.0) * 2.0 - 1.0) / 10.0;
strdata.Format("%lf\t",w.InLinkHide[j]);
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 [nTimes];
Err = new double [nTimes];
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[j] * pInput;
}
m_pHideIn[j] = sigma;
m_pHideOut[j] = Trans(sigma);
}
//计算输出层输入
sigma = 0.0;
for (j = 0; j < hide; j++)
{
sigma += w.hideLinkOut[j] * m_pHideOut[j];
}
m_pOutIn = sigma;
m_pNetOut[hh] = Trans(sigma);
//外层输出误差,为滤波后信号
Err[hh] = lfExOut - m_pNetOut[hh];
//外层一般化误差,用来调节网络权重
m_pOutErr = ITrans(m_pNetOut[hh])*Err[hh];
//隐层的一般化误差,用来调节网络权重
for (j = 0; j < hide; j++)
{
m_pHideErr[j] = m_pOutErr * w.hideLinkOut[j] * ITrans(m_pHideOut[j]);
}
//输出至隐层的权重调整
for (j = 0; j < hide; j++)
{
w.hideLinkOut[j] += ratio * m_pOutErr * m_pHideOut[j];
}
//隐层至输入层的权值调整
for (j = 0; j < hide; j++)
{
for (i = 0; i < in; i++)
{
w.InLinkHide[j] += ratio * m_pHideErr[j] * pInput;
}
}
hh++;
}
这是程序的主体部分,用了三层BP网络,可以拓展到多层 |