-
Notifications
You must be signed in to change notification settings - Fork 108
/
Copy patharray_analysis.py
304 lines (255 loc) · 9.65 KB
/
array_analysis.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# -*- coding: utf-8 -*-
"""
Created on Fri Oct 19 19:24:49 2019
@author: mlin
"""
import matplotlib.pyplot as plt
from collections import OrderedDict
import numpy as np
import copy
import scipy.optimize as optimize
import json
class ffd():
def __init__(self, ffd_file, incident_Power_W=1):
self.incident_Power_W=incident_Power_W
with open(ffd_file) as f:
self.theta=[int(i) for i in f.readline().split()]
self.phi=[int(i) for i in f.readline().split()]
f.readline()
self.frequency=float(f.readline().split()[1])
theta_range=np.linspace(*self.theta)
phi_range= np.linspace(*self.phi)
self.Ntheta=len(theta_range)
self.Nphi=len(phi_range)
self._dtheta=theta_range[1]-theta_range[0]
self._dphi=phi_range[1]-phi_range[0]
self._theta=np.array([i for i in theta_range for j in phi_range])
EF=np.loadtxt(ffd_file, skiprows=4)
Etheta=np.vectorize(complex)(EF[:,0], EF[:,1])
Ephi=np.vectorize(complex)(EF[:,2], EF[:,3])
self._EF=np.column_stack((Etheta, Ephi))
self._calculate()
def __eq__(self, other):
if self.theta!=other.theta:
return False
if self.phi!=other.phi:
return False
if self.frequency!=other.frequency:
return False
return True
def __add__(self, other):
if self==other:
x=copy.deepcopy(self)
x._EF+=other._EF
x.incident_Power_W+=other.incident_Power_W
x._calculate()
return x
def getE(self, theta, phi):
index=(theta/self._dtheta)*self.Nphi+phi/self._dphi
return self._EF[int(index),:]
def getGain(self, theta, phi):
index=(theta/self._dtheta)*self.Nphi+phi/self._dphi
return self.realized_gain[int(index)]
def shiftPhase(self, angle):
x=copy.deepcopy(self)
x._EF=x._EF*np.exp(1j*np.radians(angle))
x._calculate()
return x
def _calculate(self):
pd=np.sum(np.power(np.absolute(self._EF), 2),1)/377/2
self.U=max(pd)
self.cell_area=np.radians(self._dtheta)*np.radians(self._dphi)*np.sin(np.radians(self._theta))
#self.radiated_power=sum(self.cell_area*pd)
#uniform_power=self.radiated_power/sum(self.cell_area)
#self.peak_directivity=self.U/uniform_power
self.realized_gain=10*np.log10(pd/(self.incident_Power_W/4/np.pi))
self.peak_realized_gain=max(self.realized_gain)
def compare(self, other):
x=np.abs(self._EF)
dx=np.abs(other._EF-self._EF)
return np.amax(dx/x)
def __call__(self, mag, phase):
x=copy.deepcopy(self)
x._EF=np.sqrt(mag)*np.exp(1j*np.radians(phase))*self._EF
x.incident_Power_W=mag
x._calculate()
return x
def getCDF(self):
x, y=[], []
accumulated_area=0
for gain, area in sorted(zip(self.realized_gain, self.cell_area)):
x.append(gain)
accumulated_area+=area
y.append(accumulated_area)
return x, y/y[-1]
def plotRealizedGain(self):
plt.figure(figsize=(8, 4))
size=(self.theta[2], self.phi[2])
gain_map=self.realized_gain.reshape(size)
plt.title('Map of Realized Gain(dB)')
plt.xlabel('Phi (degree)')
plt.ylabel('Theta (degree)')
maxV=np.max(gain_map)
[row, col] = np.where(gain_map==maxV)
plt.plot(col, row, 'w*')
plt.annotate(round(maxV,3), (col+3, row+3), color='white')
plt.imshow(gain_map, cmap='jet')
plt.colorbar()
CS=plt.contour(gain_map)
plt.clabel(CS, inline=1, fontsize=10)
class aggregatebeam():
def __init__(self, *args):
self.args=args
self.max_gain=np.copy(args[0].realized_gain)
self.beam_occupy=0*np.copy(self.max_gain)
for beamid, i in enumerate(self.args[1:], 1):
for n in range(len(self.max_gain)):
if i.realized_gain[n]>self.max_gain[n]:
self.beam_occupy[n]=beamid
self.max_gain[n]=i.realized_gain[n]
self.map_size=(args[0].theta[2], args[0].phi[2])
def plotCDF(self):
x, y=[], []
accumulated_area=0
for gain, area in sorted(zip(self.max_gain, self.args[0].cell_area)):
x.append(gain)
accumulated_area+=area
y.append(accumulated_area)
plt.figure()
plt.title('Cumulative Distribution Function')
plt.xlabel('Realized Gain (dB)')
plt.ylabel('CDF')
plt.grid(True)
plt.plot(x, y/y[-1])
plt.show()
return (x, y/y[-1])
def plotGainMap(self):
gain_map=self.max_gain.reshape(self.map_size)
plt.figure(figsize=(8, 4))
plt.title('Gain Map(dB)')
plt.xlabel('Phi (degree)')
plt.ylabel('Theta (degree)')
maxV=np.max(gain_map)
[row, col] = np.where(gain_map==maxV)
plt.plot(col, row, 'w*')
plt.annotate(round(maxV,3), (col+3, row+3), color='white')
plt.imshow(gain_map, cmap='jet')
plt.colorbar()
CS=plt.contour(gain_map)
plt.clabel(CS, inline=1, fontsize=10)
def plotBeamMap(self):
beam_map=self.beam_occupy.reshape(self.map_size)
plt.figure(figsize=(8, 4))
plt.title('Beam Map')
plt.xlabel('Phi (degree)')
plt.ylabel('Theta (degree)')
plt.imshow(beam_map, cmap='rainbow')
plt.colorbar()
plt.contour(beam_map)
class GainOptimizer():
def __init__(self, ffds):
self.ffds=ffds
def _factory(self, theta, phi):
def weighting(x):
U, V=0+0j, 0+0j
for i, j in zip(self.ffds,x):
U+=i.getE(theta, phi)[0]*np.exp(1j*np.radians(j))
V+=i.getE(theta, phi)[1]*np.exp(1j*np.radians(j))
return -np.sqrt(np.power(np.absolute(U),2)+np.power(np.absolute(V),2))
return weighting
def run(self, resolution=20):
theta=range(0, 180+resolution, resolution)
phi=range(0, 360+resolution, resolution)
initial_guess=[0]*len(self.ffds)
self.phase_table=OrderedDict()
self.area_gain=[]
for i in theta:
for j in phi:
if (i==0 and j!=0) or (i==180 and j!=0):
continue
weighting=self._factory(i, j)
result= optimize.minimize(weighting, initial_guess)
if result.success:
ffdsum=self.ffds[0].shiftPhase(result.x[0])
initial_guess=[i%360 for i in result.x]
for k, m in zip(self.ffds[1:], result.x[1:]):
ffdsum+=k.shiftPhase(m)
maxGain=ffdsum.getGain(i,j)
self.phase_table[f'{i},{j}']=([i%360 for i in result.x], maxGain)
self.area_gain.append((maxGain, np.sin(np.radians(i))))
print(f'{i:4d},{j:4d}: {maxGain:.3f}(dB)')
print(result.x)
else:
self.phase_table[(i,j)]='Optimization Failed'
def saveJSON(self, file):
with open(file, 'w') as f:
json.dump(self.phase_table, f, indent=2)
def plotOptimalCDF(self):
A=0
x, y=[], []
for i,j in sorted(self.area_gain):
x.append(i)
A+=j
y.append(A)
y=[i/y[-1] for i in y]
plt.figure()
plt.title('Optimal CDF')
plt.xlabel('Realized Gain (dB)')
plt.ylabel('CDF')
plt.grid(True)
plt.plot(x, y)
plt.show()
return (x, y)
def plotCDFtable(table, png=None):
'''table={'A':(gain , cdf), 'B':(gain, cdf), }'''
plt.figure()
plt.title('Cumulative Distribution Function')
plt.xlabel('Realized Gain (dB)')
plt.ylabel('CDF')
plt.grid(True)
for i in table:
plt.plot(*table[i], label=i)
plt.legend()
if png:
plt.savefig(png)
plt.show()
#%%
path='D:/demo3/28000000000'
x1=ffd(path+'/Module_0_Bump_h1.ffd')
x2=ffd(path+'/Module_0_Bump_h2.ffd')
x3=ffd(path+'/Module_0_Bump_h3.ffd')
x4=ffd(path+'/Module_0_Bump_h4.ffd')
pt=GainOptimizer([x1, x2, x3, x4])
pt.run(10)
pt.saveJSON(path+'/phase.json')
#%%
table={}
table['optimal']=pt.plotOptimalCDF()
plotCDFtable(table)
#%%
beam0=x1(1,0) +x2(1,0) +x3(1,0) +x4(1,0)
beam0.plotRealizedGain()
beam1=x1(1,0) +x2(1,60) +x3(1,120) +x4(1,180)
beam1.plotRealizedGain()
beam2=x1(1,0) +x2(1,120) +x3(1,240) +x4(1,360)
beam2.plotRealizedGain()
beam3=x1(1,0) +x2(1,-60) +x3(1,-120) +x4(1,-180)
beam3.plotRealizedGain()
beam4=x1(1,0) +x2(1,-120) +x3(1,-240) +x4(1,-360)
beam4.plotRealizedGain()
z0=aggregatebeam(beam0)
table['0']=z0.plotCDF()
z1=aggregatebeam(beam1)
table['1']=z1.plotCDF()
z2=aggregatebeam(beam2)
table['2']=z2.plotCDF()
z3=aggregatebeam(beam3)
table['3']=z3.plotCDF()
z4=aggregatebeam(beam4)
table['4']=z4.plotCDF()
z01234=aggregatebeam(beam0, beam1, beam2,beam3,beam4)
#%%
table['01234']=z01234.plotCDF()
plotCDFtable(table)
z01234.plotGainMap()
z01234.plotBeamMap()