Home > classes > @ao > smoother.m

smoother

PURPOSE ^

SMOOTHER smooths a given series of data points using the specified method.

SYNOPSIS ^

function varargout = smoother(varargin)

DESCRIPTION ^

 SMOOTHER smooths a given series of data points using the specified method.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

 DESCRIPTION: SMOOTHER smooths a given series of data points using
              the specified method.

 CALL:        b = smoother(a, pl)

 PARAMETERS:
   width  - the width of the smoothing filter [default: 20 samples]
   hc     - a cutoff to throw away outliers (0-1)  [default: 0.8]
   method - the smoothing method:
            'median'  [default]
            'mean', 'min', 'max', 'mode'   

 The following call returns a parameter list object that contains the
 default parameter values:
 
 >> pl = smoother('Params')
 
 The following call returns a string that contains the routine CVS version:

 >> version = smoother(ao,'Version')

 The following call returns a string that contains the routine category:

 >> category = smoother(ao,'Category')

 
 VERSION: $Id: smoother.m,v 1.3 2008/03/02 19:17:38 hewitson Exp $

 HISTORY: 02-03-08 M Hewitson
             Creation

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function varargout = smoother(varargin)
0002 % SMOOTHER smooths a given series of data points using the specified method.
0003 %
0004 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0005 %
0006 % DESCRIPTION: SMOOTHER smooths a given series of data points using
0007 %              the specified method.
0008 %
0009 % CALL:        b = smoother(a, pl)
0010 %
0011 % PARAMETERS:
0012 %   width  - the width of the smoothing filter [default: 20 samples]
0013 %   hc     - a cutoff to throw away outliers (0-1)  [default: 0.8]
0014 %   method - the smoothing method:
0015 %            'median'  [default]
0016 %            'mean', 'min', 'max', 'mode'
0017 %
0018 % The following call returns a parameter list object that contains the
0019 % default parameter values:
0020 %
0021 % >> pl = smoother('Params')
0022 %
0023 % The following call returns a string that contains the routine CVS version:
0024 %
0025 % >> version = smoother(ao,'Version')
0026 %
0027 % The following call returns a string that contains the routine category:
0028 %
0029 % >> category = smoother(ao,'Category')
0030 %
0031 %
0032 % VERSION: $Id: smoother.m,v 1.3 2008/03/02 19:17:38 hewitson Exp $
0033 %
0034 % HISTORY: 02-03-08 M Hewitson
0035 %             Creation
0036 %
0037 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0038 
0039 ALGONAME = mfilename;
0040 VERSION  = '$Id: smoother.m,v 1.3 2008/03/02 19:17:38 hewitson Exp $';
0041 CATEGORY = 'Signal Processing';
0042 bs       = [];
0043 
0044 % Check if this is a call for parameters
0045 if nargin == 2
0046   if isa(varargin{1}, 'ao') && ischar(varargin{2})
0047     in = char(varargin{2});
0048     if strcmp(in, 'Params')
0049       varargout{1} = getDefaultPlist();
0050       return
0051     elseif strcmp(in, 'Version')
0052       varargout{1} = VERSION;
0053       return
0054     elseif strcmp(in, 'Category')
0055       varargout{1} = CATEGORY;
0056       return
0057     end
0058   end
0059 end
0060 
0061 % Collect input ao's, plist's and ao variable names
0062 in_names = {};
0063 for ii = 1:nargin
0064   in_names{end+1} = inputname(ii);
0065 end
0066 
0067 [as, pl, aonames] = collect_inputs(varargin, in_names);
0068 
0069 pl = combine(pl, getDefaultPlist);
0070 
0071 % Get parameters from plist
0072 bw      = find(pl, 'width');
0073 hc      = find(pl, 'hc');
0074 method  = find(pl, 'method');
0075 
0076 % check the method
0077 if  ~strcmp(method, 'median') && ...
0078     ~strcmp(method, 'mean') && ...
0079     ~strcmp(method, 'min') && ...
0080     ~strcmp(method, 'max') && ...
0081     ~strcmp(method, 'mode')
0082   help(mfilename)
0083   error('### Unknown smoothing method');
0084 end
0085 
0086 % Loop over input AOs
0087 bo = [];
0088 for j=1:length(as)
0089 
0090   a = as(j);
0091   d = a.data;
0092 
0093   disp(sprintf('* smoothing %s', a.name));
0094   
0095   ys = smooth(d.y, bw, hc, method);
0096 
0097   %------- Make output AO
0098 
0099   % make data
0100   d_new = set(d, 'y', ys);
0101   d_new = set(d_new, 'name', sprintf('smoother(%s)', d_new.name));
0102 
0103   % create new output history
0104   h = history(ALGONAME, VERSION, pl, a.hist);
0105   h = set(h, 'invars', aonames);
0106 
0107   % make output analysis object
0108   b = ao(d_new, h);
0109 
0110   % set name
0111   % name for this object
0112   b = setnh(b, 'name', sprintf('smoother(%s)', aonames{j}));
0113 
0114   % Add to output array
0115   bo = [bo b];
0116 
0117 end
0118 
0119 varargout{1} = bo;
0120 
0121 %--------------------------------------------------------------------------
0122 % get default parameter list
0123 function pl = getDefaultPlist()
0124 
0125 pl = plist();
0126 pl = append(pl, param('width', 20));
0127 pl = append(pl, param('hc', 0.8));
0128 pl = append(pl, param('method', 'median'));
0129 
0130 
0131 %--------------------------------------------------------------------------
0132 % compute noise floor estimate
0133 function ys = smooth(y, bw, hc, method)
0134 
0135 N = length(y);
0136 ys = zeros(size(y));
0137 
0138 for j=1:N
0139 
0140   if mod(j, 1000)==0
0141     disp(sprintf(' -- smoothed %06d samples', j));
0142   end
0143   
0144   % Determine the interval we are looking in
0145   interval = j-bw/2:j+bw/2;
0146 %   idx = find(interval<=0);
0147 %   interval(idx)=1;
0148 %   idx = find(interval>N);
0149 %   interval(idx)=N;
0150   
0151 %   idx = find(interval<=0);
0152   interval(interval<=0)=1;
0153 %   idx = find(interval>N);
0154   interval(interval>N)=N;
0155 
0156   % calculate median value of interval
0157   % after throwing away outliers
0158   trial = sort(y(interval));
0159   b = round(hc*length(trial));
0160   eval(sprintf('ys(j) = %s(trial(1:b));', method));
0161 
0162 end
0163 
0164 % END
0165

Generated on Mon 31-Mar-2008 13:54:54 by m2html © 2003