function new_images = average_smooth(images, window)
% AVERAGE_SMOOTH: Given sequence of 1D data and window size 5,
%                 say [1,2,4,2,1] it will do the averaging and
%                 assign it to the centre position as it parses
%                 the image.
%
% Code written by Katherine Smith, 2003
%
%    GENERAL
%
%      new_images = average_smooth(images, window)
%
%    INPUT/S
%
%      -images:
%           Input images.
%
%      -window:
%           Scope of averaging or a type of 1-D mask.
%           
%    OUTPUT/S
%
%      -new_images:
%           The images with average smooth applied.
%
%    PENDING WORK
%
%      -Add a few comments to file.
%
%    KNOWN BUG/S
%
%      -None.
%
%    COMMENT/S
%
%      -Is a similar function available elsewhere already?
%
%    RELATED FUNCTION/S
%
%      GAUSSIAN_SMOOTH
%
%    ABOUT
%
%      -Created:     November 23rd, 2003
%      -Last update: Novermber 25th, 2003
%      -Revision:    0.0.2
%      -Author:      R. S. Schestowitz, University of Manchester
% ==============================================================


if(mod(window,2)~=1) % if even (not odd) sized window
  error('Window must be an odd number');
  return; % leave with no smoothing applied
end
images_width = size(images, 1); % extract width of images
border = floor((window-1)/2); % find middle position of window
new_images = zeros(size(images)); % create fully black image, sized as images
for i = 1:images_width % for each pixel along the width (1-D data)
  if ((i < border+1) | (i > images_width-border)) % if below middle or beyond middle
    new_images(i,:) = images(i,:); % copy pixel
  else  
    new_images(i,:) = mean(images(i-border:i+border,:)); 
                  % if in middle, set average of the whole line
  end
end