Moavg.m

From Pnb
Jump to navigation Jump to search
function mavg = moavg(flag,diff,r,c,matrix,trend,pts)
%created by Erik Stassinos
%3/18/2010
%function called by dspike to calculate moving average of points
%passes in row and column position to do average around

cnt = 0;
sum = 0;

width_2 = (flag)/2;
width_2 = floor(width_2);
%width_2 = 2
%width_2 = floor(width_2);
start = r;
%r number of rows (row location)
%c number of columns (column location)
%matrix is up or downcast matrix
%diff is array of differences calculated
%----------------looping to find sum,cnt and trapflag-------------
r;
c;

%--------moving avg for start of matrix

if start <= width_2
    for i = start:(start+width_2) % rows in column
            if ((i==start) || (diff(i,c) > trend) || (matrix(i,c) < -1000));
            else
            sum = sum + matrix(i,c);
            cnt = cnt +1;
            end
            if cnt ==0
                cnt;
                mavg = -9.9E35;
                %sprintf('inconclusive mavg calc, trapflag set')
                %break
            else
               sum;
               mavg = sum/cnt;
            end
    end
end
clear i
%-------moving avg for end of matrix
if start >= (pts - width_2)
    for i = (start-width_2):start       
            if ((i==start) || (diff(i,c) > trend) || (matrix(i,c) < -1000));
            else
            sum = sum + matrix(i,c);
            cnt = cnt+1;
            end
            if cnt ==0
                cnt;
                 mavg = -9.9E35;
            %sprintf('inconclusive mavg calc, trapflag set')
            
            else
            mavg = sum/cnt;
            end
    end  
end
clear i

%-------moving avg for everything in between
if start > width_2 && start <= (pts - width_2)
  for i= (start - width_2):(start+width_2)
            if ((i==start) || (diff(i,c) > trend) || (matrix(i,c) < -1000));
            else
            sum = sum + matrix(i,c);
            matrixic = matrix(i,c);
            cnt = cnt+1;
            end
            if cnt ==0
            cnt;
            mavg = -9.9E35;
            %sprintf('inconclusive mavg calc, trapflag set')
            else
            mavg = sum/cnt;
            end
  end
end
clear i
%----------------------end looping--------------------------

moavg