声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

查看: 2710|回复: 4

[经典算法] [求助]求LMS算法的编程程序

[复制链接]
发表于 2006-4-6 09:17 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?我要加入

x
各位兄弟姐妹有LMS算法的麻烦你们发一发
谢谢。。。。。。。。。

[ 本帖最后由 风花雪月 于 2006-11-12 08:10 编辑 ]
回复
分享到:

使用道具 举报

发表于 2006-4-12 07:24 | 显示全部楼层

回复:(hmilykk)[求助]求LMS算法的编程程序

matlab lms 算法实例
  1. function [wn,en]=lms(xn,dn,M,delt,varargin)
  2. % LMS Algorithm ,返回滤波器加权系数矩阵和误差向量
  3. %
  4. % 调用格式
  5. % [wn,en]=lms(xn,dn,M,delt,itr)
  6. % en=滤波器输出和d(n)的误差序列,为列向量
  7. % wn=滤波器的加权参量序列,为一矩阵,其每行代表一个加权参量,每列代表一次迭代;初始化值设为0
  8. % xn=输入列向量信号
  9. % dn=期望列向量信号
  10. % M=滤波器阶数
  11. % delt=步长
  12. % itr=迭代次数
  13. %
  14. % [wn,en]=lms(xn,dn,M,delt)
  15. % 迭代次数为默认值,即等于x(n)的点数
  16. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  17. % 输入输出参数的检查
  18. %--------------------------------------------------------------------------
  19. vin=length(varargin); Item=vin+4;
  20. error(nargchk(4,Item,nargin)); % 检查输入变量数目是否合适,其中前四个参数必须输入
  21. if nargout>2 % 检查输出变量数目是否合适
  22. error('Too many output arguments');
  23. end
  24. %------------------------------------------------------------------------
  25. Nx=length(xn); % x(n)的长度
  26. if Nx~=length(dn) % 检查x(n)和d(n)长度是否相等
  27. error('The length of x(n) is not equal to that of d(n)');
  28. end
  29. %------------------------------------------------------------------------
  30. sizex=size(xn); % 检查输入向量是否为列向量
  31. if sizex(1)xn=xn.';
  32. end
  33. sizedn=size(dn); % 检查期望信号向量是否为列向量
  34. if sizedn(1)dn=dn.';
  35. end
  36. %-------------------------------------------------------------------------
  37. itr=Nx; % 迭代次数取默认值
  38. %-------------------------------------------------------------------------
  39. % 当输入变量为5个时
  40. if Item==5 % 确定迭代次数
  41. itr=varargin{1};
  42. if itr>Nx | itrerror('Too many or too few iterations');
  43. end
  44. end
  45. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  46. % 参数的初始化
  47. en=zeros(itr,1);
  48. wn=zeros(M,itr); % 每行代表一个加权参量,每列代表一次迭代
  49. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  50. % 迭代计算
  51. for k=M:itr % 第k次迭代
  52. x_tap=xn(k:-1:k-M+1);
  53. en(k)=dn(k)-wn(:,k-1)'*x_tap;
  54. wn(:,k)=wn(:,k-1)+2*delt*en(k,1)*x_tap;
  55. end
复制代码

[ 本帖最后由 风花雪月 于 2006-11-12 08:10 编辑 ]
发表于 2006-6-26 23:35 | 显示全部楼层

回复:(hmilykk)[求助]求LMS算法的编程程序

给另一个,其实原理一样的。多看个程序,参考一下。
关键是这里有一个规一化LMS,可以跟一般的LMS比较。

  1. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2. %
  3. %LMS; Normalized LMS
  4. % filter parameters
  5. %This program cannot run,because in defect of the "awgn" function,which is
  6. %in communication bockset.so,this funciton is omitted temporarily to test other codes.
  7. %
  8. %
  9. %as the results, the study time of the NLMS is shorter than LMS.
  10. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  11. clear all;
  12. %close all;

  13. snr=20;
  14. order=8;
  15. Hn =[0.8783 -0.5806 0.6537 -0.3223 0.6577 -0.0582
  16. 0.2895 -0.2710 0.1278 -0.1508 0.0238 -0.1814
  17. 0.2519 -0.0396 0.0423 -0.0152 0.1664 -0.0245
  18. 0.1463 -0.0770 0.1304 -0.0148 0.0054 -0.0381
  19. 0.0374 -0.0329 0.0313 -0.0253 0.0552 -0.0369
  20. 0.0479 -0.0073 0.0305 -0.0138 0.0152 -0.0012
  21. 0.0154 -0.0092 0.0177 -0.0161 0.0070 -0.0042
  22. 0.0051 -0.0131 0.0059 -0.0041 0.0077 -0.0034
  23. 0.0074 -0.0014 0.0025 -0.0056 0.0028 -0.0005
  24. 0.0033 -0.0000 0.0022 -0.0032 0.0012 -0.0020
  25. 0.0017 -0.0022 0.0004 -0.0011 0 0 ];
  26. Hn=Hn(1:order);
  27. N=1000;
  28. % r=wavread('C:\Matlab\work\Write_In_Paper\Shi_Yan.wav');
  29. % r=r(1:N);
  30. % r=awgn(r,snr,'measured');
  31. EE=zeros(N,1); Loop=10; mu=0.3;
  32. for nn=1:Loop
  33. r=sign(rand(N,1)-0.5); % r=awgn(r,snr,'measured');
  34. output=conv(r,Hn);
  35. %output=awgn(output,snr,'measured');
  36. win=zeros(1,order);
  37. error=zeros(1,N)';

  38. for i=order:N
  39. input=r(i:-1:i-order+1);
  40. e=output(i)-win*input;
  41. win=win+mu*e*input'/(1+input'*input);
  42. error(i)=error(i)+e^2;
  43. end;
  44. EE=EE+error;
  45. end;
  46. error=EE/Loop;

  47. figure;
  48. %error=10*log10(error(order:N));
  49. plot(error,'r'); axis tight; grid on;
  50. %figure;
  51. %semilogy(error(order:N),'b'); grid; axis tight;
  52. %subplot(212);
  53. %plot(win,'r*');
  54. %hold on; plot(Hn,'b'); grid; axis tight;

  55. snr=20;
  56. order=8;
  57. Hn =[0.8783 -0.5806 0.6537 -0.3223 0.6577 -0.0582
  58. 0.2895 -0.2710 0.1278 -0.1508 0.0238 -0.1814
  59. 0.2519 -0.0396 0.0423 -0.0152 0.1664 -0.0245
  60. 0.1463 -0.0770 0.1304 -0.0148 0.0054 -0.0381
  61. 0.0374 -0.0329 0.0313 -0.0253 0.0552 -0.0369
  62. 0.0479 -0.0073 0.0305 -0.0138 0.0152 -0.0012
  63. 0.0154 -0.0092 0.0177 -0.0161 0.0070 -0.0042
  64. 0.0051 -0.0131 0.0059 -0.0041 0.0077 -0.0034
  65. 0.0074 -0.0014 0.0025 -0.0056 0.0028 -0.0005
  66. 0.0033 -0.0000 0.0022 -0.0032 0.0012 -0.0020
  67. 0.0017 -0.0022 0.0004 -0.0011 0 0 ];
  68. Hn=Hn(1:order);
  69. N=1000;
  70. % r=wavread('C:\Matlab\work\Write_In_Paper\Shi_Yan.wav');
  71. % r=r(1:N);
  72. % r=awgn(r,snr,'measured');
  73. EE=zeros(N,1); Loop=150; mu=0.01;
  74. for nn=1:Loop
  75. r=sign(rand(N,1)-0.5); % r=awgn(r,snr,'measured');
  76. output=conv(r,Hn);
  77. %output=awgn(output,snr,'measured');
  78. win=zeros(1,order);
  79. error=zeros(1,N)';

  80. for i=order:N
  81. input=r(i:-1:i-order+1);
  82. e=output(i)-win*input;
  83. win=win+mu*e*input';
  84. error(i)=error(i)+e^2;
  85. end;
  86. EE=EE+error;
  87. end;
  88. error=EE/Loop;

  89. hold on;
  90. %error=10*log10(error(order:N));
  91. plot(error); axis tight; grid on;
  92. % semilogy(error(order:N),'r'); grid; axis tight;
  93. % subplot(212);
  94. % plot(win,'r');
  95. % hold on; plot(Hn,'b'); grid; axis tight;
复制代码

[ 本帖最后由 风花雪月 于 2006-11-12 08:11 编辑 ]

评分

1

查看全部评分

发表于 2006-8-10 13:12 | 显示全部楼层
要的就是这个!谢谢楼上的诸位!
发表于 2006-8-10 13:26 | 显示全部楼层
在“数字信号处理及其Matlab实现”(FTP中有该书)有LMS计算的函数:
  1. function [h,y] = lms(x,d,delta,N)

  2. % LMS Algorithm for Coefficient Adjustment

  3. % ----------------------------------------

  4. % [h,y] = lms(x,d,delta,N)

  5. %     h = estimated FIR filter

  6. %     y = output array y(n)

  7. %     x = input array x(n)

  8. %     d = desired array d(n), length must be same as x

  9. % delta = step size

  10. %     N = length of the FIR filter

  11. %

  12. M = length(x); y = zeros(1,M);

  13. h = zeros(1,N);

  14. for n = N:M

  15.     x1 = x(n:-1:n-N+1);

  16.      y = h * x1';

  17.      e = d(n) - y;

  18.      h = h + delta*e*x1;

  19. end
复制代码

供楼主参考。

[ 本帖最后由 风花雪月 于 2006-11-12 08:11 编辑 ]

评分

1

查看全部评分

您需要登录后才可以回帖 登录 | 我要加入

本版积分规则

QQ|小黑屋|Archiver|手机版|联系我们|声振论坛

GMT+8, 2024-5-21 02:56 , Processed in 0.058785 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表