QuietPeriods - Find periods of immobility. Find periods of immobility, i.e. periods of sufficient duration where instantaneous linear velocity remains low. Brief movements can be ignored. USAGE [periods,quiescence] = QuietPeriods(v,velocity,duration,brief) v linear velocity <a href="matlab:help samples">samples</a> [t v] velocity maximum velocity of a quiet period duration minimum duration of a quiet period brief optional maximum duration of a 'brief' movement OUTPUT periods list of [start stop] pairs quiescence list of [t s] pairs, where s is 1 if the animal is quiet at time t (and 0 otherwise) SEE See also BrainStates, PlotIntervals.
0001 function [periods,quiescence] = QuietPeriods(v,velocity,duration,brief) 0002 0003 %QuietPeriods - Find periods of immobility. 0004 % 0005 % Find periods of immobility, i.e. periods of sufficient duration 0006 % where instantaneous linear velocity remains low. Brief movements 0007 % can be ignored. 0008 % 0009 % USAGE 0010 % 0011 % [periods,quiescence] = QuietPeriods(v,velocity,duration,brief) 0012 % 0013 % v linear velocity <a href="matlab:help samples">samples</a> [t v] 0014 % velocity maximum velocity of a quiet period 0015 % duration minimum duration of a quiet period 0016 % brief optional maximum duration of a 'brief' movement 0017 % 0018 % OUTPUT 0019 % 0020 % periods list of [start stop] pairs 0021 % quiescence list of [t s] pairs, where s is 1 if the animal 0022 % is quiet at time t (and 0 otherwise) 0023 % 0024 % SEE 0025 % 0026 % See also BrainStates, PlotIntervals. 0027 0028 % Copyright (C) 2008-2011 by Michaƫl Zugaro 0029 % 0030 % This program is free software; you can redistribute it and/or modify 0031 % it under the terms of the GNU General Public License as published by 0032 % the Free Software Foundation; either version 3 of the License, or 0033 % (at your option) any later version. 0034 0035 if nargin < 3, 0036 error('Incorrect number of parameters (type ''help <a href="matlab:help QuietPeriods">QuietPeriods</a>'' for details).'); 0037 end 0038 if nargin == 3, 0039 brief = 0; 0040 end 0041 0042 % Determine beginning/end of quiet periods 0043 below = v(:,2) < velocity; 0044 crossings = diff(below); % yields -1 for upward crossings, and 1 for downward crossings 0045 start = find(crossings == 1); 0046 stop = find(crossings == -1); 0047 0048 % The previous code would ignore quiet periods beginning at the first sample, or ending at the last sample; correct for this 0049 if below(1), 0050 start = [1;start]; 0051 end 0052 if below(end), 0053 stop = [stop;length(below)]; 0054 end 0055 0056 % Determine durations of movements, and discard brief ones 0057 durations = v(start(2:end),1) - v(stop(1:end-1),1); 0058 ignore = find(durations <= brief); 0059 start(ignore+1) = []; 0060 stop(ignore) = []; 0061 0062 % Keep only long enough periods 0063 durations = v(stop,1)-v(start,1); 0064 discard = durations < duration; 0065 start(discard) = []; 0066 stop(discard) = []; 0067 0068 % Outputs 0069 periods = [v(start,1) v(stop,1)]; 0070 quiescence = zeros(size(v,1),2); 0071 quiescence(:,1) = v(:,1); 0072 for i = 1:length(start), 0073 quiescence(start(i):stop(i),2) = 1; 0074 end