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
0100
0101
0102
0103
0104
0105
0106
0107
0108
0109
0110
0111
0112
0113
0114
0115
0116 ALGONAME = 'split';
0117 VERSION = '$Id: split.m,v 1.35 2008/02/26 16:02:54 ingo Exp $';
0118 CATEGORY = 'Signal Processing';
0119
0120 bo = [];
0121
0122
0123 if nargin == 2
0124 if isa(varargin{1}, 'ao') && ischar(varargin{2})
0125 in = char(varargin{2});
0126 if strcmp(in, 'Params')
0127 bo = getDefaultPL();
0128 return
0129 elseif strcmp(in, 'Version')
0130 bo = VERSION;
0131 return
0132 elseif strcmp(in, 'Category')
0133 bo = CATEGORY;
0134 return
0135 end
0136 end
0137 end
0138
0139
0140 in_names = {};
0141 for ii = 1:nargin
0142 in_names{end+1} = inputname(ii);
0143
0144 if isnumeric(varargin{ii})
0145 samples = [samples varargin{ii}];
0146 end
0147 end
0148
0149 [as, pl, invars] = collect_inputs(varargin, in_names);
0150
0151 if ~isempty (pl)
0152 pl = combine(pl);
0153 end
0154
0155
0156 for j=1:numel(as)
0157
0158
0159 split_type = find(pl, 'split_type');
0160
0161
0162 if ~isempty(find(pl, 'samples'))
0163 split_type = 'samples';
0164 elseif ~isempty(find(pl, 'times')) || ...
0165 ~isempty(find(pl, 'frequencies'))
0166 split_type = 'times';
0167 end
0168
0169 if isempty(split_type)
0170 error('### please specify the key ''split_type'' in the parameter list');
0171 end
0172
0173
0174 d = as(j).data;
0175
0176 [x,y] = get_xy_values(as(j).data);
0177
0178
0179
0180
0181 if strcmpi(split_type, 'times') || strcmpi(split_type, 'frequencies')
0182
0183 times = find(pl, 'times');
0184 frequencies = find(pl, 'frequencies');
0185
0186 if ~isempty(times)
0187 disp('* splitting by time...');
0188 split_x_axis.type = 'times';
0189 split_x_axis.value = times;
0190 else
0191 disp('* splitting by frequency...');
0192 split_x_axis.type = 'frequencies';
0193 split_x_axis.value = frequencies;
0194 end
0195
0196
0197 ntimes = length(split_x_axis.value);
0198 if mod(ntimes, 2) ~= 0
0199 error('### please specify a start and stop for each interval.')
0200 end
0201
0202
0203 for i=1:2:ntimes
0204
0205 is = split_x_axis.value(i);
0206 ie = split_x_axis.value(i+1);
0207 idx = find(x>=is & x <ie);
0208
0209
0210 d.x = x(idx);
0211 d.y = y(idx);
0212 d.name = sprintf('split(%s)', d.name);
0213
0214
0215 h = history(ALGONAME, VERSION, plist(param(split_x_axis.type, [is ie])), as(j).hist);
0216 h.invars = invars(j);
0217
0218
0219 if isa(d, 'tsdata')
0220 if ~isempty(d.x)
0221 d.nsecs = d.x(end) - d.x(1) + 1/d.fs;
0222 else
0223 d.nsecs = 0;
0224 end
0225 end
0226
0227
0228 b = ao(d, h);
0229
0230
0231 b.name = sprintf('split(%s)', invars{j});
0232
0233
0234 bo = [bo b];
0235
0236 end
0237
0238
0239
0240
0241 elseif strcmpi(split_type, 'samples')
0242
0243 disp('* splitting by samples...');
0244
0245 samples = find(pl, 'samples');
0246
0247
0248 npairs = length(samples);
0249 if mod(npairs, 2) ~= 0
0250 error('### please specify a start and stop for each interval.')
0251 end
0252
0253
0254 if length(x) ~= length(y) && ~isempty(x)
0255 error('### Something is wrong with the x/y vectors. I can''t split this data.');
0256 end
0257
0258
0259 disp(sprintf('Split: \f'));
0260 c = 1;
0261 dtmp = d;
0262 dtmp.x = [];
0263 dtmp.y = [];
0264 for i=1:2:npairs
0265
0266 disp(sprintf('\b%03d ', round(c)));
0267 if mod(c,11) == 0 && c < npairs
0268 disp(sprintf('Chunk: \f'));
0269 end
0270 is = samples(i);
0271 ie = samples(i+1);
0272
0273
0274 if ~isempty(x)
0275 x1 = x(is:ie);
0276 else
0277 x1 = [];
0278 end
0279 y1 = y(is:ie);
0280
0281
0282 if isa(d, 'tsdata')
0283 d.x = x1-x1(1);
0284 else
0285 d.x = x1;
0286 end
0287 d.y = y1;
0288 d.name = sprintf('split(%s)[%d]', dtmp.name, c);
0289
0290
0291 if isa(d, 'tsdata')
0292 t0 = dtmp.t0 + x1(1);
0293 d.t0 = t0;
0294 d.nsecs = length(y1) ./ dtmp.fs;
0295 end
0296
0297
0298 h = history(ALGONAME, VERSION, plist(param('samples', [is ie])), as(j).hist);
0299 h.invars = invars;
0300
0301
0302 b = ao(d, h);
0303
0304
0305 b.name = sprintf('split(%s)[%d]', invars{j}, c);
0306
0307
0308 bo = [bo b];
0309 c = c + 1;
0310 end
0311
0312
0313
0314
0315 elseif strcmpi(split_type, 'chunks')
0316
0317 N = find(pl, 'N');
0318
0319 disp(sprintf('* splitting into %d chunks', N));
0320
0321
0322 csize = floor(length(y)/N);
0323
0324
0325 idx = [];
0326 for kk=1:N
0327 is = 1+(kk-1)*csize;
0328 ie = csize*kk;
0329 idx = [idx is ie];
0330 end
0331
0332
0333 b = split(as, plist('split_type', 'samples', 'samples', idx));
0334 bo = [bo b];
0335 return
0336
0337
0338
0339
0340 elseif strcmpi(split_type, 'interval')
0341
0342
0343 duration = find(pl, 'duration');
0344 start_time = find(pl, 'start_time');
0345 end_time = find(pl, 'end_time');
0346 format_str = find(pl, 'format_str');
0347 timespan = find(pl, 'timespan');
0348
0349
0350 if ~isempty(start_time) && isa(start_time, 'time')
0351 if ~isempty(format_str)
0352 start_time = set(start_time, 'timeformat', format_str);
0353 else
0354 format_str = char(start_time.timeformat);
0355 end
0356
0357 start_time = char(start_time);
0358 end
0359
0360 if ~isempty(end_time) && isa(end_time, 'time')
0361 if ~isempty(format_str)
0362 end_time = set(end_time, 'timeformat', format_str);
0363 else
0364 format_str = char(end_time.timeformat);
0365 end
0366
0367 end_time = char(end_time);
0368 end
0369
0370 if ~isempty(duration) && isa(duration, 'time')
0371 if ~isempty(format_str)
0372 duration = set(duration, 'timeformat', format_str);
0373 else
0374 format_str = char(duration.timeformat);
0375 end
0376
0377 duration = char(duration);
0378 end
0379
0380
0381 if ~isempty(timespan)
0382 start_time = char(timespan.start);
0383 end_time = char(timespan.end);
0384 format_str = char(timespan.timeformat);
0385 duration = '';
0386 end
0387
0388 UTC = false;
0389
0390
0391 if ~isa(as(j).data, 'tsdata')
0392 error ('### the split type: ''duration'' only works with tsdata.')
0393 end
0394
0395 if (isempty(start_time) && ...
0396 (isempty(duration) || isempty(end_time)))
0397 error ('### please specify the key ''start_time'' AND ''end_stime'' OR ''duration'' in the parameter list')
0398 end
0399
0400 if (ischar(start_time) || ...
0401 ischar(end_time) || ...
0402 ischar(duration) ) && ...
0403 isempty(format_str)
0404
0405
0406 if isUTCformat(start_time) || ...
0407 isUTCformat(end_time) || ...
0408 isUTCformat(duration)
0409 format_str = 'yyyy-mm-dd HH:MM:SS';
0410 else
0411 error ([char (10) '### please specify the key ''format_str'' for the keys ' ...
0412 '''start_time'', ''end_time'' OR/AND ''duration'''])
0413 end
0414 end
0415
0416 disp('* splitting by interval ...');
0417
0418
0419 if isUTCformat(start_time)
0420 UTC = true;
0421 start_time = time(start_time);
0422 start_time = start_time.utc_epoch_milli/1000;
0423 elseif ischar (start_time)
0424 start_time = getSeconds(start_time, format_str);
0425 end
0426
0427
0428 if isUTCformat(end_time)
0429 UTC = true;
0430 end_time = time(end_time);
0431 end_time = end_time.utc_epoch_milli/1000;
0432 elseif ischar (end_time)
0433 end_time = getSeconds(end_time, format_str);
0434 end
0435
0436
0437 if ~isempty(end_time) && ~isempty(duration)
0438 warning (['### in the parameter list are both parameter '...
0439 '''end_time'' and ''duration''. Using the parameter ''end_time'''])
0440 elseif ischar(duration) && ~isempty(duration)
0441 duration = getSeconds(duration, format_str);
0442 end
0443
0444
0445 t0 = as(j).data.t0;
0446 t_unix_time = t0.utc_epoch_milli/1000;
0447 t1 = x(1);
0448
0449 if UTC == true
0450 ts = start_time-t_unix_time+t1;
0451 if ~isempty(end_time)
0452 te = end_time-t_unix_time+t1;
0453 else
0454 te = ts+duration;
0455 end
0456 else
0457
0458 t0_offset = format(t0, format_str);
0459 t0_offset = getSeconds(t0_offset, format_str);
0460
0461 ts = start_time+t1-t0_offset;
0462 if ~isempty(end_time)
0463 te = end_time+t1-t0_offset;
0464 else
0465 te = start_time+duration+t1-t0_offset;
0466 end
0467 end
0468
0469 idx = find(x>=ts & x <te);
0470
0471
0472 d.x = x(idx);
0473 d.y = y(idx);
0474 d.name = sprintf('split(%s)', d.name);
0475
0476
0477 if isa(d, 'tsdata')
0478 if ~isempty(d.x)
0479 d.nsecs = d.x(end) - d.x(1) + 1/d.fs;
0480 else
0481 d.nsecs = 0;
0482 end
0483 end
0484
0485
0486 h = history(ALGONAME, VERSION, pl, as(j).hist);
0487 h.invars = invars;
0488
0489
0490 b = ao(d, h);
0491
0492
0493 b.name = sprintf('split(%s)', invars{j});
0494
0495
0496
0497 if isempty(duration)
0498 duration = end_time - start_time;
0499 end
0500 start_time = find(pl, 'start_time');
0501 b.data.t0 = b.data.t0+b.data.x(1);
0502 b.data.x = b.data.x - b.data.x(1);
0503
0504
0505 bo = [bo b];
0506
0507
0508 else
0509 error('### do not know how to split.')
0510 end
0511
0512 end
0513
0514
0515 if numel(bo) == numel(as)
0516 bo = reshape(bo, size(as));
0517 end
0518
0519 end
0520
0521
0522
0523
0524
0525
0526
0527
0528
0529
0530
0531
0532 function plo = getDefaultPL()
0533
0534 plo = plist();
0535 plo = append(plo, param('split_type', ''));
0536 plo = append(plo, param('times', []));
0537 plo = append(plo, param('frequencies', []));
0538 plo = append(plo, param('samples', []));
0539 plo = append(plo, param('N', []));
0540 plo = append(plo, param('duration', ''));
0541 plo = append(plo, param('start_time', ''));
0542 plo = append(plo, param('end_time', ''));
0543 plo = append(plo, param('format_str', ''));
0544
0545 end
0546
0547
0548
0549
0550
0551
0552
0553
0554
0555
0556
0557
0558 function bool = isUTCformat(time_string)
0559
0560 bool = false;
0561
0562 if ischar(time_string)
0563 if regexp(time_string, '\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}')
0564 bool = true;
0565 end
0566 end
0567
0568 end
0569
0570
0571
0572
0573
0574
0575
0576
0577
0578
0579
0580
0581
0582
0583 function sec = getSeconds(time_str, format_str)
0584
0585
0586 if regexp(format_str, '[ymd]')
0587 if ~strcmp('yyyy-mm-dd HH:MM:SS', format_str)
0588 error (['### if you want to specify the time without using the UTC time '...
0589 'use only hours(HH) minutes(MM) and seconds(SS)'])
0590 end
0591 end
0592
0593 try
0594 sec = ( datenum(time_str, format_str) - fix(datenum(time_str, format_str)) ) * 86400;
0595 sec = round(sec*1000)/1000;
0596 catch
0597 last_error = lasterror;
0598 if strfind(last_error.identifier, 'datenum')
0599 error ([last_error.message char(10) char(10) ...
0600 'time_str = ''' time_str ''' format string = ''' format_str ''''])
0601 else
0602 error (last_error.message)
0603 end
0604 end
0605
0606 end