Moving window percentile computation using Conv

Hi
How to calculate 90th percentile of signal on a moving window set by conv. function. I have came across several posts on moving average calculations, such as here: https://www.mathworks.com/matlabcentral/answers/59561-how-can-i-calculate-a-centered-moving-average-with-window-size-7
But couldn't find any function to determine percentiles.

 採用された回答

Mathieu NOE
Mathieu NOE 2024 年 9 月 23 日

0 投票

hello
maybe this ? NB it does not need conv but requires Statistics and Machine Learning Toolbox
% dummy data
t= 0:0.25:100;
y = sin(2*pi*t./max(t))+rand(size(t));
%% main code (you choose the amount of samples and overlap)
buffer_size = 50; % in samples
overlap = 25; % overlap expressed in samples
[new_time,data_pc] = my_fn(t,y,buffer_size,overlap,90); %90th percentile of buffered signal
figure(1),
plot(t,y,new_time,data_pc,'*-r');
%%%%%%%%%% function %%%%%%%%%%%%%%
function [new_time,data_out] = my_fn(t,y,buffer_size,overlap,p)
% NB : buffer size and overlap are integer numbers (samples)
% data (in , out) are 1D arrays (vectors)
shift = buffer_size-overlap; % nb of samples between 2 contiguous buffers
samples = numel(y);
nb_of_loops = fix((samples-buffer_size)/shift +1);
for k=1:nb_of_loops
start_index = 1+(k-1)*shift;
stop_index = min(start_index+ buffer_size-1,samples);
x_index(k) = round((start_index+stop_index)/2);
data_out(k) = prctile(y(start_index:stop_index),p); %
end
new_time = t(x_index); % time values are computed at the center of the buffer
end

その他の回答 (1 件)

Matt J
Matt J 2024 年 9 月 23 日

0 投票

8 件のコメント

Poulomi
Poulomi 2024 年 9 月 23 日
Basically I want to calculate centered moving 90th percentile of a Nx1 signal over a 11 point time window, -5 point backward and +5 point forward. The above solution won't work for me.
Matt J
Matt J 2024 年 9 月 23 日
編集済み: Matt J 2024 年 9 月 23 日
Why wouldn't it work?
Poulomi
Poulomi 2024 年 9 月 23 日
Its not clear how should I consider -5 and +5 day overlap, In this case, would it be buffer size as 10 and overlap as 5? In that case, are we not going to ignore the 0th time point?
Matt J
Matt J 2024 年 9 月 23 日
No, it's just
ordfilt2(signal,round(11*.9), ones(11,1))
Poulomi
Poulomi 2024 年 9 月 23 日
could you pls. explain why it is 11x9 here?
Bruno Luong
Bruno Luong 2024 年 9 月 23 日
編集済み: Bruno Luong 2024 年 9 月 23 日
pick the element approximate at position (90 %) of 11 sorted( elements) = 10
round(0.9*11)
ans = 10
t= 0:0.25:100;
y = sin(2*pi*t./max(t))+rand(size(t));
y90 = ordfilt2(y(:), round(11*.9), ones(11,1));
plot(y,'b')
hold on
plot(y90,'r.')
Poulomi
Poulomi 2024 年 9 月 23 日
Actually, this is not working for my case. Series remained constant for 4 or more data points all together, which is eroneous. I wish to get a centered moving window -5 to +5 time point, with 90th percentile within that 11-hr time point.
Bruno Luong
Bruno Luong 2024 年 9 月 23 日
編集済み: Bruno Luong 2024 年 9 月 23 日
Matt's solution picks the 10th largest elements of the neighborhood of 11 elements current index + (-5:5). This is 90.91 % (=10/11), granted not exactly 90% as requested.
If you need something different you need to explain. Not many people here can see why " Series remained constant for 4 or more data points all together, which is eroneous"

サインインしてコメントする。

製品

リリース

R2024a

質問済み:

2024 年 9 月 23 日

編集済み:

2024 年 9 月 23 日

Community Treasure Hunt

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

Start Hunting!

Translated by