# kalman filter to improve the gps data received from an iPhone

Started by May 25, 2010
```Hi,

I am currently working on an navigation application for an iPhone device.
The GPS data that I receive is not very accurate and I want to use a kalman
filter. The problem is that I don't really know how to build the kalman
equations for x and y coordinates. What kind of model should I use?

```
```On 05/25/2010 07:28 AM, andreivig wrote:
> Hi,
>
> I am currently working on an navigation application for an iPhone device.
> The GPS data that I receive is not very accurate and I want to use a kalman
> filter. The problem is that I don't really know how to build the kalman
> equations for x and y coordinates. What kind of model should I use?

Without having some sort of independent measurement or estimate of
motion, there's not much that a Kalman filter can do for you.  If you
know the phone isn't moving then the best you can do is also simple:
just average a whole bunch of position readings.  If you have no
guarantee that the phone is staying still then you need more data.

Usually this is done with a 6-axis inertial measurement unit (three
accelerometers and three gyros).  But you need very accurate devices --
you can spend thousands of dollars on your IMU and still have it be
insufficient for all but specialized uses.  Worse, the IMU with
electronics but without batteries occupies at least a dozen cubic
inches, much more than an iPhone, and that's without the considerable
battery pack you'd need to carry it around.

Combining GPS with odometry and a compass may work, but would be awkward.

What resources does the phone give you beyond the GPS?  I understand
that there is some inertial measurement going on, but my understanding
is that it is more for gesture recognition, which really doesn't put it
in a class to work for this sort of thing.

--
Tim Wescott
Control system and signal processing consulting
www.wescottdesign.com
```
```[cut]
> What resources does the phone give you beyond the GPS? &#2013266080;I understand
> that there is some inertial measurement going on, but my understanding
> is that it is more for gesture recognition, which really doesn't put it
> in a class to work for this sort of thing.

There should be just a 3 axis accelerometer, a very cheap/small one.
You can't get much help from that...    :)

Best way may be to improve the GPS reception using A-GPS and try have
a good visibility of the sky.
```
```On 05/25/2010 09:00 AM, claudegps wrote:
> [cut]
>> What resources does the phone give you beyond the GPS?  I understand
>> that there is some inertial measurement going on, but my understanding
>> is that it is more for gesture recognition, which really doesn't put it
>> in a class to work for this sort of thing.
>
> There should be just a 3 axis accelerometer, a very cheap/small one.
> You can't get much help from that...    :)
>
> Best way may be to improve the GPS reception using A-GPS and try have
> a good visibility of the sky.

Not just an accelerometer.  Where you really need help with the combined
IMU/GPS solution is in the gyro, which has to be good relative to the
speed at which the earth-relative acceleration vector on the IMU and
antenna is changing (i.e. the more jerk there is, and the more different
directions it occurs, the better off you are).

You _could_ get by with a cheap set of gyros, but only if you had a lot
of guaranteed motion of the platform -- and even then a cheap gyro is a
heck of a lot less cheap than a cheap accelerometer!

--
Tim Wescott
Control system and signal processing consulting
www.wescottdesign.com
```
```On 25 Mag, 18:10, Tim Wescott <t...@seemywebsite.now> wrote:
> On 05/25/2010 09:00 AM, claudegps wrote:
>
> > [cut]
> >> What resources does the phone give you beyond the GPS? &#2013266080;I understand
> >> that there is some inertial measurement going on, but my understanding
> >> is that it is more for gesture recognition, which really doesn't put it
> >> in a class to work for this sort of thing.
>
> > There should be just a 3 axis accelerometer, a very cheap/small one.
> > You can't get much help from that... &#2013266080; &#2013266080;:)
>
> > Best way may be to improve the GPS reception using A-GPS and try have
> > a good visibility of the sky.
>
> Not just an accelerometer. &#2013266080;Where you really need help with the combined
> IMU/GPS solution is in the gyro, which has to be good relative to the
> speed at which the earth-relative acceleration vector on the IMU and
> antenna is changing (i.e. the more jerk there is, and the more different
> directions it occurs, the better off you are).

I understand. I was referring that the iPhone, as far as I know, does
not have gyro but only the accelerometer.
Not sure about the magnetometer... I think some Nokias have it, but no
gyro around.
Take into account MEMS accelerometer are in 3\$ range for 1k pcs...
gyro are at 20\$ !!  :)    (very rough pricing.... just to give an
idea)

> You _could_ get by with a cheap set of gyros, but only if you had a lot
> of guaranteed motion of the platform -- and even then a cheap gyro is a
> heck of a lot less cheap than a cheap accelerometer!

Having a complete set of inertial measurements could certainly help.
I think that if those hardware would be available, then the iPhone SW
(or the GPS sw... wherewere the main KF runs...). should already be
able to do a tightly coupled integration of the data for the best
performance.

```
```On 05/25/2010 10:41 AM, claudegps wrote:
> On 25 Mag, 18:10, Tim Wescott<t...@seemywebsite.now>  wrote:
>> On 05/25/2010 09:00 AM, claudegps wrote:
>>
>>> [cut]
>>>> What resources does the phone give you beyond the GPS?  I understand
>>>> that there is some inertial measurement going on, but my understanding
>>>> is that it is more for gesture recognition, which really doesn't put it
>>>> in a class to work for this sort of thing.
>>
>>> There should be just a 3 axis accelerometer, a very cheap/small one.
>>> You can't get much help from that...    :)
>>
>>> Best way may be to improve the GPS reception using A-GPS and try have
>>> a good visibility of the sky.
>>
>> Not just an accelerometer.  Where you really need help with the combined
>> IMU/GPS solution is in the gyro, which has to be good relative to the
>> speed at which the earth-relative acceleration vector on the IMU and
>> antenna is changing (i.e. the more jerk there is, and the more different
>> directions it occurs, the better off you are).
>
> I understand. I was referring that the iPhone, as far as I know, does
> not have gyro but only the accelerometer.

I know.  They _really_ didn't make it for that, but the price would have

> Not sure about the magnetometer... I think some Nokias have it, but no
> gyro around.
> Take into account MEMS accelerometer are in 3\$ range for 1k pcs...
> gyro are at 20\$ !!  :)    (very rough pricing.... just to give an
> idea)

Yup.  And that's for gyros that would be dismal at this sort of
application -- they probably wouldn't help at all for just walking around.

>> You _could_ get by with a cheap set of gyros, but only if you had a lot
>> of guaranteed motion of the platform -- and even then a cheap gyro is a
>> heck of a lot less cheap than a cheap accelerometer!
>
> Having a complete set of inertial measurements could certainly help.
> I think that if those hardware would be available, then the iPhone SW
> (or the GPS sw... wherewere the main KF runs...). should already be
> able to do a tightly coupled integration of the data for the best
> performance.

An iPhone probably has the processing horsepower, but as mentioned the
IMU capability just doesn't cut it.

And your written English is just fine.  Had you not apologized for it I
wouldn't have known.

--
Tim Wescott
Control system and signal processing consulting
www.wescottdesign.com
```
```On an iPhone I have a GPS and 3 axis accelerometer. I can also get the
heading. I use the GPS data for my navigation, but as I said this is
sometimes really bad. Sometimes I receive a position with an error within
100 to 400 meters.

Is there any algorithm that I could use to optimize/smoothen this data?

From my readings, a solution would be the kalman filter but I don't know
how to build the model/equations in order to calculate the gain and the
covariance.

I found some code here but the equations does not seem right:
http://github.com/lacker/ikalman/

```
```On 26 Mag, 09:00, "andreivig" <vig.andrei@n_o_s_p_a_m.gmail.com>
wrote:
> On an iPhone I have a GPS and 3 axis accelerometer. I can also get the
> heading. I use the GPS data for my navigation, but as I said this is
> sometimes really bad. Sometimes I receive a position with an error within
> 100 to 400 meters.
>
> Is there any algorithm that I could use to optimize/smoothen this data?

Maybe that you can just try to filter out the "bad" fixes by looking
at the solution status.
If you can have access to GPS data like  N. of satellites used in the
solution, their CN0 and DOPs you can determine which fixes have a good
probability to be accurate and which ones have a really poor accuracy
(few satellites, low CN0s, high DOPs)

```
```>Maybe that you can just try to filter out the "bad" fixes by looking
>at the solution status.
>If you can have access to GPS data like  N. of satellites used in the
>solution, their CN0 and DOPs you can determine which fixes have a good
>probability to be accurate and which ones have a really poor accuracy
>(few satellites, low CN0s, high DOPs)
>
>

The data that I receive from the GPS is :
- coordinates : x, y;
- altitude
- horizontal accuracy
- vertical accuracy
- speed
- course

Which algorithm is best suited to improve coordinates data based on the
other inputs I have?
```
```On 05/26/2010 12:00 AM, andreivig wrote:
> On an iPhone I have a GPS and 3 axis accelerometer. I can also get the
> heading. I use the GPS data for my navigation, but as I said this is
> sometimes really bad. Sometimes I receive a position with an error within
> 100 to 400 meters.
>
> Is there any algorithm that I could use to optimize/smoothen this data?
>
>  From my readings, a solution would be the kalman filter but I don't know
> how to build the model/equations in order to calculate the gain and the
> covariance.
>
> I found some code here but the equations does not seem right:
> http://github.com/lacker/ikalman/

You got the long, nice, detailed, "no", and it didn't take.  Let me try
again:

No.

Did it work that time?

In case you're wondering why, go to behindthename.com and look up
'Kalman'.  You will find that it does not mean "magic".  It means
"remainder" (why they named a king "remainder" is beyond me, but they did).

So a "Kalman filter" is not a "magic filter".  It is just a filter
designed using a method that -- under certain narrow conditions --
guarantees that your filter is the best possible filter.  But, being an
earthly construct by earthly people that has to work on earth, it can
only find the best possible filter, it can't find an impossible filter.

I'm not sure what you're reading, so I don't know if it's wrong or if
accelerometer _along with_ a three axis gyro is sufficient.  A
three-axis accelerometer plus a good compass isn't good enough -- the
compass will only give your orientation in two out of the possible three
dimensions, which will leave the accelerometer data more or less
useless.  Bind the iPhone so that it is somehow always exactly level and
you can use the compass -- but you'd need to be _always_ level to within
the accuracy of your compass, and if you think about it that's a pretty
absurd requirement.

--
Tim Wescott
Control system and signal processing consulting
www.wescottdesign.com
```