Home > utilities > get_runs.m

get_runs

PURPOSE ^

GET_RUNS Finds runs of continuous change in some value beyond some length

SYNOPSIS ^

function totalVolume = get_runs(plotOPTS,data,cellVolume,lengthThreshold,changeThreshold)

DESCRIPTION ^

 GET_RUNS Finds runs of continuous change in some value beyond some length
 and magnitude thresholds.
 
 We're just doing lots of time series analyses here, so provide only a
 time series as DATA.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function totalVolume = get_runs(plotOPTS,data,cellVolume,lengthThreshold,changeThreshold)
0002 % GET_RUNS Finds runs of continuous change in some value beyond some length
0003 % and magnitude thresholds.
0004 %
0005 % We're just doing lots of time series analyses here, so provide only a
0006 % time series as DATA.
0007 
0008 % Make sure we have a totalVolume value to output even if this time step
0009 % doesn't match the threshold conditions specified.
0010 totalVolume=0;
0011 
0012 % Make an array of the time indices.
0013 timeIdx=1:length(data);
0014 % Calculate the differences ...
0015 dataChange=diff(data);
0016 % and find where they're negative.
0017 negIdx=dataChange<0;
0018 
0019 % Use the diff of the negative indices to find the start and end of each
0020 % run.
0021 dn=diff(negIdx);
0022 dnIdxStart=timeIdx(dn>0)+1; % add one to move start along to correct index
0023 dnIdxEnd=timeIdx(dn<0);
0024 
0025 % Check if the start and end are identical (i.e. we have a spike).
0026 if (numel(dnIdxStart)==1 || numel(dnIdxEnd)==1)
0027     if dnIdxStart==dnIdxEnd
0028 %         warning('Single spike in time series, so carry on.')
0029         longOnesIdx(:,1:2)=nan(1,2);
0030         return
0031     end
0032 end
0033 % Check for no values
0034 if sum(dnIdxStart)==0 || sum(dnIdxEnd)==0
0035 %     warning('No appropriate values here, so carry on.')
0036     longOnesIdx(:,1:2)=nan(1,2);
0037     return
0038 end
0039 % Check the first index in dnIdxEnd is larger than the dnIdxStart.
0040 if dnIdxEnd(1)<dnIdxStart(1)
0041     % Strip it out and adjust the dnIdxStart index accordingly.
0042     dnIdxEnd=dnIdxEnd(2:end);
0043     dnIdxStart=dnIdxStart(1:end-1);
0044 end
0045 
0046 % If arrays are different lengths, lop off the last one from the longer
0047 % array.
0048 if length(dnIdxEnd)>length(dnIdxStart)
0049     timeIndices=(dnIdxEnd(1:end-1)-dnIdxStart)>=lengthThreshold;
0050 elseif length(dnIdxEnd)<length(dnIdxStart)
0051     timeIndices=(dnIdxEnd-dnIdxStart(1:end-1))>=lengthThreshold;
0052 elseif length(dnIdxEnd)==length(dnIdxStart)
0053     if dnIdxStart(1)==1
0054         timeIndices=(dnIdxEnd(1:end-1)-dnIdxStart(2:end))>=lengthThreshold;
0055     elseif dnIdxEnd(1)==1
0056         timeIndices=(dnIdxEnd(2:end)-dnIdxStart(1:end-1))>=lengthThreshold;
0057     else
0058         timeIndices=(dnIdxEnd-dnIdxStart)>=lengthThreshold;
0059     end
0060 else
0061     return
0062 end
0063 
0064 if sum(timeIndices~=0)
0065     longOnesIdx(:,1)=dnIdxStart(timeIndices);
0066     longOnesIdx(:,2)=dnIdxEnd(timeIndices);
0067 else
0068     return
0069 end    
0070 
0071 totalVolume=0;
0072 for jj=1:size(longOnesIdx,1)
0073     % Average change
0074 %     changeMetric=mean(data(longOnesIdx(jj,1):longOnesIdx(jj,2)));
0075     % Maximum change (i.e. closest to zero in our case)
0076     changeMetric=max(data(longOnesIdx(jj,1):longOnesIdx(jj,2)));
0077     if changeMetric<changeThreshold
0078         totalVolume=totalVolume+sum(cellVolume(longOnesIdx(jj,1):longOnesIdx(jj,2)));
0079     end
0080 end
0081 
0082 end
0083

Generated on Tue 18-Dec-2012 12:37:31 by m2html © 2005