% Moving average filter (binary window - large lengths)
%j=sqrt(-1);
Nfft = 256;
range=(1:Nfft);
Fs=32000;           % sampling rate
N=2^18;           % length of a filter
d=N/2;              % delay
wscale = pi/8000;  % max freq. to plot
w=wscale/Nfft*(0:Nfft-1);
 
E=exp(-j*w*d);
M=7;
h=binwin(N,M);
hscale = sum(h);
clear h
%H=freqz(h,1,w);

aons = ones(size(w));
z1=exp(-j*w);
a = 1/2; L = N-2*M;
H1= a*(z1.^M - a^M*aons)./(z1 - a*aons);
H2= z1.^M .* (aons - z1.^L) ./ (aons - z1);
H3 = a*(aons - a^M*z1.^M) ./ (aons - a*z1) .* z1.^(M+L);
H = (H1+H2+H3)/hscale;

mH=20*log10(abs(H));
 
G=E-H;
mG=20*log10(abs(G));
trmH = mH;
idx = find(mH<-70);
sidx = size(idx);
if max(sidx) > 0, trmH(idx) = -70*ones(sidx(1),sidx(2)); end;
plot(Fs/2*w(range)/pi, trmH(range), Fs/2*w(range)/pi, mG(range));
