What I wanted to do. Matrix calculation to read necessary data from HSPICE and use it for machine learning.
I found a circuit simulator library called DeCiDa that runs on Python3. By using this library, it is possible to operate the circuit simulator directly from DeCiDa, but this time it was used only for data conversion.
Code
import numpy as np
from scipy import interpolate
from decida.Data import Data
d_sampling = 0.005
s_sampling = 0
e_sampling = 70
n_sampling = int((e_sampling - s_sampling)/d_sampling + 1)
time_r = np.linspace(s_sampling,e_sampling,n_sampling)
#read .tr0
d = Data()
d.read_hspice('xxx.tr0')
rows = d.nrows()
cols = d.ncols()
#delete unnecessary cols
leave_cols = [0,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,42]
n_leave_cols = len(leave_cols)
for i in range( cols ):
j = cols - i
if not (j in leave_cols) :
d.delete(j)
#d.delete( tuple of col_name )
#trans to numpy array
original_data = np.empty((n_leave_cols,rows))
for i in range(n_leave_cols):
original_data[i] = d.get(i)
#delete rows which is not change TIME
k = np.zeros(rows-1)
for i in range(rows-1):
j = rows-i-1
if (original_data[0,j] <= original_data[0,j-1] ):
original_data = np.delete(original_data,j,1)
# resamp
def resamp(y):
f = interpolate.interp1d(original_data[0], y, kind='linear')
return f(time_r)
r_data = np.empty((n_leave_cols,n_sampling))
r_data[0] = time_r
r_data[1:] = resamp(original_data[1:])
np.save('rData.npy',r_data)
It is possible to read the HSPICE simulation result on Python with the Data class of DeCiDa.
・ S_sampling, e_sampling The start and end times of the simulation in Spice. ・ D_sampling Sampling interval at read time.
d = Data() Data can be stored in d with d.read_hspice ('xxx.xxx'). There are also methods that support CSV and NGSpice.
I think there is a data array that is not necessary for calculation, d.delete(col) Can be erased with. col can be specified as an integer value or column name. You can erase multiple tuples by passing them. (I had the column names to keep this time, so I deleted them one by one)
You can get the column number from the column name with d.index ("zzz").
It is available at d.get (col). It seems that you can do it without using the for statement ……. It seems that it can be integrated with the unnecessary data eraser in the above chapter ....
Since the time axis of Spice data is not evenly spaced, resampling is performed with SciPy's interpolate. However, if the time data is fogged, resampling cannot be performed, so erasing is performed.
Then, it was linearly complemented and saved as Numpy data.
I explained the code to convert the analog circuit simulator and Python data. Currently, Spice is turned separately and the data is read from the result, but since DeCiDa itself has a method to operate Spice, I would like to consider improving efficiency by using that method.
Recommended Posts