-
Notifications
You must be signed in to change notification settings - Fork 1
/
pyzmatch.py
228 lines (185 loc) · 7.42 KB
/
pyzmatch.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
# Standard Libraries
import math
import cmath
import numbers
# Major Libraries
import matplotlib.pyplot as plt
import numpy as np
########Global Viarables########
FREQ = 1 # frequency in GHz
################################
########Class Def###############
class Cable:
def __init__(self, loss, velocity, length, impedance=50):
# impedance in Ohm, loss in dB/100ft, velocity in c, length in cm
# self.loss is transformed into /m
self.impedance = impedance
self.loss = - math.log (10 ** (- loss / 20.0)) / 30.5
self.vel = velocity
self.length = length / 100.0
self.nexts = []
def alpha(self):
global FREQ
alpha = self.loss * math.sqrt(FREQ)
return alpha
def beta(self):
global FREQ
beta = 2 * math.pi * FREQ * (10 ** 9) / (self.vel * 3 * (10 ** 8))
return beta
def gamma(self):
gamma = complex(self.alpha(),self.beta())
return gamma
def set_length(self, length):
self.length = length / 100
def set_nexts(self, next_element):
self.nexts.append(next_element)
def network_impedance(self):
if self.nexts == []:
impedance = Z_cable(self)
else:
admittance = 0
for i in self.nexts:
admittance += 1.0/i.network_impedance()
load = 1.0/admittance
impedance = Z_distant(load, self)
return impedance
class Lumped_Element:
def __init__(self, resistance, capacitance, inductance):
# PARALLEL resistance in Ohm, Capacitance in pF, inductance in nH
self.resistance = resistance
self.capacitance = capacitance
self.inductance = inductance
self.nexts = []
def self_impedance(self):
global FREQ
if self.capacitance != 0:
impedance = complex(self.resistance, 2 * math.pi * FREQ * self.inductance - 1.0 / ( 2 * math.pi * FREQ * self.capacitance * 10 ** (-3)))
else:
impedance = complex(self.resistance, 2 * math.pi * FREQ * self.inductance)
return impedance
def set_r(self, resistance):
self.resistance = resistance
def set_c(self, capacitance):
self.capacitance = capacitance
def set_l(self, inductance):
self.inductance = inductance
def set_nexts(self, next_element):
self.nexts.append(next_element)
def network_impedance(self):
if (self.nexts == []):
impedance = self.self_impedance()
else:
impedance = 0
admittance = 0
for i in self.nexts:
admittance += 1.0/i.network_impedance()
impedance = 1.0 / admittance + self.self_impedance()
return impedance
###############################
#######Function Def############
def V_reflection(impedance1, impedance2):
#voltage reflection when wave goes from media1 with impedance1 to media2 with impedance2
tau = complex(impedance2 - impedance1) / complex(impedance2 + impedance1)
return tau
def Z_distant(Z_load, cable):
#calculate the impedance seen at one end of a cable with the other end connecting to a load with impedance Z_load
Z_distant = cable.impedance * (1 + V_reflection(cable.impedance, Z_load) * cmath.exp(-2 * cable.gamma() * cable.length)) / (1 - V_reflection(cable.impedance, Z_load) * cmath.exp(-2 * cable.gamma() * cable.length))
return Z_distant
def Z_cable(cable):
#calculate the impedance seen at one end of a cable with the other end open
#the same effect with Z_distant(10 ** 10, cable)
Z_cable = cable.impedance * (1 + cmath.exp(-2 * cable.gamma() * cable.length)) / (1 - cmath.exp(-2 * cable.gamma() * cable.length))
return Z_cable
def change_parameter(target, parameter_name, new_value):
global FREQ
if (parameter_name == "frequency"):
FREQ = new_value
else:
dict_short = {"resistance":"r", "capacitance":"c", "inductance":"l", "length":"length"}
getattr(target, "set_"+dict_short[parameter_name])(new_value)
def one_parameter_search(target, parameter_name, parameter_range, END, tip):
S11_list = []
dS11_list = []
S11_linear = []
f = open("./one_parameter_search_output.txt","w")
for i in parameter_range:
f.write(str(i)+",")
change_parameter(target, parameter_name, i)
#calculate S11
S11 = V_reflection(50, END.network_impedance())
S11_linear.append(abs(S11))
f.write(str(abs(S11))+"\n")
S11_dB = 20 * math.log(abs(S11), 10)
S11_list.append(S11_dB)
#calculate delta S11
tip.set_c(tip.capacitance+10**-6)
dS11 = abs(V_reflection(50, END.network_impedance()) - S11)
dS11_list.append(dS11)
tip.set_c(tip.capacitance-10**-6)
f.close()
#Plot the simulation results
fig, ax1 = plt.subplots()
ax1.plot(parameter_range, S11_list, 'b-')
ax1.set_xlabel(parameter_name)
# Make the y-axis label and tick labels match the line color.
ax1.set_ylabel('S11', color='b')
for tl in ax1.get_yticklabels():
tl.set_color('b')
ax2 = ax1.twinx()
ax2.plot(parameter_range, dS11_list, 'r-')
ax2.set_ylabel('Delta S11', color='r')
for tl in ax2.get_yticklabels():
tl.set_color('r')
plt.show()
return S11_list, dS11_list
def two_parameter_search(target1, parameter_name1, parameter_range1, target2, parameter_name2, parameter_range2, END, tip):
S11_map = []
dS11_map = []
for i in parameter_range1:
change_parameter(target1, parameter_name1, i)
S11_line = []
dS11_line = []
for j in parameter_range2:
change_parameter(target2, parameter_name2, j)
#S11
S11 = V_reflection(50, END.network_impedance())
S11_dB = 20 * math.log(abs(S11), 10)
S11_line.append(S11_dB)
#delta S11
tip.set_c(tip.capacitance+10**-6)
dS11 = abs(V_reflection(50, END.network_impedance()) - S11)
dS11_line.append(dS11)
tip.set_c(tip.capacitance-10**-6)
S11_map.append(S11_line)
dS11_map.append(dS11_line)
from mpl_toolkits.axes_grid1 import make_axes_locatable
f, (ax1, ax2) = plt.subplots(ncols=2)
im1 = ax1.imshow(S11_map, aspect='auto', extent=[parameter_range2[0], parameter_range2[-1], parameter_range1[0], parameter_range1[-1]], origin='lower')
ax1.set_title('S11')
divider1 = make_axes_locatable(ax1)
cax1 = divider1.append_axes("right", size="3%", pad=0.1)
cbar1 = plt.colorbar(im1, cax=cax1)
im2 = ax2.imshow(dS11_map, aspect='auto', extent=[parameter_range2[0], parameter_range2[-1], parameter_range1[0], parameter_range1[-1]], origin='lower')
ax2.set_title('dS11')
divider2 = make_axes_locatable(ax2)
cax2 = divider2.append_axes("right", size="3%", pad=0.1)
cbar2 = plt.colorbar(im2, cax=cax2)
plt.show()
###############################
##########Example##############
def test_stub_tuning():
global FREQ
#Define Z-Match Config.
END = Lumped_Element(0, 0, 0)
Stub = Cable(20, 0.7, 4.5)
QWC = Cable(73, 0.7, 4.2)
tip = Lumped_Element(4, 1, 2)
END.set_nexts(Stub)
END.set_nexts(QWC)
QWC.set_nexts(tip)
#Search!
two_parameter_search(FREQ, "frequency", np.linspace(0.7, 1.1, 100), Stub, "length", np.linspace(4, 7, 100), END, tip)
Stub.set_length(4.57)
one_parameter_search(FREQ, "frequency", np.linspace(0.9, 1.1, 200), END, tip)
if __name__ == '__main__':
test_stub_tuning()