%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Project : Moving Average Filter (Batch-Express)
% Version : 1.00a
% Date : 2011.05.09
% Explanation : Test Program of MAF, Reference : A-Jin , Application of KalmanFilter
% Author : Yoon JaWoong
% CopyRight : MILab
% ¹èÄ¡½ÄÀ» »ç¿ëÇÑ ÀÌµ¿ Æò±Õ ÇÊÅÍ
% n = point ¼ö , x = input data
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function avg = MovAvgFilter2(n,x)
persistent xbuf
persistent firstRun
if isempty(firstRun)
xbuf = x*ones(n, 1);
firstRun = 1;
end
for m=1:n-1
xbuf(m) = xbuf(m+1);
end
xbuf(n) = x;
avg = sum(xbuf) / n;