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

function energy = avg_energy(spch, winlength)
%       ENERGY is the average energy output array
%       ARRAY is the input vector
%       WINLENGTH is the length of the moving window 

winlen = 220;
movewin = winlen/2;
winstart = 1;
winend = winlen;
endspeech = length(spch);
wn = hamming(winlen);
index = 1;

while(winend < endspeech)
    xn = spch(winstart:winend);
   yn = xn.*wn;
   energy(index)=sum(yn.^2);
   winstart = winstart+movewin;
   winend = winstart+winlen-1;
   index = index +1;
end