summaryrefslogtreecommitdiff
path: root/examples/kalman.hs
diff options
context:
space:
mode:
authorAlberto Ruiz <aruiz@um.es>2007-09-21 18:28:08 +0000
committerAlberto Ruiz <aruiz@um.es>2007-09-21 18:28:08 +0000
commit0198366bba7a5f2d67338633f9eb90889ffc31b2 (patch)
tree4897d90233b333ee2092e63a4b74c7bcb2d22577 /examples/kalman.hs
parentd4cb2692f9dae748da23371057a983deca4b2f80 (diff)
add examples
Diffstat (limited to 'examples/kalman.hs')
-rw-r--r--examples/kalman.hs56
1 files changed, 56 insertions, 0 deletions
diff --git a/examples/kalman.hs b/examples/kalman.hs
new file mode 100644
index 0000000..ccb8083
--- /dev/null
+++ b/examples/kalman.hs
@@ -0,0 +1,56 @@
1import LinearAlgebra
2import Graphics.Plot
3import LinearAlgebra.Instances
4
5--import GSLHaskell
6
7vector l = fromList l :: Vector Double
8matrix ls = fromLists ls :: Matrix Double
9diagl = diag . vector
10
11f = matrix [[1,0,0,0],
12 [1,1,0,0],
13 [0,0,1,0],
14 [0,0,0,1]]
15
16h = matrix [[0,-1,1,0],
17 [0,-1,0,1]]
18
19q = diagl [1,1,0,0]
20
21r = diagl [2,2]
22
23s0 = State (vector [0, 0, 10, -10]) (diagl [10,0, 100, 100])
24
25data System = System {kF, kH, kQ, kR :: Matrix Double}
26data State = State {sX :: Vector Double , sP :: Matrix Double}
27type Measurement = Vector Double
28
29kalman :: System -> State -> Measurement -> State
30kalman (System f h q r) (State x p) z = State x' p' where
31 px = f <> x -- prediction
32 pq = f <> p <> trans f -- its covariance
33 y = z - h <> px -- residue
34 cy = h <> pq <> trans h + r -- its covariance
35 k = pq <> trans h <> inv cy -- kalman gain
36 x' = px + k <> y -- new state
37 p' = (ident (dim x) - k <> h) <> pq -- its covariance
38
39sys = System f h q r
40
41zs = [vector [15-k,-20-k] | k <- [0..]]
42
43xs = s0 : zipWith (kalman sys) xs zs
44
45des = map (sqrt.takeDiag.sP) xs
46
47evolution n (xs,des) =
48 vector [1.. fromIntegral n]:(toColumns $ fromRows $ take n (zipWith (-) (map sX xs) des)) ++
49 (toColumns $ fromRows $ take n (zipWith (+) (map sX xs) des))
50
51main = do
52 print $ fromRows $ take 10 (map sX xs)
53 mapM_ (print . sP) $ take 10 xs
54 mplot (evolution 20 (xs,des))
55
56--(<>) = multiply \ No newline at end of file