信源编码作业——LMS算法
来源:互联网 发布:php静态计数器 编辑:程序博客网 时间:2024/05/22 05:20
一:LMS算法说明:
全称Least mean square 算法,又叫最小均方算法,用于修正滤波器参数使均方差(Mean Square Error,MSE)达到最小,均方差公式如下所示:
步骤:
(1)设置变量和参量:
X(n)为输入向量,或称为训练样本
W(n)为权值向量
b(n)为偏差
d(n)为期望输出
y(n)为实际输出
η为学习速率
n为迭代次数
(2)初始化,赋给w(0)各一个较小的随机非零值,令n=0
(3)对于一组输入样本x(n)和对应的期望输出d,计算
e(n)=d(n)-X^T(n)W(n)
W(n+1)=W(n)+ηX(n)e(n)
(4)判断是否满足条件,若满足算法结束,若否n增加1,转入第(3)步继续执行
二、算法实现——LMS算法的代码:
- const unsigned int nTests =4;
- const unsigned int nInputs =2;
- const double rho =0.005;
- struct lms_testdata
- {
- doubleinputs[nInputs];
- doubleoutput;
- };
- double compute_output(constdouble * inputs,double* weights)
- {
- double sum =0.0;
- for (int i = 0 ; i < nInputs; ++i)
- {
- sum += weights[i]*inputs[i];
- }
- //bias
- sum += weights[nInputs]*1.0;
- return sum;
- }
- //计算均方差
- double caculate_mse(constlms_testdata * testdata,double * weights)
- {
- double sum =0.0;
- for (int i = 0 ; i < nTests ; ++i)
- {
- sum += pow(testdata[i].output -compute_output(testdata[i].inputs,weights),2);
- }
- return sum/(double)nTests;
- }
- //对计算所得值,进行分类
- int classify_output(doubleoutput)
- {
- if(output> 0.0)
- return1;
- else
- return-1;
- }
- int _tmain(int argc,_TCHAR* argv[])
- {
- lms_testdata testdata[nTests] = {
- {-1.0,-1.0, -1.0},
- {-1.0, 1.0, -1.0},
- { 1.0,-1.0, -1.0},
- { 1.0, 1.0, 1.0}
- };
- doubleweights[nInputs + 1] = {0.0};
- while(caculate_mse(testdata,weights)> 0.26)//计算均方差,如果大于给定值,算法继续
- {
- intiTest = rand()%nTests;//随机选择一组数据
- doubleoutput = compute_output(testdata[iTest].inputs,weights);
- doubleerr = testdata[iTest].output - output;
- //调整输入端的权值
- for (int i = 0 ; i < nInputs ; ++i)
- {
- weights[i] = weights[i] + rho * err* testdata[iTest].inputs[i];
- }
- weights[nInputs] = weights[nInputs] +rho * err;
- cout<<"mse:"<<caculate_mse(testdata,weights)<<endl;
- }
- for(int w = 0 ; w < nInputs + 1 ; ++w)
- {
- cout<<"weight"<<w<<":"<<weights[w]<<endl;
- }
- cout<<"\n";
- for (int i = 0 ;i < nTests ; ++i)
- {
- cout<<"rightresult:êo"<<testdata[i].output<<"\t";
- cout<<"caculateresult:" << classify_output(compute_output(testdata[i].inputs,weights))<<endl;
- }
- //
- char temp ;
- cin>>temp;
- return 0;
- }
阅读全文
0 0
- 信源编码作业——LMS算法
- 【信源编码 作业五】LMS算法
- 【信源编码 LMS算法】
- 【信源编码作业】6. 最小均方算法(LMS)
- 信源编码第五次作业-LMS最小均方算法
- 信源编码作业之六:LMS最小均方算法(python)
- 信源编码作业——矢量量化
- 信源编码第三次作业——lzo-实时数据库压缩
- 第一次信源编码作业
- 信源编码作业一
- 信源编码第一次作业
- 信源编码第一次作业
- 信源编码第二次作业
- 信源编码作业二
- 信源编码第二次作业
- 信源编码第二次作业
- 信源编码第三次作业
- 信源编码第二次作业
- 转载 MYSQL协议分析
- Java中Arrays.asList()
- JVM内存模型
- 计算机网络总结
- 三.第一个Gradle应用程序(Application形式的jar程序)
- 信源编码作业——LMS算法
- 牛客网彩色瓷砖C/C++解决
- 简析Lock与synchronized
- This version of the rendering library is more recent than your version of Android Studio. Please upd
- Mysql开发技巧
- 基于pycaffe从零开始写mnist(第一篇)——生成txt文件
- Codeforces Round #425 (Div. 2) A. Sasha and Sticks
- LeetCode 21. Merge Two Sorted Lists
- socket套接字编程