-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathplotting.py
221 lines (173 loc) · 7.3 KB
/
plotting.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
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Feb 1 19:38:03 2018
@author: farismismar
"""
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import rc
import scipy.special
import matplotlib.ticker as tick
import os
os.chdir('/Users/farismismar/Desktop/4- Q-Learning Algorithm for VoLTE Closed-Loop Power Control in Indoor Small Cells')
qfunc = lambda x: 0.5-0.5*scipy.special.erf(x/np.sqrt(2))
# This has the Delta_gamma component
def ber_modified(sinr, delta=0, q=140):
# sinr is in dB
error = 1 - (1 - qfunc(np.sqrt(2.*(delta + 10**(sinr/10.))))) ** q # ** q
return error
# sinr = np.linspace(0,18,100)
#per = [ber(x) for x in sinr]
#plt.figure(figsize=(7,5))
#plt.rc('text', usetex=True)
#plt.rc('font', family='serif')
##plot_edge, = plt.semilogy(sinr, ber_modified(sinr, delta=0, q=1), linestyle='-', color='k', label='QPSK (one OFDM symbol)')
#
#ax = plt.gca()
#ax.set_yscale('log')
#ax.get_xaxis().get_major_formatter().labelOnlyBase = False
#
#plot_baseline, = plt.semilogy(sinr, ber_modified(2.*sinr), linestyle='-', color='b', label='Average user $i$ (FPA)')
#
## Note the improvement was computed from Fig 11 in the paper.
##plot_vpc, = plt.semilogy(sinr, ber_modified(2.*sinr, 2/20+3*18/20), linestyle='-', color='r', label='Average user $i$ (proposed power control)')
#plot_dpc, = plt.semilogy(sinr, ber_modified(2.*sinr, 3*18/20), linestyle='-', color='g', label='Average user $i$ (DQN)')
#
#plt.grid(True,which="both")#,ls="-", color='0.65')
#
#plt.xlabel('Average DL SINR (dB)')
#plt.xlim(xmin=0,xmax=9)
#plt.ylabel('$\Xi$ PER')
#plt.title('Voice Packet Error Lower Bound Plot vs SINR -- One VoLTE Frame')
#
#plt.legend(handles=[plot_baseline, plot_dpc]) #plot_vpc, plot_dpc])
#plt.savefig('figures/packet_error.pdf', format="pdf")
#plt.show()
#plt.close()
#######
# 10/30/2018 run for Asilomar
'''
Episode 707 finished after 16 timesteps (and epsilon = 0.01).
Network alarms progress:
[0, 0.0, 0.0, -3.0, 0, 0.0, -0.0, 0.0, 0, 0, -6.28, 0, -0.0, 3.0, 0, 6.28]
PC state progress:
['start', 0, 2, 1, 0, 2, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 'end']
PC action progress:
[-1, 1, 0, -1, 1, 1, 1, 1, 1, 1, -1, 1, 1, 1, 1, 1]
SINR progress:
[4.0, 3.0, 4.0, 4.0, -0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, -1.28, -0.28, 0.72, 4.72, 5.72, 13.0]
FPA:
Episode 707 finished after 20 timesteps.
SINR progress:
[4.0, 4.0, 1.0, 1.0, 1.0, 1.0, -3, -3, -3, -3.0, -3.0, -3.0, -3.0, 2.0, -3.0, 2.0, 2.0, -3, 0.0, 0.0, 0.0, 'end']
'''
# Plot PC for both algorithms
# TODO: Obtain TPC from the proposed PC
tau = 20
tpc = [0, 2, 1, 0, 2, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1,0,0,0] # pad with zeros
time= np.arange(tau)
fig, ax1 = plt.subplots(figsize=(7,5))
plt.rc('text', usetex=True)
plt.rc('font', family='serif')
plt.grid(True)
fpa = ax1.axhline(y=0, xmin=0, color="green", linewidth=1.5, label='Power commands -- FPA')
proposed, = ax1.step(np.arange(len(tpc)), tpc, color='b', linewidth=2.5, label='Power commands -- Proposed')#
ax1.set_xlabel('Transmit time interval (TTI)')
ax1.set_ylabel('Number of power commands')
ax1.set_yticks([-1,0,1,2,3])
ax1.xaxis.set_major_formatter(tick.FormatStrFormatter('%0g'))
ax1.xaxis.set_ticks(np.arange(0, tau + 1))
#ax2 = ax1.twinx()
#sinr, = plt.plot(time, SINR, linestyle='-', color='b', label='DL SINR')
plt.title(r'Power Commands ')
plt.legend(handles=[fpa, proposed])# proposed, deep])
#ax2.set_ylabel('Average DL SINR $\gamma_{DL}$(dB)')
plt.xlim(xmin=0,xmax=tau)
fig.tight_layout()
plt.savefig('figures/tpc.pdf', format="pdf")
plt.show(fig)
plt.close(fig)
####################################################################################
# Plot MOS
tau = 20 # ms
T = tau #6 * 1e3 * tau # 120 sec
sinr = np.linspace(-2,14,100)
# Obtain the corrective/improvement factors from the PC plot before you run this snipped
def payload(T, tau=20, NAF=0.5, Lamr=0, Lsid=61): # T and tau in ms, Lamr/Lsid is in bits
Lsid = Lsid * tau / 8 # from bits to bytes per sec
return NAF * Lamr * np.ceil(T/tau) + (1 - NAF) * Lsid * np.ceil(T/(8*tau))
fig = plt.figure(figsize=(7,5))
# TODO: compute improvement due to PC as a weighted av
for improvement in np.array([0, 12/20*1 + 3/20*2 + 5/20*0 ]): # find this ratio as a weighted av from PC
result = []
for framelength in np.arange(1000): # 1000 taus
volterate = 23.85 # kbps
NAF = 0.7
ber = [ber_modified(x, delta=improvement, q=7*framelength*tau) for x in sinr] # for bit error rate, we compute number of REs per frame hence * 7
per = np.round(np.log10(ber), 0) # we actually need the exponent.
N = len(per)
fer = sum([1 for x in per if x > -2]) / N #scale(per) # will get a frame error for this bit error rate
Lamr = volterate * tau / 8 # in bytes per sec
payld = payload(T=tau, tau=tau, NAF=NAF, Lamr=Lamr) # in bytes
MOS = 4 - 0.7*np.log(fer) - 0.1 * np.log(tau * framelength * fer * N) # fer ok, second term: duration of lost packets in ms?
MOS = min(4, MOS) #4 if MOS >= 4 else MOS # for i in MOS]
MOS = max(1, MOS) #1 if MOS <= 1 else MOS# for i in MOS]
# print(improvement, fer, MOS)
result.append(MOS)
#print('{} {:.0f}% {:.1f}'.format(volterate, 100 * NAF, payld))
if (improvement == 0):
str = 'FPA'
else:
str = 'Proposed'
plt.plot(result, label='AMR = {} kbps, AF = {}, Power control = {}'.format(volterate, NAF, str))
plt.legend()
plt.title('Experimental mean opinion score vs packet error rate')
plt.xlabel('Packet error rate')
plt.ylabel('MOS')
plt.rc('text', usetex=True)
plt.rc('font', family='serif')
plt.xlim(xmin=0,xmax=250)
plt.grid(True)
# Fix the x axis to show packet error rates
ax = plt.gca()
#ax.set_xticks([0,200,400,600,800,1000])
ax.set_xticks([0,50,100,150,200,250])
#ax.set_xticklabels([0,0.1,0.2,0.3,0.4,0.5])
ax.set_xticklabels([0,0.05,0.10,0.15,0.2,0.25])
plt.savefig('figures/mos.pdf', format='pdf')
plt.show()
plt.close(fig)
####################################
# Plotting the episodes on one graph
####################################
SINR_MIN = -3 #dB
baseline_SINR_dB = 4.0
final_SINR_dB = baseline_SINR_dB + 2.0 # this is the improvement
max_timesteps_per_episode = 20
# TODO: Fill in vectors
score_progress_cl = [4.0, 3.0, 4.0, 4.0, -0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, -1.28, -0.28, 0.72, 4.72, 5.72, 9]
score_progress_fpa = [4.0, 4.0, 1.0, 1.0, 1.0, 1.0, -3, -3, -3, -3.0, -3.0, -3.0, -3.0, 2.0, -3.0, 2.0, 2.0, -3, 0.0, 0.0, 0.0]
# Do some nice plotting here
fig = plt.figure()
plt.rc('text', usetex=True)
plt.rc('font', family='serif')
plt.xlabel('Transmit Time Intervals (1 ms)')
# Only integers
ax = fig.gca()
ax.xaxis.set_major_formatter(tick.FormatStrFormatter('%0g'))
ax.xaxis.set_ticks(np.arange(0, max_timesteps_per_episode + 1))
ax.set_autoscaley_on(False)
plt.plot(score_progress_fpa, marker='o', linestyle=':', color='b', label='FPA')
plt.plot(score_progress_cl, marker='D', linestyle='-', color='k', label='Proposed')
plt.xlim(xmin=0, xmax=max_timesteps_per_episode)
plt.axhline(y=SINR_MIN, xmin=0, color="red", linewidth=1.5)
plt.axhline(y=final_SINR_dB, xmin=0, color="green", linewidth=1.5)
plt.ylabel('Average DL Received SINR (dB)')
plt.title('Final Episode')
plt.grid(True)
plt.ylim(-8,10)
plt.legend()
plt.savefig('figures/episode_final_output.pdf', format="pdf")
plt.show(block=True)
plt.close(fig)