function [VADi]=detektor(vst_signal,delka_ramce,init)

%funkce pro detekci recove aktivity zalozena na kombinaci vystupu tri
%detektoru (energeticky, intenzity a kepstralni)
%
% Priklad spusteni funkce:  [VADi]=detektor('franek',256,10)
% y vraci vzorkova data. Amplitudove hodnoty jsou v rozsahu [-1,+1]
% vst_signal = 'franek';
%
% 2008   Michal Adamec VUT Brno (v ramci Diplomove prace - Moderni rozpoznavace reci)

[y,Fvz] = wavread(vst_signal);
signal      = y;
t           = (0:length(signal)-1)/Fvz;                                         %vektor poctu vzorku signalu y. Je potrebny k vykresleni grafu.
[cepst]=vadcepstralni(vst_signal,delka_ramce,init,0.3,0.6,0,0);
[intenz]=vadintenz(vst_signal,delka_ramce,init,0.1,0.2,0,0);
[v,cas,pocet_ramcu]=vadenerg(vst_signal,delka_ramce,init,0.2,0.1,0,0);
cas  = cas';
init = 10;
VAD(init,1) = 0;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%% vicebodove rozhodovaci pravidlo %%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for i = init:pocet_ramcu

    flag = 0;
    if VAD(i-2,1) == 1 && VAD(i-1,1) == 1 && cepst(i,1)==0 && v(i,1)==0         % sestupna hrana
        VAD(i-2,1)= 0;
        VAD(i-1,1)= 0;
        VAD(i,1) = cepst(i,1) ;
        flag = 1;


    elseif VAD(i-2,1) == 0 && VAD(i-1,1) == 0 && intenz(i,1)==1 && flag == 0;    % nabezna hrana
        VAD(i,1) = intenz(i,1) ;
        flag = 1;
    elseif VAD(i-2,1) == 0 && VAD(i-1,1) == 1 && flag == 0;                     % prechod 0/1
        flag = 1;
        VAD(i,1) = 1 ;

    elseif VAD(i-2,1) == 1 && VAD(i-1,1) == 0 && flag == 0;                     % prechod 1/0
        flag = 1;
        VAD(i,1) = 0 ;

    elseif flag==0
        VAD(i,1) = median([v(i,1) intenz(i,1) cepst(i,1)]);
    end
    if flag ==0
        VAD(i,1) = median([VAD(i-2,1) VAD(i-1,1) VAD(i,1)]);
    end
    VADi(i-2,1) = VAD(i-2,1);
    VADi(i-1,1) = VAD(i-1,1);
    VADi(i,1) = VAD(i,1);
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% Graficke vyjadreni %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

figure(8)
plot(t,signal,cas,VADi)
xlabel('Cas (s)')
ylabel('')
title('Modifikovany detektor')
legend('signal', 'detekce', 'modifik')
