-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbin_pack.m
More file actions
executable file
·68 lines (54 loc) · 2.47 KB
/
bin_pack.m
File metadata and controls
executable file
·68 lines (54 loc) · 2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
%%%%% Bin data and put into data structure %%%%%
function [dd] = bin_pack(timeframe, method, varargin)
% Averages data over specified timeframes and returns a structure:
%
% dd.{num_hours}_hour.{data_name} = [value1, value2, ... valueN]
%
% Assumes original data is recorded in hourly intervals.
%
% :param timeframes: array specifying the lengths of time frames (int)
% :param method: indicates the desired method (string)
% :param varargin: pairs of desired data name and associated array (cell)
% :return dd: struct formated as dd.timeframe.dataname = binned array
% loop over timeframes
formatSpec = 'hour%d';
for tt = 1:max(size(timeframe))
outstr = sprintf(formatSpec, timeframe(tt));
% loop over pairs of data names and arrays
for ii = 1:max(size(varargin))
% read in pair
in_cell = varargin{ii};
% if the time frame is just 1, copy into struct
if (timeframe(tt) == 1)
dd.(outstr).(in_cell{1}) = in_cell{2};
else
if (strcmp(method, 'block'))
% try to break into appropriate sized chunks
try
out = mean(reshape(in_cell{2}, timeframe(tt), ...
max(size(in_cell{2}))/timeframe(tt)));
dd.(outstr).(in_cell{1}) = out;
catch ME
if (strcmp(ME.identifier, 'MATLAB:getReshapeDims:notSameNumel'))
msg = ['Number of array elements cannot change when binning: ', ...
in_cell{1}, ' has ', num2str(max(size(in_cell{2}))), ...
' elements and new reshaped array has dimsension', ...
num2str(tt), 'x', num2st(max(size(in_cell{2}))/tt), ...
'. Check input array size and desired bin width.'];
causeException = MException('MATLAB:bin_pack:dimensions',msg);
ME = addCause(ME, causeException);
end
rethrow(ME)
end
elseif (strcmp(method, 'run_mean'))
% compute running mean in windows of size timeframe
% (ignoring nans)
dd.(outstr).(in_cell{1}) = movmean(in_cell{2}, timeframe(tt),...
'omitnan');
else
sprintf('Undefined averaging method')
end
end
end
end
end