%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Ryan Hass                                         %
% John Romain                                       %
% Kevin Sidebottom                                  %
% ECE 480 - Final Project                           %
% AVERAGE MAGNITUDE FUNCTION                        %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function magnitude = avg_magnitude(array, winlength)
%       MAGNITUDE is the average magnitude output array
%       ARRAY is the input vector
%       WINLENGTH is the length of the moving window 

winstart = 1;
winend = winlength;
endspeech = length(array);
wn = hamming(winlength);
index = 1;
movewin = winlength/2;

while(winend < endspeech)
    xn = array(winstart:winend);
    yn = xn.*wn;
    magnitude(index)=sum(yn);
    winstart = winstart+movewin;
    winend = winstart+winlength-1;
    index = index +1;
end