Home > bml > sync > bml_crop_idx.m

bml_crop_idx

PURPOSE ^

BML_CROP_IDX calculates sample indices for a time window and file coordinates

SYNOPSIS ^

function [starts_idx,ends_idx] = bml_crop_idx(cfg, starts, ends, samples)

DESCRIPTION ^

 BML_CROP_IDX calculates sample indices for a time window and file coordinates

 Use as 
   [starts_idx,ends_idx] = bml_crop_idx(cfg)
   [starts_idx,ends_idx] = bml_crop_idx(cfg, starts, ends)
   [starts_idx,ends_idx] = bml_crop_idx(cfg, starts, [], samples)
   [starts_idx,ends_idx] = bml_crop_idx(cfg, [], ends, samples)

 where cfg is a configuration structure or roi table row
 cfg.starts
 cfg.ends
 (cfg.samples)
 cfg.t1
 cfg.s1
 cfg.t2
 cfg.s2
 cfg.tol - double: tolerance. Defaults to 1e-3/Fs. 
 cfg.nSamples - double: optional total number of samples in file

 if starts and ends are given (3 argument call) the values of cfg are
 ignored

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [starts_idx,ends_idx] = bml_crop_idx(cfg, starts, ends, samples)
0002 
0003 % BML_CROP_IDX calculates sample indices for a time window and file coordinates
0004 %
0005 % Use as
0006 %   [starts_idx,ends_idx] = bml_crop_idx(cfg)
0007 %   [starts_idx,ends_idx] = bml_crop_idx(cfg, starts, ends)
0008 %   [starts_idx,ends_idx] = bml_crop_idx(cfg, starts, [], samples)
0009 %   [starts_idx,ends_idx] = bml_crop_idx(cfg, [], ends, samples)
0010 %
0011 % where cfg is a configuration structure or roi table row
0012 % cfg.starts
0013 % cfg.ends
0014 % (cfg.samples)
0015 % cfg.t1
0016 % cfg.s1
0017 % cfg.t2
0018 % cfg.s2
0019 % cfg.tol - double: tolerance. Defaults to 1e-3/Fs.
0020 % cfg.nSamples - double: optional total number of samples in file
0021 %
0022 % if starts and ends are given (3 argument call) the values of cfg are
0023 % ignored
0024 
0025 % if istable(cfg)
0026 %   if height(cfg) > 1; error('cfg should be a 1 row table'); end
0027 %   cfg=table2struct(cfg);
0028 % end
0029 
0030 pTT = 9; %pTT = pTimeTolerenace = - log10(timetol)
0031 
0032 if nargin==1
0033   starts=round(bml_getopt(cfg,'starts'),pTT);
0034   ends=round(bml_getopt(cfg,'ends'),pTT);
0035   samples=bml_getopt(cfg,'samples');
0036 elseif nargin==3
0037   samples=[];
0038 elseif nargin~=4
0039   error('unsupported call to bml_crop_idx, see usage');
0040 end
0041   
0042 if ~isempty(starts) + ~isempty(ends) + ~isempty(samples) ~= 2
0043   error('two of the three following parameters are required: ''starts'', ''ends'', ''samples'''); 
0044 end
0045 
0046 t1=bml_getopt(cfg,'t1');
0047 s1=bml_getopt(cfg,'s1');
0048 t2=bml_getopt(cfg,'t2');
0049 s2=bml_getopt(cfg,'s2');
0050 
0051 Fs=(s2-s1)/round(t2-t1,pTT);
0052 
0053 tol=bml_getopt(cfg,'tol',1e-2/Fs);
0054 
0055 if ~isempty(starts)
0056   starts_idx = round((t2*s1-s2*t1)/(t2-t1)+Fs*starts+tol);
0057   %starts_idx = ceil(0.5+(t2*s1-s2*t1)/(t2-t1)+Fs*starts);
0058 end
0059 
0060 if isempty(ends)
0061   ends_idx = starts_idx + samples; 
0062 else
0063   ends_idx = round((t2*s1-s2*t1)/(t2-t1)+Fs*ends-tol);
0064   %ends_idx = floor(0.5+(t2*s1-s2*t1)/(t2-t1)+Fs*ends);
0065 end
0066 
0067 if isempty(starts) 
0068   starts_idx = ends_idx - samples;
0069 end
0070

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