IIR filter as amoving averege

1 visualización (últimos 30 días)
Rica
Rica el 22 de Mzo. de 2013
hi! I want to use the filter function as a moving averege filter. the Type of the filter should be IIR Filter. Te window length is 3. for exemple:
%
X=rand(1:300)
B=1/3
A=1-B;
Y=filter(B,A,X)->First order IIR filter
the filter functi should start after reaching the window lenght. for the first 2 value (before reaching the window lenght of 3) , i wan to calculate just the mean value.
%
Y(1)=X(1)
Y(2)=mean(Y(1)+X(2));
Y(3:end)=filter(B,A,X(3:end))
could someone help me ?
thank you

Respuesta aceptada

Andrei Bobrov
Andrei Bobrov el 22 de Mzo. de 2013
Editada: Andrei Bobrov el 22 de Mzo. de 2013
EDIT
out = filter([1 1 1]/3, 1, x(:));
out(1:2) = [x(1);sum(x(1:2))/2];

Más respuestas (1)

Image Analyst
Image Analyst el 22 de Mzo. de 2013
Editada: Image Analyst el 22 de Mzo. de 2013
To get the middle chunk, do this:
yMiddle = conv(x, [1 1 1]/3, 'valid');
Then prepend and append the endpoints as you already calculated.
Edit: Haven't heard from you, so here's a full-blown demo for you. It shrinks the window as it gets closer to the edges. Let me know if you need a version robust enough to handle any window size.
x = 1:10
% Get the first and last elements.
yFirst = mean(x(1:2))
yLast = mean(x(end-1:end))
% Now get the middle with a moving average.
yMiddle = conv(x, [1 1 1]/3, 'valid')
yFinal = [yFirst yMiddle, yLast]
In the command window you see:
x =
1 2 3 4 5 6 7 8 9 10
yFirst =
1.5000
yLast =
9.5000
yMiddle =
2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000
yFinal =
1.5000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 9.5000

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by