Identify the duration of zero values and count the number of such zero regions

조회 수: 7 (최근 30일)
Hello,
I have a signal shown above.
  1. First I want to identify the duration(length) of each zero valued regions represented as T, T1, T2, T3 etc., (You may generate any signal with zeroed region similar to my graph)
  2. Count how many such zero valued regions are there in 1 sec(or within a specified time).
Any help interms of code is well appreciated. Thank you in advance.

채택된 답변

Davide Masiello
Davide Masiello 2022년 12월 1일
편집: Davide Masiello 2022년 12월 1일
time = 0:100;
signal = rand(size(time));
signal([3:6,15:21,43:55,61:64,87:92]) = 0;
plot(time,signal)
r = regionprops(signal==0,"SubarrayIdx");
for k = 1:length(r)
time_indexes = r(k).SubarrayIdx{2};
durations(k) = time(time_indexes(end))-time(time_indexes(1));
end
durations
durations = 1×5
3 6 12 3 5
Obviously, the number of zero regions is the length of the array durations.
  댓글 수: 3
Davide Masiello
Davide Masiello 2022년 12월 1일
편집: Davide Masiello 2022년 12월 1일
That's because it seems you don't have any set of consecutive zeros, but rather many isolated ones
T = readmatrix('data.csv');
time = T(:,1);
signal = T(:,2);
plot(time,signal)
find(signal==0) % indexes of signal array where signal is zero
ans = 4795×1
4372 4402 4411 4417 4418 4420 4422 4429 4430 4432
What you could do is define a threshold to define small values as equal to zero. Be careful, depending on the threshold value you might significantly change your signal.
thr = 0.5;
signal(signal > -thr & signal < thr) = 0;
plot(time,signal)
r = regionprops(signal==0,"SubarrayIdx");
for k = 1:length(r)
time_indexes = r(k).SubarrayIdx{1};
durations(k) = time(time_indexes(end))-time(time_indexes(1));
end
Because of the noise of your signal, it will still find single zero values or very small zero intervals, which I assume you don't want to see.
Set a new threshold defining that you want only intervals that are larger than a certain time span.
durations(durations<0.0005) = []
durations = 1×5
0.0009 0.0011 0.0032 0.0012 0.0024
In this way, it finds only the intervals that you can see with the naked eye in the plot (in fact, it's five of them).
By fiddling around with the various thresholds, you can refine your code further.
Image Analyst
Image Analyst 2022년 12월 1일
Or, more simply
zeroRegions = (signal == 0); % Identify elements that are exactly 0.
% Throw out runs less than, say, 3 elements long.
zeroRegions = bwareaopen(zeroRegions, 3);
% Measure what's left (regions 3 or more elements long).
r = regionprops(zeroRegions,"Area");
durations = [r.Area] % Extract all run lengths from structure into vector.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Measurements and Spatial Audio에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by