Integration and differentiation questions ...

In my application I am receiving continuous data representing either velocity or acceleration. The data is at 10 samples per second. I keep a buffer of 10 seconds of data.

I would like to use either integration or differentiation to convert from one to the other.

I did calculus some time ago at school, but have hardly used it since and can't think how to implement it in software.

If I want to calculate acceleration from velocity, I use differentiation and if I want to calculate velocity from acceleration I use Integration. Am I right?

Differentiation: a(t) = dv(t) / dt

So, to calculate acceleration I just get the (absolute?) difference between the most recent velocity value and divide it by the time difference (0.1 s). Am I right?

Integration: v(t) = t0 to t a(T) dT + v(t0) (the underlined bit represents the stylised "s" or "f" thing).

Is this, like differentiation, done just on a pair of values?

Thanks for your help. Any answers, advice, comments or links to (simple-ish) sites welcome.

Mike

That is more or less right, except that when differentiating you use the difference, not the absolute difference.

The calculation will go easier if you choose your units so that you can use integer arithmetic throughout.

Note that you will inevitably have some rounding errors in your calculation so your calculated integrals will gradually accumulate errors over time. You need to either keep the duration short, or have some way to detect and correct these cumulative errors.

Just remember:
Differentiation calculates the slope (tangent) and Integration calculates the area.

Integration is done by summing the product of dt and A for each step into another variable - approximating
the area under the graph.

Or you can wait till you have all the points and use Simpson's Rule.

I did calculus some time ago at school, but have hardly used it since and can't think how to implement it in software.

here some integration and differentiation code.

void setup()
{
  Serial.begin(115200);
  Serial.println("start");

  Serial.println("\ndt:\trange:\tstep\tint:");
  Serial.println("---------------------------------------------");

  for (int steps = 2; steps <= 10000; steps*=2)
  {
    float sum = 0;
    float b = 0;  // begin
    float e = PI/2;
    float range = e - b;
    float dt = range/steps;
    for (float n = b; n < e; n += dt)
    {
      sum += f(n) * dt;
    }
    Serial.print(dt, 4);
    Serial.print("\t");
    Serial.print(range,4);
    Serial.print("\t");
    Serial.print(steps);
    Serial.print("\t");
    Serial.print(sum, 6);
    Serial.print("\t");
    Serial.println(abs(1-sum), 6);
  }

  Serial.println();
  for (int x = 0; x < 12; x++)
  {
    float v = x * (2*PI/12.0);
    float h = 0.001;
    float x1 = f(v-h/2);
    float x2 = f(v+h/2);
    float diff = (x2-x1)/h;

    Serial.print("value:\t");
    Serial.print(v, 6);
    Serial.print("\tdif:\t");
    Serial.println(diff, 6);
  }
}

void loop()
{
}

float f(float n)
{
  return sin(n);   // or analogRead(A0);  // replace with your own function
}

Thanks for your answers, guys. They are very helpful.

Regards,

Mike