%Funciton THR_ADAPT - Fixed threshold for general VAD criterial function.
%
%Usage: [out,Dv,Dpv]=thr_fixed(Dv,[perc]);              
%Input parameters:
%  Dv     - vector with dsitance function (criterial function)
%Optional input parameters:                                    <Default values>
%  perc   - percentage of dynamics for threshold setting                   <20>
%Output parameters:
%  out    - output vector with values 0 or 1 for pause or speech
%  Dv     - vector with current distance (input)
%  Dpv    - vector with current thresholds 

%                              Made by PP
%                             CVUT FEL K331
%                         Last change 29/03/2010

function [out,Dv,Dpv]=thr_fixed(Dv,perc);

if nargin<2,
	  perc=20;
end;

Dmax=max(Dv);
Dmin=min(Dv);
Dp=Dmin+perc/100*(Dmax-Dmin);

out=[];

wnum=length(Dv);

for i=1:wnum,

  D=Dv(i);

  if ( D>Dp ),
    out=[out 1];
  else
    out=[out 0];
  end;

end;


out=out(:);
Dv=Dv(:);
Dpv=Dp.*ones(wnum,1);  

if nargout < 1,
  figure(gcf); clf;
  x=1:length(out);
  subplot(211);
  plot(x,Dv,'r-',x,Dpv,'b--');
  title('Distance and fixed threshold');
  xlabel('Frame No.');
  ylabel('Dv');

  subplot(212);
  plot(x,out,'g.-');
  title('Final detection');
  xlabel('Frame No.');
  ylabel('out');
  hold off
end;

