Home > functions > device > trigger_timing > vb_trigger_onset.m

vb_trigger_onset

PURPOSE ^

Find onset time that signal exceed the threshold value 'y0'

SYNOPSIS ^

function ix = vb_trigger_onset(y,y0,t_length,t_interval)

DESCRIPTION ^

 Find onset time that signal exceed the threshold value 'y0'
    ix = vb_trigger_onset(y,y0,t_length)
 --- Input
 y  : analog signal
 y0 : threshold value
 t_length  : period length (sample number)
   if t_length < 1, t_length = t_length * (max period)
 --- Output
 ix : extracted onset time index
 --- Condition for onset
 'y' value shoud be larger than 'y0' for period length 't_length'
 If length between two events < t_interval, two events are merged

 2009-6-11 Masa-aki Sato
 2012-1-8  Masa-aki Sato 
   Check exceptional cases
 2012-2-18  Masa-aki Sato 
   Check t_interval

 Copyright (C) 2011, ATR All Rights Reserved.
 License : New BSD License(see VBMEG_LICENSE.txt)

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function    ix = vb_trigger_onset(y,y0,t_length,t_interval)
0002 % Find onset time that signal exceed the threshold value 'y0'
0003 %    ix = vb_trigger_onset(y,y0,t_length)
0004 % --- Input
0005 % y  : analog signal
0006 % y0 : threshold value
0007 % t_length  : period length (sample number)
0008 %   if t_length < 1, t_length = t_length * (max period)
0009 % --- Output
0010 % ix : extracted onset time index
0011 % --- Condition for onset
0012 % 'y' value shoud be larger than 'y0' for period length 't_length'
0013 % If length between two events < t_interval, two events are merged
0014 %
0015 % 2009-6-11 Masa-aki Sato
0016 % 2012-1-8  Masa-aki Sato
0017 %   Check exceptional cases
0018 % 2012-2-18  Masa-aki Sato
0019 %   Check t_interval
0020 %
0021 % Copyright (C) 2011, ATR All Rights Reserved.
0022 % License : New BSD License(see VBMEG_LICENSE.txt)
0023 
0024 T = length(y);
0025 
0026 if nargin < 3 || isempty(t_length), t_length = 0; end;
0027 
0028 % find event start (signal exceed the threshold)
0029 ix1 = vb_trigger_start(y,y0);
0030 
0031 if isempty(ix1), ix=[]; return; end;
0032 if t_length == 0, return; end;
0033 
0034 % find event end   (signal goes under the threshold)
0035 ix2 = vb_trigger_end(y,y0);
0036 
0037 % condition : end_time > first start_time
0038 jj  = find(ix2 > ix1(1));
0039 ix2 = ix2(jj);
0040 
0041 if isempty(ix2), ix=ix1; return; end;
0042 
0043 N1  = length(ix1);
0044 N2  = length(ix2);
0045 
0046 % Check start & end pairing
0047 % N1 = N2     : all start & end are paired
0048 % N1 = N2 + 1 : last start do not have paired end
0049 if N2 > N1, error('onset extraction error'); end;
0050 if N1 > N2+1, error('onset extraction error'); end;
0051 
0052 ix1 = ix1(:);
0053 ix2 = ix2(:);
0054 
0055 if N1 == N2+1
0056     ix2 = [ix2; T];
0057 end
0058 
0059 % event period length
0060 ixd = ix2 - ix1;
0061 
0062 if any(ixd < 0), error('onset extraction error'); end;
0063 
0064 if t_length < 1, 
0065     t_length = max(ixd)*t_length;
0066 end
0067 
0068 if nargin == 4 && ~isempty(t_interval)
0069 % --- If length between event < t_interval, events are merged
0070 % n-th event period: ix1(n) - ix2(n)
0071 % period between event: ix2(n) - ix1(n+1)
0072 
0073     flg  = zeros(T,1);
0074     for n = 1:N1
0075         flg(ix1(n):ix2(n)) = 1;
0076         if n < N1 && (ix1(n+1) - ix2(n)) < t_length
0077             flg(ix2(n):ix1(n+1)) = 1;
0078         end
0079     end
0080     
0081     %  [0 flg] : [ 0 0 1 1  1 0 0 1 1 1  1 0 0 0 1 1 1]
0082     %  diff      : [ 0 1 0 0 -1 0 1 0 0 0 -1 0 0 1 0 0]
0083     
0084     % start of event
0085     ix1 = find( diff([0; flg]) > 0 );
0086     % end of event
0087     ix2 = find( diff([0; flg]) < 0 );
0088     
0089     if length(ix1) > length(ix2),
0090         ix2 = [ix2; T];
0091     end
0092 end
0093 
0094 % reject event whose period length is less than t_length
0095 jj = find( (ix2 - ix1) >= t_length );
0096 
0097 ix = ix1(jj);

Generated on Tue 27-Aug-2013 11:46:04 by m2html © 2005