-
Notifications
You must be signed in to change notification settings - Fork 0
/
1_AC_Sweep.py
226 lines (171 loc) · 7.36 KB
/
1_AC_Sweep.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
# -*- coding: utf-8 -*-
"""
Created on Mon Jun 6 15:19:30 2022
@author: nutchanonj
"""
# import PyLTSpice
from PyLTSpice.LTSpiceBatch import SimCommander
from PyLTSpice import LTSpice_RawRead
# import math function
from math import log10
from math import pi
from cmath import phase
# import
import numpy as np
from matplotlib import pyplot as plt
import time
# function to change complex number to abs
def to_dB(x):
return 20*log10(abs(x))
# function to change complex number to phase
def to_degree(x):
out = phase(x)/pi*180
if 0 < out < 180:
out = out - 360
return out
# function to receive raw data and generate result
def run_result(exp_name):
# read data from raw file
LTR = LTSpice_RawRead.RawRead(f"{exp_name}.raw")
print("Read Success")
freq = np.array(LTR.get_trace('frequency'), dtype=np.longdouble)
vout = np.array(LTR.get_trace('V(vout)'), dtype=np.clongdouble)
vout_abs = np.fromiter(map(lambda x: to_dB(x), vout), dtype=np.longdouble)
vout_phase = np.fromiter(map(lambda x: to_degree(x), vout), dtype=np.longdouble)
# # plot the result
# figure = plt.figure()
# ax = figure.add_subplot(111)
# plt.xscale("log")
# ax.plot(freq, vout_abs)
# figure.savefig(f'{exp_name}.png', dpi=300)
# find dc gain
dc_gain = vout_abs[0]
# find crossover frequency and phase margin
zero_crossing = int(np.where(np.diff(np.sign(vout_abs)))[0])
cross_freq = freq[zero_crossing]/1000000 # unit in MHz
phase_margin = vout_phase[zero_crossing] + 180
return dc_gain, cross_freq, phase_margin
# export result
def export_result(export_name, dc_gain_data, cross_freq_data, phase_margin_data, rows, columns):
# export dc gain
dc_gain_exp = np.reshape(dc_gain_data, (rows, columns))
np.savetxt(f"{export_name}_dc_gain.csv", dc_gain_exp, delimiter=",", fmt='%.2f')
# export crossover freq
cross_freq_exp = np.reshape(cross_freq_data, (rows, columns))
np.savetxt(f"{export_name}_cross_freq.csv", cross_freq_exp, delimiter=",", fmt='%.2f')
# export phase margin
phase_margin_exp = np.reshape(phase_margin_data, (rows, columns))
np.savetxt(f"{export_name}_phase_margin.csv", phase_margin_exp, delimiter=",", fmt='%.2f')
# params
corners = ["SS","TT","FF"]
temps = [0,35,70]
LTC = SimCommander("SIM_ac_sweep.asc", parallel_sims=8)
dc_gain_data = []
cross_freq_data = []
phase_margin_data = []
for corner in corners:
# run the simulation
LTC.add_instruction(f".include cmos018_{corner}.lib")
# change the power supply to 1.1 V
Vsupply = 1.1
LTC.set_component_value('Vsupply', Vsupply)
for Vin in [0.1, 0.3, 0.5, 0.7, 0.9]:
LTC.set_component_value('Vsource', Vin)
LTC.set_component_value('Voffset', 0)
for temp in temps:
LTC.add_instruction(f".temp {temp}")
exp_name = f"SIM_ac_sweep_Vin_{corner}_{Vsupply:.1f}_{Vin:.1f}_{temp:.1f}"
LTC.run(run_filename=f"{exp_name}.net")
LTC.remove_instruction(f".temp {temp}")
for Voffset in [-0.45, 0, 0.45]:
Vin = 0.55
Vout = Vin + Voffset
LTC.set_component_value('Vsource', Vin)
LTC.set_component_value('Voffset', Voffset)
for temp in temps:
LTC.add_instruction(f".temp {temp}")
exp_name = f"SIM_ac_sweep_Vout_{corner}_{Vsupply:.1f}_{Vout:.1f}_{temp:.1f}"
LTC.run(run_filename=f"{exp_name}.net")
LTC.remove_instruction(f".temp {temp}")
# change the power supply to 1.8 V
Vsupply = 1.8
LTC.set_component_value('Vsupply', Vsupply)
for Vin in [0.1, 0.4, 0.7, 1.0, 1.3, 1.6]:
LTC.set_component_value('Vsource', Vin)
LTC.set_component_value('Voffset', 0)
for temp in temps:
LTC.add_instruction(f".temp {temp}")
exp_name = f"SIM_ac_sweep_Vin_{corner}_{Vsupply:.1f}_{Vin:.1f}_{temp:.1f}"
LTC.run(run_filename=f"{exp_name}.net")
LTC.remove_instruction(f".temp {temp}")
for Voffset in [-0.8, -0.4, 0, 0.4, 0.8]:
Vin = 0.9
Vout = Vin + Voffset
LTC.set_component_value('Vsource', Vin)
LTC.set_component_value('Voffset', Voffset)
for temp in temps:
LTC.add_instruction(f".temp {temp}")
exp_name = f"SIM_ac_sweep_Vout_{corner}_{Vsupply:.1f}_{Vout:.1f}_{temp:.1f}"
LTC.run(run_filename=f"{exp_name}.net")
LTC.remove_instruction(f".temp {temp}")
# remove old instruction
LTC.remove_instruction(f".include cmos018_{corner}.lib")
LTC.wait_completion()
for corner in corners:
# change the power supply to 1.1 V
Vsupply = 1.1
for Vin in [0.1, 0.3, 0.5, 0.7, 0.9]:
for temp in temps:
exp_name = f"SIM_ac_sweep_Vin_{corner}_{Vsupply:.1f}_{Vin:.1f}_{temp:.1f}"
dc_gain, cross_freq, phase_margin = run_result(exp_name)
dc_gain_data.append(dc_gain)
cross_freq_data.append(cross_freq)
phase_margin_data.append(phase_margin)
rows = 5;
columns = 3;
export_name = f"SIM_ac_sweep_Vin_{corner}_{Vsupply:.1f}"
export_result(export_name, dc_gain_data, cross_freq_data, phase_margin_data, rows, columns)
dc_gain_data = []; cross_freq_data = []; phase_margin_data = [];
for Voffset in [-0.45, 0, 0.45]:
Vin = 0.55
Vout = Vin + Voffset
for temp in temps:
exp_name = f"SIM_ac_sweep_Vout_{corner}_{Vsupply:.1f}_{Vout:.1f}_{temp:.1f}"
dc_gain, cross_freq, phase_margin = run_result(exp_name)
dc_gain_data.append(dc_gain)
cross_freq_data.append(cross_freq)
phase_margin_data.append(phase_margin)
rows = 3;
columns = 3;
export_name = f"SIM_ac_sweep_Vout_{corner}_{Vsupply:.1f}"
export_result(export_name, dc_gain_data, cross_freq_data, phase_margin_data, rows, columns)
dc_gain_data = []; cross_freq_data = []; phase_margin_data = [];
# change the power supply to 1.8 V
Vsupply = 1.8
LTC.set_component_value('Vsupply', Vsupply)
for Vin in [0.1, 0.4, 0.7, 1.0, 1.3, 1.6]:
for temp in temps:
exp_name = f"SIM_ac_sweep_Vin_{corner}_{Vsupply:.1f}_{Vin:.1f}_{temp:.1f}"
dc_gain, cross_freq, phase_margin = run_result(exp_name)
dc_gain_data.append(dc_gain)
cross_freq_data.append(cross_freq)
phase_margin_data.append(phase_margin)
rows = 6;
columns = 3;
export_name = f"SIM_ac_sweep_Vin_{corner}_{Vsupply:.1f}"
export_result(export_name, dc_gain_data, cross_freq_data, phase_margin_data, rows, columns)
dc_gain_data = []; cross_freq_data = []; phase_margin_data = [];
for Voffset in [-0.8, -0.4, 0, 0.4, 0.8]:
Vin = 0.9
Vout = Vin + Voffset
for temp in temps:
exp_name = f"SIM_ac_sweep_Vout_{corner}_{Vsupply:.1f}_{Vout:.1f}_{temp:.1f}"
dc_gain, cross_freq, phase_margin = run_result(exp_name)
dc_gain_data.append(dc_gain)
cross_freq_data.append(cross_freq)
phase_margin_data.append(phase_margin)
rows = 5;
columns = 3;
export_name = f"SIM_ac_sweep_Vout_{corner}_{Vsupply:.1f}"
export_result(export_name, dc_gain_data, cross_freq_data, phase_margin_data, rows, columns)
dc_gain_data = []; cross_freq_data = []; phase_margin_data = [];