%D_smooth_example_opt_2dP
ima1=input('image: Barbara--1;Boat--2; Finger--3; Fabric--4; Lena--5; 0--former?')
if ima1==1
   load Bloch_ba
elseif ima1==2
   load Bloch_bo
elseif ima1==3
   load Bloch_fi
   
elseif ima1==4
   load Bloch_fa
elseif ima1==5
   load Bloch_len
   
end 


seg1=input('segment:11,or 12,or 21,or 22;33--whole image;0--former; ?')
if seg1~=0
    seg=seg1;
end
if seg==33
    blo=bloch;
else
eval(['blo=bloch' int2str(seg) ';'])
end
[vb M]=size(blo);

ToX=.0001;
range_rho1=100000000000;%input('range_rho1; 111-former; ?')
if range_rho1~=111
    range_rho=[0 range_rho1];
end

 Par =input('spline order for sd; ?')
 Ll=input(' depth of downsampling;0 for none ?')

st1=input('std noise; 111-former; ?')

if st1~=111
    noiss=randn(vb);
 st=st1;  
end
nois=st*noiss; 
nblo=blo+nois;

if Ll>=0;
figure;
subplot(2,2,1)
deci=2^Ll;
D=(1:deci:M);

noi=nois(D,D);
x=nblo(D,D);

ee=sum(noi(:).^2);
if range_rho(2)>0
  
%rho=defroB0(Par,x,ee,BSUV,range_rho,ToX);
rho=D_defroP(Par,x,ee,Ll,range_rho,ToX);
else
    rho=0;
end

s=D_smoothsplaP_2d(x,Par,Ll,rho) ;
ai(x);
ste=256/deci;
set(gca,'YTick',0:ste:2*ste);set(gca,'YTickLabel',{})
set(gca,'XTick',0:ste:2*ste);set(gca,'XTickLabel',{})
if Ll==0
    [PSNR mablod]=psnB(blo,x);
title(['\fontsize{16}Sampl=' int2str(deci) ':1, STD=' num2str(st) ', PSNR=' num2str(PSNR)])
else
title(['\fontsize{16}Sampl=' int2str(deci) ':1, STD=' num2str(st)])
end
subplot(2,2,2)
[PSNR mablod]=psnB(blo,s);

ai(s)
title(['\fontsize{16}Spl. order: '  int2str(Par) ', PSNR=' num2str(PSNR,4 ) ', \rho=' num2str(rho,2) ])
%title(['\fontsize{16}\rho=' num2str(rho)])
set(gca,'YTick',0:256:512);set(gca,'YTickLabel',{})
set(gca,'XTick',0:256:512);set(gca,'XTickLabel', {})


subplot(2,2,3)
%deci=2*deci;
lev=log2(deci);
D=(1:deci:M);

nois=4*st*noiss; 
nblo=blo+nois;
x=nblo(D,D);
noi=nois(D,D);
ee=sum(noi(:).^2);
if range_rho(2)>0
 rho=D_defroB(Par,x,ee,Ll,range_rho,ToX);
else
    rho=0;
end

s=D_smoothsplaB_2d(x,Par,Ll,rho) ;

ai(x);
ste=256/deci;
set(gca,'YTick',0:ste:2*ste);set(gca,'YTickLabel',{})
set(gca,'XTick',0:ste:2*ste);set(gca,'XTickLabel',{})
%title(['\fontsize{16} Sampling=1:' int2str(deci)])
%title(['\fontsize{16}Sampl=1:' int2str(deci) ', STD=' num2str(4*st)])
if Ll==0
    [PSNR mablod]=psnB(blo,x);
title(['\fontsize{16}Sampl=1:' int2str(deci) ', STD=' num2str(4*st) ', PSNR=' num2str(PSNR)])
else
title(['\fontsize{16}Sampl=1:' int2str(deci) ', STD=' num2str(4*st)])
end

subplot(2,2,4)
[PSNR mablod]=psnB(blo,s);

ai(s)
title(['\fontsize{16}Spl. order: '  int2str(Par) ', PSNR=' num2str(PSNR,4 ) ', \rho=' num2str(rho,2) ])
%title(['\fontsize{16}\rho=' num2str(rho)])
set(gca,'YTick',0:256:512);set(gca,'YTickLabel',{})
set(gca,'XTick',0:256:512);set(gca,'XTickLabel', {})
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

elseif Ll<0
 figure;
subplot(2,2,1)
deci=2^Ll;
% D=(1:deci:M);
% 
 noi=nois;
%x=nblo(D,D);
x=nblo;

ee=sum(noi(:).^2);
if range_rho(2)>0
  
%rho=defroB0(Par,x,ee,BSUV,range_rho,ToX);
rho=D_defroB(Par,x,ee,-Ll,range_rho,ToX);
else
    rho=0;
end
deci=2^Ll;
s=D_smoothsplaB_2d(x,Par,-Ll,rho) ;
ai(x);
ste=256/deci;
set(gca,'YTick',0:256:512);set(gca,'YTickLabel',{})
set(gca,'XTick',0:256:512);set(gca,'XTickLabel', {})

title(['\fontsize{16}STD=' num2str(st)])
%end
subplot(2,2,2)
%[PSNR mablod]=psnB(blo,s);

ai(s)
title(['\fontsize{16}Spl. order: '  int2str(Par)  ', Sampl=1:1/' int2str(1/deci) ',  \rho=' num2str(rho,2) ])
%title(['\fontsize{16}\rho=' num2str(rho)])

set(gca,'YTick',0:ste:2*ste);set(gca,'YTickLabel',{})
set(gca,'XTick',0:ste:2*ste);set(gca,'XTickLabel',{})

subplot(2,2,3)
%deci=2*deci;
lev=log2(deci);
D=(1:deci:M);

nois=4*st*noiss; 
nblo=blo+nois;
x=nblo;
noi=nois;
ee=sum(noi(:).^2);
if range_rho(2)>0
 rho=D_defroB(Par,x,ee,-Ll,range_rho,ToX);
else
    rho=0;
end

s=D_smoothsplaB_2d(x,Par,-Ll,rho) ;

ai(x);
ste=256/deci;
set(gca,'YTick',0:256:512);set(gca,'YTickLabel',{})
set(gca,'XTick',0:256:512);set(gca,'XTickLabel', {})  
%title(['\fontsize{16} Sampling=1:' int2str(deci)])
%title(['\fontsize{16}Sampl=1:' int2str(deci) ', STD=' num2str(4*st)])
title(['\fontsize{16}STD=' num2str(4*st)])

subplot(2,2,4)
%[PSNR mablod]=psnB(blo,s);

ai(s)
title(['\fontsize{16}Spl. order: '  int2str(Par)  ', Sampl=1:1/' int2str(1/deci) ',  \rho=' num2str(rho,2) ])

 
set(gca,'YTick',0:ste:2*ste);set(gca,'YTickLabel',{})
set(gca,'XTick',0:ste:2*ste);set(gca,'XTickLabel',{})
end
 