How do I collect all data strings of an array that meet 2 criteria

I am trying to collect data from an array that meet two criteria. More specifically, I want to start collecting the values of an array that cross a threshold and continue to collect the values in the array until they fall below another threshold. I can do the first part, but I can't figure out how to continue to collect the values until they fall below the second threshold (ThresholdOFF in the below attempt). Can you help me collect continuous values of the array (x) that cross above Threshold and continue to collect until the next value that falls below ThresholdOFF? Then I want the indices of the values that meet these criteria. Here is what I have so far:
Fs = 8000; % samples per second
dt = 1/Fs; % seconds per sample
StopTime = 0.25; % seconds
t = (0:dt:StopTime-dt)'; % seconds
%%Sine wave:
Fc = 60; % hertz
x = cos(2*pi*Fc*t);
% Plot the signal versus time:
figure;
plot(t,x);
xlabel('time (in seconds)');
title('Signal versus Time');
zoom xon;
StDevPower=std(x);
MeanPower=mean(x);
Threshold=((StDevPower*1)+MeanPower);
ThresholdOFF=((StDevPower*0.5)+MeanPower);
AboveSD=[];
for j=1:length(x)
if x(j)>Threshold
AboveSD=[AboveSD; x(j)];
end
end
figure, plot(AboveSD)

 Accepted Answer

A much more efficient way that does not involve growing the result array through a loop (always going to be slow):
%inputs:
%x: the array, starts below Threshold
%Threshold: start threshold
%ThresholdOff: end threshold
startidx = find(x > Threshold, 1);
endidx = startidx + find(x(startidx+1:end) < ThresholdOff, 1);
AboveSD = x(startidx:endidx-1);

9 Comments

Thank you for your input too! By "start" do you mean "startidx" on the second line?
Yes, sorry about the typo. Fixed, now.
This works well for finding the first occurrence of the signal going above threshold and then returning below thresholdOFF. The signal will pass above threshold again many times and I want to collect all of those occurrences. Can you help me make that happen? Thank you!!
You could just wrap the above in a loop, restarting the search from endidx.
Otherwise, this may work (code untested):
starts = find(x(1:end-1) <= Threshold & x(2:end) > Threshold); %crossing Threshold going up
endings = find(x(1:end-1) >= ThresholdOff & x(2:end) < ThresholdOff); %crossing ThresholdOff going down
%If Threshold == ThresholdOff, there should be the same number
%of starts and ends, or one more of either
%If the Theshold differ then there may be a lot more of either
%Therefore find the first ThresholdOff after a Threshold:
trans = bsxfun(@gt, endings(:), starts(:)');
%columns in trans correspond to starts, rows to endings. The first 1 in each column correspond to the first ending after the corresponding start.
idx = cellfun(@(v) find(v, 1), num2cell(trans, 2), 'UniformOutput', false); %for each column find first ending after start. Could be empty if not ending after a start
idx = [idx{:}]; %gets rid of the empties. Just keep the endings indices
endings = endings(idx);
%remove starts without endings (e.g. signal ends going up)
starts(all(~trans)) =[]; %start without endings have all 0 in their column
%There should now be as many starts and endings
assert(numel(starts) == numel(endings), 'I've made a mistake');
%Get segments:
AboveSD = arrayfun(@(s, e) x(s+1:e), starts, endings, 'UniformOutput', false);
So sorry to keep bugging you. I'm getting the "I've made a mistake error" with the untested code you suggested, and I like the idea of the loop, but I don't know what to use as the counter or how to get the loop to restart the search from endidx for each iteration. Can you help a bit more? Thank you!
Here is a way to wrap my original code in a loop:
AboveSD = {};
endidx = 0; %haven't found anything yet
while true %loop until we break out
startidx = endidx + find(x(endidx+1 : end) > Threshold, 1);
if isempty(startidx)
break; didn't find a start, end the loop
end
endidx = startidx + find(x(startidx + 1:end) < ThresholdOff, 1);
if isempty(endidx)
break; didn't find an end, end the loop
end
AboveSD = [AboveSD; {x(startidx:endidx-1)}]; %#ok<AGROW> We don't know beforehand how many segment there are.
end
Again, untested. For me to find the error in the assumptions for the other code, you'd have to post the data you used to test it.
Attached is an example of the data I would use. From there, I can see what I am looking for from:
StDevPower=std(GPsmooth);
MeanPower=mean(GPsmooth)
Threshold=((StDevPower*ThresholdMultiple)+MeanPower);
ThresholdOFF=((StDevPower*1)+MeanPower);
t=[0:0.2:((y)/5)];
figure, plot((t),GPsmooth,'k')
hline=refline([0 Threshold])
hline.Color = 'r';
hline=refline([0 ThresholdOFF])
hline.Color = 'g';
xlabel 'Time(sec)'
ylabel 'Power'
yeah, that works! The last thing I want to grab are the indices in the original data set (GPsmooth) for the data that were collected in AboveSD. Thank you!
Oh, never mind. I got the indices. I'm all set. Thank you thank you!!!

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Asked:

on 4 Feb 2016

Edited:

on 9 Feb 2016

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!