Imagine you have a very long sequence. What is the most efficient way of finding the intervals where the sequence is all zeros (or more precisely the sequence drops to near-zero values abs(X)<eps
):
For simplicity, lets assume the following sequence:
sig = [1 1 0 0 0 0 1 1 1 1 1 0 1 0 0 0 1 1 1 1 1 1 1 1 0 0 1 1 1 0];
I'm trying to get the following information:
startIndex EndIndex Duration
3 6 4
12 12 1
14 16 3
25 26 2
30 30 1
then using this information, we find the intervals with duration >= to some specified value (say 3
), and returning the indices of the values in all these intervals combined:
indices = [3 4 5 6 14 15 16];
That last part is related to a previous question:
MATLAB: vectorized array creation from a list of start/end indices
This is what I have so far:
sig = [1 1 0 0 0 0 1 1 1 1 1 0 1 0 0 0 1 1 1 1 1 1 1 1 0 0 1 1 1 0];
len = length(sig);
thresh = 3;
%# align the signal with itself successively shifted by one
%# v will thus contain 1 in the starting locations of the zero interval
v = true(1,len-thresh+1);
for i=1:thresh
v = v & ( sig(i:len-thresh+i) == 0 );
end
%# extend the 1's till the end of the intervals
for i=1:thresh-1
v(find(v)+1) = true;
end
%# get the final indices
v = find(v);
I'm looking to vectorize/optimize the code, but I'm open to other solutions. I have to stress that space and time efficiencies are very important, since I'm processing a large number of long bio-signals.
These are the steps I would take to solve your problem in a vectorized way, starting with a given vector
sig
:First, threshold the vector to get a vector
tsig
of zeros and ones (zeroes where the absolute value of the signal drops close enough to zero, ones elsewhere):Next, find the starting indices, ending indices, and duration of each string of zeroes using the functions DIFF and FIND:
Then, find the strings of zeroes with a duration greater than or equal to some value (such as 3, from your example):
Finally, use the method from my answer to the linked question to generate your final set of indices:
the above answer by genovice can be modified to find the indices of non-zero elements in a vector as:
As gnovice showed, we'll do a threshold test to make "near zero" really zero:
Then find regions where the cumulative sum isn't increasing:
Remembering gnovice's great method for filling in ranges of indexes
We note that our
islands
vector already has ones in thestartInd
locations, and for our purposesendInd
always comesthresh
spots later (longer runs have runs of ones inislands
)Test
I think the most MATLAB/"vectorized" way of doing it is by computing a convolution of your signal with a filter like [-1 1]. You should look at the documentation of the function conv. Then on the output of conv use find to get the relevant indexes.
You can solve this as a string search task, by finding strings of zeros of length
thresh
(STRFIND function is very fast)Note that longer substrings will get marked in multiple locations but will eventually be joined once we add in-between locations from intervals start at
startIndex
to end atstart+thresh-1
.Note that you can always swap this last step with the CUMSUM/FIND solution by @gnovice from the linked question.