//moving average filter transfer function empirical
fw=100; //filter width
mw=1; //move width
col='b'; //plot color
nts=1000; //time samples
frs=0:500; //signal frequency
is=zeros(1,length(frs));
ic=is;
in=is;
ip=is;
tin=linspace(0,2*%pi,nts);

for fi=1:length(frs)
fr=frs(fi);
dt=tin(2)-tin(1);
te1=dt*((-floor(fw/2)):1:-1);
te2=dt*(1:1:(fw-1-floor(fw/2)));
tine=[te1, tin, te2];
tout=tin(1:mw:$);

vin=cos(tin*fr);
vine=cos(tine*fr);
vh1=cumsum(vine);
vh=vh1(fw:$)-[0,vh1(1:($-fw))];
vh=vh/fw;

vout=vh(1:mw:$);
vos=sin(tout*fr);
voc=cos(tout*fr);

is(fi)=sum(vos.*vout);
ic(fi)=sum(voc.*vout);
in(fi)=norm([is(fi),ic(fi)])/(0.5*length(vos));
ip(fi)=atan(is(fi),ic(fi));
end
in(1)=in(1)/2; //zero frequency correction
//disp([is,ic,norm([is,ic])]/(0.5*length(vos)));

//plot(abs(fft(vin(1:($-fw)))),'k');
//plot(abs(fft(vout)),'r');
//plot(tin,vin);plot(tout,vout,'r')
//return
subplot(2,1,1)
plot(frs,in,col);
subplot(2,1,2)
plot(frs,ip,col);
