-
Notifications
You must be signed in to change notification settings - Fork 0
/
Kalman.html
227 lines (185 loc) · 19.2 KB
/
Kalman.html
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
<html>
<head>
<script src="utils.js" type="text/javascript"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML-full" id=""></script>
</head>
<body>
<table border="0" width="600" height="160" cellspacing="4" cellpadding="4">
<tbody><tr>
<td> Time <span class="MathJax_Preview" style="color: inherit; display: none;"></span><span class="MathJax" id="MathJax-Element-16-Frame" tabindex="0" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>k</mi></math>" role="presentation" style="position: relative;"><nobr aria-hidden="true"><span class="math" id="MathJax-Span-176" style="width: 0.484em; display: inline-block;"><span style="display: inline-block; position: relative; width: 0.424em; height: 0px; font-size: 104%;"><span style="position: absolute; clip: rect(1.746em, 1000.42em, 2.767em, -999.997em); top: -2.581em; left: 0em;"><span class="mrow" id="MathJax-Span-177"><span class="mi" id="MathJax-Span-178" style="font-family: STIXGeneral-Italic;">k<span style="display: inline-block; overflow: hidden; height: 1px; width: 0.003em;"></span></span></span><span style="display: inline-block; width: 0px; height: 2.587em;"></span></span></span><span style="display: inline-block; overflow: hidden; vertical-align: -0.059em; border-left: 0px solid; width: 0px; height: 0.878em;"></span></span></nobr><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>k</mi></math></span></span> </td>
<td> </td>
<td> 0 </td>
<td> 1 </td>
<td> 2 </td>
<td> 3 </td>
<td> 4 </td>
<td> <b>5</b> </td>
<td> <b>6</b> </td>
<td> 7 </td>
<td> 8 </td>
<td> 9 </td>
</tr>
<tr>
<td> True state <font color="blue"><span class="MathJax_Preview" style="color: inherit; display: none;"></span><span class="MathJax" id="MathJax-Element-17-Frame" tabindex="0" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>x</mi><mi>k</mi></msub></math>" role="presentation" style="position: relative;"><nobr aria-hidden="true"><span class="math" id="MathJax-Span-179" style="width: 0.904em; display: inline-block;"><span style="display: inline-block; position: relative; width: 0.844em; height: 0px; font-size: 104%;"><span style="position: absolute; clip: rect(1.505em, 1000.84em, 2.467em, -999.997em); top: -2.1em; left: 0em;"><span class="mrow" id="MathJax-Span-180"><span class="msubsup" id="MathJax-Span-181"><span style="display: inline-block; position: relative; width: 0.844em; height: 0px;"><span style="position: absolute; clip: rect(3.428em, 1000.42em, 4.21em, -999.997em); top: -4.023em; left: 0em;"><span class="mi" id="MathJax-Span-182" style="font-family: STIXGeneral-Italic;">x<span style="display: inline-block; overflow: hidden; height: 1px; width: 0.003em;"></span></span><span style="display: inline-block; width: 0px; height: 4.029em;"></span></span><span style="position: absolute; top: -3.903em; left: 0.424em;"><span class="mi" id="MathJax-Span-183" style="font-size: 70.7%; font-family: STIXGeneral-Italic;">k<span style="display: inline-block; overflow: hidden; height: 1px; width: 0.003em;"></span></span><span style="display: inline-block; width: 0px; height: 4.029em;"></span></span></span></span></span><span style="display: inline-block; width: 0px; height: 2.106em;"></span></span></span><span style="display: inline-block; overflow: hidden; vertical-align: -0.247em; border-left: 0px solid; width: 0px; height: 0.753em;"></span></span></nobr><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>x</mi><mi>k</mi></msub></math></span></span></font>
</td><td> </td>
<td> <font color="blue"><output id="x0">65</output></font></td>
<td> <font color="blue"><output id="x1">75</output></font></td>
<td> <font color="blue"><output id="x2">75</output></font></td>
<td> <font color="blue"><output id="x3">70</output></font></td>
<td> <font color="blue"><output id="x4">76</output></font></td>
<td> <font color="blue"><output id="x5">80</output></font></td>
<td> <font color="blue"><output id="x6">80</output></font></td>
<td> <font color="blue"><output id="x7">85</output></font></td>
<td> <font color="blue"><output id="x8">80</output></font></td>
<td> <font color="blue"><output id="x9">80</output></font></td>
</tr>
<tr>
<td> Measure <font color="red"><span class="MathJax_Preview" style="color: inherit; display: none;"></span><span class="MathJax" id="MathJax-Element-18-Frame" tabindex="0" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>z</mi><mi>k</mi></msub></math>" role="presentation" style="position: relative;"><nobr aria-hidden="true"><span class="math" id="MathJax-Span-184" style="width: 0.844em; display: inline-block;"><span style="display: inline-block; position: relative; width: 0.784em; height: 0px; font-size: 104%;"><span style="position: absolute; clip: rect(1.505em, 1000.78em, 2.467em, -999.997em); top: -2.1em; left: 0em;"><span class="mrow" id="MathJax-Span-185"><span class="msubsup" id="MathJax-Span-186"><span style="display: inline-block; position: relative; width: 0.784em; height: 0px;"><span style="position: absolute; clip: rect(3.428em, 1000.36em, 4.27em, -999.997em); top: -4.023em; left: 0em;"><span class="mi" id="MathJax-Span-187" style="font-family: STIXGeneral-Italic;">z</span><span style="display: inline-block; width: 0px; height: 4.029em;"></span></span><span style="position: absolute; top: -3.903em; left: 0.364em;"><span class="mi" id="MathJax-Span-188" style="font-size: 70.7%; font-family: STIXGeneral-Italic;">k<span style="display: inline-block; overflow: hidden; height: 1px; width: 0.003em;"></span></span><span style="display: inline-block; width: 0px; height: 4.029em;"></span></span></span></span></span><span style="display: inline-block; width: 0px; height: 2.106em;"></span></span></span><span style="display: inline-block; overflow: hidden; vertical-align: -0.247em; border-left: 0px solid; width: 0px; height: 0.753em;"></span></span></nobr><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>z</mi><mi>k</mi></msub></math></span></span></font>
</td><td> </td>
<td> <font color="red"><output id="z0">65</output></font></td>
<td> <font color="red"><output id="z1">70</output></font></td>
<td> <font color="red"><output id="z2">75</output></font></td>
<td> <font color="red"><output id="z3">75</output></font></td>
<td> <font color="red"><output id="z4">75</output></font></td>
<td> <font color="red"><output id="z5">79</output></font></td>
<td> <font color="red"><output id="z6">79</output></font></td>
<td> <font color="red"><output id="z7">83</output></font></td>
<td> <font color="red"><output id="z8">80</output></font></td>
<td> <font color="red"><output id="z9">80</output></font></td>
</tr>
<tr>
<td> Pred.error <span class="MathJax_Preview" style="color: inherit; display: none;"></span><span class="MathJax" id="MathJax-Element-19-Frame" tabindex="0" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>p</mi><mi>k</mi></msub></math>" role="presentation" style="position: relative;"><nobr aria-hidden="true"><span class="math" id="MathJax-Span-189" style="width: 0.965em; display: inline-block;"><span style="display: inline-block; position: relative; width: 0.904em; height: 0px; font-size: 104%;"><span style="position: absolute; clip: rect(1.505em, 1000.9em, 2.467em, -999.997em); top: -2.1em; left: 0em;"><span class="mrow" id="MathJax-Span-190"><span class="msubsup" id="MathJax-Span-191"><span style="display: inline-block; position: relative; width: 0.904em; height: 0px;"><span style="position: absolute; clip: rect(3.428em, 1000.48em, 4.39em, -999.997em); top: -4.023em; left: 0em;"><span class="mi" id="MathJax-Span-192" style="font-family: STIXGeneral-Italic;">p</span><span style="display: inline-block; width: 0px; height: 4.029em;"></span></span><span style="position: absolute; top: -3.903em; left: 0.484em;"><span class="mi" id="MathJax-Span-193" style="font-size: 70.7%; font-family: STIXGeneral-Italic;">k<span style="display: inline-block; overflow: hidden; height: 1px; width: 0.003em;"></span></span><span style="display: inline-block; width: 0px; height: 4.029em;"></span></span></span></span></span><span style="display: inline-block; width: 0px; height: 2.106em;"></span></span></span><span style="display: inline-block; overflow: hidden; vertical-align: -0.247em; border-left: 0px solid; width: 0px; height: 0.816em;"></span></span></nobr><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>p</mi><mi>k</mi></msub></math></span></span></td>
<td> </td>
<td> <output id="p0">1</output></td>
<td> <output id="p1"></output></td>
<td> <output id="p2"></output></td>
<td> <output id="p3"></output></td>
<td> <output id="p4"></output></td>
<td> <output id="p5"></output></td>
<td> <output id="p6"></output></td>
<td> <output id="p7"></output></td>
<td> <output id="p8"></output></td>
<td> <output id="p9"></output></td>
</tr>
<tr>
<td> State estimate <font color="green"> <span class="MathJax_Preview" style="color: inherit; display: none;"></span><span class="MathJax" id="MathJax-Element-20-Frame" tabindex="0" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mrow class="MJX-TeXAtom-ORD"><mover><mi>x</mi><mo stretchy="false">&#x005E;</mo></mover></mrow><mi>k</mi></msub></math>" role="presentation" style="position: relative;"><nobr aria-hidden="true"><span class="math" id="MathJax-Span-194" style="width: 0.904em; display: inline-block;"><span style="display: inline-block; position: relative; width: 0.844em; height: 0px; font-size: 104%;"><span style="position: absolute; clip: rect(1.145em, 1000.84em, 2.467em, -999.997em); top: -2.1em; left: 0em;"><span class="mrow" id="MathJax-Span-195"><span class="msubsup" id="MathJax-Span-196"><span style="display: inline-block; position: relative; width: 0.844em; height: 0px;"><span style="position: absolute; clip: rect(3.068em, 1000.42em, 4.21em, -999.997em); top: -4.023em; left: 0em;"><span class="texatom" id="MathJax-Span-197"><span class="mrow" id="MathJax-Span-198"><span class="munderover" id="MathJax-Span-199"><span style="display: inline-block; position: relative; width: 0.424em; height: 0px;"><span style="position: absolute; clip: rect(3.428em, 1000.42em, 4.21em, -999.997em); top: -4.023em; left: 0em;"><span class="mi" id="MathJax-Span-200" style="font-family: STIXGeneral-Italic;">x<span style="display: inline-block; overflow: hidden; height: 1px; width: 0.003em;"></span></span><span style="display: inline-block; width: 0px; height: 4.029em;"></span></span><span style="position: absolute; top: -4.084em; left: 0.123em;"><span style="height: 0em; vertical-align: 0em; width: 0.364em; display: inline-block; overflow: hidden;"></span><span class="mo" id="MathJax-Span-201" style="font-family: STIXGeneral-Regular;">̂ <span style="height: 0em; vertical-align: 0em; margin-left: -0.237em;"></span></span><span style="display: inline-block; overflow: hidden; height: 1px; width: 0em;"></span><span style="display: inline-block; width: 0px; height: 4.029em;"></span></span></span></span></span></span><span style="display: inline-block; width: 0px; height: 4.029em;"></span></span><span style="position: absolute; top: -3.903em; left: 0.424em;"><span class="mi" id="MathJax-Span-202" style="font-size: 70.7%; font-family: STIXGeneral-Italic;">k<span style="display: inline-block; overflow: hidden; height: 1px; width: 0.003em;"></span></span><span style="display: inline-block; width: 0px; height: 4.029em;"></span></span></span></span></span><span style="display: inline-block; width: 0px; height: 2.106em;"></span></span></span><span style="display: inline-block; overflow: hidden; vertical-align: -0.247em; border-left: 0px solid; width: 0px; height: 1.066em;"></span></span></nobr><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mrow class="MJX-TeXAtom-ORD"><mover><mi>x</mi><mo stretchy="false">^</mo></mover></mrow><mi>k</mi></msub></math></span></span></font>
</td><td> </td>
<td> <font color="green"><output id="xhat0">65</output></font></td>
<td> <font color="green"><output id="xhat1"></output></font></td>
<td> <font color="green"><output id="xhat2"></output></font></td>
<td> <font color="green"><output id="xhat3"></output></font></td>
<td> <font color="green"><output id="xhat4"></output></font></td>
<td> <font color="green"><output id="xhat5"></output></font></td>
<td> <font color="green"><output id="xhat6"></output></font></td>
<td> <font color="green"><output id="xhat7"></output></font></td>
<td> <font color="green"><output id="xhat8"></output></font></td>
<td> <font color="green"><output id="xhat9"></output></font></td>
</tr>
<tr>
<td> K.gain <span class="MathJax_Preview" style="color: inherit; display: none;"></span><span class="MathJax" id="MathJax-Element-21-Frame" tabindex="0" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>g</mi><mi>k</mi></msub></math>" role="presentation" style="position: relative;"><nobr aria-hidden="true"><span class="math" id="MathJax-Span-203" style="width: 0.965em; display: inline-block;"><span style="display: inline-block; position: relative; width: 0.904em; height: 0px; font-size: 104%;"><span style="position: absolute; clip: rect(1.505em, 1000.9em, 2.467em, -999.997em); top: -2.1em; left: 0em;"><span class="mrow" id="MathJax-Span-204"><span class="msubsup" id="MathJax-Span-205"><span style="display: inline-block; position: relative; width: 0.904em; height: 0px;"><span style="position: absolute; clip: rect(3.428em, 1000.48em, 4.39em, -999.997em); top: -4.023em; left: 0em;"><span class="mi" id="MathJax-Span-206" style="font-family: STIXGeneral-Italic;">g</span><span style="display: inline-block; width: 0px; height: 4.029em;"></span></span><span style="position: absolute; top: -3.903em; left: 0.484em;"><span class="mi" id="MathJax-Span-207" style="font-size: 70.7%; font-family: STIXGeneral-Italic;">k<span style="display: inline-block; overflow: hidden; height: 1px; width: 0.003em;"></span></span><span style="display: inline-block; width: 0px; height: 4.029em;"></span></span></span></span></span><span style="display: inline-block; width: 0px; height: 2.106em;"></span></span></span><span style="display: inline-block; overflow: hidden; vertical-align: -0.247em; border-left: 0px solid; width: 0px; height: 0.816em;"></span></span></nobr><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>g</mi><mi>k</mi></msub></math></span></span></td>
<td> </td>
<td> <output id="g0"></output></td>
<td> <output id="g1"></output></td>
<td> <output id="g2"></output></td>
<td> <output id="g3"></output></td>
<td> <output id="g4"></output></td>
<td> <output id="g5"></output></td>
<td> <output id="g6"></output></td>
<td> <output id="g7"></output></td>
<td> <output id="g8"></output></td>
<td> <output id="g9"></output></td>
</tr>
</tbody>
</table>
<div>
<canvas id="canvas" width="400" height="160" style="border:1px solid #000000;"></canvas>
<div>X0 = <input type="text" size="10" id="xinit" value="65"></div>
<div>r = <input type="text" size="10" id="r" value="10"></div>
<div>HR reduction = <input type="text" size="10" id="a" value="0.95"></div>
<div><button onclick="run()">Run</button></div>
</div>
<script>
// Globals
var x_array = [];
var z_array = [];
function init() {
// Clear canvas
ctx = getCanvasContext();
ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height)
x = getFloatValue("xinit");
a = getFloatValue("a");
r = getFloatValue("r");
// Initialize arrays for plot
x_array = [];
z_array = [];
for (k = 0; k < 10; k++) {
setIndexedValue("x", k, x, 0);
if (k == 5)
z = x + ((Math.random()<0.5 ? -1 : +1) * 5 * r);
else if (k == 6)
z = -z;
else
z = x + 2 * r * Math.random() - r;
setIndexedValue("z", k, z, 0);
// Append values to arrays for plotting
x_array.push(x);
z_array.push(z);
if (k == 0) {
setIndexedValue("xhat", k, z, 0);
}
else {
clearValue("xhat", k);
}
clearValue("p", k);
clearValue("g", k);
// Update X
x *= a;
}
// Plot arrays
plotMyArray(x_array, "blue");
plotMyArray(z_array, "red");
setIndexedValue("p", 0, 1, 0)
} // init
function plotMyArray(array, color) {
plotArray(getCanvasContext(), array, color, -20, 100);
}
function run() {
init();
// Initialize parameters from values in textboxes
a = 1; //getFloatValue("a");
r = getFloatValue("r");
// Initialize state estimate and error covariance
xhat = z_array[0];
p = 1;
// Initialize array of state estimates
xhat_array = [xhat];
// Compute values from Kalman filter and add them to the table
for (k = 1; k < 10; k++) {
// Get values from array
z = z_array[k];
// Compute new values using Kalman filter formulas: based on
// http://en.wikipedia.org/wiki/Kalman_filter#Update
// Predict
xhat = a * xhat;
p = a * p * a;
// Update
g = p == 0 ? 1 : p / (p + r);
xhat = xhat + g * (z - xhat);
p = (1 - g) * p;
// Set new values in table
setIndexedValue("g", k, g, 3);
setIndexedValue("xhat", k, xhat, 0);
setIndexedValue("p", k, p, 3);
// Append values to arrays for plotting
z_array.push(z);
xhat_array.push(xhat);
}
// Plot arrays
plotMyArray(xhat_array, "green");
}
function setIndexedValue(tagbase, index, value, prec) {
document.querySelector(indexToTag(tagbase, index)).value = roundToPrecision(value, prec);
}
</script>
</body>
</html>