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

4 ビュー (過去 30 日間)
Lu Da Silva
Lu Da Silva 2022 年 3 月 24 日
編集済み: Bruno Luong 2022 年 3 月 29 日
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.

回答 (2 件)

Mathieu NOE
Mathieu NOE 2022 年 3 月 24 日
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 2022 年 3 月 29 日
編集済み: Bruno Luong 2022 年 3 月 29 日
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!

Translated by