1

Edited

I would to preface this question, that I understand the effects of noise/error on this.

I have a bunch of data in x, y,z coordinates of linear acceleration as well as time.

I am looking to get distance traveled over time using this data via a double integral. Does this look like the right way to do this?

    private class SampleReading
    {
        public float X { get; set; }
        public float Y { get; set; }
        public float Z { get; set; }
        public long TimeTick { get; set; }
    }


    private static void ProcessSamples(IList<SampleReading> samples)
    {
        float dx = 0;
        float vx = 0;
        float dy = 0; 
        float vy = 0;
        float dz = 0;
        float vz = 0;

        var graphSb = new StringBuilder();

        for (var i = 1; i < samples.Count; i++)
        {
            var curr = samples[i];
            var prev = samples[i - 1];

            var dt = curr.TimeTick - prev.TimeTick;
            if (dt == 0) continue;

            vx += (prev.X + curr.X) / 2.0f * dt;
            dx += vx * dt;

            vy += (prev.Y + curr.Y) / 2.0f * dt;
            dy += vy * dt;

            vz += (prev.Z + curr.Z) / 2.0f * dt;
            dz += vz * dt;

            var distance = Math.Sqrt(dx * dx + dy * dy + dz * dz);
            graphSb.AppendLine(curr.TimeTick + ", " + distance);
        }

        var foo = graphSb.ToString();
    }

I've read this post an all of the supporting materials: How can I find distance traveled with a gyroscope and accelerometer?

My result for stationary reading is a an exponential curve. Just want to make sure that my algo is correct and this is just a result of the side effect.

Community
  • 1
  • 1
Greg R
  • 1,670
  • 4
  • 26
  • 46
  • Is this a calculus question or a programming question? Do you have the algorithm? What part are you having trouble with, exactly? –  Apr 07 '14 at 18:46
  • This question appears to be off-topic because it is primarily about mathematics and physics. – andand Apr 07 '14 at 18:58
  • I am trying to implement this in C# – Greg R Apr 07 '14 at 18:59
  • @GregR, what do you have so far? Do you have an algorithm? –  Apr 07 '14 at 19:06
  • 2
    There are entire undergraduate and graduate level mathematics courses in numerical methods / analysis. You're asking about numerical integration, one topic of such a course. If you try implementing a numerical integration tool in C# and run into trouble doing so, then this is an appropriate forum to ask questions about your implementation. If you are looking to better understand those techniques before writing your own, you should be posting this question on a more appropriate forum, or doing some research to help improve your understanding of the relevant techniques. – andand Apr 07 '14 at 19:08
  • I don't fully understand the math involved, but something like this as a starting point: float dx=0.0f; float vx=0.0f; for (int i=1; i – Greg R Apr 07 '14 at 19:08
  • Edited to include code – Greg R Apr 07 '14 at 22:26
  • 1.it looks ok , but you do not need to integrate average values (prev.X + curr.X) / 2.0f just sum direct value sample[i].X instead (the output will be almost the same). 2.if your dynamics does allow rotation then this is not solvable withouth knowing orientation of the sensors in time !!! so unless this is on some kind of rails (like train) then you can not compute dtistance from acceleration only !!! – Spektre Apr 08 '14 at 07:25
  • We are assuming projectile path (a ball being kicked). So would like to calculate approximate displacement – Greg R Apr 08 '14 at 18:51
  • When you say the result of integrating stationary readings is "an exponential curve" do you really mean exponential? The correct answer should be quadratic, and have the form 0.5 * acceleration * (time^2). Your method for double-integrating the accelerations looks broadly sensible, but I don't think you'll get higher accuracy from averaging the acceleration between adjacent timesteps as compared to just using the acceleration at the previous timestep. – rwp Mar 10 '18 at 19:06

1 Answers1

0

This does not look right to me. You have the measurements prev.X/Y/Z and curr.X/Y/Z so have all the information you need to get the updated position or displacement without calculating the velocity.

If you actually want the velocity, then again what you are doing is not correct. Velocity is not additive the way you have it. From the instantaneous positions prev.X/Y/Z and curr.X/Y/Z you get the respective velocities at the current time-step via:

vxt = (curr.X - prev.X) / dt;
vyt = (curr.Y - prev.Y) / dt;
vzt = (curr.Z - prev.Z) / dt;

where X/Y/Z indicates the the respective directions and is pseudo code.

Where you could make this better and account for the noise of measurement is incorporate a Kalman Filter and Smoother on the positional data.

MoonKnight
  • 23,214
  • 40
  • 145
  • 277
  • Thanks. I am looking for displacement. The coordinates is linear acceleration. What would be the correct way of getting distance from that? (I don't need the velocity directly). So do I use the pseudo code above to calculate velocity and then multiply it by dt to get distance traveled? – Greg R Apr 08 '14 at 18:49
  • Ah, I see what you mean. Then what you are doing looks reasonable! – MoonKnight Apr 08 '14 at 21:09
  • I actually have some concerns regarding its correctness, as you pointed out. I am computing average acceleration over time (which means velocity)? I am not sure that I am getting out distance. What are your thoughts? – Greg R Apr 09 '14 at 21:05