function [RS,tau]=herst(x,NK)
%Âû÷èñëåíèå ïîêàçàòåë? Õåðñòà
%  [RS,tau]=herst(x,[NK])

N=length(x);
if nargin<2
   NK=nextpow2(N);   % Âîçìîæíîå êîë-âî äåëåíèé èñõîäíîãî ð?äà íà 2
   if (2 .^NK > N)
      NK = NK-1;
   end
   NK=NK-2;
end

RS=zeros(NK,1);
tau=zeros(NK,1);
R=zeros(NK,1);
kk=0;
for k=1:NK,
   NN=N/(2 .^(k-1));
   disp([k NN])
%   RSL=zeros(2 .^(k-1),1);
   for l=1:2 .^(k-1),
      l1=NN*(l-1)+1; l2=NN*l;
      X=cumsum(x(l1:l2)-mean(x(l1:l2)));
      s=std(x(l1:l2));
      kk=kk+1;
      if (s==0)
         RS(kk)=inf;
      else
         RS(kk)=( max(X)-min(X) ) ./ s;
      end
      tau(kk)=NN;
   end
%   RS(k)=mean(RSL);
%   tau(k)=NN;
end
