How do I collect all data strings of an array that meet 2 criteria
1 view (last 30 days)
Show older comments
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)
0 Comments
Accepted Answer
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
More Answers (0)
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!