How to implement a sliding window with a condition

The code is supposed to show 1 when residual deviates beyond the threshold for x consecutive sample times and 0 when deviates once after y sampling time. The residual is a column matrix 2000 x1 . I tried using a sliding window approach to achieve this.

2 Comments

Jan
Jan on 12 Apr 2022
Edited: Jan on 12 Apr 2022
Please post, what you have tried so far and explain, what is not working as wanted. A small example would be useful also to understand, what you want. E.g. what does "show 1" mean? What is "sampling time"?
i tried using a sliding window to count the faults ( 1) in a specific time period . for example 0 to 400 , then 401 to 800. Then plot a rectangular pulse .
left = 0;
right = 0;
k = 400; %% sample = 5s / 0.0125 interval
windcount = 0;
while ( right<k )
windcount = windcount + fault(right+1);
right = right + 1;
end
while right < size(Threshold)
windcount = windcount + fault(right+1) - fault(left+1);
for i=size(Threshold)
if (fault(i) == 1)
windcount = windcount +1 ;
disp(windcount)
end
end
left = right
right = right +400
end
disp('while')
if windcount > 250
fault2(i) = 1;
else
fault2(i) = 0;
end
disp('Plotting')
plot(Time, fault2)

Sign in to comment.

Answers (1)

You can probably just use movmin().
R = rand(100,1);
th = 0.5;
w = 2;
exceeds_th = movmin(R,w)>th;
[R exceeds_th]
ans = 100×2
0.3394 0 0.7952 0 0.6381 1.0000 0.4328 0 0.2726 0 0.7042 0 0.7297 1.0000 0.3039 0 0.1256 0 0.1311 0
You can play around with the options for movmin() to adjust how it behaves.

1 Comment

Thank You, i tried this but it didnt give me the results i wanted. 'if a fault occurs or goes beyond a certain threshold for a consecutive number of times , show 1 ' this is the result i wanted

Sign in to comment.

Categories

Asked:

on 12 Apr 2022

Commented:

on 19 Apr 2022

Community Treasure Hunt

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

Start Hunting!