%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Simple pattern (letter) recognition 
% based on threshold perceptron and delta rule
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 clear; clf;
 nIn =156; nOut=26;
 wOut=1*(rand(nOut,nIn)-0.5);
 rOut=zeros(nOut,1);
 
% training vectors (Letters)
 load pattern1;
 letterVectors=reshape(pattern1', 12*13, 26);
 rDes=diag(ones(1,26));

% Updating and training network 
% with sigmoid activation function and delta rule
 for sweep=1:1000;
     for i=1:26
        rIn=letterVectors(:,i);
        rOut=(wOut*rIn)>0.5;
        wOut=wOut+0.1*(rDes(:,i)-rOut)*rIn';
     end
 
     % test all pattern
     distH=0;
     for i=1:26
        rIn=letterVectors(:,i);
        rOut=(wOut*rIn)>0.5;
        distH=distH+ sum((rDes(:,i)-rOut).^2);
     end
       error(sweep)=distH;
       disp([sweep, distH]);
 end

 plot(error)
 title('threshold perceptron')
 xlabel('# training steps')
 ylabel('average error (Hamming distance)')
 
 count=0;
 for iNoise = 0:1:90;      % loop over noise level
     count=count+1;
     noise(count)=iNoise;
     for iTrial=1:10;           % loop over trails
        distH(iTrial)=0;
        for iLetter=1:26                 % loop over letter set
            rIn=letterVectors(:,iLetter);
            noiseVec=ranBinVec(156,iNoise*156/100);
            rIn(noiseVec==1)=rIn(noiseVec==1)==0;
            rOut=(wOut*rIn)>0.5;
            distH(iTrial)= distH(iTrial) + sum((rDes(:,iLetter)-rOut).^2);
        end
     end
     meanDistH(count)=mean(distH);
     stdDistH(count)=std(distH);
 end

 figure;
 errorbar(noise,meanDistH,stdDistH)
 title('threshold perceptron')
 axis([0 90 0 26]);
 xlabel('noise [%]')
 ylabel('average Hamming distance')