% smooth.m                                           Steve Mann, 1992/1993
%
% same as in PV-Wave or IDL
%
% Example: B=smooth(A,5)           % smooths and pads with NaN to get same size
%          B=smooth(A,5,'expand')  % includes full support; imagesize+filtsize-1
%          B=smooth(A,5,'truncate')% diminishes support to only valid numbers
%          B=smooth(A,5,'wrap')    % maintains size by wrap-around
%          B=smooth(A,5,'mirror')  % maintains size by mirroring filter
%          B=smooth(A,5,'decay')   % just conv and truncate to same size
%          B=smooth(A,5,'nan')     % maintains same size by nan unknowns
%
% Bugs: does not take advantage of the separability of the kernel...
%       will rewrite to call mex file ``conv_separable.c''
%
%       wrap-around, mirroring, etc not yet implemented
%       later, include: ``full'', ``same'', ``valid'', ``nan'', ``mirror'',...

function B=wave_is_better_than_matlab(A,boxsize,method)

if nargin<3      % default method; same size image but pad bad parts with NaN
  disp('smooth: method defaulting to NaN: truncating then padding to same size')
  method='nan';
end%if

% randomly remind user...
if rand(1,1)*100>99
  disp('smooth: RANDOM NOTE: should re-write to take advantage of ker. seperability')
end%if

if ~rem(boxsize,2)
  disp('|--------------------------------------------------------------|')
  disp('| boxsize must be even for image to be same size and unshifted |')
  disp('|--------------------------------------------------------------|')
end%if

filt=ones(boxsize);
B=conv2(A,filt);%later use B=conv_separable(A,ones(boxsize,1),ones(1,boxsize));

[M,N]=size(A);
[M_toobig,N_toobig]=size(B);
if strcmp(method,'wrap')  | strcmp(method,'mirror') |...
   strcmp(method,'decay') | strcmp(method,'nan')
  B=B(1+floor(boxsize/2):M_toobig-(ceil(boxsize/2)-1),... % make it same size
      1+floor(boxsize/2):N_toobig-(ceil(boxsize/2)-1)...
     );
end%if

if ~rem(boxsize,2)
  disp('image has now been shifted 1/2 pixel along both axes')
end%if

if strcmp(method,'nan')
  % disp('smooth: about to pad invalid entries with NaN')
  C=ones(size(A))*NaN;
  C(1+floor(boxsize/2):M-(ceil(boxsize/2)-1),...  % fill middle of C with good B
    1+floor(boxsize/2):N-(ceil(boxsize/2)-1)...
   )=...
  B(1+floor(boxsize/2):M-(ceil(boxsize/2)-1),...
    1+floor(boxsize/2):N-(ceil(boxsize/2)-1)...
   );
  B=C;
end%if

