function RO=defroPSm(Par,yy,ee,rpara,ToX)

%yy=y+nois;
%finding the optimal regularization parameter for smoothing splines 
%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
if ee==0
    RO=0;
else
[m,N]=size(yy);
[u,~]=juviP(Par,N);
si=(2*sin(pi*(0:N-1)/N)).^(Par);

if m==1
erho=@(rho)((1/N)*sum(abs(fft(yy).*(rho*si)./(rho*si+u)).^2)-ee);
else
    
   [u1,~]=juviP(Par,N);
 [u4,~]=juviP(2*Par,N);
 u2=u1'*u1;
 u2=u2.^2;

   W=si.*u1;
   W=W'*u4;
   %W=repmat(W,lx,1);
   W2=W'+W; 
    
 erho=@(rho)((1/N/m)*sum(sum(abs(fft2(yy).*(rho*W2)./(rho*W2+u2)).^2))-ee);   
end       
 
  options=optimset('TolX',ToX);
 RO = fzero(erho,rpara,options) ;
end
end
%%%%%%%%%%%%%%%%%%%%%%%
