summaryrefslogtreecommitdiff
path: root/src/kalman/kalman_filter.5c
blob: b676a47b1ddcfd357ceb3b1a44685dc61b64ad7c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
load "matrix.5c"

/*
 * Copyright © 2011 Keith Packard <keithp@keithp.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
 */

namespace kalman {

	import matrix;

	public typedef struct {
		vec_t	x;		/* state */
		mat_t	p;		/* error estimate */
		mat_t	k;		/* kalman factor */
	} state_t;

	public typedef struct {
		mat_t	a;		/* model */
		mat_t	q;		/* model error covariance */
		mat_t	r;		/* measurement error covariance */
		mat_t	h;		/* measurement from model */
	} parameters_t;

	public typedef struct {
		mat_t	a;		/* model */
		mat_t	k;		/* kalman coefficient */
		mat_t	h;		/* measurement from model */
	} parameters_fast_t;

	vec_t measurement_from_state(vec_t x, mat_t h) {
		return multiply_mat_vec(h, x);
	}


	void print_state(string name, state_t s) {
		print_vec(sprintf("%s state", name), s.x);
		print_mat(sprintf("%s error", name), s.p);
	}

	public bool debug = false;

	public state_t predict (state_t s, parameters_t p) {
		state_t	n;

		if (debug) {
			printf ("--------PREDICT--------\n");
			print_state("current", s);
		}

		/* Predict state
		 *
		 * x': predicted state
		 * a:  model
		 * x:  previous state
		 *
		 * x' = a * x;
		 */

		n.x = multiply_mat_vec(p.a, s.x);

		/* t0 = a * p */
		mat_t t0 = multiply (p.a, s.p);
		if (debug)
			print_mat("t0", t0);

		/* t1 = a * p * transpose(a) */

		mat_t t1 = multiply (t0, transpose(p.a));

		/* Predict error
		 *
		 * p': predicted error
		 * a:  model
		 * p:  previous error
		 * q:  model error
		 *
		 * p' = a * p * transpose(a) + q
		 */

		n.p = add(t1, p.q);
		if (debug)
			print_state("predict", n);
		return n;
	}

	public vec_t predict_fast(vec_t x, parameters_fast_t p) {
		if (debug) {
			printf ("--------FAST PREDICT--------\n");
			print_vec("current", x);
		}
		vec_t new = multiply_mat_vec(p.a, x);
		if (debug)
			print_vec("predict", new);
		return new;
	}

	public vec_t correct_fast(vec_t x, vec_t z, parameters_fast_t p) {
		if (debug) {
			printf ("--------FAST CORRECT--------\n");
			print_vec("measure", z);
			print_vec("current", x);
		}
		vec_t	model = multiply_mat_vec(p.h, x);
		if (debug)
			print_vec("extract model", model);
		vec_t	diff = vec_subtract(z, model);
		if (debug)
			print_vec("difference", diff);
		vec_t	adjust = multiply_mat_vec(p.k, diff);
		if (debug)
			print_vec("adjust", adjust);

		vec_t new = vec_add(x,
			       multiply_mat_vec(p.k,
						vec_subtract(z,
							     multiply_mat_vec(p.h, x))));
		if (debug)
			print_vec("correct", new);
		return new;
	}

	public state_t correct(state_t s, vec_t z, parameters_t p) {
		state_t	n;

		if (debug) {
			printf ("--------CORRECT--------\n");
			print_vec("measure", z);
			print_state("current", s);
		}

		/* t0 = p * T(h) */

		/* 3x2 = 3x3 * 3x2 */
		mat_t t0 = multiply(s.p, transpose(p.h));
		if (debug)
			print_mat("t0", t0);

		/* t1 = h * p */

		/* 2x3 = 2x3 * 3x3 */
		mat_t t1 = multiply(p.h, s.p);
		if (debug)
			print_mat("t1", t1);

		/* t2 = h * p * transpose(h) */

		/* 2x2 = 2x3 * 3x2 */
		mat_t t2 = multiply(t1, transpose(p.h));
		if (debug)
			print_mat("t2", t2);

		/* t3 = h * p * transpose(h) + r */

		/* 2x2 = 2x2 + 2x2 */
		mat_t t3 = add(t2, p.r);
		if (debug)
			print_mat("t3", t3);

		/* t4 = inverse(h * p * transpose(h) + r) */

		/* 2x2 = 2x2 */
		mat_t t4 = inverse(t3);
		if (debug)
			print_mat("t4", t4);

		/* Kalman value */

		/* k: Kalman value
		 * p: error estimate
		 * h: state to measurement matrix
		 * r: measurement error covariance
		 *
		 * k = p * transpose(h) * inverse(h * p * transpose(h) + r)
		 *
		 * k = K(p)
		 */

		/* 3x2 = 3x2 * 2x2 */
		mat_t k = multiply(t0, t4);
		if (debug)
			print_mat("k", k);
		n.k = k;

		/* t5 = h * x */

		/* 2 = 2x3 * 3 */
		vec_t t5 = multiply_mat_vec(p.h, s.x);
		if (debug)
			print_vec("t5", t5);

		/* t6 = z - h * x */

		/* 2 = 2 - 2 */
		vec_t t6 = vec_subtract(z, t5);
		if (debug)
			print_vec("t6", t6);

		/* t7 = k * (z - h * x) */

		/* 3 = 3x2 * 2 */
		vec_t t7 = multiply_mat_vec(k, t6);
		if (debug)
			print_vec("t7", t7);

		/* Correct state
		 *
		 * x:  predicted state
		 * k:  kalman value
		 * z:  measurement
		 * h:  state to measurement matrix
		 * x': corrected state
		 *
		 * x' = x + k * (z - h * x)
		 */

		n.x = vec_add(s.x, t7);
		if (debug)
			print_vec("n->x", n.x);

		/* t8 = k * h */

		/* 3x3 = 3x2 * 2x3 */
		mat_t t8 = multiply(k, p.h);
		if (debug)
			print_mat("t8", t8);

		/* t9 = 1 - k * h */

		/* 3x3 = 3x3 - 3x3 */
		mat_t t9 = subtract(identity(dim(s.x)), t8);
		if (debug)
			print_mat("t9", t9);

		/* Correct error
		 *
		 * p:  predicted error
		 * k:  kalman value
		 * h:  state to measurement matrix
		 * p': corrected error
		 *
		 * p' = (1 - k * h) * p
		 *
		 * p' = P(k,p)
		 */

		/* 3x3 = 3x3 * 3x3 */
		n.p = multiply(t9, s.p);
		if (debug) {
			print_mat("n->p", n.p);
#			print_state("correct", n);
		}
		return n;
	}

	real distance(mat_t a, mat_t b) {
		int[2]	d = dims(a);
		int	i_max = d[0];
		int	j_max = d[1];
		real	s = 0;

		for (int i = 0; i < i_max; i++)
			for (int j = 0; j < j_max; j++)
				s += (a[i,j] - b[i,j]) ** 2;
		return sqrt(s);
	}

	public mat_t converge(parameters_t p) {
		int	model = dims(p.a)[0];
		int	measure = dims(p.r)[0];
		int	reps = 0;
		state_t	s = {
			.x = (real[model]) { 0 ... },
			.p = (real[model,model]) { { 0 ... } ... },
			.k = (real[model,measure]) { { 0 ... } ... }
		};

		vec_t	z = (real [measure]) { 0 ... };
		for (;;) {
			state_t	s_pre = predict(s, p);
			state_t s_post = correct(s_pre, z, p);
			real	d = distance(s.k, s_post.k);
			s = s_post;
			reps++;
			if (d < 1e-10 && reps > 10)
				break;
		}
		return s.k;
	}

	public parameters_fast_t convert_to_fast(parameters_t p) {
		return (parameters_fast_t) {
			.a = p.a, .k = converge(p), .h = p.h
		};
	}
}