📄 lms_algorithm.m
字号:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% This program simulates plant identification least mean square (NLMS) alogrithm %
% reference: 《LMS算法的频域快速实现》 % %
% Created originally by Ni Min,Chen Zuo and Mao Xinsheng on Nov 11,2007 %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [w1,lms_mse] = LMS_algorithm(x,d,u,N,num);
%generator1: [lms_mse] = LMS_algorithm(x,d,0.0002,64,num);
%generator2: [lms_mse] = LMS_algorithm(x,d,0.01,64,num);
% Input parameters:
% u: step size
% N: number of weights
% x: input signal
% d: output signal
% y: expectation output sequence of the AF
% e: the difference sequence
% K: experiment time
% w: weight
% #: edit up to user
%----------------------------------------------------%
% ---------------initialization----------------------%
K=length(x); % length of input signal
y=zeros(1,K); % the practical output of the idetification plant
w1=zeros(1,N); % the weight vector
wk=zeros((K-N+1),N); % store each weight value
e=zeros(1,K); % the error signal of d and y
lms_mse=zeros(1,K); % the mean square error
block=11; % the size used to smooth the mse
%-------------lms algorithm begin--------------------%
x=[zeros(1,N-1),x];
for n=N:K+N-1
xb=x(n:-1:n-N+1); % input signal is delayed by the N weights
y(n-N+1)=w1*xb';
e(n-N+1)=d(n-N+1)-y(n-N+1);
w1=w1+2*u*e(n-N+1)*xb;
wk((n-N+1),:)=w1; % store weight every time
end;
%-------------lms algorithm end----------------------%
%----------compute the mean square error-------------% % compute the mean square error
E=e.^2;
E1=[zeros(1,fix(block/2)),E,zeros(1,fix(block/2)+1)];
for i=1:K
sum_E=0;
for j=1:block
sum_E=sum_E+E1(j+i);
end
lms_mse(i)=sum_E/block;
end
%-----------plot the weight comparison---------------% % plot the weight comparison
figure(1);
stem(1:N,num,'r');
hold on;
stem(1:N,w1(1:N),'b');
title('weight comparison');
xlabel('weight index');
ylabel('weight value');
%-----------plot the mse-----------------------------% % plot the mse
figure(2);
plot(1:K,lms_mse);
title('mean square error');
xlabel('iteration n');
ylabel('mse');
%-------display the mismatch--------------------------% % display the mismatch
MIS=10*log10(norm(num-w1(1:N))/(norm(num)));
disp('LMS_MIS=');
disp(MIS);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -