-
Notifications
You must be signed in to change notification settings - Fork 1
/
wiener_plots.py
86 lines (67 loc) · 2.49 KB
/
wiener_plots.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import numpy as np
import matplotlib.pyplot as plt
from wiener_interpolator import wiener_interpolator1, wiener_interpolator2
from parameters import *
def generate_obs_seq():
"""
Generates a realization of the WSS observation
"""
x = np.zeros(N)
variance_x = sigma_w ** 2 / (1 - alpha ** 2)
sigma_x = np.sqrt(variance_x)
x_0 = sigma_x * np.random.randn(1)
x[0] = x_0
for n in range(1, N):
w = sigma_w * np.random.randn(1)
x[n] = alpha * x[n - 1] + w
return x
def main():
"""
For the two Wiener filter interpolators, calculate the theoretical MSE and
Bayesian MSE averaged over NUM_REALIZATIONS for different values of alpha
"""
alpha_list = np.arange(0.1, 1, 0.1)
# Averaged MSEs
TMSE_1 = np.zeros(len(alpha_list))
TMSE_2 = np.zeros(len(alpha_list))
BMSE_1 = np.zeros(len(alpha_list))
BMSE_2 = np.zeros(len(alpha_list))
# For each value of alpha find the averaged MSE
for ind, alpha in enumerate(alpha_list):
TMSE_1_ALPHA = np.zeros(NUM_REALIZATIONS)
TMSE_2_ALPHA = np.zeros(NUM_REALIZATIONS)
BMSE_1_ALPHA = np.zeros(NUM_REALIZATIONS)
BMSE_2_ALPHA = np.zeros(NUM_REALIZATIONS)
# Finding the MSE for each realization
for i in range(NUM_REALIZATIONS):
x = generate_obs_seq()
x_n0 = x[n0]
x = np.delete(x, n0)
x_n0_pred_1, BMSE_1_ALPHA[i] = wiener_interpolator1(x, n0, alpha)
x_n0_pred_2, BMSE_2_ALPHA[i] = wiener_interpolator2(x, n0, alpha)
TMSE_1_ALPHA[i] = (x_n0 - x_n0_pred_1) ** 2
TMSE_2_ALPHA[i] = (x_n0 - x_n0_pred_2) ** 2
# Record the average MSE
BMSE_1[ind] = np.sum(BMSE_1_ALPHA) / NUM_REALIZATIONS
BMSE_2[ind] = np.sum(BMSE_2_ALPHA) / NUM_REALIZATIONS
TMSE_1[ind] = np.sum(TMSE_1_ALPHA) / NUM_REALIZATIONS
TMSE_2[ind] = np.sum(TMSE_2_ALPHA) / NUM_REALIZATIONS
# Plot and save the required plots
plt.figure()
plt.plot(alpha_list, BMSE_1, label="BMSE")
plt.plot(alpha_list, TMSE_1, label="TMSE")
plt.xlabel("Alpha")
plt.ylabel("MSE")
plt.title("Wiener interpolator 1")
plt.legend()
plt.savefig('./results/Wiener_1.png')
plt.figure()
plt.plot(alpha_list, BMSE_2, label="BMSE")
plt.plot(alpha_list, TMSE_2, label="TMSE")
plt.xlabel("Alpha")
plt.ylabel("MSE")
plt.title("Wiener interpolator 2")
plt.legend()
plt.savefig('./results/Wiener_2.png')
if __name__ == '__main__':
main()