What is meant by "centered moving average" ? Is it different from 'moving average'?
Mostra commenti meno recenti
Hi, I am new to matlab. I was solving a problem where I have to find moving average of a sample single .The output should be centered. I wrote the following code
t=linspace(-pi,pi,100);
rng default
x=sin(t) +0.25*rand(size(t));
window_s=5;
output_l=length(t) -window_s+1;
y=zeros(1,output_l);
for k=1:output_l
y(k)=(x(k) +x(k+1) + x(k+2)+x(k+3)+x(k+4))/5;
end
plot(t,x)
hold on
plot(t(1:output_l),y)
legend('Input Data', 'Filtered Data')
I dont know what is the meaning of centered moving average? Can someone tell me how can make the out put centered?
Risposta accettata
Più risposte (2)
Image Analyst
il 3 Apr 2018
It looks like it's the same as a moving average. The output pixel is the same location as the center of the window. If you didn't have an odd number of pixels in your window (but had an even number instead) then the center of the image would be a half pixel shifted, and would not be centered, but that's normally not something you would want to do. Since you have 5 elements, you're centered. You can skip the for loop and use conv(). Instead of:
for k=1:output_l
y(k)=(x(k) +x(k+1) + x(k+2)+x(k+3)+x(k+4))/5;
end
use conv():
y = conv(x, ones(1, 5)/5, 'valid');
Andrei Bobrov
il 3 Apr 2018
Modificato: Andrei Bobrov
il 3 Apr 2018
variant for your case
n = 5;
x = randi(8,1,10);
y = movmean(x,n);
y = y((n-1)/2+1:end-(n-1)/2);
Categorie
Scopri di più su Descriptive Statistics in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!