clear all;
clc;
s = serial('COM6');
set(s,'BaudRate',460800,'Parity','none','Terminator','w','Timeout',5, 'InputBufferSize',1);

 trai_pairs=30;
 out_neurons=5;
 hid_neurons=6;
 in_nodes=13;
 eata=0.1;emax=0.001;q=1;e=0;lamda=.7;  t=1;
 load backp.mat W V;
 
 Fs=8000;
 Nseconds = 1;
% 
% while(1)
% 
%     Fs=8000;
%     Nseconds = 1;
%     fprintf('say any word immediately after hitting enter');
%     fprintf('\n');
% 
%     x= wavrecord(Nseconds*Fs,Fs,'double'); 
%     y=fft(x);
%     plot(abs(y));
%     axis([-100 2000 0 10])
%     grid on;
%     flag=1;
%     for i=30:1:2000
%         if(abs(y(i))>30)
%         flag=0;
%         %i
%         break;
%         end
%     end
% 
%     if(flag==0)
%         %fprintf('we succeed');
%     
%         [kk,g] = lpc(x,12);
%         Z=(kk);
%         Z=double(Z);
%         p1=max(Z);
%         Z=Z/p1;
% 
% 
%         for p=1:trai_pairs
%         %p
%         z=transpose(Z(p,:));
%         %  calculate output
%         y=(tansig(V*(z)));
%         o=(tansig(W*(y)))
%         break
%         end
% 
% 
%     
%         bb=o(1);
%         cc=o(2);
%         dd=o(3);
%         ff=o(4);
%         gg=o(5);
%         hh=o(6);
%         jj=o(7);
%         a= max(o);
%         if (bb==a )
%         display('AHEAD');
%         fprintf(obj1,'y');
%         elseif (cc== a)
%         display('STOP');
%         fprintf(obj1,'z'); 
%         elseif (dd== a)
%         display('BACK');
%         fprintf(obj1,'x'); 
%         elseif (ff== a)
%         display('LEFT ');
%         fprintf(obj1,'v'); 
%         elseif (gg== a)
%         display('RIGHT');
%         fprintf(obj1,'w'); 
%         elseif (hh== a)
%         display('SLOW');
%         fprintf(obj1,'u'); 
%         else (jj== a)
%         display('SPEED');
%          fprintf(obj1,'t');
%         end
%     
%     end
% 
% end














while(1)
fprintf('say any word immediately after hitting enter');
input('');
x= wavrecord(Nseconds*Fs,Fs,'double');
[kk,g] = lpc(x,12);
Z=(kk);
 Z=double(Z);
 p1=max(Z);
 Z=Z/p1;


for p=1:trai_pairs
    p
    z=transpose(Z(p,:));
%  calculate output
   y=(tansig(V*(z)));
   o=(tansig(W*(y)))
   break
end


    
    b=o(1);
    c=o(2);
    d=o(3);
    e=o(4);
    f=o(5);
    a= max(o);
    if (b==a )
    display('AHEAD');
    fopen(s);
    try
    fprintf(s,'1');
    end                     
    fclose(s);

    elseif (c== a)
        display('STOP');
        fopen(s);
        try
        fprintf(s,'0');
        end                     
        fclose(s); 
        
    elseif (d== a)
        display('BACK');
        fopen(s);
        try
        fprintf(s,'2');
        end                     
        fclose(s); 
      
    elseif (e==a)
        display('LEFT');
        fopen(s);
        try
        fprintf(s,'3');
        end                     
        fclose(s); 
        
    else
        display('RIGHT');
        fopen(s);
        try
        fprintf(s,'4');
        end                     
        fclose(s); 
        
    end
end