IIR filter as amoving averege

조회 수: 1 (최근 30일)
Rica
Rica 2013년 3월 22일
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

채택된 답변

Andrei Bobrov
Andrei Bobrov 2013년 3월 22일
편집: Andrei Bobrov 2013년 3월 22일
EDIT
out = filter([1 1 1]/3, 1, x(:));
out(1:2) = [x(1);sum(x(1:2))/2];

추가 답변 (1개)

Image Analyst
Image Analyst 2013년 3월 22일
편집: Image Analyst 2013년 3월 22일
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

카테고리

Help CenterFile Exchange에서 Filter Design에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by