%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Ryan Hass                                         %
% John Romain                                       %
% Kevin Sidebottom                                  %
% ECE 480 - Final Project                           %
% ZERO CROSSINGS FUNCTION                           %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function crossings = zero_crossings(array, winlength)
%   ZERO_CROSSINGS calculate the number of zero crossings output array
%       ARRAY is the input vector
%       WINLENGTH is the length of the moving window (even number of
%       points)

index = 1;
winstart = 1;
movewin = winlength / 2;
winend = winlength;
endspch = length(array);
wn = rectwin(winlength);
xnprev = 0;
while(winend < endspch)
    xn = array(winstart:winend);
    yn = (abs(sign(xnprev) - sign(xn)) / 2) .* wn
    crossings(index) = sum(yn);
    winstart = winstart + movewin;
    winend = winstart + winlength - 1;
    index = index + 1;
    xnprev = xn;    
end