Home > chronux_1_15 > spikesort > utility > datatools > trig_events.m

trig_events

PURPOSE ^

TRIG_EVENTS Extracts events based on a trigger.

SYNOPSIS ^

function events = trig_events(trig, trace, before, after);

DESCRIPTION ^

TRIG_EVENTS       Extracts events based on a trigger.
   EVENTS = TRIG_EVENTS(TRIGGER, TRACE, BEFORE_SAMPLES, AFTER_SAMPLES);
   Returns an N x (BEFORE+1+AFTER) matrix where each row contains the
   values of TRACE lined up with respect to the (N) 0->~0 transitions of
   TRIGGER; both TRIGGER and TRACE must be the same length.  BEFORE and
   AFTER specify the number of samples to extract before and after the
   time of the transition, respectively.

   NOTE: When a trigger is within BEFORE samples of the start of the data
   vector or within AFTER samples of the end, NaN values are used for the
   invalid TRACE samples.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function events = trig_events(trig, trace, before, after);
0002 %TRIG_EVENTS       Extracts events based on a trigger.
0003 %   EVENTS = TRIG_EVENTS(TRIGGER, TRACE, BEFORE_SAMPLES, AFTER_SAMPLES);
0004 %   Returns an N x (BEFORE+1+AFTER) matrix where each row contains the
0005 %   values of TRACE lined up with respect to the (N) 0->~0 transitions of
0006 %   TRIGGER; both TRIGGER and TRACE must be the same length.  BEFORE and
0007 %   AFTER specify the number of samples to extract before and after the
0008 %   time of the transition, respectively.
0009 %
0010 %   NOTE: When a trigger is within BEFORE samples of the start of the data
0011 %   vector or within AFTER samples of the end, NaN values are used for the
0012 %   invalid TRACE samples.
0013 
0014 datalen = length(trace);
0015 if (datalen ~= length(trig)), error('Trigger and data vector lengths do not match.');  end;
0016 
0017 % Get 0->~0 transitions & make index matrix
0018 marks = find(leading_edges(trig(:)));   
0019 inds = repmat(marks, 1, before+after+1) + repmat([-before:after], length(marks), 1);
0020 
0021 % Extract events
0022 trace = double(trace);
0023 trace(end+1) = NaN;                                     % store a dummy value here ...
0024 inds([inds < 1] | [inds > datalen]) = datalen + 1;      %  ... and assign overflows to dummy pos
0025 events = trace(inds);
0026 
0027 % Screwy Matlab indexing: if inds is N x M, trace(inds) will be N x M;
0028 %   Except if inds is 1 x M, in which case the dimensions of trace(inds)
0029 %   are M x 1 if trace is a column vector.  So ...
0030 if (size(events,2) == 1),  events = events';  end

Generated on Tue 15-Aug-2006 22:51:57 by m2html © 2003