-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdyn_simple_moving_avg_demo.py
67 lines (51 loc) · 1.78 KB
/
dyn_simple_moving_avg_demo.py
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
import numpy as np
import movavgs as ma
num_samples = 50
# Truth process (constant):
truth = 220.6
x = np.linspace(0, 4*np.pi, num_samples)
model_type = 'Dynamic'
truth_vec = truth+np.sin(x)
#model_type = 'Static'
#truth_vec = truth*np.ones(num_samples)
# Estimation sequence and initial guess:
est = np.zeros(num_samples)
# Generate noisy measurements
rg = np.random.default_rng(1)
mu=0
sigma = 0.5
meas = truth_vec + rg.normal(mu,sigma,num_samples)
# Window size:
w_size=10
gain=1/w_size
# To initialize the estimates prior to the Sw_sizeA starting,
# we just do a simple average:
est = ma.sma(meas,w_size)
if w_size<num_samples:
avgtype = 'Simple Moving'
else:
avgtype = 'Running'
ylabel = 'Resistance (Ohms)'
title = 'Ground Truth: ' + model_type + ' Resistance'
ma.tme_plot(1,truth_vec, ylabel=ylabel, title=title)
title = 'Ground Truth + Noise = Measurements: ' + model_type + ' Resistance'
ma.tme_plot(2,truth_vec, meas, ylabel=ylabel, title=title, sigma=sigma)
title = avgtype + ' Average Estimator: ' + model_type + ' Resistance (Window='+str(w_size)+')'
ma.tme_plot(3,truth_vec, meas, est, ylabel, title, sigma)
# Window size:
w_size=5
gain=1/w_size
# To initialize the estimates prior to the Sw_sizeA starting,
# we just do a simple average:
est = ma.sma(meas,w_size)
if w_size<num_samples:
avgtype = 'Simple Moving'
else:
avgtype = 'Running'
ylabel = 'Resistance (Ohms)'
title = avgtype + ' Average Estimator: ' + model_type + ' Resistance (Window='+str(w_size)+')'
ma.tme_plot(4,truth_vec, meas, est, ylabel, title, sigma)
est = ma.sma(meas,num_samples)
avgtype = 'Running'
title = avgtype + ' Average Estimator: ' + model_type + ' Resistance '
ma.tme_plot(5,truth_vec, meas, est, ylabel, title, sigma)