Calculate the duration that variable remains in a specific value
9 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Daniel
el 23 de Feb. de 2023
Respondida: jason mcilvenny
el 3 de Mzo. de 2023
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!
0 comentarios
Respuesta aceptada
Kevin Holly
el 23 de Feb. de 2023
Editada: Kevin Holly
el 23 de Feb. de 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
answer = t2.duration(2:2:length(t2.epochtime))
Más respuestas (2)
Image Analyst
el 23 de Feb. de 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]
% Show histogram of all the durations
subplot(2, 1, 2);
histogram(S_3000);
grid on;
title('Durations of Pulses')
xlabel('Duration')
ylabel('Count')
0 comentarios
jason mcilvenny
el 3 de Mzo. de 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
0 comentarios
Ver también
Categorías
Más información sobre Data Distribution Plots en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!