Mercurial > hg > ltpda
comparison m-toolbox/classes/@ao/normdist.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 % NORMDIST computes the equivalent normal distribution for the data. | |
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
3 % | |
4 % DESCRIPTION: NORMDIST computes the equivalent normal distribution for the | |
5 % data. The mean and standard deviation are computed from the | |
6 % data. The method returns the normal distribution evaluated | |
7 % at the bin centers. | |
8 % | |
9 % CALL: b = normdist(a) | |
10 % b = normdist(a, pl) | |
11 % | |
12 % INPUTS: a - input analysis object(s) | |
13 % pl - a parameter list | |
14 % | |
15 % OUTPUTS: b - xydata type analysis object(s) containing the | |
16 % normal distribution pdf | |
17 % | |
18 % <a href="matlab:utils.helper.displayMethodInfo('ao', 'normdist')">Parameters Description</a> | |
19 % | |
20 % VERSION: $Id: normdist.m,v 1.11 2011/04/08 08:56:13 hewitson Exp $ | |
21 % | |
22 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
23 function varargout = normdist(varargin) | |
24 | |
25 % Check if this is a call for parameters | |
26 if utils.helper.isinfocall(varargin{:}) | |
27 varargout{1} = getInfo(varargin{3}); | |
28 return | |
29 end | |
30 | |
31 import utils.const.* | |
32 utils.helper.msg(msg.PROC3, 'running %s/%s', mfilename('class'), mfilename); | |
33 | |
34 % Collect input variable names | |
35 in_names = cell(size(varargin)); | |
36 for ii = 1:nargin,in_names{ii} = inputname(ii);end | |
37 | |
38 % Collect all AOs and plists | |
39 [as, ao_invars] = utils.helper.collect_objects(varargin(:), 'ao', in_names); | |
40 [pli, pl_invars, rest] = utils.helper.collect_objects(varargin(:), 'plist', in_names); | |
41 | |
42 pl = parse(pli, getDefaultPlist('Number of bins')); | |
43 normalize = utils.prog.yes2true(find(pl, 'norm')); | |
44 | |
45 % start looping | |
46 bs(numel(as),1) = ao(); | |
47 | |
48 for jj=1:numel(bs) | |
49 | |
50 % compute histogram to get bin centers. | |
51 h = hist(as(jj), pl); | |
52 % compute mean and standard deviation from the data | |
53 mu = mean(as(jj).y); | |
54 sig = std(as(jj).y); | |
55 % Compute exponent | |
56 e = ((h.x-mu)./sig).^2; | |
57 % compute PDF | |
58 y = (exp(-0.5.*e))./(sig*sqrt(2*pi)); | |
59 % Introduce normalization | |
60 if normalize | |
61 yunits = (as(jj).data.yunits)^(-1); | |
62 else | |
63 nn = sum(y); | |
64 nd = sum(h.y); | |
65 y = y.*nd./nn; | |
66 yunits = 'Count'; | |
67 end | |
68 % construct new AO | |
69 % make a new xydata object | |
70 xy = xydata(h.x, y); | |
71 xy.setXunits(as(jj).data.yunits); | |
72 xy.setYunits(yunits); | |
73 bs(jj) = ao(xy); | |
74 bs(jj).procinfo = plist('mu', mu, 'sig', sig); | |
75 % name for this object | |
76 bs(jj).name = sprintf('normdist(%s)', ao_invars{jj}); | |
77 % Add history | |
78 bs(jj).addHistory(getInfo('None'), pl, ao_invars(jj), bs(jj).hist); | |
79 end | |
80 | |
81 % Set output | |
82 if nargout == numel(bs) | |
83 % List of outputs | |
84 for ii = 1:numel(bs) | |
85 varargout{ii} = bs(ii); | |
86 end | |
87 else | |
88 % Single output | |
89 varargout{1} = bs; | |
90 end | |
91 end | |
92 | |
93 %-------------------------------------------------------------------------- | |
94 % Get Info Object | |
95 %-------------------------------------------------------------------------- | |
96 function ii = getInfo(varargin) | |
97 if nargin == 1 && strcmpi(varargin{1}, 'None') | |
98 sets = {}; | |
99 pls = []; | |
100 elseif nargin == 1 && ~isempty(varargin{1}) && ischar(varargin{1}) | |
101 sets{1} = varargin{1}; | |
102 pls = getDefaultPlist(sets{1}); | |
103 else | |
104 sets = {'Number Of Bins'}; | |
105 pls = []; | |
106 for kk=1:numel(sets) | |
107 pls = [pls getDefaultPlist(sets{kk})]; | |
108 end | |
109 end | |
110 % Build info object | |
111 ii = minfo(mfilename, 'ao', 'ltpda', utils.const.categories.sigproc, '$Id: normdist.m,v 1.11 2011/04/08 08:56:13 hewitson Exp $', sets, pls); | |
112 end | |
113 | |
114 %-------------------------------------------------------------------------- | |
115 % Get Default Plist | |
116 %-------------------------------------------------------------------------- | |
117 function plout = getDefaultPlist(set) | |
118 persistent pl; | |
119 persistent lastset; | |
120 if exist('pl', 'var')==0 || isempty(pl) || ~strcmp(lastset, set) | |
121 pl = buildplist(set); | |
122 lastset = set; | |
123 end | |
124 plout = pl; | |
125 end | |
126 | |
127 function plo = buildplist(set) | |
128 switch lower(set) | |
129 case 'number of bins' | |
130 plo = plist(); | |
131 | |
132 % N number of bins | |
133 p = param({'N', ['The number of bins to compute the histogram on. <br>' ... | |
134 'This defines the bin centers for the PDF.']}, {1, {10}, paramValue.OPTIONAL}); | |
135 plo.append(p); | |
136 | |
137 % normalized output | |
138 p = param({'norm', ['Normalized output. If set to true, it will give the normal distrubution PDF. <br>' ... | |
139 'Otherwise, it will give an output comparable to the ao/hist method']}, paramValue.TRUE_FALSE); | |
140 p.val.setValIndex(2); | |
141 plo.append(p); | |
142 | |
143 otherwise | |
144 error('### Unknown default plist for the set [%s]', set); | |
145 end | |
146 end | |
147 | |
148 |