Calculate the duration that variable remains in a specific value

9 visualizzazioni (ultimi 30 giorni)
Hello,
i need to calucale, how long are the intervals a variable with a specific value stays until it change again. In the picture below you can see the plot of the variable i am working with (speed vs time in min).
i would like to have a result like this: The Speed 3000 have followed Interval duration in the Dataset
S_3000 = [6 5 4 7 8 9 4 5] % in Minutes or Datapoints
Where the entries of S_3000 are the number of Intervals found in the Dataset. I have tried already some Ideas, but i don't find any solution. I will also attacht the Data i am working with. Thanks in Advance!

Risposta accettata

Kevin Holly
Kevin Holly il 23 Feb 2023
Modificato: Kevin Holly il 23 Feb 2023
load('tab.mat')
logical_array = tab.s==3000; %chose value for speed here
bar(tab.t,logical_array)
t2 = table;
count = 0;
timepoint = "";
epochtime = [];
Here, I detected when the speed stayed at the value of 3000 for at least 3 datapoints. In addition to calculating the duration I also created timestamps for the start and end of each detected duration.
for ii = 3:length(logical_array)-1
if logical_array(ii) == 1 && logical_array(ii+1) == 1 && logical_array(ii+2) == 1 && timepoint(end) ~= "start time"
count = count + 1;
if timepoint == ""
timepoint = "start time";
epochtime = [epochtime; tab.t(ii)];
else
timepoint = [timepoint; "start time"];
epochtime = [epochtime; tab.t(ii)];
end
end
if logical_array(ii) == 0 && logical_array(ii-1) == 1 && logical_array(ii-2) == 1 && timepoint(end)=="start time" %detects 110, i.e. End Point
count = 0;
timepoint = [timepoint; "end time"];
epochtime = [epochtime; tab.t(ii)];
end
end
t2.epochtime = epochtime;
t2.timepoint = timepoint;
t2.duration(2:2:length(t2.epochtime)) = t2.epochtime(2:2:end)-t2.epochtime(1:2:end-1);
t2.duration(1:2:length(t2.timepoint)) = NaN;
t2
t2 = 58×3 table
epochtime timepoint duration _________ ____________ ________ 13 "start time" NaN 23 "end time" 10 25 "start time" NaN 28 "end time" 3 37 "start time" NaN 47 "end time" 10 58 "start time" NaN 65 "end time" 7 72 "start time" NaN 75 "end time" 3 93 "start time" NaN 96 "end time" 3 101 "start time" NaN 109 "end time" 8 111 "start time" NaN 114 "end time" 3
answer = t2.duration(2:2:length(t2.epochtime))
answer = 29×1
10 3 10 7 3 3 8 3 7 3

Più risposte (2)

Image Analyst
Image Analyst il 23 Feb 2023
If you have the Image Processing Toolbox, you can get the duration of all the pulses in one line of code simple by calling regionprops:
% Measure duration of each region
props = regionprops(mask, 'Area')
Here is the full demo
s = load('tab.mat');
t = s.tab;
x = t.t;
y = t.s;
subplot(2, 1, 1);
plot(x, y, 'b-', 'LineWidth', 2);
grid on;
hold on;
title('Original Data')
% Plot a line at the threshold.
threshold = 3000;
yline(threshold, 'LineWidth', 2, 'Color', 'r')
title('Original Data')
xlabel('t')
ylabel('S')
%-----------------------------------------------------------------------------------
% Get a logical vector for where y exceeds the threshold.
mask = y > threshold;
% Measure duration of each region
props = regionprops(mask, 'Area');
% Get the durations of all regions into a vector (extracting from the structure)
S_3000 = [props.Area]
S_3000 = 1×70
3 2 2 5 1 3 6 1 1 1 1 1 1 5 1 2 1 2 1 2 3 4 4 4 1 1 1 2 2 1
% Show histogram of all the durations
subplot(2, 1, 2);
histogram(S_3000);
grid on;
title('Durations of Pulses')
xlabel('Duration')
ylabel('Count')

jason mcilvenny
jason mcilvenny il 3 Mar 2023
Another solution:
idx = t.s > 3000;
transitions = diff(idx);
starts = find(transitions == 1);
ends = find(transitions == -1);
durations = ends - starts;
durdates=t.t(starts);
s3000=timetable(durdates, durations);
bar(s3000.durdates, s3000.durations);
datetick

Categorie

Scopri di più su Data Distribution Plots in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by