Home > bml > sync > bml_idx2time.m

bml_idx2time

PURPOSE ^

BML_IDX2TIME calculates samples midpoint times from a index vector and file coordinates

SYNOPSIS ^

function time=bml_idx2time(cfg, idx, skipFactor)

DESCRIPTION ^

 BML_IDX2TIME calculates samples midpoint times from a index vector and file coordinates

 Use as 
   time=bml_idx2time(cfg, idx)
   time=bml_idx2time(cfg, idx, skipFactor)

 idx - numeric vector of indices to transform to time
 skipFactor - numeric, integer indicating skip factor as used by blackrock
     NPMK package. Defaults to 1.

 where cfg is a configuration structure or roi table 
 cfg.t1
 cfg.s1
 cfg.t2
 cfg.s2
 cfg.nSamples - double: optional total number of samples in file

 if cfg has more than one row, (s1, s2) should be non-overlaping

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function time=bml_idx2time(cfg, idx, skipFactor)
0002 
0003 % BML_IDX2TIME calculates samples midpoint times from a index vector and file coordinates
0004 %
0005 % Use as
0006 %   time=bml_idx2time(cfg, idx)
0007 %   time=bml_idx2time(cfg, idx, skipFactor)
0008 %
0009 % idx - numeric vector of indices to transform to time
0010 % skipFactor - numeric, integer indicating skip factor as used by blackrock
0011 %     NPMK package. Defaults to 1.
0012 %
0013 % where cfg is a configuration structure or roi table
0014 % cfg.t1
0015 % cfg.s1
0016 % cfg.t2
0017 % cfg.s2
0018 % cfg.nSamples - double: optional total number of samples in file
0019 %
0020 % if cfg has more than one row, (s1, s2) should be non-overlaping
0021 
0022 pTT = 9; %pTT = pTimeTolerenace = - log10(timetol)
0023 
0024 if nargin==2
0025   skipFactor=1;
0026 elseif nargin==3
0027   skipFactor=round(skipFactor);
0028 else
0029   error('unsupported call to bml_idx2time, see usage');
0030 end
0031 
0032 if istable(cfg)
0033   if height(cfg) > 1
0034     %dealing with split sync
0035     assert(isempty(bml_annot_overlap(cfg(:,{'s1','s2'}))),...
0036       "cfg table contains overlapping segments of samples");
0037     
0038     time = zeros(1,length(idx));
0039     for i=1:height(cfg)
0040       t1=round(cfg.t1(i),pTT);
0041       s1=cfg.s1(i);
0042       t2=round(cfg.t2(i),pTT);
0043       s2=cfg.s2(i);
0044       Fs=round((s2-s1)/round(t2-t1,pTT),pTT,'significat');  
0045       if skipFactor > 1
0046         s1=ceil(s1/skipFactor);
0047         s2=floor(s2/skipFactor);
0048         t1=t1+(skipFactor-1)*0.5/Fs;
0049         t2=t2-(skipFactor-1)*0.5/Fs;
0050         Fs = round((s2-s1)/round(t2-t1,pTT),pTT,'significat');  
0051       end
0052       idx_filt = idx>=s1 & idx<=s2;
0053       time(idx_filt) = double(idx(idx_filt))/Fs - 0.5/Fs + (s2*t1-t2*s1)/(s2-s1);
0054     end
0055     return
0056   end
0057   cfg=table2struct(cfg);
0058 end
0059   
0060 %dealing with simple sync
0061 t1=round(bml_getopt(cfg,'t1'),pTT);
0062 s1=bml_getopt(cfg,'s1');
0063 t2=round(bml_getopt(cfg,'t2'),pTT);
0064 s2=bml_getopt(cfg,'s2');
0065 Fs = round((s2-s1)/round(t2-t1,pTT),pTT,'significant'); 
0066 if skipFactor > 1
0067   s1=ceil(s1/skipFactor);
0068   s2=floor(s2/skipFactor);
0069   t1=t1+(skipFactor-1)*0.5/Fs;
0070   t2=t2-(skipFactor-1)*0.5/Fs;
0071   Fs = round((s2-s1)/round(t2-t1,pTT),pTT,'significant');
0072 end
0073  
0074 time = double(idx)/Fs - 0.5/Fs + (s2*t1-t2*s1)/(s2-s1);

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