How to find 5 consecutive values above threshold within a certain window?
1 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
回答(2 个)
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
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]
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))'
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!