
const unsigned LENGTH = 20;
float buffer[LENGTH];       // circular buffer of samples
unsigned position = 0;      // index of the oldest sample



// Put a new value into a circular sample buffer, overwriting the oldest sample.
void ringfilter_put(float value)
{
  if (position < LENGTH) buffer[position] = value;
  if (++position >= LENGTH) position = 0;
}


// Calculate the first derivative as the finite difference between the newest
// and oldest values.
float ringfilter_deriv(float delta_t)
{
  float oldest = buffer[position];
  float newest = (position < LENGTH-1) ? buffer[position+1] : buffer[0];
  return (newest - oldest) / ((LENGTH-1) * delta_t);
}

