function [a, err, W_list, b_list]=learn_a2(P,T,Learn_b,Lumbda,mc) %[a, err, W_list, b_list]=learn_a2(P,T,Learn_b,Lumbda,mc); %% Adaline learning rule with TF---purelin %% This is the on-line or incremental LMS learning algorithm. %% P----Input pattern %% T----Target pattern %% Set Learn_b=1 to update bias or 0 to neglect bias learning. %% Lumbda---Learning factor (0 to 1), i.e. 0.3. %% mc -- Forgetting rate, a real number between 0 and 1. %% a --- Record of NN output patterns %% err-- Record of error between target and NN output %% W_list and b_list: Progress lists %% %% [Calling example] %t=0:0.5/600:0.5;N=length(t); %k=0:N-1; %s=0.1*randn(size(t))+0.2*sin(2*pi*20*(1+0.02*randn(size(t))).*t)+... % 0.08*sin(2*pi*40*(1+0.01*randn(size(t))).*t); %v=2.2*sin(2*pi*60*t)+0.8*randn(size(k)); %m=2*(0.23*sin(2*pi*60*t+pi/2)+0.07*randn(size(k))); %c=s+m; %P=[v;[0 v(1:N-1)];[0 0 v(1:N-2)];[0 0 0 v(1:N-3)];... % [0 0 0 0 v(1:N-4)];[0 0 0 0 0 v(1:N-5)];... % [0 0 0 0 0 0 v(1:N-6)];]; %[S R]=size(P); %T=c; %[a e w_list]=learn_a2(P,T,0,0.015,0); %figure;subplot(211);plot(t,s,'.',t,e);axis([0 0.5 -1 1]) %title('Adaptive noise cancellation example, Signal and Restored'); %subplot(212);plot(t,s,'.',t,c);axis([0 0.5 -1 1]) %title('Signal and Contaminated signal') %xlabel('Samples'); % Written by PenChen Chou, 2002-7-18 % Get R [S, R]=size(P);[U, garbage]=size(T); if R~=length(T) error('===>I/O sample size is not matched'); end % Set defaults if nargin<=4 mc=0; end if nargin<=3 Lumbda=0.3; end if nargin<=2 Learn_b=0; end % Initiallization W_list=[]; b_list=[]; % W=randn(U,S); b=randn(U,1); % W=zeros(U,S); b=randn(U,1); W= [1.3 2 -0.4 1 1.2 -1.7 -1.7 0.1 -1 0]; b=randn(U,1); % Conditionally collected W_list=W'; b_list=b; % Search weight W and bias b. fprintf('\n {Now running on-line learn_a2.} \n'); old_dw=0*W; old_db=0*b; if mc>=1, mc=1; end; for i=1:R; n=W*P(:,i); % Get NN output if Learn_b n=n+b; end a(:,i)=n; % It means purelin err(:,i)=T(:,i)-a(:,i); % Find err dw=mc*old_dw+(1-mc)*Lumbda*err(:,i)*[P(:,i)]'; W=W+dw; old_dw=dw; if Learn_b db=mc*old_db+(1-mc)*Lumbda*err(:,i); b=b+db; old_db=db; end W_list=[W_list W']; b_list=[b_list b]; end if ~Learn_b b_list=0*b_list; end % Remove the last one of W_list [N1, N2]=size(W_list); W_list(:,N2)=[]; % Print out Err=sum(sum(err.^2)); fprintf(' ===>Sum of squared err = %6.4f\n',Err); fprintf(' {On-line learn_a2. execution completed} \n'); % End of this function file.