function discrete_time_moving_average_demo()
clf;

M = 10; % Length of moving average
f = 0.0; % Frequency of pole
make_plot(M,f);

hui = uicontrol('style','slider','value',M,'min',1,'max',100,...
                'position',[10 10 300 20],'callback',{@nestfunc});

huj = uicontrol('style','slider','value',f,'min',0,'max',0.5,...
                'position',[400 10 300 20],'callback',{@nestfunc});

function [num,den]=make_sys(M,f)
    w = 2*pi*f;
    num = (cos(w*[0:M-1])).*ones(1,M);
    num = num/sum(abs(num));
    den = 1
end
    
function make_plot(M,f)
    NFFT = 2^10;
    [num,den]=make_sys(M,f);
    subplot(221);
    zplane(num,den);
    [H,W]=freqz(num,den,NFFT,'whole');
    f = W/pi;
    subplot(222);
    plot(f,20*log10(abs(H)));
    subplot(223);
    plot(f,angle(H),'b'); hold on;
    plot(f,phase(H),'Color',[0 0.5 0]); hold off;
    subplot(224);
    [G,W]=grpdelay(num,den,NFFT,'whole');
    plot(f,G);
end

function nestfunc(src,evd)
M = round(get(hui,'Value'));
f = get(huj,'Value');
make_plot(M,f);
end

end

