%% find the mean slope rate
% @author Andi Dittrich
% @date 09.04.2012
function slopeTime = MeanSlopeRate(signal)
    % absolute derivative
    di = abs(diff(signal.signals.values));
    
    % mean offset value
    m = mean(di)*10;
    
    % output count;
    spikeCount = 0;
    
    % tmp count
    tmpCount = 0;
    buffer = 0;
    
    % vector of all detected spikes
    spikes = 0;
    
    for i=1:size(di)
       % value found ?
       if (di(i) > m)
          tmpCount = tmpCount+1;
          
          % add value to mean buffer
          buffer = buffer + di(i);
       else
           % ignore non spikes -> reset
           if (tmpCount > 5)
               tmpCount = 0;
               buffer = 0;
               
           else
               % spike found ?
               if (tmpCount > 0)
                   % store mean spike value
                   spikeCount = spikeCount+1;
                   spikes(spikeCount) =  mean(buffer);
                   
                   % reset
                   tmpCount = 0;
                   buffer = 0;

               end
           end
       end
    end
    
    % mean value of all detected spikes
    slopeTime = mean(spikes);
end

