comparison m-toolbox/classes/@ao/smoother.m @ 0:f0afece42f48

Import.
author Daniele Nicolodi <nicolodi@science.unitn.it>
date Wed, 23 Nov 2011 19:22:13 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:f0afece42f48
1 % SMOOTHER smooths a given series of data points using the specified method.
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %
4 % DESCRIPTION: SMOOTHER smooths a given series of data points using
5 % the specified method.
6 %
7 % CALL: b = smoother(a, pl)
8 %
9 % <a href="matlab:utils.helper.displayMethodInfo('ao', 'smoother')">Parameters Description</a>
10 %
11 % VERSION: $Id: smoother.m,v 1.30 2011/11/11 15:21:19 luigi Exp $
12 %
13 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
14
15 function varargout = smoother(varargin)
16
17 callerIsMethod = utils.helper.callerIsMethod;
18
19 % Check if this is a call for parameters
20 if utils.helper.isinfocall(varargin{:})
21 varargout{1} = getInfo(varargin{3});
22 return
23 end
24
25 import utils.const.*
26 utils.helper.msg(msg.PROC3, 'running %s/%s', mfilename('class'), mfilename);
27
28 % Collect input variable names
29 in_names = cell(size(varargin));
30 for ii = 1:nargin,in_names{ii} = inputname(ii);end
31
32 % Collect all AOs and plists
33 [as, ao_invars] = utils.helper.collect_objects(varargin(:), 'ao', in_names);
34 [pl, pl_invars] = utils.helper.collect_objects(varargin(:), 'plist', in_names);
35
36 % Decide on a deep copy or a modify
37 bs = copy(as, nargout);
38
39 % combine plists
40 pl = parse(pl, getDefaultPlist());
41
42 % Get parameters from plist
43 bw = find(pl, 'width');
44 hc = find(pl, 'hc');
45 method = find(pl, 'method');
46
47 % check the method
48 if ~strcmp(method, 'median') && ...
49 ~strcmp(method, 'mean') && ...
50 ~strcmp(method, 'min') && ...
51 ~strcmp(method, 'max') && ...
52 ~strcmp(method, 'mode')
53 help(mfilename)
54 error('### Unknown smoothing method');
55 end
56
57 % Loop over input AOs
58 for j=1:numel(bs)
59 utils.helper.msg(msg.PROC1, 'smoothing %s', bs(j).name);
60 switch lower(method)
61 case {'median', 'mean', 'min', 'max'}
62 bs(j).data.setY(ltpda_smoother(bs(j).data.getY, bw, hc, method));
63 otherwise
64 bs(j).data.setY(smooth(bs(j).data.getY, bw, hc, method));
65 end
66 % set name
67 bs(j).name = sprintf('smoother(%s)', ao_invars{j});
68 % Add history
69 if ~callerIsMethod
70 bs(j).addHistory(getInfo('None'), pl, ao_invars(j), bs(j).hist);
71 end
72 end
73
74 % clear errors
75 bs.clearErrors;
76
77 % Set output
78 if nargout == numel(bs)
79 % List of outputs
80 for ii = 1:numel(bs)
81 varargout{ii} = bs(ii);
82 end
83 else
84 % Single output
85 varargout{1} = bs;
86 end
87 end
88
89 %--------------------------------------------------------------------------
90 % Get Info Object
91 %--------------------------------------------------------------------------
92 function ii = getInfo(varargin)
93 if nargin == 1 && strcmpi(varargin{1}, 'None')
94 sets = {};
95 pl = [];
96 else
97 sets = {'Default'};
98 pl = getDefaultPlist;
99 end
100 % Build info object
101 ii = minfo(mfilename, 'ao', 'ltpda', utils.const.categories.sigproc, '$Id: smoother.m,v 1.30 2011/11/11 15:21:19 luigi Exp $', sets, pl);
102 end
103
104
105 %--------------------------------------------------------------------------
106 % smooth data
107 function ys = smooth(y, bw, hc, method)
108 N = length(y);
109 ys = zeros(size(y));
110
111 % function to smooth with
112 mfcn = eval(['@(x) ' method '(x)' ]);
113
114 for kk=1:N
115 if mod(kk, 1000)==0
116 utils.helper.msg(utils.const.msg.PROC1, 'smoothed %06d samples', kk);
117 end
118 % Determine the interval we are looking in
119 interval = kk-bw/2:kk+bw/2;
120 interval(interval<=0)=1;
121 interval(interval>N)=N;
122 % calculate method(values) of interval
123 % after throwing away outliers
124 trial = sort(y(interval));
125 b = round(hc*length(trial));
126 ys(kk) = mfcn(trial(1:b));
127 end
128 end
129
130 %--------------------------------------------------------------------------
131 % Get Default Plist
132 %--------------------------------------------------------------------------
133 function plout = getDefaultPlist()
134 persistent pl;
135 if exist('pl', 'var')==0 || isempty(pl)
136 pl = buildplist();
137 end
138 plout = pl;
139 end
140
141 function pl = buildplist()
142 pl = plist();
143
144 % width
145 p = param({'width', 'The width of the smoothing filter.'}, paramValue.DOUBLE_VALUE(20));
146 pl.append(p);
147
148 % hc
149 p = param({'hc', 'A cutoff to throw away outliers (0-1).'}, paramValue.DOUBLE_VALUE(0.8));
150 pl.append(p);
151
152 % Method
153 p = param({'method', 'The smoothing method.'}, {1, {'median', 'mean', 'max', 'mode'}, paramValue.SINGLE});
154 pl.append(p);
155
156 end
157 % END
158
159 % PARAMETERS: width - the width of the smoothing filter [default: 20 samples]
160 % hc - a cutoff to throw away outliers (0-1) [default: 0.8]
161 % method - the smoothing method:
162 % 'median' [default]
163 % 'mean', 'min', 'max', 'mode'
164 %