0001 function bo = split(varargin)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086
0087
0088
0089
0090
0091
0092
0093
0094
0095
0096
0097
0098
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
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
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
0134 bo = [];
0135
0136
0137 split_type = find(pl, 'split_type');
0138
0139
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';
0145 end
0146
0147 if isempty(split_type)
0148 error('### please specify the key ''split_type'' in the parameter list');
0149 end
0150
0151
0152 d = as.data;
0153
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
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
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
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
0195 nameStr = sprintf('split(%s)', d.name);
0196
0197
0198 d = set_xy_axis(d, x(idx), y(idx));
0199 d = set(d, 'name', nameStr);
0200
0201
0202 h = history(ALGONAME, VERSION, plist(param(split_x_axis.type, [is ie])), as.hist);
0203 h = set(h, 'invars', invars);
0204
0205
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
0215 b = ao(d, h);
0216
0217
0218 b = set(b, 'name', sprintf('split(%s)', n1));
0219
0220
0221 bo = [bo b];
0222
0223 end
0224
0225
0226
0227
0228 elseif strcmpi(split_type, 'samples')
0229
0230 disp('* splitting by samples...');
0231
0232 samples = find(pl, 'samples');
0233
0234
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
0241 for i=1:2:npairs
0242
0243 is = samples(i);
0244 ie = samples(i+1);
0245
0246
0247 nameStr = sprintf('split(%s)', d.name);
0248
0249
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
0256 h = history(ALGONAME, VERSION, plist(param('samples', [is ie])), as.hist);
0257 h = set(h, 'invars', invars);
0258
0259
0260 b = ao(d, h);
0261
0262
0263 b = set(b, 'name', sprintf('split(%s)', n1));
0264
0265
0266 bo = [bo b];
0267
0268 end
0269
0270
0271
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
0282
0283 elseif strcmpi(split_type, 'interval')
0284
0285
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
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
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
0310 end_time = char(end_time);
0311 end
0312
0313
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
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
0338
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
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
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
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
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
0405 nameStr = sprintf('split(%s)', d.name);
0406
0407
0408 d = set_xy_axis(d, x(idx), y(idx));
0409 d = set(d, 'name', nameStr);
0410
0411
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
0421 h = history(ALGONAME, VERSION, pl, as.hist);
0422 h = set(h, 'invars', invars);
0423
0424
0425 b = ao(d, h);
0426
0427
0428 b = set(b, 'name', sprintf('split(%s)', n1));
0429
0430
0431 bo = [bo b];
0432
0433
0434 else
0435 error('### do not know how to split.')
0436 end
0437
0438 end
0439
0440
0441
0442
0443
0444
0445
0446
0447
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
0467
0468
0469
0470
0471
0472
0473
0474
0475
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
0490
0491
0492
0493
0494
0495
0496
0497
0498
0499
0500
0501
0502
0503
0504 function sec = getSeconds(time_str, format_str)
0505
0506
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