0001 function coord = bml_roi2coord(cfg, roi)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015 if nargin==1
0016 if istable(cfg)
0017 cfg = struct('roi',cfg);
0018 end
0019 elseif nargin~=2
0020 error('incorrect number of arguments');
0021 end
0022
0023 roi = bml_getopt(cfg,'roi');
0024 timetol = bml_getopt(cfg,'timetol',1e-3);
0025
0026 assert(~isempty(roi),'non-empty roi required');
0027
0028 roi.filetype_chantype = strcat(roi.filetype,roi.chantype,num2str(roi.Fs));
0029 assert(length(unique(roi.filetype_chantype))==1,...
0030 'rows in roi belong to files of different type');
0031
0032
0033 cfg=[];
0034 cfg.criterion = @(x) sum(x.duration)-max(x.ends)+min(x.starts) < height(x)*timetol;
0035 roi_cont = bml_annot_consolidate(cfg,roi);
0036
0037 assert(height(roi_cont)==1,'files in roi are not time contiguous');
0038
0039 if height(roi)>1
0040
0041
0042 cs = cumsum(roi.s2-roi.s1) + roi.s1(1);
0043 cs = cs + (0:(height(roi)-1))';
0044 cs = [0; cs(1:end-1)];
0045 roi.raw1 = roi.s1 + cs;
0046 roi.raw2 = roi.s2 + cs;
0047
0048
0049 s = [roi.raw1; roi.raw2];
0050 t = [roi.t1; roi.t2];
0051 p = polyfit(s,t,1);
0052 tfit = polyval(p,s);
0053
0054 if max(abs(t - tfit)) <= timetol
0055 roi.t1 = polyval(p,roi.raw1);
0056 roi.t2 = polyval(p,roi.raw2);
0057 else
0058 warning('can''t consolidate within tolerance');
0059 end
0060
0061 else
0062 roi.raw1 = roi.s1;
0063 roi.raw2 = roi.s2;
0064 end
0065
0066 coord = [];
0067 coord.s1 = min(roi.raw1);
0068 coord.s2 = max(roi.raw2);
0069 coord.t1 = min(roi.t1);
0070 coord.t2 = max(roi.t2);
0071 coord.nSamples = sum(roi.raw2 - roi.raw1 + 1);
0072
0073 assert(coord.nSamples == coord.s2 - coord.s1 + 1, 'inconsistent coord');
0074
0075
0076
0077
0078