%function that sets a reference for the vad. Therefore the power included in a certain time
%frame of noise (input from the user) is calculated
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [Pref,P_ref] = call_Pref(signal,framelength)
%make sure some global variabels are used
global adapt;

display('Calculation of a power reference started.'),tic

k = 1;
while (length(signal)-k*framelength >=0)
    
    %read in the samples
    in = signal((k-1)*framelength+1:k*framelength);

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

    %calculate the power in the spectrum
    Pref(k) = sum(abs(I.^2));
    
    k = k+1;
end

%calculate the mean during periods of non speech activity on the basis of
%the adapt signal
P_ref = 0;
for i = 1:1:length(Pref)
    if (adapt(framelength*(i-1)+1) == 1)
        P_ref = P_ref+Pref(i);
    end
    
end
P_ref = P_ref/length(Pref);
display('Calculation of a power reference finished.'),toc