function RO=D_defroP(Par,yy,ee,m,rpara,ToX)


%finding the optimal regularization parameter for discrete smoothing splines

%yy=y+nois;
%m span of the spline space
%rpara-- range for parameter search
%ToX--termination tolerance on the solution
%ee--estimated squared norm of errors 
%Par -- order of approximating spline should be even number

[mm,N]=size(yy);
[~, ~, ~, u]=per_dbspP(2^m*N,m,Par);
%[ u]=per_dspUP(2^m*N,m,Par);
n=0:N-1;
   si=sin(pi*n/N);
   W=(2^(1-m)*si).^Par;

if mm==1
erho=@(rho)((1/N)*sum(abs(fft(yy).*(rho*W)./(rho*W+u)).^2)-ee);
else
    u=u'*u;
    %W=W'*W;
    W=repmat(W,N,1); 
  
    W=W'+W;
 erho=@(rho)((1/N/mm)*sum(sum(abs(fft2(yy).*(rho*W)./(rho*W+u)).^2))-ee);   
end       
 
  options=optimset('TolX',ToX);
 RO = fzero(erho,rpara,options) ;
  end
   % Copyright (c) 2013. Valery A. Zheludev