MRaff16
MRaff16

Reputation: 5

Extract data over a threshold

So if I have a 2000 x 4 matrix, where row 1 is a time stamp, row 2-4 is data for different variables.

What I'm looking to do is extract data from one one variable (say column 2) is over a certain threshold, however I want to extract the previous 100 samples and the next 100 samples from when the value goes back below the threshold again.

Upvotes: 0

Views: 188

Answers (1)

Aster Vanhecke
Aster Vanhecke

Reputation: 11

Detecting if a value is over a threshold:

overThresh=sampleMatrix(:,columnOfInterest)>threshold;

This creates a logical column vector with the same length as your matrix, with 'true' indicating elements that are bigger than your threshold.

Now to expand these regions by 100 samples, I will use a trick from image processing: image dilation:

SE = strel('arbitrary', ones(100,1))
extractVector=imdilate(overThresh,SE)

Now extractVector is true for the samples you want (over threshold + 100 next to them).

interestingSamples=sampleMatrix(extractVector,:)

This gives you a matrix with the values of all columns, but only the rows you are interested in.

Edit: You can assign labels (integers) to each event of threshold violation, using:

overthreshLabeled=bwlabel(overthresh);

Now each region will consist of a number instead of "true's". With each number corresponding to a specific region of adjacent events of going over the threshold. You can then loop through the region numbers and extract the values, and put them all in a cell (it has to be a cell because they can have different lengths).

ROIs{1,max(labeledThreshold)}=[]; % preallocation

for i=1:max(labeledThreshold)
    region=labeledThreshold==i;
    extractVector=imdilate(overThresh,SE) % Note: set SE before loop
    ROIs{1,i}=sampleMatrix(extractVector,:)
end

Upvotes: 1

Related Questions