function [out]=learnedrecon(I,mask,WT,init)
% 
%--------------------------------------------------------------------------
% part of codes for
% [1]  Joy A, Paul J S. "Multi-channel MR Image Reconstruction using
%      Statistically Optimized Non-Linear Diffusion", Magnetic Resonance in 
%      Medicine, June 2017, doi:10.1002/mrm.26774.
% Email: ajin.joy@iiitmk.ac.in, j.paul@iiitmk.ac.in
%
% Coded by : Ajin Joy on March 2017
%--------------------------------------------------------------------------

[M,N]     = size(mask);
soft      =@(x,t) sign(x).*(((abs(x)-t)+abs((abs(x)-t)))/2);
RLNE      =@(x,y) norm(x(:)-y(:),2)./norm(x(:),2);
FTf       =@(x)   (1/sqrt(M*N)*fftshift(fft2(ifftshift(x)))).*mask; % fourier transform with sampling
FTi       =@(x)   sqrt(M*N)*fftshift(ifft2(ifftshift(x.*mask))); % inverse fourier transform
K         = FTf(I);
Irec      = init;
Irec_last = Irec;
Irec_hat  = Irec;
numitr    = 200;
tol       = 1e-5;
lambda    = 1e-4;
gamma     = 1;
t_last    = 1;
err       = zeros(1,numitr);
tim       = zeros(1,numitr);
tim_start = cputime;
threshold = lambda*gamma;

for k=1:numitr
    Irec       = WT'*soft(WT*(Irec_hat + gamma*(FTi(K - FTf(Irec_hat)))),threshold);
    t_now      = (1 + sqrt(1 + 4*t_last*t_last))/2;
    Irec_hat   = Irec + (t_last - 1)/t_now*(Irec - Irec_last);
    t_last     = t_now;
    err(k)     = RLNE(I,Irec);
    tim(k)     = cputime - tim_start;
    if k>1, if RLNE(Irec_last,Irec)<tol, break; end; end
    Irec_last  = Irec;
end
out.Irecon = Irec;
out.err    = err(1:k);
out.tim    = tim(1:k);