Mercurial > hg > ltpda
comparison m-toolbox/classes/@ao/average.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 % AVERAGE averages aos point-by-point | |
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
3 % | |
4 % DESCRIPTION: AVERAGE averages aos point-by-point. | |
5 % For each point, an average is taken over all the input objects. | |
6 % The uncertainty is calculated as the standard deviation of the mean. | |
7 % The objects must have the same length and yunits. | |
8 % | |
9 % s1: 2 1 2 5 2 3 3 | |
10 % s2: 7 2 3 4 2 1 1 | |
11 % s3: 0 0 7 6 5 5 5 | |
12 % =================== | |
13 % out: 3 1 4 5 3 3 3 | |
14 % | |
15 % CALL: b = average(a1, a2, a3, ..., pl) | |
16 % | |
17 % EXAMPLES: | |
18 % | |
19 % a1 = ao(plist('waveform','noise', 'nsecs', 1000,'fs',1,'yunits','m')); | |
20 % a2 = ao(plist('waveform','noise', 'nsecs', 1000,'fs',1,'yunits','m')); | |
21 % a3 = ao(plist('waveform','noise', 'nsecs', 1000,'fs',1,'yunits','m')); | |
22 % a4 = average(a1,a2,a3); | |
23 % | |
24 % <a href="matlab:utils.helper.displayMethodInfo('ao', 'average')">Parameters Description</a> | |
25 % | |
26 % VERSION: $Id: average.m,v 1.5 2011/04/08 08:56:12 hewitson Exp $ | |
27 % | |
28 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
29 | |
30 function varargout = average(varargin) | |
31 | |
32 % Check if this is a call for parameters | |
33 if utils.helper.isinfocall(varargin{:}) | |
34 varargout{1} = getInfo(varargin{3}); | |
35 return | |
36 end | |
37 | |
38 % Check if the method was called by another method | |
39 callerIsMethod = utils.helper.callerIsMethod; | |
40 | |
41 import utils.const.* | |
42 utils.helper.msg(msg.PROC3, 'running %s/%s', mfilename('class'), mfilename); | |
43 | |
44 if nargout == 0 | |
45 error('### average cannot be used as a modifier. Please give an output variable.'); | |
46 end | |
47 | |
48 % Collect input variable names | |
49 in_names = cell(size(varargin)); | |
50 for ii = 1:nargin,in_names{ii} = inputname(ii);end | |
51 | |
52 % Collect all AOs and plists | |
53 [as, ao_invars] = utils.helper.collect_objects(varargin(:), 'ao', in_names); | |
54 pl = utils.helper.collect_objects(varargin(:), 'plist', in_names); | |
55 | |
56 % Combine plists | |
57 pl = combine(pl, getDefaultPlist); | |
58 | |
59 % Make a copy of the input objects history | |
60 inhists = [as(:).hist]; | |
61 | |
62 % Copy the input objects so we inherit all properties | |
63 b = copy(as(1), true); | |
64 | |
65 % Collect the data, and check that: | |
66 % - all objects belong to the same class | |
67 % - all objects have the same yunits | |
68 % - all objects have the same length | |
69 | |
70 Nobj = numel(as); | |
71 data_class = class(as(1).data); | |
72 data_yunits = as(1).yunits; | |
73 data_matrix = as(1).data.getY; | |
74 data_length = numel(data_matrix); | |
75 | |
76 | |
77 for jj = 2:Nobj | |
78 % - all objects should belong to the same class | |
79 if ~strcmp(class(as(jj).data), data_class) | |
80 error('### The first ao data object is a %s, while the %dth is a %s. The data must all belong to the same class!', ... | |
81 data_class, jj, class(as(jj).data)); | |
82 end | |
83 | |
84 % - all objects should have the same yunits | |
85 if as(jj).yunits ~= data_yunits | |
86 error('### The first ao data object has yunits = %s, while the %dth has yunits = %s. The data must all have the same yunits!', ... | |
87 char(data_yunits), jj, char(as(jj).yunits)); | |
88 end | |
89 | |
90 % - all objects should have the same length | |
91 try | |
92 % The data.getY methods always give columns, so this syntax should give a proper matrix | |
93 data_matrix = [data_matrix as(jj).data.getY]; | |
94 catch ME | |
95 switch ME.identifier | |
96 case 'MATLAB:catenate:dimensionMismatch' | |
97 error('### The first ao data object has %d points, while the %dth has %d points. The data must all have the same size!', ... | |
98 data_length, jj, numel(as(jj).data.getY)); | |
99 otherwise | |
100 error('### Something went wrong while concatenating the data. Stopping.'); | |
101 end | |
102 | |
103 end | |
104 end | |
105 | |
106 % Go for the actual calculation. | |
107 if Nobj > 1 | |
108 % The data.getY methods always give columns, so we just need to operate on the second dimension | |
109 dim = 2; | |
110 b.data.setY(mean(data_matrix, dim)); | |
111 b.data.setDy(std(data_matrix, 0, dim) / sqrt(Nobj)); | |
112 else | |
113 % Nothing to do in this case | |
114 end | |
115 | |
116 if ~callerIsMethod | |
117 % create new output history | |
118 b.addHistory(getInfo('None'), pl, [ao_invars(:)], inhists); | |
119 % set name | |
120 b.name = sprintf('average(%s)', [ao_invars{:}]); | |
121 end | |
122 | |
123 % Set output | |
124 varargout{1} = b; | |
125 | |
126 end | |
127 | |
128 %-------------------------------------------------------------------------- | |
129 % Get Info Object | |
130 %-------------------------------------------------------------------------- | |
131 function ii = getInfo(varargin) | |
132 if nargin == 1 && strcmpi(varargin{1}, 'None') | |
133 sets = {}; | |
134 pl = []; | |
135 else | |
136 sets = {'Default'}; | |
137 pl = getDefaultPlist; | |
138 end | |
139 % Build info object | |
140 ii = minfo(mfilename, 'ao', 'ltpda', utils.const.categories.sigproc, '$Id: average.m,v 1.5 2011/04/08 08:56:12 hewitson Exp $', sets, pl); | |
141 ii.setModifier(false); | |
142 end | |
143 | |
144 %-------------------------------------------------------------------------- | |
145 % Get Default Plist | |
146 %-------------------------------------------------------------------------- | |
147 | |
148 function plout = getDefaultPlist() | |
149 persistent pl; | |
150 if ~exist('pl', 'var') || isempty(pl) | |
151 pl = buildplist(); | |
152 end | |
153 plout = pl; | |
154 end | |
155 | |
156 function pl = buildplist() | |
157 | |
158 pl = plist.EMPTY_PLIST; | |
159 | |
160 end | |
161 |