[onsets, offsets] = find_thresh_cross(data,params); Returns vectors containing the sample indices at which threshold values in the data vector were crossed. Will return both onsets and offsets. params is a structure that governs how the threshold crossings are found .thresh - value that must be matched or exceeded .dir - direction to look at. If 'pos', then the algorithm look for values that are more positive than the threshold value. If it is 'neg' then the algorithm looks for values that are more negative than the threshold value. .remove_mean - if true, the mean is removed from the data before the thresholds are found. Note that the threshold value refers to the zero-mean data.
0001 function [onsets, offsets] = find_thresh_cross(data,params) 0002 % [onsets, offsets] = find_thresh_cross(data,params); 0003 % 0004 % Returns vectors containing the sample indices at which 0005 % threshold values in the data vector were crossed. Will return both onsets and 0006 % offsets. 0007 % 0008 % params is a structure that governs how the threshold crossings are found 0009 % 0010 % .thresh - value that must be matched or exceeded 0011 % .dir - direction to look at. If 'pos', then the algorithm look for values 0012 % that are more positive than the threshold value. If it is 'neg' then 0013 % the algorithm looks for values that are more negative than the 0014 % threshold value. 0015 % .remove_mean - if true, the mean is removed from the data before the 0016 % thresholds are found. Note that the threshold value refers to the 0017 % zero-mean data. 0018 0019 % 07/20/06 Petr Janata 0020 0021 % Check for input arguments 0022 error(nargchk(2,2,nargin)) 0023 0024 onsets = []; 0025 offsets = []; 0026 0027 % Make sure we have the required fields in the parameter structure 0028 if ~isfield(params,'dir'), myfprintf('Missing .dir field\n'), return, end 0029 if ~isfield(params,'thresh'), myfprintf('Missing .thresh field\n'), return, end 0030 0031 % Check to make sure that we are dealing with a vector 0032 dims = size(data); 0033 if all(dims > 1), myfprintf('Input must be a vector'), return, end 0034 0035 data = data(:); 0036 0037 % Remove the mean if desired 0038 if isfield(params,'remove_mean') && params.remove_offset 0039 data = data-mean(data); 0040 end 0041 0042 switch params.dir 0043 case {'pos','positive'} 0044 mask = data >= params.thresh; 0045 case {'neg','negative'} 0046 mask = data <= params.thresh; 0047 otherwise 0048 myfprintf(sprintf('Unknown direction: %s', params.dir)); 0049 return 0050 end 0051 0052 diff_vect = diff(mask); 0053 onsets = find(diff_vect == 1)+1; 0054 offsets = find(diff_vect == -1)+1; 0055 0056 function myfprintf(msg) 0057 fprintf('find_thresh_cross: %s\n', msg); 0058 return 0059