Home > atmlab > math > boxcarfilter.m

boxcarfilter

PURPOSE ^

BOXCARFILTER Rectangular filtering of data series

SYNOPSIS ^

function yf = boxcarfilter(x,y,xw)

DESCRIPTION ^

 BOXCARFILTER   Rectangular filtering of data series

    A rectangular filtering is performed. For each abscissa point, the 
    filtered value is based on all data inside +-xw/2.

    The data are assumed to be equidistant, but gaps in the data series
    are allowed. The distance between data points is taken as 
    min( diff( x ) ).

    The filter is throughout applied symmetrically. Parts of the filter
    extending outside the complete data range, r into data gaps, 
    are neglected.

 FORMAT   yf = boxcarfilter(x,y,xw)
        
 OUT   yf   Filtered data.
 IN    x    Data abscissa.
       y    Data values. Must be given as a column vector or a matrix.
       xw   Width (total) of the filter.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

DOWNLOAD ^

boxcarfilter.m

SOURCE CODE ^

0001 % BOXCARFILTER   Rectangular filtering of data series
0002 %
0003 %    A rectangular filtering is performed. For each abscissa point, the
0004 %    filtered value is based on all data inside +-xw/2.
0005 %
0006 %    The data are assumed to be equidistant, but gaps in the data series
0007 %    are allowed. The distance between data points is taken as
0008 %    min( diff( x ) ).
0009 %
0010 %    The filter is throughout applied symmetrically. Parts of the filter
0011 %    extending outside the complete data range, r into data gaps,
0012 %    are neglected.
0013 %
0014 % FORMAT   yf = boxcarfilter(x,y,xw)
0015 %
0016 % OUT   yf   Filtered data.
0017 % IN    x    Data abscissa.
0018 %       y    Data values. Must be given as a column vector or a matrix.
0019 %       xw   Width (total) of the filter.
0020 
0021 % 2006-04-05   Created by Patrick Eriksson.
0022 
0023 
0024 function yf = boxcarfilter(x,y,xw)
0025 
0026 if size(y,1) ~= length(x)
0027   error('Size of *y* does not match size of *x*.');
0028 end
0029 %
0030 if ~issorted(x)
0031   error('The vector *x* must be sorted.');
0032 end
0033 
0034 
0035 %= Set-up output variable
0036 %
0037 yf = y;
0038 
0039 
0040 %= Length of each data range
0041 %
0042 l  = min( diff( x ) );
0043 
0044 
0045 %= Make sure that *x* is a column vector
0046 %
0047 x = vec2col(x);
0048 
0049 
0050 for i = 1:length(x)
0051 
0052   % Calculate distance to closest edge of data ranges
0053   d       = abs( x - x(i) ) - l/2;
0054   d(i)    = 0;
0055 
0056   % What data points to consider
0057   ind     = find( d < xw/2 );
0058 
0059   % Calculate effective length
0060   %
0061   leff      = repmat( l, length(ind), 1 );
0062   %
0063   if d(ind(1)) + l > xw/2
0064     leff(1)   = d(ind(1)) + l - xw/2;
0065   end
0066   if d(ind(end)) + l > xw/2;
0067     leff(end) = d(ind(end)) + l - xw/2;
0068   end
0069 
0070   % Filter
0071   yf(i,:) = sum(y(ind,:).*repmat(leff,1,size(y,2))) / sum(leff);
0072 
0073 end
0074

Generated on Mon 15-Sep-2014 13:31:28 by m2html © 2005