function [Btime, Bvalue] = backave(T, A, amin, amax, delta, n)
  Bn = 1;
  Bfound = 0;
   
  for i = 1 : length(A)

    if (isbackdelta(A, i, amin, amax, delta, n))
      if (Bfound == 0)
        Bfound = i;
      endif
    else
      if (Bfound > 0)
        Bave = mean(A(Bfound:i));
        Btime(Bn) = T(Bfound);
        Bvalue(Bn) = Bave;
        Btime(Bn+1) = T(i);
        Bvalue(Bn+1) = Bave;
        Bn = Bn + 2;
        Bfound = 0;
      endif
    endif
  endfor

  if (Bfound > 0)
    Bave = mean(A(Bfound:i));
    Btime(Bn) = T(Bfound);
    Bvalue(Bn) = Bave;
    Btime(Bn+1) = T(i);
    Bvalue(Bn+1) = Bave;
  endif

endfunction
