function [I,O,perf]= main()

clc, clear all, close all;

%% Display data into variables

Cd = cdata(); %cartao
Td = tdata(); %tapete
Pd = pdata(); %preto
Ld = toalhadata(); %toalha

%% T,H & H/T
%% Extract steps 
[Ld_inpn, Ld_inph, Ld_inpu] = linput(Ld);
[Pd_inpn, Pd_inph, Pd_inpu] = pinput(Pd);
[Td_inpn, Td_inph, Td_inpu] = tinput(Td);
[Cd_inpn, Cd_inph, Cd_inpu] = cinput(Cd);

%% Pad

aux0=max(cellfun('size',Cd_inpn,1));
aux1=max(cellfun('size',Td_inpn,1));
aux2=max(cellfun('size',Pd_inpn,1));
aux3=max(cellfun('size',Ld_inpn,1)); %- min(cellfun('size',Cd_inpu,1));

AUX=max([aux0,aux1,aux2,aux3]);

for HH=1:1:size(Cd_inpn,2)
    Cd_inpn{HH}=padarray(Cd_inpn{HH},(AUX-size(Cd_inpn{HH},1)),0,'post');
    Cd_inph{HH}=padarray(Cd_inph{HH},(AUX-size(Cd_inph{HH},1)),0,'post');
    Cd_inpu{HH}=padarray(Cd_inpu{HH},(AUX-size(Cd_inpu{HH},1)),0,'post');
end

for HH=1:1:size(Pd_inpn,2)
    Pd_inpn{HH}=padarray(Pd_inpn{HH},(AUX-size(Pd_inpn{HH},1)),0,'post');
    Pd_inph{HH}=padarray(Pd_inph{HH},(AUX-size(Pd_inph{HH},1)),0,'post');
    Pd_inpu{HH}=padarray(Pd_inpu{HH},(AUX-size(Pd_inpu{HH},1)),0,'post');
end

for HH=1:1:size(Td_inpn,2)
    Td_inpn{HH}=padarray(Td_inpn{HH},(AUX-size(Td_inpn{HH},1)),0,'post');
    Td_inph{HH}=padarray(Td_inph{HH},(AUX-size(Td_inph{HH},1)),0,'post');
    Td_inpu{HH}=padarray(Td_inpu{HH},(AUX-size(Td_inpu{HH},1)),0,'post');
end

for HH=1:1:size(Ld_inpn,2)
    Ld_inpn{HH}=padarray(Ld_inpn{HH},(AUX-size(Ld_inpn{HH},1)),0,'post');
    Ld_inph{HH}=padarray(Ld_inph{HH},(AUX-size(Ld_inph{HH},1)),0,'post');
    Ld_inpu{HH}=padarray(Ld_inpu{HH},(AUX-size(Ld_inpu{HH},1)),0,'post');
end

%% Input (raw data)

Iaux= [Cd_inph, Td_inph, Pd_inph, Ld_inph];
for HH=1:1:size(Iaux,2)
   I(:,HH)=Iaux{HH}; 
end

%% manipulated data (FFT)
% for HH=1:1:size(I,2)
% IN(:,HH)=fft(I(:,HH));
% end
% 
% for HH=1:1:size(I,2)
% Input(:,HH)=abs(IN(:,HH));
% end
% 
% figure(1);
% for HH=1:1:106
%     plot(abs(Input(1:end/2,HH))); hold on
% end
% figure(2);
% 
% for HH=107:1:107+45
%     plot(abs(Input(1:end/2,HH))); hold on
% end

%% Output

for o=1:1:size(Cd_inpn,2)
    O(:,o) = [1;0;0;0];
end

for o=size(Cd_inpn,2):1:size(Td_inpn,2)+size(Cd_inpn,2)
    O(:,o) = [0;1;0;0];
end



for o=size(Td_inpn,2)+size(Cd_inpn,2):1:size(Td_inpn,2)+size(Cd_inpn,2)+size(Pd_inpn,2)
    O(:,o) = [0;0;1;0];
end

for o=size(Td_inpn,2)+size(Cd_inpn,2)+size(Pd_inpn,2):1:size(Td_inpn,2)+size(Cd_inpn,2)+size(Pd_inpn,2)+size(Ld_inpn,2)
    O(:,o) = [0;0;0;1];
end

for zz=1:1:352
    if zz < 107
        
        kk(zz,1)= 'a';
    elseif zz > 106 && zz < 152
        kk(zz,1) ='b'
    elseif zz > 152 && zz < 238
        kk(zz,1) ='c';
    else 
        kk(zz,1) ='d';
    end
end

AA=table(I', kk);

%sz=[size(Cd_inpn,2),size(Pd_inpn,2),size(Td_inpn,2),size(Ld_inpn,2)];

%%NN
perf =1;
save_min=1;
auxx=1;
HL=17;
auxxx=1;
while (perf > 0.015)
    auxx=auxx+1;
    if (auxx < 20000)
        net1 = patternnet(HL); % layers
        net1.trainparam.epochs=1000;
        net1.trainparam.goal=0;
        net1 = train(net1,I,O);

        y=net1(I);
        perf= perform(net1,O,y);

        if perf<save_min
        save_min=perf
        auxxx=HL
        end

    else 
    auxx =1;
    HL= HL +1;
    end
end




end




