CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

| Download
Project: test
Views: 91872
1
# -*- coding: utf-8 -*-
2
"""
3
Created on Thu May 15 16:07:26 2014
4
5
@author: RL
6
"""
7
from __future__ import division
8
import matplotlib.pyplot as plt
9
import numpy.random as random
10
11
12
13
14
15
16
def g_h_filter (data, x, dx, g, h, dt=1.):
17
results = []
18
for z in data:
19
x_est = x + (dx*dt)
20
residual = z - x_est
21
22
dx = dx + h * (residual / float(dt))
23
x = x_est + g * residual
24
print('gx',dx,)
25
26
results.append(x)
27
28
29
return results
30
31
32
'''
33
computation of x
34
x_est = weight + gain
35
residual = z - weight - gain
36
x = weight + gain + g (z - weight - gain)
37
38
w + gain + gz -wg -ggain
39
w -wg + gain - ggain + gz
40
41
w(1-g) + gain(1-g) +gz
42
43
(w+g)(1-g) +gz
44
45
'''
46
'''
47
gain computation
48
49
gain = gain + h/t* (z - weight - gain)
50
= gain + hz/t -hweight/t - hgain/t
51
52
= gain(1-h/t) + h/t(z-weight)
53
54
'''
55
'''
56
gain+ h*(z-w -gain*t)/t
57
58
gain + hz/t -hw/t -hgain
59
60
gain*(1-h) + h/t(z-w)
61
62
63
'''
64
def weight2():
65
w = 0
66
gain = 200
67
t = 10.
68
weight_scale = 1./6
69
gain_scale = 1./10
70
71
weights=[2060]
72
for i in range (len(weights)):
73
z = weights[i]
74
w_pre = w + gain*t
75
76
new_w = w_pre * (1-weight_scale) + z * (weight_scale)
77
78
print('new_w',new_w)
79
80
gain = gain *(1-gain_scale) + (z - w) * gain_scale/t
81
82
print (z)
83
print(w)
84
85
#gain = new_gain * (gain_scale) + gain * (1-gain_scale)
86
w = new_w
87
print ('w',w,)
88
print ('gain=',gain)
89
90
91
def weight3():
92
w = 160.
93
gain = 1.
94
t = 1.
95
weight_scale = 6/10.
96
gain_scale = 2./3
97
98
weights=[158]
99
for i in range (len(weights)):
100
z = weights[i]
101
w_pre = w + gain*t
102
103
new_w = w_pre * (1-weight_scale) + z * (weight_scale)
104
105
print('new_w',new_w)
106
107
gain = gain *(1-gain_scale) + (z - w) * gain_scale/t
108
109
print (z)
110
print(w)
111
112
#gain = new_gain * (gain_scale) + gain * (1-gain_scale)
113
w = new_w
114
print ('w',w,)
115
print ('gain=',gain)
116
weight3()
117
'''
118
#zs = [i + random.randn()*50 for i in range(200)]
119
zs = [158.0, 164.2, 160.3, 159.9, 162.1, 164.6, 169.6, 167.4, 166.4, 171.0]
120
121
#zs = [2060]
122
data= g_h_filter(zs, 160, 1, .6, 0, 1.)
123
124
'''
125
126
data = g_h_filter([2060], x=0, dx=200, g=1./6, h = 1./10, dt=10)
127
print data
128
129
130
'''
131
print data
132
print data2
133
plt.plot(data)
134
plt.plot(zs, 'g')
135
plt.show()
136
'''
137
138