-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSonnet_Auto.py
160 lines (99 loc) · 4.7 KB
/
Sonnet_Auto.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
# -*- coding: utf-8 -*-
"""
Created on Mon Oct 22 15:13:01 2021
@author: Cathal McAleer
Description:
This programme allows the user to import a .csv file from the \
EM simulation software Sonnet, extracts and prints;\
Parameter titles, Resonant Frequency and Quality Factor from the file.
"""
#............................................................................#
#Importing libraries
import csv
from scipy.interpolate import splev, splrep, sproot
import numpy as np
import matplotlib.pyplot as plt
#...........................................................................#
#Lists to append to
S11_Mag_loop=[] #All S11 Magnitudes in .csv file
S11_Phase_loop=[] #All S11 Phases in .csv file
S21_Mag_loop = [] #All S21 Magnitudes in .csv file
S21_Phase_loop=[] #All S21 Phases in .csv file
all_Freq=[] #All frequencies in .csv file
Param=[] #List for reading all dimension parameters
ac_res_freq = [] #Actual resonant frequencies list of each sweep
title=[] #Actual Parameter titles for each sweep
forloop=[0] #number of frequencies being scanned
Qual_Fac=[] #list to append Quality Factor values to for each parameter
#............................................................................#
#Only input into code
print("\nThis programme allows the user to import a .csv file from Sonnet\
and exports the parameters swept, resonant frequencies and corresponding\
quality factor from the file.")
#...........................................................................#
#File directory where file is imported
with open("C:\\Users\\scath\\Desktop\\VariationCapacitor.csv",\
"r") as csvfile:
csvReader=csv.reader(csvfile)
for row in csvReader:
try:
Param.append(row[0]) #Read entire first row
all_Freq.append(float(row[0])) #Read in first row of values
S11_Mag_loop.append(float(row[1]))
S11_Phase_loop.append(float(row[2]))
S21_Mag_loop.append(float(row[5]))
S21_Phase_loop.append(float(row[6]))
except ValueError: #for Freq and mag. If row isn't a float, continue
continue
End_Freq = max(all_Freq)
#looping through each value of frequencies
for i in range(len(all_Freq)):
a=all_Freq[i]
if a==End_Freq: #if a = end of frequency sweep.
#Get's # of frequencies swept by Sonnet
forloop.append(i) #append to forloop list
else:
continue
#number of sweeps performed = elemenets in forloop list - 1
num_of_sweeps=len(forloop)-1
#Main prog
for n in range(0,num_of_sweeps-1):
b=all_Freq[forloop[n]+1:forloop[n+1]+1]
S11_Mag = S11_Mag_loop[forloop[n]+1:forloop[n+1]+1]
S11_Phase = S11_Phase_loop[forloop[n]+1:forloop[n+1]+1]
S21_Mag = S21_Mag_loop[forloop[n]+1:forloop[n+1]+1]
S21_Phase = S21_Phase_loop[forloop[n]+1:forloop[n+1]+1]
c=forloop[n]+9*n+8 #Skipping Comments in .csv file
#plt.plot(S21_Mag,S21_Phase, label=label1)
if c==8:
start=Param[8]
title.append(start)
else:
start=Param[forloop[n]+9*n+9]
title.append(start)
minimum_db = min(S21_Mag) #minimum S21 corresponding to res. frequency
HM=(S21_Mag[0]+minimum_db)/2 #Half-width of dip
#Spline interpolate data to obtain roots at
tck=splrep(b, S21_Mag) # spline coeff returned as a tuple
y_output = splev(b, tck)
#Move data from y=HM to y=0 and solve roots for x
tck2=splrep(b, y_output-HM)
x=sproot(tck2)#Roots (I.e points at Half-Max)
FWHM=x[1]-x[0] #Full width-Half-Maximum
#Finding corresponding res. frequency in list
for t in range(len(S21_Mag)):
if S21_Mag[t]==minimum_db:
#Append all resonant frequencies to empty list
ac_res_freq.append(b[t]*1e3)
#Append quality factor (res.freq/Fullwidth-halfmax)
Qual_Fac.append(b[t]/FWHM)
else: #else, continue code
continue
#.........................Printing Table of Results.........................#
#Print titles
print("\n{:<8}{:>25}{:>20}".format("Parameter(\u03BCm)", "Res.Freq(GHz)"\
,"Quality Factor"))
for i in range(len(ac_res_freq)):
print("\n{:<8}{:>20.4f}{:>20.2f}".format(title[i], ac_res_freq[i]\
,Qual_Fac[i]))
#...........................................................................#