6

(I think this question is platform independent, but I happen to be coding for a Nexus One).

About "current speed": I'm getting a callback every second or so telling me what my current latitude and longitude are. I can calc the distance between the current location and the previous location, so I can keep track of cumulative distance and cumulative time. With that I can say what the average speed has been for the ENTIRE trip.

But how do I calc current speed? I suspect I need to use the most recent N samples, right? Am I thinking about this the right way? What's a good rule of thumb for N? How many samples, or how many seconds back?

About "stop time": If I'm just standing still, I can still get slightly different latitudes and longitudes reported to me, right? So, deciding that I'm not really moving means saying something like, "the previous X locs have all been within Y meters of each other", right? Am I thinking about this the right way? What's a good rule of thumb for X and Y?

Even about "distance": Will I be understating it because I'm literally cutting corners? Is there an algorithm or a rule of thumb, for determining when I'm "turning" and should I add in a little fudge?

EDIT: I APOLOGIZE: I feel bad about wasting people's time and good will, but sadly, the device IS giving me speed. I thought it wasn't because in the emulator it wasn't, but on the real device it is. Thanks everybody. There's still some rule-of-thumb code I need to write, but speed was the biggest challenge.

EDIT: I retract the apology. In my original question I wrote that distance too is a derived value. If I just use the raw GPS data, I will be overstating distance because of the inaccuracies. I might be walking a straight line, but the raw GPS lat/long will wobble, so if I calc total distance by measuring the distance between the points, I will be overstating it. Here's some links that are related to this problem.

Smooth GPS data
http://www.cs.unc.edu/~welch/kalman/Levy1997/index.html
How to intelligently degrade or smooth GIS data (simplifying polygons)?
How to 'smooth' data and calculate line gradient?

MackM
  • 2,906
  • 5
  • 31
  • 45
Corey Trager
  • 22,649
  • 18
  • 83
  • 121
  • 1
    if you are taking this data from a device, particularly one using the Sirf chipset then velocity will probably be provided as part of the returned data. – Steve Weet Jan 22 '10 at 14:19

5 Answers5

3

Remember a short history of position, going back a few seconds. 5 seconds should give you a reasonably accurate result that updates fairly quickly...

// delay is the time difference between the 2 samples you have
delay = 5;   // 5 second delay

// figure out how far along x and y we have moved since last time
dx = newx - oldx;
dy = newy - oldy;

// distance travelled
distance = sqrt(dx*dx + dy*dy);

// find the speed. if the positions were measured in metres and the time in seconds
// this will be the average speed in metres per second, over the last 5 seconds
speed = distance / delay;

The longer you can wait between samples (eg, if you keep the last 30 position samples and use a 30 second delay), the more stable your answer will be (ie, the less noisy it will be), but the slower it will be to react to any changes in speed.

Why do you need to add this delay stuff? well, the GPS unit in your phone probably isn't very accurate. If you are standing still, the position it returns each second might wobble about a fair bit. This wobble noise will make it look like you are sprinting randomly around the room and might cause you to report a moderately high speed, even though you're not moving at all. The solution I have listed will not really help when you are standing still, as the result from 30 seconds ago will be just as wrong as the position from 1 second ago. What you really need to do is average the position over a while, then compare that to the average position from a slightly earlier time. eg...

Take 10 samples of position and average them. This is position 1.
Take another 10 samples and average them. This is position 2.
Use these 2 positions with the code above to get the speed.

Again, the more samples you can take, the more accurate and stable your positions will become, but this will make your speed measurement less responsive.

Rik Heywood
  • 13,816
  • 9
  • 61
  • 81
  • My question is really just about the 5 seconds. Did you just make that up, or does it come from actual field experience with GPS imprecision? How do you know that 4 isn't a better answer, or 6? – Corey Trager Jan 22 '10 at 14:25
  • I've updated a bit to try and make it clearer. Basically, the more samples you can do, the more stable your answer gets, but the slower it is at reacting to changes in speed. How much accuracy and how responsive you need your application to be is up to you. It also depends on the speeds you are travelling at the how much position noise is coming from the device. If your device is accurate to +-2 metres, then that is a massive amount of noise at walking speeds, but probably no problem at aircraft speeds. There are too many "it depends" to give you a good guess without knowing more. – Rik Heywood Jan 22 '10 at 14:31
  • You added a lot more to text to your answer, but now there is another magical number, "10". How do you know that "9" isn't a better number? – Corey Trager Jan 22 '10 at 14:32
  • As I say, what magic number works best for you depends on the application, device accuracy etc etc. If you wanted to get smart you can adjust the magic number based on the data you are getting from the device. If the device is giving you very stable position data, you can reduce the number of samples. If it is getting worse, you may need to increase it. Signal quality varies after all. – Rik Heywood Jan 22 '10 at 14:36
2

Your approach is not really wrong, but naive. There's a huge mathematical foundation for such tasks - called, unsurprisingly, filters. You can get much better than 'averaging last N values'.

For one, Kalman filters are easy to implement and tweak and are generally good enough for practical tasks.

Also, don't try to smooth or average GPS signal - GPS receiver does that itself. Instead, base filter on expected accelerations of the vehicle (or human).

Finally, momentary velocity can be calculated from frequency shift, if you can get that information.

ima
  • 8,105
  • 3
  • 20
  • 19
1

Regarding 'cutting corners', you can always approximate a curve (using splines or for real overkill - regression), otherwise I think you are on the right track.

Regarding how many samples and standing still, instead of taking a constant N, I would look at the deviation of the samples.

Ofir
  • 8,194
  • 2
  • 29
  • 44
1

about the current speed: most gps devices send you this information on their own

Thanos Papathanasiou
  • 904
  • 2
  • 12
  • 23
  • 1
    Because some programmer calculated it. Today, I'm that programmer. The raw data is just lat/long and a timestamp. – Corey Trager Jan 22 '10 at 14:40
  • Not necessarily. My GPS Devices noticed velocity changes way before location change was noticed (especially when rapidly accelerating). I think it is measured directly by the Doppler distortion of the signal. – Adam Matan Jan 22 '10 at 14:47
  • If you are sure of the lat/long and i mean absolutely sure then OK. you can calculate it with them and report back. It wouldn't hurt to see if the device you are programming on has an accelerometer though :) – Thanos Papathanasiou Jan 22 '10 at 15:20
  • I feel bad about wasting people's time and good will, but sadly, the device *IS* giving me speed. I thought it wasn't because in the emulator it wasn't, but on the real device it is. – Corey Trager Jan 22 '10 at 15:33
  • 2
    Speed is indeed calculated independently from location. Instantaneous speed at the time of a fix being reported is determined by measuring the Doppler shift in the signals received from visible satellites by the receiver. If you know the speed of a satellite moving towards the receiver (from the orbital equations) you can calculate the expected Doppler shift assuming the receiver was stationary. The difference between the expected and measured Doppler allows you to calculate the receiver speed. And this calculated speed is usually quite accurate. – Keir Finlow-Bates Jan 18 '12 at 13:12
0

The current speed is the difference between Position(now) and Position(previous) so to get the current speed you only need to compare the current and last position.

BUT: As this is quite vulnerable to small inaccuracies in time/position tracking it is not reliable so you have to average it over the last Positions. How many depends on the use case, the longer the time frame the less "current" the speed is but the more precise it is.

  • @dbemerlin - My whole question is about the "BUT" part of your answer. – Corey Trager Jan 22 '10 at 14:10
  • ... which depends on the use case: To show the speed while driving the timeframe should be around ~5 seconds, to show the speed on a map for trucks in transportation (tracking) it can be a minute or more... –  Jan 22 '10 at 14:26