


SPLIT split an analysis object into the specified segments.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
DESCRIPTION: SPLIT split an analysis object into the specified segments.
CALL: b = split(a, pl)
INPUTS: a - input analysis object
pl - input parameter list (see below for parameters)
OTPUTS: b - array of analysis objects
PARAMETERS: <key> <value> <description>
'split_type' 'times' split the ao into time segments
'frequencies' split the ao into frequencies segments
'samples' split the ao into sample segments
'chunks' split the ao into chunks
'duration' select a duration of a tsdata
Necessary for the individual split types:
<split type> <key> <description>
'times' 'times' an array of start/stop times to split by
'frequencies' 'frequencies' an array of start/stop frequencies to split by
'samples' 'samples' an array of start/stop samples to split by
'chunks' 'N' split into N contiguous pieces
'interval' 'start_time', 'stop_time'
- start/stop time can be either a string or a time
object (if a string, 'format_str' must be
specified or specify the strings in UTC time format)
'interval' 'start_time', 'duration'
- start time and the duration can be either a string
or a time object (if a string, 'format_str' must be
specified or specify the strings in UTC time format)
'interval' 'timespan'
- the start/end time are specified in the time span
object.
The UTC time format is: 'yyyy-mm-dd HH:MM:SS'
If more than one splitting method is specified, the priority
goes like the list above.
The time vector in the output AO retains the original
time values (i.e. it doesn't start from zero).
The splitting is done as s<=t<e.
Arrays of start/stop values should be like: [s1 e1 s2 e2 ....]
EXAMPLES: 1.) Split method by frequency. Get the values from 10-100 Hz
pl = plist('split_type', 'frequencies', ...
'frequencies', [10 100]);
ao_new = split(a1, pl);
2.) Split method by time.
Get the values from 0.0 to 1.0 Seconds AND from 1.0 to 2.5 seconds
pl = plist('split_type', 'time', ...
'time', [0.0 1.0 1.0 2.5]);
ao_new = split(a1, pl);
3.) Split method by samples.
Get the samples from 0 to 50 AND from 150 to 200.
pl = plist('split_type', 'samples', ...
'samples', [0 50 150 200]);
ao_new = split(a1, pl);
4.1) Select an interval with strings
pl = plist('split_type', 'interval', ...
'start_time', '14:00:01', ...
'end_time', '14:00:02', ...
'format_str', 'HH:MM:SS');
ao_new = split(a1, pl);
4.2) Select an interval with time objects
pl = plist('split_type', 'interval', ...
'start_time', time('14:00:01', 'HH:MM:SS'), ...
'end_time', time('2007-10-10 14:00:03'));
ao_new = split(a1, pl);
4.3) Select an interval with a time span object
pl = plist('split_type', 'interval', ...
'timespan', timespan('14:00:00', '14:00:05', 'HH:MM:SS'));
ao_new = split(a1, pl);
The following call returns a parameter list object that
contains the default parameter values:
>> pl = split(ao, 'Params')
VERSION: $Id: split.m,v 1.17 2007/08/20 14:41:11 ingo Exp $
HISTORY: 02-03-07 M Hewitson
Creation.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


0001 function bo = split(varargin) 0002 % SPLIT split an analysis object into the specified segments. 0003 % 0004 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0005 % 0006 % DESCRIPTION: SPLIT split an analysis object into the specified segments. 0007 % 0008 % CALL: b = split(a, pl) 0009 % 0010 % INPUTS: a - input analysis object 0011 % pl - input parameter list (see below for parameters) 0012 % 0013 % OTPUTS: b - array of analysis objects 0014 % 0015 % PARAMETERS: <key> <value> <description> 0016 % 'split_type' 'times' split the ao into time segments 0017 % 'frequencies' split the ao into frequencies segments 0018 % 'samples' split the ao into sample segments 0019 % 'chunks' split the ao into chunks 0020 % 'duration' select a duration of a tsdata 0021 % 0022 % Necessary for the individual split types: 0023 % <split type> <key> <description> 0024 % 'times' 'times' an array of start/stop times to split by 0025 % 'frequencies' 'frequencies' an array of start/stop frequencies to split by 0026 % 'samples' 'samples' an array of start/stop samples to split by 0027 % 'chunks' 'N' split into N contiguous pieces 0028 % 'interval' 'start_time', 'stop_time' 0029 % - start/stop time can be either a string or a time 0030 % object (if a string, 'format_str' must be 0031 % specified or specify the strings in UTC time format) 0032 % 'interval' 'start_time', 'duration' 0033 % - start time and the duration can be either a string 0034 % or a time object (if a string, 'format_str' must be 0035 % specified or specify the strings in UTC time format) 0036 % 'interval' 'timespan' 0037 % - the start/end time are specified in the time span 0038 % object. 0039 % 0040 % The UTC time format is: 'yyyy-mm-dd HH:MM:SS' 0041 % 0042 % If more than one splitting method is specified, the priority 0043 % goes like the list above. 0044 % 0045 % The time vector in the output AO retains the original 0046 % time values (i.e. it doesn't start from zero). 0047 % 0048 % The splitting is done as s<=t<e. 0049 % 0050 % Arrays of start/stop values should be like: [s1 e1 s2 e2 ....] 0051 % 0052 % EXAMPLES: 1.) Split method by frequency. Get the values from 10-100 Hz 0053 % pl = plist('split_type', 'frequencies', ... 0054 % 'frequencies', [10 100]); 0055 % ao_new = split(a1, pl); 0056 % 0057 % 2.) Split method by time. 0058 % Get the values from 0.0 to 1.0 Seconds AND from 1.0 to 2.5 seconds 0059 % pl = plist('split_type', 'time', ... 0060 % 'time', [0.0 1.0 1.0 2.5]); 0061 % ao_new = split(a1, pl); 0062 % 0063 % 3.) Split method by samples. 0064 % Get the samples from 0 to 50 AND from 150 to 200. 0065 % pl = plist('split_type', 'samples', ... 0066 % 'samples', [0 50 150 200]); 0067 % ao_new = split(a1, pl); 0068 % 0069 % 4.1) Select an interval with strings 0070 % pl = plist('split_type', 'interval', ... 0071 % 'start_time', '14:00:01', ... 0072 % 'end_time', '14:00:02', ... 0073 % 'format_str', 'HH:MM:SS'); 0074 % ao_new = split(a1, pl); 0075 % 0076 % 4.2) Select an interval with time objects 0077 % pl = plist('split_type', 'interval', ... 0078 % 'start_time', time('14:00:01', 'HH:MM:SS'), ... 0079 % 'end_time', time('2007-10-10 14:00:03')); 0080 % ao_new = split(a1, pl); 0081 % 0082 % 4.3) Select an interval with a time span object 0083 % pl = plist('split_type', 'interval', ... 0084 % 'timespan', timespan('14:00:00', '14:00:05', 'HH:MM:SS')); 0085 % ao_new = split(a1, pl); 0086 % 0087 % The following call returns a parameter list object that 0088 % contains the default parameter values: 0089 % >> pl = split(ao, 'Params') 0090 % 0091 % VERSION: $Id: split.m,v 1.17 2007/08/20 14:41:11 ingo Exp $ 0092 % 0093 % HISTORY: 02-03-07 M Hewitson 0094 % Creation. 0095 % 0096 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0097 0098 % Check if this is a call for parameters 0099 if nargin == 2 0100 if isa(varargin{1}, 'ao') && ischar(varargin{2}) 0101 in = char(varargin{2}); 0102 if strcmp(in, 'Params') 0103 bo = getDefaultPL(); 0104 return 0105 end 0106 end 0107 end 0108 0109 % capture input variable names 0110 invars = {}; 0111 for j=1:nargin 0112 if isa(varargin{j}, 'ao') 0113 invars = [invars cellstr(inputname(j))]; 0114 end 0115 end 0116 0117 ALGONAME = 'split'; 0118 VERSION = '$Id: split.m,v 1.17 2007/08/20 14:41:11 ingo Exp $'; 0119 0120 % Look at inputs 0121 if nargin < 1 0122 error('### Incorrect number of inputs.') 0123 end 0124 if nargin == 1 0125 as = varargin{1}; 0126 pl = plist(); 0127 end 0128 if nargin == 2 0129 as = varargin{1}; 0130 pl = varargin{2}; 0131 end 0132 0133 % Initialise output 0134 bo = []; 0135 % Unpack parameter list 0136 0137 split_type = find(pl, 'split_type'); 0138 0139 % check old version without the key 'split_type' 0140 if ~isempty(find(pl, 'samples')) 0141 split_type = 'samples'; 0142 elseif ~isempty(find(pl, 'times')) || ... 0143 ~isempty(find(pl, 'frequencies')) 0144 split_type = 'times'; % times is the same to frequencies 0145 end 0146 0147 if isempty(split_type) 0148 error('### please specify the key ''split_type'' in the parameter list'); 0149 end 0150 0151 % look at input data 0152 d = as.data; 0153 % name for this object 0154 if isempty(invars{1}) 0155 n1 = as.name; 0156 else 0157 n1 = invars{1}; 0158 end 0159 0160 [x,y] = get_xy_axis(as.data); 0161 0162 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0163 % splitting by time or frequency % 0164 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0165 if strcmpi(split_type, 'times') || strcmpi(split_type, 'frequencies') 0166 0167 times = find(pl, 'times'); 0168 frequencies = find(pl, 'frequencies'); 0169 0170 if ~isempty(times) 0171 disp('* splitting by time...'); 0172 split_x_axis.type = 'times'; 0173 split_x_axis.value = times; 0174 else 0175 disp('* splitting by frequency...'); 0176 split_x_axis.type = 'frequencies'; 0177 split_x_axis.value = frequencies; 0178 end 0179 0180 % examine time list 0181 ntimes = length(split_x_axis.value); 0182 if mod(ntimes, 2) ~= 0 0183 error('### please specify a start and stop for each interval.') 0184 end 0185 0186 % go over each interval now 0187 for i=1:2:ntimes 0188 0189 is = split_x_axis.value(i); 0190 ie = split_x_axis.value(i+1); 0191 0192 idx = find(x>=is & x <ie); 0193 0194 % Make output analysis object 0195 nameStr = sprintf('split(%s)', d.name); 0196 0197 % create new output data 0198 d = set_xy_axis(d, x(idx), y(idx)); 0199 d = set(d, 'name', nameStr); 0200 0201 % create new output history 0202 h = history(ALGONAME, VERSION, plist(param(split_x_axis.type, [is ie])), as.hist); 0203 h = set(h, 'invars', invars); 0204 0205 % Set nsecs for tsdata 0206 if isa(d, 'tsdata') 0207 if ~isempty(d.t) 0208 d.nsecs = d.t(end) - d.t(1) + 1/d.fs; 0209 else 0210 d.nsecs = 0; 0211 end 0212 end 0213 0214 % make output analysis object 0215 b = ao(d, h); 0216 0217 % set name 0218 b = set(b, 'name', sprintf('split(%s)', n1)); 0219 0220 % Add to output array 0221 bo = [bo b]; 0222 0223 end 0224 0225 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0226 % splitting by samples % 0227 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0228 elseif strcmpi(split_type, 'samples') 0229 0230 disp('* splitting by samples...'); 0231 0232 samples = find(pl, 'samples'); 0233 0234 % examine time list 0235 npairs = length(samples); 0236 if mod(npairs, 2) ~= 0 0237 error('### please specify a start and stop for each interval.') 0238 end 0239 0240 % go over each interval now 0241 for i=1:2:npairs 0242 0243 is = samples(i); 0244 ie = samples(i+1); 0245 0246 % Make output analysis object 0247 nameStr = sprintf('split(%s)', d.name); 0248 0249 % create new output data 0250 x1 = x(is:ie); 0251 y1 = y(is:ie); 0252 d = set_xy_axis(d, x1, y1); 0253 d = set(d, 'name', nameStr); 0254 0255 % create new output history 0256 h = history(ALGONAME, VERSION, plist(param('samples', [is ie])), as.hist); 0257 h = set(h, 'invars', invars); 0258 0259 % make output analysis object 0260 b = ao(d, h); 0261 0262 % set name 0263 b = set(b, 'name', sprintf('split(%s)', n1)); 0264 0265 % Add to output array 0266 bo = [bo b]; 0267 0268 end 0269 0270 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0271 % splitting into chunks % 0272 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0273 elseif strcmpi(split_type, 'chunks') 0274 0275 N = find(pl, 'N'); 0276 0277 disp(sprintf('* splitting into %d chunks', N)); 0278 error('### I have not been written yet. Please code me up.'); 0279 0280 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0281 % splitting into interval % 0282 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0283 elseif strcmpi(split_type, 'interval') 0284 0285 %% get values from the parameter list 0286 duration = find(pl, 'duration'); 0287 start_time = find(pl, 'start_time'); 0288 end_time = find(pl, 'end_time'); 0289 format_str = find(pl, 'format_str'); 0290 timespan = find(pl, 'timespan'); 0291 0292 %%%%%%%%%%%%%%%%%%%%%%%% Convert the time object %%%%%%%%%%%%%%%%%%%%%%%% 0293 if ~isempty(start_time) && isa(start_time, 'time') 0294 if ~isempty(format_str) 0295 start_time = set(start_time, 'timeformat', format_str); 0296 else 0297 format_str = char(start_time.timeformat); 0298 end 0299 % format_str = char(start_time.timeformat); 0300 start_time = char(start_time); 0301 end 0302 0303 if ~isempty(end_time) && isa(end_time, 'time') 0304 if ~isempty(format_str) 0305 end_time = set(end_time, 'timeformat', format_str); 0306 else 0307 format_str = char(end_time.timeformat); 0308 end 0309 % format_str = char(end_time.timeformat); 0310 end_time = char(end_time); 0311 end 0312 0313 %%%%%%%%%%%%%%%%%%%%% Convert the time span object %%%%%%%%%%%%%%%%%%%%%% 0314 if ~isempty(timespan) 0315 start_time = char(timespan.start); 0316 end_time = char(timespan.end); 0317 format_str = char(timespan.timeformat); 0318 duration = ''; 0319 end 0320 0321 UTC = false; 0322 0323 %% check the input values 0324 if ~isa(as.data, 'tsdata') 0325 error ('### the split type: ''duration'' only works with tsdata.') 0326 end 0327 0328 if (isempty(start_time) && ... 0329 (isempty(duration) || isempty(end_time))) 0330 error ('### please specify the key ''start_time'' AND ''end_stime'' OR ''duration'' in the parameter list') 0331 end 0332 0333 if (ischar(start_time) || ... 0334 ischar(end_time) || ... 0335 ischar(duration) ) && ... 0336 isempty(format_str) 0337 %% are the time strings in UTC (yyy-mm-dd HH:MM:SS) 0338 %% then set the format string to the UTC format. 0339 if isUTCformat(start_time) || ... 0340 isUTCformat(end_time) || ... 0341 isUTCformat(duration) 0342 format_str = 'yyyy-mm-dd HH:MM:SS'; 0343 else 0344 error ([char (10) '### please specify the key ''format_str'' for the keys ' ... 0345 '''start_time'', ''end_time'' OR/AND ''duration''']) 0346 end 0347 end 0348 0349 disp('* splitting by duration ...'); 0350 0351 %% convert the start time 0352 if isUTCformat(start_time) 0353 UTC = true; 0354 start_time = time(start_time); 0355 start_time = start_time.utc_epoch_milli/1000; 0356 elseif ischar (start_time) 0357 start_time = getSeconds(start_time, format_str); 0358 end 0359 0360 %% convert the end time 0361 if isUTCformat(end_time) 0362 UTC = true; 0363 end_time = time(end_time); 0364 end_time = end_time.utc_epoch_milli/1000; 0365 elseif ischar (end_time) 0366 end_time = getSeconds(end_time, format_str); 0367 end 0368 0369 %% convert the duration 0370 if ~isempty(end_time) && ~isempty(duration) 0371 warning (['### in the parameter list are both parameter '... 0372 '''end_time'' and ''duration''. Using the parameter ''end_time''']) 0373 elseif ischar(duration) && ~isempty(duration) 0374 duration = getSeconds(duration, format_str); 0375 end 0376 0377 %% get/convert the t0 0378 t0 = as.data.t0; 0379 t_unix_time = t0.utc_epoch_milli/1000; 0380 t1 = x(1); 0381 0382 if UTC == true 0383 ts = start_time-t_unix_time+t1; 0384 if ~isempty(end_time) 0385 te = end_time-t_unix_time+t1; 0386 else 0387 te = ts+duration; 0388 end 0389 else 0390 0391 t0_offset = format(t0, format_str); 0392 t0_offset = getSeconds(t0_offset, format_str); 0393 0394 ts = start_time+t1-t0_offset; 0395 if ~isempty(end_time) 0396 te = end_time+t1-t0_offset; 0397 else 0398 te = start_time+duration+t1-t0_offset; 0399 end 0400 end 0401 0402 idx = find(x>=ts & x <te); 0403 0404 % Make output analysis object 0405 nameStr = sprintf('split(%s)', d.name); 0406 0407 % create new output data 0408 d = set_xy_axis(d, x(idx), y(idx)); 0409 d = set(d, 'name', nameStr); 0410 0411 % Set nsecs for tsdata 0412 if isa(d, 'tsdata') 0413 if ~isempty(d.t) 0414 d.nsecs = d.t(end) - d.t(1) + 1/d.fs; 0415 else 0416 d.nsecs = 0; 0417 end 0418 end 0419 0420 % create new output history 0421 h = history(ALGONAME, VERSION, pl, as.hist); 0422 h = set(h, 'invars', invars); 0423 0424 % make output analysis object 0425 b = ao(d, h); 0426 0427 % set name 0428 b = set(b, 'name', sprintf('split(%s)', n1)); 0429 0430 % Add to output array 0431 bo = [bo b]; 0432 0433 0434 else 0435 error('### do not know how to split.') 0436 end 0437 0438 end % split 0439 0440 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0441 % 0442 % FUNCTION: getDefaultPL 0443 % 0444 % DESCRIPTION: Returns the default parameter list 0445 % 0446 % HISTORY: 02-03-07 M Hewitson 0447 % Creation. 0448 % 0449 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0450 0451 function plo = getDefaultPL() 0452 0453 disp('* creating default plist...'); 0454 plo = plist(); 0455 plo = append(plo, param('split_type', '')); 0456 plo = append(plo, param('times', [])); 0457 plo = append(plo, param('frequencies', [])); 0458 plo = append(plo, param('samples', [])); 0459 plo = append(plo, param('N', [])); 0460 plo = append(plo, param('duration', '')); 0461 plo = append(plo, param('start_time', '')); 0462 plo = append(plo, param('end_time', '')); 0463 plo = append(plo, param('format_str', '')); 0464 disp('* done.'); 0465 0466 end % getDefaultPL 0467 0468 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0469 % 0470 % FUNCTION: isUTCformat 0471 % 0472 % DESCRIPTION: Check the input to the UTC time format (yyyy-mm-dd- HH:MM:SS) 0473 % 0474 % HISTORY: 11-07-07 Diepholz 0475 % Creation. 0476 % 0477 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0478 0479 function bool = isUTCformat(time_string) 0480 0481 bool = false; 0482 0483 if ischar(time_string) 0484 if regexp(time_string, '\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}') 0485 bool = true; 0486 end 0487 end 0488 0489 end % isUTCformat 0490 0491 0492 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0493 % 0494 % FUNCTION: getSeconds 0495 % 0496 % DESCRIPTION: Returns the seconds for any time format with hours minutes 0497 % ans seconds. 0498 % 0499 % HISTORY: 11-07-07 Diepholz 0500 % Creation. 0501 % 0502 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0503 0504 function sec = getSeconds(time_str, format_str) 0505 0506 % The customer use year, month or day to specify the time_str 0507 if regexp(format_str, '[ymd]') 0508 if ~strcmp('yyyy-mm-dd HH:MM:SS', format_str) 0509 error (['### if you want to specify the time without using the UTC time '... 0510 'use only hours(HH) minutes(MM) and seconds(SS)']) 0511 end 0512 end 0513 0514 try 0515 sec = ( datenum(time_str, format_str) - fix(datenum(time_str, format_str)) ) * 86400; 0516 sec = round(sec*1000)/1000; 0517 catch 0518 last_error = lasterror; 0519 if strfind(last_error.identifier, 'datenum') 0520 error ([last_error.message char(10) char(10) ... 0521 'time_str = ''' time_str ''' format string = ''' format_str '''']) 0522 else 0523 error (last_error.message) 0524 end 0525 end 0526 0527 end % getSeconds