Calculate the duration that variable remains in a specific value
조회 수: 9 (최근 30일)
이전 댓글 표시
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
채택된 답변
Kevin Holly
2023년 2월 23일
편집: Kevin Holly
2023년 2월 23일
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))
추가 답변 (2개)
Image Analyst
2023년 2월 23일
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
jason mcilvenny
2023년 3월 3일
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
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Distribution Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!