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

1 view (last 30 days)
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

Guillaume
Guillaume on 4 Feb 2016
Edited: Guillaume on 5 Feb 2016
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
Georgia
Georgia on 8 Feb 2016
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!
Georgia
Georgia on 8 Feb 2016
Oh, never mind. I got the indices. I'm all set. Thank you thank you!!!

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!