Home > bml > sync > bml_roi2coord.m

bml_roi2coord

PURPOSE ^

BML_ROI2COORD calculates raw coordinates for entries in roi

SYNOPSIS ^

function coord = bml_roi2coord(cfg, roi)

DESCRIPTION ^

 BML_ROI2COORD calculates raw coordinates for entries in roi

 Use as
   coord = bml_roi2coord(roi)
   coord = bml_roi2coord(cfg, roi)

 roi - roi table with one or several rows for contiguous files (e.g.
       neuroomega)
 cfg.timetol - double, time tolerance in seconds, defaults to 1e-3

 returns a coord struct with elements s1, s2, t1, t2, nSamples

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function coord = bml_roi2coord(cfg, roi)
0002 
0003 % BML_ROI2COORD calculates raw coordinates for entries in roi
0004 %
0005 % Use as
0006 %   coord = bml_roi2coord(roi)
0007 %   coord = bml_roi2coord(cfg, roi)
0008 %
0009 % roi - roi table with one or several rows for contiguous files (e.g.
0010 %       neuroomega)
0011 % cfg.timetol - double, time tolerance in seconds, defaults to 1e-3
0012 %
0013 % returns a coord struct with elements s1, s2, t1, t2, nSamples
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 %detecting contiguos stretches
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   %calculating raw samples of contiguous file
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   %doing linear fit to asses if consolidation is plausible
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 %consolidating
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

Generated on Tue 25-Sep-2018 10:08:19 by m2html © 2005