%RMP_runD

%--------------------------- Load Parameters ----------------------------------


params = loadParametersMP_D();

imageFileName = params{1};
levs = str2double(params{3});
levels =1:levs;
no_iter = str2double(params{4});
spl_ord= str2double(params{2});


time_param = str2double(params{5});

noiseFactor = str2double(params{7});% STD of Gaussian noise

 % wavelet packet's order;

changeNoise = params{8};

%preparation of the orinal image 'bloch', blurred image 'cloch', noised blurred input 'Z', 
%  blurring kernel 'ker_nel', its DFT 'fker_nel' and additive noise


%blurring the  original array 

[bloch, cloch, Z,  fker_nel,ker_nel,nois,fnois] = buildInputFunctions_RMP_D( changeNoise, noiseFactor, time_param,  imageFileName);
%
[ln,hn]=size(bloch);
%calculation of SHA spectra of  wavelet packets and  of DFT of testing Theta waveforms

[w_coef,t_coef ]=D_wP_theta_coefP(spl_ord,ln,levs,fker_nel);


norm_t=tnorrP(t_coef,levels);
[~,O]=sort(norm_t(:,4),'descend');

list=norm_t(O,2:4);
[vt ~]=size(list)
 list=[list (1:vt)'] ;
fZ=transpose(fft(Z));

band= str2double(params{6});
if band==0
    fnois_m=fnois;
    else 

fnois_m=D_nois_model(fZ,band);
end    
[vn,hn]=size(fnois_m);
rn=randperm(vn);

[alpha_comb, ~, ~, ~, ~] =D_purs_optP(list, t_coef,fnois_m(rn(1),:), fZ(rn(1),:));
[al, ial] =sort(abs(alpha_comb(:,1)),'descend');
mal=min(al);
figure(31);bar(al);title(['Oblique projection coefficients for selected Theta waveforms, min=' num2str(mal)])
T1=input('threshold for stopping;0--former; ?')        
if T1>0
    T=T1;
end

fl=find(abs(alpha_comb(:,1))>T);

list=list(fl,:)

lsl=length(fl);
[sonorm,O]=sort(list(:,3),'descend');
if time_param==0
    T0=lsl;
else
figure(33);bar(sonorm);title('Sorted norms of Theta waveforms')
 T10=input('number of Theta waveforms;0--former; ?')
if T10>0
    T0=T10;
end
end
list=list(1:T0,:);



iter = str2double(params{4});


tic
CF=[];OR=[];






conv_func=[];original2=[];ITE=[];
decreg2=[];decwin2=[];
RHO=[];

tic
for kk=1:hn
    fN=fnois_m(kk,:);
  %regularized matching pursuit of the kk column of the array Z
    [conv_func1, original1,  ite,  list0,Lambda] = D_rm_pursP(fZ(kk,:), iter,w_coef, t_coef, list,T,fN);

original2=[original2 original1'];
conv_func=[conv_func; conv_func1];

[kk;ite]
 %RHO=[RHO rhodec];
ITE=[ITE ite];
end
toc
original=original2.*(original2>0).*(original2<=255)+255*(original2>255);
conv_func=conv_func';

D_rmp_displayP(hn,bloch,Z,original)

% Copyright (c) 2013. Valery A. Zheludev 