
% Function D_cl = rem_noise_GenLap(D,W,sigma,T)
% shrinks all the coefficients in a given subband according to the generalized likelihood ratio
% and assuming the generalized Laplacian prior on noise-free coefficients, as described in
% A. Pizurica, Image Denoising Using Wavelets and Spatial Context Modeling, Ph.D. thesis,
% Ghent University, June 2002.
%
% INPUT:
%   D - noisy subband
%   W - "half window size", e.g., W=1 for 3x3 window and W=2 for 5x5 window 
%   sigma - noise standard deviation
%   T - threshold that defines the notion of the "signal of interest"
%
% OUTPUT:
%   D_cl - denoised ("cleaned") subband
%
% Author: Aleksandra Pizurica/TELIN/Ghent University
% E-mail: Aleksandra.Pizurica@telin.UGent.be
% Last modified: 3. May 2004.
% Copyright: All software, documentation, and related files in this distribution
%            are Copyright (c) 2003 Aleksandra Pizurica. All rights reserved.
%
% This software is distributed and licensed to you on a non-exclusive 
% basis, free-of-charge. Redistribution and use in source and binary forms, 
% with or without modification, are permitted provided that the above 
% copyright notice is clearly maintained.



function D_cl = rem_noise_GenLap(D,W,sigma,T);


[M,N]=size(D);

 % estimate parameters of the pdf of noise free wavelet coefficients
 % Laplacian model exp(-x/s)^p is assumed
 
 
 [p,s]=FindGenLapPar(D,sigma);
 
 %K=p/(2*s*gamma(1/p));
 %I=K*(s/p)*gamma(1/p)*gammainc((sigma/s)^p,1/p);
 %PrFac=(1-2*I)/(2*I)
 
 
 I=gammainc((sigma/s)^p,1/p);
 
 PrFac=(1-I)/(I);
 if(PrFac<1e-6)
     PrFac=0;
 end
 
 if(PrFac==0)
    D_cl=zeros(M,N);
else
    Lim=ceil(max(max(abs(D))));

   % Compute pdfs p(m|0) and p(m|1) in the range m=0:Lim
   [LR,pdf_m_s,pdf_m_n]=MagPDFs_varT(p,s,sigma,T,Lim);
 

   % Compute pdfs p(e|0) and p(e|1) in the range m=0:Lim
   [PR,pdf_am_s,pdf_am_n]=AverageMagPDFs(pdf_m_s,pdf_m_n,W,Lim);

    SHRINK=genlikE_shrink(D,LR,PR,W,PrFac);
    D_cl=D.*SHRINK;

end







