How to find 5 consecutive values above threshold within a certain window?

3 views (last 30 days)
I have a vecor A:
A = [6 7 8 8 8 7 6 7 6 2 7 8 9 3 3 4 6 7 8 9]
The threshold is 5. I want to keep all values that are above the threshold. However, if within a window of 5 values, one out of the 5 is below the threshold, that's ok and I want to keep that one too. If in a window 2 or more values are below the threshold, then I do not want those values.
Thus, the new vector B would be:
B = [6 7 8 8 8 7 6 7 6 2 4 6 7 8 9]
NB: the window moves in steps of 5, so it takes 5 elements, analyeses them, then moves to the next 5.

Answers (2)

Mathieu NOE
Mathieu NOE on 24 Mar 2022
hello
here you are my friend :
A = [6 7 8 8 8 7 6 7 6 2 7 8 9 3 3 4 6 7 8 9];
buffer = 5; % nb of samples in one buffer (buffer size)
threshold = 5;
%%%% main loop %%%%
m = length(A);
B = [];
for ci=1:fix((m-buffer)/buffer +1)
start_index = 1+(ci-1)*buffer;
stop_index = min(start_index+ buffer-1,m);
data_buffer = A(start_index:stop_index) %
ind = find(data_buffer<threshold);
if numel(ind)<2
B = [B data_buffer];
end
end
B

Bruno Luong
Bruno Luong on 29 Mar 2022
Edited: Bruno Luong on 29 Mar 2022
A = [6 7 8 8 8 7 6 7 6 2 7 8 9 3 3 4 6 7 8 9]
A = 1×20
6 7 8 8 8 7 6 7 6 2 7 8 9 3 3 4 6 7 8 9
win = 5;
threshold=5;
maxreject=3;
% Pad NaNs so that the length is multiple of win
Ap=A;
Ap(end+1:ceil(end/win)*win)=NaN;
% Rejection
Ap=reshape(Ap,win,[]);
Ap(:,sum(Ap>threshold,1)<=maxreject)=[];
% Reshape and remove trailing NaNs
Ak=Ap(isfinite(Ap))'
Ak = 1×15
6 7 8 8 8 7 6 7 6 2 4 6 7 8 9

Community Treasure Hunt

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

Start Hunting!