function [y]= NNEMG(x_in, t,clsd) 
persistent indx;
persistent trainFcn;  % Bayesian Regularization backpropagation.%
persistent inputDelays;
persistent feedbackDelays;
persistent hiddenLayerSize;
persistent net;
persistent xfo;
persistent last_t;
persistent afo;%for saving net state after running/training steps
persistent delaySize;
delaySize = 4;
%mlock
persistent x;

%x = [x;u]
if(not(isequal(x_in,[]) && isequal(x_in,x(size(x,1),:))))
    %we suppose that emg signal never can be equal, anywaay euql states do not needs computation
   
    disp('espando il vettore samples set');
    if(isempty(x))
        x = x_in;
    else
        x = [x,x_in];
    end
end

if(isequal(indx,[]))
    disp('inizializzo narnx');
    indx = 0
    %persistent trainFcn;  % Bayesian Regularization backpropagation.
    trainFcn = 'trainbr';
    % Create a Nonlinear Autoregressive Network with External Input
    
    %persistent inputDelays;
    inputDelays = 1:delaySize;
    %persistent feedbackDelays;
    feedbackDelays = 1:delaySize;
    %persistent hiddenLayerSize;
    hiddenLayerSize = 10;
    %persistent net;
    net = narxnet(inputDelays,feedbackDelays,hiddenLayerSize,'open',trainFcn);
    
    % Choose Input and Feedback Pre/Post-Processing Functions
    net.inputs{1}.processFcns = {'removeconstantrows','mapminmax'};
    net.inputs{2}.processFcns = {'removeconstantrows','mapminmax'};
    
    
    
    % Choose a Performance Function
    % For a list of all performance functions type: help nnperformance
    net.performFcn = 'mse';  % Mean Squared Error
    net.name = [net.name ' - open'];
    disp('size:');
     net.inputs{:}.size
     net.inputs{1}.size
    % Choose Plot Functions
    % For a list of all plot functions type: help nnplot
    %net.plotFcns = {'plotperform','plottrainstate', 'ploterrhist', ...
    %  'plotregression', 'plotresponse', 'ploterrcorr', 'plotinerrcorr'};
    
    
end

%for each function call put the array in a input signal matrix,
%then if the matrix is full test the condition 'clsd' and run preparets and
%corrispective nnet(this must preserve his state along varius function
%calls
if(size(x,2) == delaySize+1 )%+1
        indx = indx + 1
        X = x; %X = tonndata(x,true,true)
        x = []; %empty the buffer matrix for next function call
    if(not(clsd))
        
       
        
        
        T = tonndata(t((delaySize)*indx-(delaySize-1):((delaySize)*indx)+1),true,true)%t((delaySize)*indx-((delaySize):(delaySize)*indx:(delaySize)  next delaySize row of the trining result dataset t
        last_t = T;
        %X = num2cell(X)
        % view(net)
        
        
        [x1,xf,af,ts] = preparets(net,X,{},T);
        %[y,xfo,afo] = net(x1,xio,aio);
        [net,y,E,xfo,afo] = adapt(net,x1,ts,xf,af);
        
        disp('dopo adapt ho afo: ');
        last_t = [last_t(2:size(last_t,2)),y];
        afo
        assignin('base','Y',y)
         
    else
        
        
        % Next the the network and its final states will be converted to
        % closed-loop form to make  predictions with only the  inputs
        % provided.
        if(isequal(net.name,['NARX Neural Network - open']))
            [net] = closeloop(net);%[net,xic,aic] = closeloop(net,xfo,afo);
            net.name = ['NARX Neural Network - closed'];
            [x2,xic,aic,ts] = preparets(net,X,{},last_t);
           
            [y,xfo,afo] = net(x2,xic,aic); %adapt(net);
            last_t = [last_t(2:size(last_t,2)),y]; 
        
        %multiStepPerformance = perform(net,T(1,predictOutputTimesteps),y2)
        else
            disp('valore ottenuto da loop chiuso oltre la prima simulazione:');
            x2 = preparets(net,X);
            [y,xfo,afo] = net(x2,xfo,afo);
            y
            last_t = [last_t(2:size(last_t,2)),y]; 
        end
    end
end