diff options
author | Alberto Ruiz <aruiz@um.es> | 2007-09-21 18:28:08 +0000 |
---|---|---|
committer | Alberto Ruiz <aruiz@um.es> | 2007-09-21 18:28:08 +0000 |
commit | 0198366bba7a5f2d67338633f9eb90889ffc31b2 (patch) | |
tree | 4897d90233b333ee2092e63a4b74c7bcb2d22577 /examples/kalman.hs | |
parent | d4cb2692f9dae748da23371057a983deca4b2f80 (diff) |
add examples
Diffstat (limited to 'examples/kalman.hs')
-rw-r--r-- | examples/kalman.hs | 56 |
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 @@ | |||
1 | import LinearAlgebra | ||
2 | import Graphics.Plot | ||
3 | import LinearAlgebra.Instances | ||
4 | |||
5 | --import GSLHaskell | ||
6 | |||
7 | vector l = fromList l :: Vector Double | ||
8 | matrix ls = fromLists ls :: Matrix Double | ||
9 | diagl = diag . vector | ||
10 | |||
11 | f = matrix [[1,0,0,0], | ||
12 | [1,1,0,0], | ||
13 | [0,0,1,0], | ||
14 | [0,0,0,1]] | ||
15 | |||
16 | h = matrix [[0,-1,1,0], | ||
17 | [0,-1,0,1]] | ||
18 | |||
19 | q = diagl [1,1,0,0] | ||
20 | |||
21 | r = diagl [2,2] | ||
22 | |||
23 | s0 = State (vector [0, 0, 10, -10]) (diagl [10,0, 100, 100]) | ||
24 | |||
25 | data System = System {kF, kH, kQ, kR :: Matrix Double} | ||
26 | data State = State {sX :: Vector Double , sP :: Matrix Double} | ||
27 | type Measurement = Vector Double | ||
28 | |||
29 | kalman :: System -> State -> Measurement -> State | ||
30 | kalman (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 | |||
39 | sys = System f h q r | ||
40 | |||
41 | zs = [vector [15-k,-20-k] | k <- [0..]] | ||
42 | |||
43 | xs = s0 : zipWith (kalman sys) xs zs | ||
44 | |||
45 | des = map (sqrt.takeDiag.sP) xs | ||
46 | |||
47 | evolution 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 | |||
51 | main = 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 | ||