% Moving average filter (binary window - small lengths)
%j=sqrt(-1);
 
Nfft = 256;
range=(1:Nfft);
Fs=32000;           % sampling rate
N=32;           % length of a filter
d=N/2;              % delay
wscale = pi/1;  % max freq. to plot
w=wscale/Nfft*(0:Nfft-1);
 
E=exp(-j*w*d);
M=9;
h=binwin(N,M);
%h=ones(1,N);
h = h/16;
H=freqz(h,1,w);

%H(1) = 1;
%range2=2:Nfft;
%H(range2) = exp(-j*w(range2)/2*(N-1)) .* sin(w(range2)*N/2) / N ./ sin(w(range2)/2);

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;

NN=20;
w2=wscale/NN*(0:NN-1);
for k = 1:NN
  x=sin(w2(k)*(0:500));
  fx = filter(h,1,x);
  msH(k) = 20*log10(max(abs(fx(100:500))));
end;

plot(Fs/2*w(range)/pi, trmH(range), Fs/2*w(range)/pi, mG(range));
%plot(Fs/2*w(range)/pi, trmH(range), Fs/2*w(range)/pi, mG(range), ...
%     Fs/2*w2/pi, msH, 'o');
