% Optical Imaging and Spectroscopy
%
% David J. Brady
% Duke University
% www.opticalimaging.org
%
% function written by Nikos Pitsianis, ECE Dept, Aristotle University
%
function x = nullSpaceSmoothing(N,x0)
% function x = nullSpaceSmoothing(N,x0)
% smooth the gradient of x0 with 
% components from basis N 
% (usually N is the null space of a transform)
% It works with multiple x0 columns (as an array)
%
% Nikos Pitsianis
% 8/16/2004
%
% Usage : 
% say you have sensor measurements g = T*f and you do not like
% the f estimate fe = T\g, 
% then try fs = denoise(null(T),fe)

dN = diff(N);
dx0 = diff(x0);

A = (dN'*dN);

if rcond(A) > 1e-16
    % c = (dN) \ (-dx0);
    % c =  A \ (-dN'*dx0);
    c =  minLS(A,-dN'*dx0);
    
    x = x0 + N*c;
else
    x = x0;
end
