Restrict - Keep only samples that fall in a given list of time intervals. Keep only samples (positions, spikes, LFP, etc.) that fall in a given list of time intervals. The remaining epochs can optionally be 'shifted' next to each other in time, removing the time gaps between them (which result from discarded samples), in which case they are also shifted globally to start at t = 0. USAGE samples = Restrict(samples,intervals,<options>) samples <a href="matlab:help samples">samples</a> to restrict intervals list of (start,stop) pairs <options> optional list of property-value pairs (see table below) ========================================================================= Properties Values ------------------------------------------------------------------------- 'shift' shift remaining epochs together in time (default = 'off') ========================================================================= NOTE For more advanced time restriction of samples, use <a href="matlab:help InIntervals">InIntervals</a>. SEE See also ConsolidateIntervals, SubtractIntervals, ExcludeIntervals, InIntervals, Restrict, FindInInterval, CountInIntervals, PlotIntervals.
0001 function samples = Restrict(samples,intervals,varargin) 0002 0003 %Restrict - Keep only samples that fall in a given list of time intervals. 0004 % 0005 % Keep only samples (positions, spikes, LFP, etc.) that fall in a given list of 0006 % time intervals. 0007 % 0008 % The remaining epochs can optionally be 'shifted' next to each other in time, 0009 % removing the time gaps between them (which result from discarded samples), 0010 % in which case they are also shifted globally to start at t = 0. 0011 % 0012 % USAGE 0013 % 0014 % samples = Restrict(samples,intervals,<options>) 0015 % 0016 % samples <a href="matlab:help samples">samples</a> to restrict 0017 % intervals list of (start,stop) pairs 0018 % <options> optional list of property-value pairs (see table below) 0019 % 0020 % ========================================================================= 0021 % Properties Values 0022 % ------------------------------------------------------------------------- 0023 % 'shift' shift remaining epochs together in time (default = 'off') 0024 % ========================================================================= 0025 % 0026 % NOTE 0027 % 0028 % For more advanced time restriction of samples, use <a href="matlab:help InIntervals">InIntervals</a>. 0029 % 0030 % SEE 0031 % 0032 % See also ConsolidateIntervals, SubtractIntervals, ExcludeIntervals, 0033 % InIntervals, Restrict, FindInInterval, CountInIntervals, PlotIntervals. 0034 0035 % Copyright (C) 2004-2011 by Michaƫl Zugaro 0036 % 0037 % This program is free software; you can redistribute it and/or modify 0038 % it under the terms of the GNU General Public License as published by 0039 % the Free Software Foundation; either version 3 of the License, or 0040 % (at your option) any later version. 0041 0042 % Default values 0043 verbose = false; 0044 shift = 'off'; 0045 0046 % Check number of parameters 0047 if nargin < 2 | mod(length(varargin),2) ~= 0, 0048 error('Incorrect number of parameters (type ''help <a href="matlab:help Restrict">Restrict</a>'' for details).'); 0049 end 0050 0051 % Check parameters 0052 if ~isdmatrix(intervals) || size(intervals,2) ~= 2, 0053 error('Incorrect intervals (type ''help <a href="matlab:help Restrict">Restrict</a>'' for details).'); 0054 end 0055 0056 if size(samples,1) == 1, 0057 samples = samples(:); 0058 end 0059 0060 % Parse parameter list 0061 for i = 1:2:length(varargin), 0062 if ~ischar(varargin{i}), 0063 error(['Parameter ' num2str(i+2) ' is not a property (type ''help <a href="matlab:help Restrict">Restrict</a>'' for details).']); 0064 end 0065 switch(lower(varargin{i})), 0066 case 'shift', 0067 shift = varargin{i+1}; 0068 if ~isastring(shift,'on','off'), 0069 error('Incorrect value for property ''shift'' (type ''help <a href="matlab:help Restrict">Restrict</a>'' for details).'); 0070 end 0071 0072 otherwise, 0073 error(['Unknown property ''' num2str(varargin{i}) ''' (type ''help <a href="matlab:help Restrict">Restrict</a>'' for details).']); 0074 end 0075 end 0076 0077 % Restrict 0078 [status,interval,index] = InIntervals(samples,intervals); 0079 samples = samples(status,:); 0080 0081 % Shift? 0082 if strcmp(shift,'on'), 0083 % Discard interval IDs for samples which belong to none of the intervals 0084 interval = interval(status); 0085 % Samples in each interval will be shifted next to end of the previous interval 0086 % Let us call dt1 the time difference between interval 1 and interval 2. Interval 2 must be 0087 % shifted by dt1, interval 3 by dt1+dt2 (since interval 2 itself will be shifted by dt1), 0088 % interval 4 by dt1+dt2+dt3, etc. 0089 % 1) Compute the cumulative shifts dt1, dt1+dt2, dt1+dt2+dt3,... 0090 start = intervals(2:end,1); % beginning of next interval 0091 stop = intervals(1:end-1,2); % end of previous interval 0092 cumulativeShift = [0;cumsum(start-stop)]; 0093 % 2) Assign these cumulative shifts to each sample 0094 shifts = cumulativeShift(interval); 0095 % 3) Shift 0096 samples(:,1) = samples(:,1) - shifts - intervals(1,1); 0097 end