FindSpikeTrains - Split long spike trains into 'active' periods. Split spike trains into 'active' events in which consecutive spikes remain in close temporal proximity. These events are separated by periods of silence, cannot exceed a certain duration, and must recruit a minimum number of units. USAGE [info,events] = FindSpikeTrains(spikes,<options>) spikes list of (t,id) pairs, e.g. obtained using <a href="matlab:help GetSpikes">GetSpikes</a> with option 'output' set to 'numbered' <options> optional list of property-value pairs (see table below) ========================================================================= Properties Values ------------------------------------------------------------------------- 'maxDuration' maximum event duration (default = 1 s) 'gap' maximum inter-spike interval in an event (default = 0.1 s) 'minUnits' minimum number of units involved in an event (default = 3) ========================================================================= OUTPUT events list of (start,end) pairs info list of (event #,t,unit #) triplets
0001 function [events,info] = FindSpikeTrains(spikes,varargin); 0002 0003 %FindSpikeTrains - Split long spike trains into 'active' periods. 0004 % 0005 % Split spike trains into 'active' events in which consecutive spikes remain 0006 % in close temporal proximity. These events are separated by periods of 0007 % silence, cannot exceed a certain duration, and must recruit a minimum number 0008 % of units. 0009 % 0010 % USAGE 0011 % 0012 % [info,events] = FindSpikeTrains(spikes,<options>) 0013 % 0014 % spikes list of (t,id) pairs, e.g. obtained using <a href="matlab:help GetSpikes">GetSpikes</a> with 0015 % option 'output' set to 'numbered' 0016 % <options> optional list of property-value pairs (see table below) 0017 % 0018 % ========================================================================= 0019 % Properties Values 0020 % ------------------------------------------------------------------------- 0021 % 'maxDuration' maximum event duration (default = 1 s) 0022 % 'gap' maximum inter-spike interval in an event (default = 0.1 s) 0023 % 'minUnits' minimum number of units involved in an event (default = 3) 0024 % ========================================================================= 0025 % 0026 % OUTPUT 0027 % 0028 % events list of (start,end) pairs 0029 % info list of (event #,t,unit #) triplets 0030 0031 % Copyright (C) 2016 by Ralitsa Todorova, Michaƫl Zugaro 0032 % 0033 % This program is free software; you can redistribute it and/or modify 0034 % it under the terms of the GNU General Public License as published by 0035 % the Free Software Foundation; either version 3 of the License, or 0036 % (at your option) any later version. 0037 0038 % Defaults 0039 maxDuration = 1; 0040 gap = 0.1; 0041 minUnits = 3; 0042 0043 % Check parameters 0044 if nargin < 1, 0045 error('Incorrect number of parameters (type ''help <a href="matlab:help FindSpikeTrains">FindSpikeTrains</a>'' for details).'); 0046 end 0047 if ~isdmatrix(spikes,'@2'), 0048 error('Incorrect spikes (type ''help <a href="matlab:help FindSpikeTrains">FindSpikeTrains</a>'' for details).'); 0049 end 0050 0051 % Parse options 0052 for i = 1:2:length(varargin), 0053 if ~ischar(varargin{i}), 0054 error(['Parameter ' num2str(i+2) ' is not a property (type ''help <a href="matlab:help FindSpikeTrains">FindSpikeTrains</a>'' for details).']); 0055 end 0056 switch(lower(varargin{i})), 0057 case 'maxduration', 0058 maxDuration = varargin{i+1}; 0059 if ~isdscalar(maxDuration,'>0'), 0060 error('Incorrect value for property ''maxDuration'' (type ''help <a href="matlab:help FindSpikeTrains">FindSpikeTrains</a>'' for details).'); 0061 end 0062 case 'gap', 0063 gap = varargin{i+1}; 0064 if ~isdscalar(gap,'>0'), 0065 error('Incorrect value for property ''gap'' (type ''help <a href="matlab:help FindSpikeTrains">FindSpikeTrains</a>'' for details).'); 0066 end 0067 case 'minunits', 0068 minUnits = varargin{i+1}; 0069 if ~isdscalar(minUnits,'>0'), 0070 error('Incorrect value for property ''minUnits'' (type ''help <a href="matlab:help FindSpikeTrains">FindSpikeTrains</a>'' for details).'); 0071 end 0072 otherwise, 0073 error(['Unknown property ''' num2str(varargin{i}) ''' (type ''help <a href="matlab:help FindSpikeTrains">FindSpikeTrains</a>'' for details).']); 0074 end 0075 end 0076 0077 spikes = sortrows(spikes); 0078 0079 % Find gaps 0080 gaps = [1;(diff(spikes(:,1))>gap)]; 0081 starts = strfind(gaps',[1 0])'; 0082 ends = strfind(gaps',[0 1])'; 0083 0084 % Make sure the last spike train has an end 0085 if length(ends) < length(starts), 0086 ends(end+1) = length(spikes); 0087 end 0088 0089 % Count units in each event 0090 for i=1:length(starts), 0091 nUnits(i,1) = numel(unique(spikes(starts(i):ends(i),2))); 0092 end 0093 0094 % Change starts and ends from indices to timestamps 0095 starts = spikes(starts,1); 0096 ends = spikes(ends,1); 0097 0098 % Event selection criteria: n units, duration 0099 enoughUnits = nUnits >= minUnits; 0100 tooLong = ends-starts > maxDuration; 0101 0102 % Keep selected events 0103 events = [starts(enoughUnits&~tooLong) ends(enoughUnits&~tooLong)]; 0104 0105 % Determine spike information 0106 [in,which] = InIntervals(spikes,events); 0107 info = [which(in) spikes(in,:)];