Mercurial > hg > ltpda
comparison m-toolbox/classes/@ao/hist.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 % HIST overloads the histogram function (hist) of MATLAB for Analysis Objects. | |
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
3 % | |
4 % DESCRIPTION: HIST overloads the histogram function (hist) of MATLAB for | |
5 % Analysis Objects. | |
6 % | |
7 % CALL: b = hist(a) | |
8 % b = hist(a, pl) | |
9 % | |
10 % INPUTS: a - input analysis object(s) | |
11 % pl - a parameter list | |
12 % | |
13 % OUTPUTS: b - xydata type analysis object(s) containing the | |
14 % histogrammed data | |
15 % | |
16 % <a href="matlab:utils.helper.displayMethodInfo('ao', 'hist')">Parameters Description</a> | |
17 % | |
18 % WARNING: the '.' method of calling hist() doesn't work since AOs have a | |
19 % property called 'hist'. Use the standard function call instead: | |
20 % | |
21 % >> a.hist % returns the history object and doesn't call ao/hist | |
22 % >> hist(a) % calls ao/hist | |
23 % | |
24 % VERSION: $Id: hist.m,v 1.42 2011/04/08 08:56:12 hewitson Exp $ | |
25 % | |
26 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
27 | |
28 function varargout = hist(varargin) | |
29 | |
30 % Check if this is a call for parameters | |
31 if utils.helper.isinfocall(varargin{:}) | |
32 varargout{1} = getInfo(varargin{3}); | |
33 return | |
34 end | |
35 | |
36 import utils.const.* | |
37 utils.helper.msg(msg.PROC3, 'running %s/%s', mfilename('class'), mfilename); | |
38 | |
39 % Collect input variable names | |
40 in_names = cell(size(varargin)); | |
41 for ii = 1:nargin,in_names{ii} = inputname(ii);end | |
42 | |
43 % Collect all AOs and plists | |
44 [as, ao_invars] = utils.helper.collect_objects(varargin(:), 'ao', in_names); | |
45 [pli, pl_invars, rest] = utils.helper.collect_objects(varargin(:), 'plist', in_names); | |
46 | |
47 pl = parse(pli, getDefaultPlist('Number of bins')); | |
48 normalize = utils.prog.yes2true(find(pl, 'norm')); | |
49 | |
50 % Decide on a deep copy or a modify | |
51 bs = copy(as, nargout); | |
52 | |
53 | |
54 % Get parameters | |
55 N = find(pl, 'N'); | |
56 X = find(pl, 'X'); | |
57 | |
58 %---------------- Loop over input AOs | |
59 | |
60 % start looping | |
61 for jj=1:numel(bs) | |
62 % Histogram this data | |
63 if isempty(X) | |
64 [n,x] = hist(bs(jj).data.y, N); | |
65 else | |
66 [n,x] = hist(bs(jj).data.y, X); | |
67 end | |
68 % Keep the data shape of the input AO | |
69 if size(bs(jj).data.y, 1) ~= 1 | |
70 x = x.'; | |
71 n = n.'; | |
72 end | |
73 % In the case of equally spaced bins, introduce normalization | |
74 if normalize && isempty(X) | |
75 dx = mean(diff(x)); | |
76 n = n / sum(n) / dx; | |
77 yunits = (bs(jj).data.yunits)^(-1); | |
78 dy = sqrt(n); | |
79 else | |
80 yunits = 'Count'; | |
81 dy = sqrt(n); | |
82 end | |
83 % make a new xydata object | |
84 xy = xydata(x, n); | |
85 xy.setXunits(bs(jj).data.yunits); | |
86 xy.setYunits(yunits); | |
87 xy.setDy(dy); | |
88 % make output analysis object | |
89 bs(jj).data = xy; | |
90 % name for this object | |
91 bs(jj).name = sprintf('hist(%s)', ao_invars{jj}); | |
92 % Add history | |
93 bs(jj).addHistory(getInfo('None'), pl, ao_invars(jj), bs(jj).hist); | |
94 % Add to outputs | |
95 % clear errors | |
96 bs(jj).clearErrors; | |
97 end % end of AO loop | |
98 | |
99 % Set output | |
100 if nargout == numel(bs) | |
101 % List of outputs | |
102 for ii = 1:numel(bs) | |
103 varargout{ii} = bs(ii); | |
104 end | |
105 else | |
106 % Single output | |
107 varargout{1} = bs; | |
108 end | |
109 end | |
110 | |
111 %-------------------------------------------------------------------------- | |
112 % Get Info Object | |
113 %-------------------------------------------------------------------------- | |
114 function ii = getInfo(varargin) | |
115 if nargin == 1 && strcmpi(varargin{1}, 'None') | |
116 sets = {}; | |
117 pls = []; | |
118 elseif nargin == 1 && ~isempty(varargin{1}) && ischar(varargin{1}) | |
119 sets{1} = varargin{1}; | |
120 pls = getDefaultPlist(sets{1}); | |
121 else | |
122 sets = {'Number Of Bins', 'Bin Centres'}; | |
123 pls = []; | |
124 for kk=1:numel(sets) | |
125 pls = [pls getDefaultPlist(sets{kk})]; | |
126 end | |
127 end | |
128 % Build info object | |
129 ii = minfo(mfilename, 'ao', 'ltpda', utils.const.categories.sigproc, '$Id: hist.m,v 1.42 2011/04/08 08:56:12 hewitson Exp $', sets, pls); | |
130 end | |
131 | |
132 %-------------------------------------------------------------------------- | |
133 % Get Default Plist | |
134 %-------------------------------------------------------------------------- | |
135 | |
136 function plout = getDefaultPlist(set) | |
137 persistent pl; | |
138 persistent lastset; | |
139 if exist('pl', 'var')==0 || isempty(pl) || ~strcmp(lastset, set) | |
140 pl = buildplist(set); | |
141 lastset = set; | |
142 end | |
143 plout = pl; | |
144 end | |
145 | |
146 function plo = buildplist(set) | |
147 switch lower(set) | |
148 case 'number of bins' | |
149 plo = plist; | |
150 | |
151 % N number of bins | |
152 p = param({'N', ['The number of bins to compute the histogram on.']}, {1, {10}, paramValue.OPTIONAL}); | |
153 plo.append(p); | |
154 | |
155 % normalized output | |
156 p = param({'norm', ['Normalized output. If set to true, it will give the output comparable <br>', ... | |
157 'to the normal distrubution PDF. <br>']}, paramValue.FALSE_TRUE); | |
158 plo.append(p); | |
159 | |
160 case 'bin centres' | |
161 plo = plist({'X', 'A vector of bin centers.'}, paramValue.EMPTY_DOUBLE); | |
162 otherwise | |
163 error('### Unknown default plist for the set [%s]', set); | |
164 end | |
165 end | |
166 | |
167 |