Documentation of myrunning_sum


Global Index (all files) (short | long) | Local Index (files in subdir) (short | long)


Function Synopsis

[y , nsum] = running_sum ( x, n, usenan ) ;

Help text

 function [y, nsum] = running_sum ( x, n, usenan ) ;

  x = input matrix or vector
  n = number of points used in averaging, default is 5;
      when n < 0; cyclic boundary conditions are assumed
      otherwise, first and last values are repeated in summing.
      nsum is the number of elements that go into each sum.
-------------------------------------- Alexis Lau (Feb 1992)
  usenan = when set to true, missing boundary values are replaced
      by NaNs.
-------------------------------------- Alexis Lau (Feb 1993)

Cross-Reference Information

This function calls This function is called by

Listing of function myrunning_sum

function [y , nsum] = running_sum ( x, n, usenan ) ;
  
  if ~exist('n'); n=0; end;
  if ~exist('usenan'); usenan=0; cyclic=0; end;

  [mx,nx] = size(x);
  if (mx==1);
    x=x'; inverted=1; [mx,nx]=size(x);
  else;
    inverted=0;
  end;
  
  if n==0; n=5; end;
  if n<0; n=-n; cyclic=1; else; cycle=0; end;
  n = min(n,mx);
%------------------------------------------------
  y = x;
  nsum = x;
  if n ~= 1; n1=n-1; ni=fix(n1/2); nj=n1-ni;

    if usenan
      x = [ NaN*ones(ni,1)*x(1,:) ; x ; NaN*ones(nj,1)*x(mx,:) ];
    elseif cyclic
      x = [       x(mx-ni+1:mx,:) ; x ; x(1:nj,:) ];
    else
      x = [     ones(ni,1)*x(1,:) ; x ; ones(nj,1)*x(mx,:) ];
    end
  
    for i=1:mx;
      [y(i,:), nsum(i,:)] = sum2(x(i:i+n1,:));
    end;
  end
  
  if inverted; y = y'; end