function mag = avgmag(sig,N,step)
% avgmag(sig,windowsize,stepsize)
%
% avgmag - short-time magnitude of a signal (sig) with rectangular window
% length N
%
% ELEC 301 Project - "Mars Lander the Theologian", aka, "Speaker Verification"
% Sara MacAlpine, Aamir Virani, Nipul Bharani, JP Slavinsky
% WRC (OCEE) Class of 2001 - Fall '99

% Determine bounds of signal
sig_start = 1;
sig_stop = length(sig);

% prepare zero-bordered signal so can start windowing from sig(1) back to
% sig(N-2)
size_sig = size(sig);
if size_sig(1) > size_sig(2)
  new_size_sig(1) = size_sig(1) + 2*N -2;
  new_size_sig(2) = size_sig(2);
else
  new_size_sig(1) = size_sig(1);
  new_size_sig(2) = size_sig(2) + 2*N -2;
end

sig_padded = zeros(new_size_sig);
mag_padded = sig_padded;
sig_padded(N:N+length(sig)-1) = sig;

% Set relative bounds on rectangular window - win_start will be the number
% of values below the current index and win_stop will be the number of
% values included above the current index.  Added together, they should
% equal N-1

win_start = floor(N/2);
win_stop = N - win_start - 1;

% Calculate Magnitude
i = 0;
for n = N:step:N+length(sig)-1,
  sig_windowed = sig_padded(n-win_start:n+win_stop);
  i = i + 1;
  mag_stepped(i) = sum(abs(sig_windowed));
end  

mag = mag_stepped;
