Home > bobstuff > boxfilt.m

boxfilt

PURPOSE ^

BOXFILT Running box-car filter.

SYNOPSIS ^

function xnew=boxfilt(x,n,nanend)

DESCRIPTION ^

 BOXFILT Running box-car filter.

 new=BOXFILT(x,n,nanend) applies a running box-car filter of 
 length n to the time series x. The time base is maintained, 
 and points at the beginning and end are returned as their 
 original values (or with NaNs if nanend==1).
    Input: 
     x   = time series data vector
     n   = length of boxcar filter
 nanend  = 1 to pad ends with NaN, 0 to leave data with original values

   Output:
  xnew   = barcar filtered data with same time base as x

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001   function xnew=boxfilt(x,n,nanend)
0002 
0003 % BOXFILT Running box-car filter.
0004 %
0005 % new=BOXFILT(x,n,nanend) applies a running box-car filter of
0006 % length n to the time series x. The time base is maintained,
0007 % and points at the beginning and end are returned as their
0008 % original values (or with NaNs if nanend==1).
0009 %    Input:
0010 %     x   = time series data vector
0011 %     n   = length of boxcar filter
0012 % nanend  = 1 to pad ends with NaN, 0 to leave data with original values
0013 %
0014 %   Output:
0015 %  xnew   = barcar filtered data with same time base as x
0016 
0017 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0018 % ver. 1: 12/1/96 (RG)
0019 % ver. 1.1: 10/2/97 (Rich Signell rsignell@usgs.gov): fixed up
0020 %   the documentation and updated the "ones" command.
0021 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0022 
0023 if nargin==2; nanend=0; end
0024 lg=length(x(:));
0025 filt=ones(size(1:n))/n;
0026 xnew=conv(x(:),filt);
0027 nbeg=ceil((n+1)/2);
0028 xnew=xnew(nbeg:nbeg-1+lg);
0029 if nanend
0030  xnew(1:nbeg-1)=x(1:nbeg-1)*nan;
0031  xnew(lg-nbeg+2:lg)=x(lg-nbeg+2:lg)*nan;
0032 else
0033  xnew(1:nbeg-1)=x(1:nbeg-1);
0034  xnew(lg-nbeg+2:lg)=x(lg-nbeg+2:lg);
0035 end
0036 [imax,jmax]=size(x);
0037 if (imax>1)&(jmax>1)
0038    xnew=remat(xnew,x);
0039    for i=0:floor(n/2)-1;
0040       xnew(1+i,:)=x(1+i,:);
0041       xnew(imax-i,:)=x(imax-i,:);
0042    end
0043 elseif size(xnew)~=size(x)
0044      xnew=xnew';
0045 end

Generated on Thu 01-Dec-2005 07:46:03 by m2html © 2003