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!