3 %> @brief Average every ... spectra/rows
5 % Modes
for determining the number of bins
6 properties(SetAccess=
private)
7 % Number of bins is floor(numberOfSpectra/n)
9 % Number of bins is ceil(numberOfSpectra/n).
10 % Obviously this mode will generate output dataset with more spectra
11 % each corresponding to averages of less spectra if compared to M_FLOOR
16 %> Average every ... n
18 %> =1. Whether to perform averaging within each group. This certainly makes sense
19 %> because you don't want to average together spectra from different samples.
21 %> = rowaggr_mean.M_FLOOR. Whether to determine the number of bins by "floor" or "ceiling" modes.
26 properties(Access=protected)
27 %> Temporary storage, released after @c use()' d
29 %> Temporary storage, released after @c use()' d
37 o.classtitle = 'Average every ...';
42 function z = get_no_bins(o, no_spectra)
43 if o.mode == o.M_FLOOR
44 z = floor(no_spectra/o.n);
46 z = ceil(no_spectra/o.n);
51 methods(Access=protected)
52 function out = do_use(o, data)
53 flag_groups = o.flag_pergroup && ~isempty(data.groupcodes);
55 % Really didn't make much of a difference, actually it seems that comparing strings is even faster
58 % Prepares output dataset
59 o.outdata = data.copy_emptyrows();
60 o.no_out = 0; % Increments on this are left to process_indexes()
61 o.outdata.groupcodes = cell(o.indata.no, 1); % Same number of rows to start with
62 o.outdata.obsnames = cell(o.indata.no, 1);
63 o.outdata.X(o.indata.no, o.indata.nf) = 0;
64 o.outdata.classes(o.indata.no, 1) = 0;
67 o = o.process_indexes(1:data.no);
69 % Determines the groups
70 codes = unique(data.groupcodes);
74 % Determines the groups
75 codes = unique(data.groupcodes);
76 idxs = find(strcmp(codes{i}, data.groupcodes)); % observation indexes
85 out.classes = out.classes(ii, :);
86 out.groupcodes = out.groupcodes(ii, :);
87 out.obsnames = out.obsnames(ii, :);
88 o.indata = []; % Cleanup
93 %> Bins the spectra indicated by idxs and averages every bin
94 function o = process_indexes(o, idxs)
96 no_bins = o.get_no_bins(no);
98 h = [1, hist(1:no, no_bins)];
99 a = cumsum(h); % start of each bin
102 o.no_out = o.no_out+1;
103 ii = idxs(a(i):a(i+1)-1);
104 o.outdata.X(o.no_out, :) = mean(o.indata.X(ii, :), 1);
105 o.outdata.obsnames{o.no_out} = sprintf(
'average of %d', h(i+1));
106 o.outdata.groupcodes{o.no_out} = o.indata.groupcodes{ii(1)};
107 o.outdata.classes(o.no_out) = o.indata.classes(ii(1));
function process_indexes(in o, in idxs)
Bins the spectra indicated by idxs and averages every bin.