clear; clc;
if (matlabpool('size')==0)
  matlabpool open;
end;

num_anfis_inputs=140;

%mode='anfis';
mode='nn';
%mode='nnv';

tic;
fprintf('Load train data...\n');
traindir = 'train1';  %nacita treningove vzorky do traindir

[inp,tgt]=wavload(traindir,num_anfis_inputs);

%% train
fprintf('Train...\n');

switch mode
    case 'anfis'
        epoch_n = 3;
        parfor i=1:size(tgt,2)
            fprintf(' %d...\n',i);
            in_fis(i) = genfis2(inp,tgt(:,i),.20);
            %in_fis(i) = genfis2(inp,tgt(:,i),.3);
            out_fis(i) = anfis([inp tgt(:,i)],in_fis(i),epoch_n,zeros(1,4));
            res(:,i) = evalfis(inp,out_fis(i));
        end
    case 'nn'
        nnet = feedforwardnet([10 10 8 8]);
        nnet = configure(nnet, inp', tgt');
        nnet.trainParam.epochs = 50;
        nnet.trainParam.goal = 0.0005;
        nnet.trainParam.min_grad=1e-10;
        nnet.trainParam.max_fail=10;
        nnet = train(nnet,inp',tgt');
        res = sim(nnet, inp')';
    case 'nnv'
        nnet = netinit(size(inp,2), size(tgt,2));
        nnet = netlearn(nnet, inp, tgt, 2000);
        res = neteval(nnet, inp);
end
%% test data

fprintf('Eval. test data...\n');
testdir = 'test1';  %nacita treningove vzorky do traindir

[tinp,ttgt]=wavload(testdir,num_anfis_inputs);

switch mode
    case 'anfis'
        warning('off', 'Fuzzy:evalfis:InputOutOfRange');
        tres=[];
        for i=1:size(tgt,2)
            fprintf(' %d...\n',i);
            tres(:,i)=evalfis(tinp,out_fis(i));
        end
    case 'nn'
        tres = sim(nnet', tinp')';
    case 'nnv'
        tres = neteval(nnet, tinp);
end

figure(1);
colormap(summer);
tres(tres<0)=0;
barh(tres./(sum(tres,2)*[1 1 1]),'stacked','DisplayName','tres ratios');
xlim([0 1]);
legend('czech','hungarian','english');
%%
toc;
fprintf('End.\n');
