# Smoothing tracks with a Kalman filter

**URL:** <https://discourse.julialang.org/t/smoothing-tracks-with-a-kalman-filter/24209>\
**Category:** General Usage\
**Tags:** question, kalman, smoothing\
**Created:** [May 14, 2019, 2:39pm UTC](https://discourse.julialang.org/t/smoothing-tracks-with-a-kalman-filter/24209 "2019-05-14T14:39:37Z")\
**Posts on this page:** 16\
**Page:** 2

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [May 27, 2020, 8:56am UTC](https://discourse.julialang.org/t/smoothing-tracks-with-a-kalman-filter/24209/21 "2020-05-27T08:56:59Z")

</div>

Of course!  
Go to [GitHub - yakir12/CoffeeBeetles.jl: Code base for "A dung beetle that homes without the use of landmarks"](https://github.com/yakir12/CoffeeBeetles.jl) (install but you don’t need to run `main()`), and then if you run:

```julia
data = CoffeeBeetles.deserialize(CoffeeBeetles.datafile)
df = CoffeeBeetles.getdf(data)

```

that `df` is a data frame with tons of tracks. To access the raw coordinates of a track in the first row of that data frame, you can do this:

```julia
t = df.track[1]
t.rawcoords

```

The smoothed ones (again via a smoothing spline) are in ` t.coords`.

Have at it!!!

(and any comments on anything are more than welcomed!)

---

<div class="post-metadata">

**Author:** ![mschauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mschauer/32/13946_2.png) [@mschauer](https://discourse.julialang.org/u/mschauer)\
**Post date:** [May 27, 2020, 11:21am UTC](https://discourse.julialang.org/t/smoothing-tracks-with-a-kalman-filter/24209/22 "2020-05-27T11:21:49Z")

</div>

I get a

```julia
julia> main()
ERROR: UndefVarError: Common not defined
Stacktrace:
 [1] deserialize_datatype(::Serialization.Serializer{IOStream}, ::Bool) at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.3/Serialization/src/Serialization.jl:1193

```

on Julia 1.3.1. A clue?

---

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [May 27, 2020, 11:23am UTC](https://discourse.julialang.org/t/smoothing-tracks-with-a-kalman-filter/24209/23 "2020-05-27T11:23:25Z")

</div>

yea, there was a warning when you added the package that you need julia 1.4 (see the info [here](https://github.com/yakir12/CoffeeBeetles.jl#troubleshooting)).

---

<div class="post-metadata">

**Author:** ![raphaelsaavedra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raphaelsaavedra/32/27631_2.png) [@raphaelsaavedra](https://discourse.julialang.org/u/raphaelsaavedra)\
**Post date:** [May 27, 2020, 2:51pm UTC](https://discourse.julialang.org/t/smoothing-tracks-with-a-kalman-filter/24209/24 "2020-05-27T14:51:25Z")

</div>

Hi @yakir12,

I might not be able to help you with the modeling part, but we have an implementation of the Kalman filter in our state space models package [GitHub - LAMPSPUC/StateSpaceModels.jl: StateSpaceModels.jl is a Julia package for time-series analysis using state-space models.](https://github.com/LAMPSPUC/StateSpaceModels.jl) that might interest you. It is flexible enough to work with any user-defined linear model.

---

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [May 27, 2020, 3:06pm UTC](https://discourse.julialang.org/t/smoothing-tracks-with-a-kalman-filter/24209/25 "2020-05-27T15:06:00Z")

</div>

The example in [GitHub - LAMPSPUC/StateSpaceModels.jl: StateSpaceModels.jl is a Julia package for time-series analysis using state-space models.](https://github.com/LAMPSPUC/StateSpaceModels.jl#vehicle-tracking) looks very very relevant and cool, but not as smooth as I had hoped (also, it would be better to have an aspect ratio of 1 on that plot, but that’s minor). Any ways to get it to be smoother?

---

<div class="post-metadata">

**Author:** ![raphaelsaavedra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raphaelsaavedra/32/27631_2.png) [@raphaelsaavedra](https://discourse.julialang.org/u/raphaelsaavedra)\
**Post date:** [May 28, 2020, 4:50pm UTC](https://discourse.julialang.org/t/smoothing-tracks-with-a-kalman-filter/24209/26 "2020-05-28T16:50:24Z")

</div>

Hmm, when you say smoother you mean increase the accuracy of the model? That example is a toy problem we created just to illustrate the functionalities of the package, so the error variances are completely arbitrary (you can check the code of the example here [https://github.com/LAMPSPUC/StateSpaceModels.jl/blob/master/examples/vehicle\_tracking.jl](https://github.com/LAMPSPUC/StateSpaceModels.jl/blob/master/examples/vehicle_tracking.jl)).

The accuracy of the filtering and smoothing is directly related to the error variances and how well the defined model adheres to the actual stochastic process it is trying to represent. In some particular cases the square-root filter might perform better too, although it is more computationally expensive.

---

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [May 28, 2020, 8:13pm UTC](https://discourse.julialang.org/t/smoothing-tracks-with-a-kalman-filter/24209/27 "2020-05-28T20:13:21Z")

</div>

I should be more clear: I try to imagine how it would look like if I compared a line drawn between each consecutive measured position and the line of the estimated position. They’d be different, for sure, and I’m sure that the error to the true position would be smaller for the estimated than for the measured one, but… It’d “look” kind of similar. I should try and see how it performs with a lot more aggressive smoothing.

---

<div class="post-metadata">

**Author:** ![mschauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mschauer/32/13946_2.png) [@mschauer](https://discourse.julialang.org/u/mschauer)\
**Post date:** [May 29, 2020, 10:20pm UTC](https://discourse.julialang.org/t/smoothing-tracks-with-a-kalman-filter/24209/28 "2020-05-29T22:20:15Z")

</div>

The problem seems to persist

```julia
julia> data = CoffeeBeetles.deserialize(CoffeeBeetles.datafile)
ERROR: UndefVarError: Common not defined
Stacktrace:
 [1] deserialize_datatype(::Serialization.Serializer{IOStream}, ::Bool) at /Users/julia/buildbot/worker/package

```

---

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [May 30, 2020, 4:59am UTC](https://discourse.julialang.org/t/smoothing-tracks-with-a-kalman-filter/24209/29 "2020-05-30T04:59:26Z")

</div>

If you are on 1.4 or higher, and you did

```julia
import Pkg
Pkg.Registry.add("General") # you may skip this line if this is not a fresh instalation of Julia and you've updated/added a packge before
Pkg.Registry.add(Pkg.RegistrySpec(url = "https://github.com/yakir12/DackeLab")) # you need to do this only once for each instalation of Julia
Pkg.add(Pkg.PackageSpec(url = "https://github.com/yakir12/CoffeeBeetles.jl")) # you need to do this only once for each environment

```

It should work. I did update the instructions lately so maybe that’s where the issue is from…

---

<div class="post-metadata">

**Author:** ![mschauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mschauer/32/13946_2.png) [@mschauer](https://discourse.julialang.org/u/mschauer)\
**Post date:** [June 1, 2020, 9:43am UTC](https://discourse.julialang.org/t/smoothing-tracks-with-a-kalman-filter/24209/30 "2020-06-01T09:43:25Z")

</div>

Thanks, it’s working now. It was first a too old, then a too new Julia version

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [May 5, 2022, 11:40am UTC](https://discourse.julialang.org/t/smoothing-tracks-with-a-kalman-filter/24209/31 "2022-05-05T11:40:24Z")

</div>

After having met @yakir12 again during a recent Julia meetup, I decided to package our experiments discussed in this thread as a tutorial on using some slightly more advanced features available in particle filtering  
[https://baggepinnen.github.io/LowLevelParticleFilters.jl/dev/beetle\_example/](https://baggepinnen.github.io/LowLevelParticleFilters.jl/dev/beetle_example/)  
Particle filters may not always be the most computationally efficient way of reaching the answer to a filtering problem, but they are incredible flexible!

---

<div class="post-metadata">

**Author:** ![cgeoga](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cgeoga/32/216186_2.png) [@cgeoga](https://discourse.julialang.org/u/cgeoga)\
**Post date:** [May 5, 2022, 1:34pm UTC](https://discourse.julialang.org/t/smoothing-tracks-with-a-kalman-filter/24209/32 "2022-05-05T13:34:45Z")

</div>

@yakir12, is this data still available? When I follow the instructions, I get the following error:

```julia
julia> main()
ERROR: UndefRefError: access to undefined reference
Stacktrace:
  [1] getindex
    @ ./array.jl:861 [inlined]
  [2] getindex
    @ ./abstractarray.jl:1221 [inlined]
  [3] desertag
    @ ~/Scratch/julia-1.7.1/share/julia/stdlib/v1.7/Serialization/src/Serialization.jl:100 [inlined]
  [4] handle_deserialize(s::Serializer{IOStream}, b::Int32)
    @ Serialization ~/Scratch/julia-1.7.1/share/julia/stdlib/v1.7/Serialization/src/Serialization.jl:839
[...]
 [20] open(f::typeof(deserialize), args::String; kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
    @ Base ./io.jl:330
 [21] open
    @ ./io.jl:328 [inlined]
 [22] deserialize(filename::String)
    @ Serialization ~/Scratch/julia-1.7.1/share/julia/stdlib/v1.7/Serialization/src/Serialization.jl:798
 [23] main()
    @ CoffeeBeetles ~/.julia/packages/CoffeeBeetles/snoun/src/CoffeeBeetles.jl:23
 [24] top-level scope
    @ REPL[11]:1

```

No sweat if you’re kind of done with this and don’t have the time to debug this for curious randos. Asking just in case. It looks like a fun dataset to tinker with.

---

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [May 5, 2022, 1:36pm UTC](https://discourse.julialang.org/t/smoothing-tracks-with-a-kalman-filter/24209/33 "2022-05-05T13:36:42Z")

</div>

It’s most definitely still relevant. I’m just swamped right now with preparations for more field work. @baggepinnen and I are going to make sure it’s reproducible and working ASAP.

Thanks for the interest!

---

<div class="post-metadata">

**Author:** ![cgeoga](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cgeoga/32/216186_2.png) [@cgeoga](https://discourse.julialang.org/u/cgeoga)\
**Post date:** [May 5, 2022, 1:38pm UTC](https://discourse.julialang.org/t/smoothing-tracks-with-a-kalman-filter/24209/34 "2022-05-05T13:38:01Z")

</div>

Awesome, I’ll keep an eye on the thread, thanks! I see that the blog posts just loads a vanilla csv, which would also be great. Best wishes for the field work.

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [May 5, 2022, 1:56pm UTC](https://discourse.julialang.org/t/smoothing-tracks-with-a-kalman-filter/24209/35 "2022-05-05T13:56:18Z")

</div>

The file is here if you’d like to experiement  
[https://github.com/baggepinnen/LowLevelParticleFilters.jl/blob/master/docs/track.csv](https://github.com/baggepinnen/LowLevelParticleFilters.jl/blob/master/docs/track.csv)

---

<div class="post-metadata">

**Author:** ![cgeoga](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cgeoga/32/216186_2.png) [@cgeoga](https://discourse.julialang.org/u/cgeoga)\
**Post date:** [May 5, 2022, 3:54pm UTC](https://discourse.julialang.org/t/smoothing-tracks-with-a-kalman-filter/24209/36 "2022-05-05T15:54:23Z")

</div>

Thanks so much!

[Previous page](https://discourse.julialang.org/t/smoothing-tracks-with-a-kalman-filter/24209.md?page=1)
