%implements a function that does voice activity detection on a given block
%of input samples. It return's a 0 when voice is asumed to be present and a
%1 when the method assumes only noise is present
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function adapt = vad(in)
%make sure the global variabel P_ref is used (also make sure to update
%P_ref if you decided to use another window length for dectection
global P_ref;

%obtain frequency spectrum of input signal
I = fft(in);

%calculate the power in the spectrum
P = sum(abs(I.^2));

if (P > P_ref)
    adapt = 0;
else
    adapt = 1;
end