function psnr = CalcPSNR( mImage1, mImage2, maxIntensity )
% CalcPSNR returns the PSNR between the original image and its approximation
%
% Calculates the Peak Signal to Noise Ratio (PSNR) between 2 matrices containing
% pixel intensity values.
%
% Usage:            psnr = CalcPSNR( mImage1, mImage2 );
%
% Inputs:
%   mImage1         matrix of pixel intensity values representing the original image
%   mImage2         matrix of pixel intensity values representing the approximated image
%   maxIntensity    maximum allowed pixel intensity, defualt is 256 (8 bit image) 
%
% Outputs:
%    psnr       the PSNR resulting from the  approximation

% See   http://www.ncrg.aston.ac.uk/Projects/HNLApprox/
%       http://www.nonlinear-approx.info/

error(nargchk(2,3,nargin));

if nargin < 3
    
    maxIntensity = 256;
    
end

mError = double(mImage1) - double(mImage2);
[ y, x ] = size(mError);
mse = sum( mError(:).^2 )/( y*x );
psnr = 10*log10(((maxIntensity-1)^2)/mse);