matlab lms 算法实例
- function [wn,en]=lms(xn,dn,M,delt,varargin)
- % LMS Algorithm ,返回滤波器加权系数矩阵和误差向量
- %
- % 调用格式
- % [wn,en]=lms(xn,dn,M,delt,itr)
- % en=滤波器输出和d(n)的误差序列,为列向量
- % wn=滤波器的加权参量序列,为一矩阵,其每行代表一个加权参量,每列代表一次迭代;初始化值设为0
- % xn=输入列向量信号
- % dn=期望列向量信号
- % M=滤波器阶数
- % delt=步长
- % itr=迭代次数
- %
- % [wn,en]=lms(xn,dn,M,delt)
- % 迭代次数为默认值,即等于x(n)的点数
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- % 输入输出参数的检查
- %--------------------------------------------------------------------------
- vin=length(varargin); Item=vin+4;
- error(nargchk(4,Item,nargin)); % 检查输入变量数目是否合适,其中前四个参数必须输入
- if nargout>2 % 检查输出变量数目是否合适
- error('Too many output arguments');
- end
- %------------------------------------------------------------------------
- Nx=length(xn); % x(n)的长度
- if Nx~=length(dn) % 检查x(n)和d(n)长度是否相等
- error('The length of x(n) is not equal to that of d(n)');
- end
- %------------------------------------------------------------------------
- sizex=size(xn); % 检查输入向量是否为列向量
- if sizex(1)<sizex(2)
- xn=xn.';
- end
- sizedn=size(dn); % 检查期望信号向量是否为列向量
- if sizedn(1)<sizedn(2)
- dn=dn.';
- end
- %-------------------------------------------------------------------------
- itr=Nx; % 迭代次数取默认值
- %-------------------------------------------------------------------------
- % 当输入变量为5个时
- if Item==5 % 确定迭代次数
- itr=varargin{1};
- if itr>Nx | itr<M
- error('Too many or too few iterations');
- end
- end
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- % 参数的初始化
- en=zeros(itr,1);
- wn=zeros(M,itr); % 每行代表一个加权参量,每列代表一次迭代
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- % 迭代计算
- for k=M:itr % 第k次迭代
- x_tap=xn(k:-1:k-M+1);
- en(k)=dn(k)-wn(:,k-1)'*x_tap;
- wn(:,k)=wn(:,k-1)+2*delt*en(k,1)*x_tap;
- end
复制代码 |