[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
Re: gEDA-user: Speaker SPICE modeling with gschem and ng-spice/gnucap
Hello Hannu,
I have a python module that can reads ngspice data and export arbitrary vector to PWL source. I'm using it for quite long time.
If You have any questions then please let me know.
I remember that I was using it to auto optimize a circuit: run ngspice from python, import it's data, perform a computation of a new waveform, export it then run ngspice again and so on. I think that I might be able to find this work as an example if You are interested.
I hope this will help,
Michael W.
Dnia 12 wrzeÅnia 2011 23:09 Hannu Vuolasaho <vuokko@xxxxxxx> napisaÅ(a):
>
> Hi!
>
> I have been playing with one guitar amplifier project for a while and so far the amplifier design has been more or less copy and paste and simulate and guess from graphs. However I bumped in net this blog post
>
> http://nordicnerd.blogspot.com/2011/08/active-speakers-spice-with-actual-audio.html
>
> Is it possible to do same thing? Input wav to simulator and get speaker's output and hear it? I know it's not perfect but it could be very helpful. Has someone done this before and provide some hints, examples or links?
>
> Best regards,
> Hannu Vuolasaho
>
>
>
> _______________________________________________
> geda-user mailing list
> geda-user@xxxxxxxxxxxxxx
> http://www.seul.org/cgi-bin/mailman/listinfo/geda-user
>
#!/usr/bin/env python
#NgSpice import / export
import numpy, pylab
import string, os, tempfile
def fromng(file):
"""
Import ngspice file to dictionary
tuple = fromng(file)
"""
ngimport = {}
fng = open(file, 'r') #opens file
while True:
line = fng.readline()
line = line.split()
ltype = string.lower(line[0])
if ltype.find("title") != -1:
ngimport.update(title = string.join(line[1:])) #title
elif ltype.find("date") != -1:
ngimport.update(date = string.join(line[1:])) #date
elif ltype.find("plotname") != -1:
ngimport.update(plot_name = string.join(line[1:])) #plot name
elif ltype.find("flags") != -1:
ngimport.update(flags = string.join(line[1:])) #flags
elif ltype.find("no.") != -1:
if string.lower(line[1]).find("variables") != -1:
ngimport.update(no_variables = int(line[2])) #number of variables
elif string.lower(line[1]).find("points") != -1:
ngimport.update(no_points = int(line[2])) #number of points
elif ltype.find("variables") != -1:
vnames = []
for i in range(ngimport["no_variables"]):
line = fng.readline()
vnames.append(line.split()[1:]) #add variable name
elif ltype.find("binary") != -1:
break #now jump to binary data
dat = numpy.fromfile(fng, 'd', ngimport["no_variables"]*ngimport["no_points"], '') #read all binary data
fng.close()
for i, var in enumerate(vnames): #create output tuple keys
ngimport.update({var[0]: dat[i:len(dat):ngimport["no_variables"]]})
print var
return ngimport
#---------------------------------------------------------------------------------------------------
def fromHP(file, comment='%', delim=' '):
"""Reads file from HP oscilloscope data file"""
data = pylab.load(file, comment,delim)
chans = data.shape[1] #load file and extract number of collumns
scopeimport = {"time": data[:,0]} #write time
scopeimport["time"] = scopeimport["time"] - min(scopeimport["time"]) #fix time (move from negative)
for i in range(1, chans):
scopeimport.update({'ch%1d'%i: data[:,i]}) #add channels entry
print 'ch%1d'%i
print "Points %d"%len(scopeimport['ch1'])
return scopeimport
#----------------------------------------------------------------------------------------------------
def fromMET(file):
"""Reads file Metrix scope TXT file"""
fmet = open(file, 'r')
dt = float(fmet.readline().split()[1])
unit = fmet.readline()
fmet.close()
scopeimport = {"ch1": pylab.load(file, skiprows=2)}
scopeimport["time"] = pylab.linspace(0, dt*2500, num=2500, endpoint=False)
print "Points %d, %s"%(len(scopeimport["ch1"]), unit)
return scopeimport
#----------------------------------------------------------------------------------------------------
def topwl(file, time, data, subckt="pwlsource"):
"""Export data array (time, values) to PWL ngspice source"""
fpwl = open(file, 'w')
dstr = "*crated by topwl\n*number of points: %d\n.SUBCKT "%len(time) +subckt+ " 1 2\nV1 1 2 PWL\n" #header
for t, d in zip(time, data):
dstr = dstr + "+" + str(t) + " " + str(d) + "\n"
dstr = dstr + ".ENDS\n"
fpwl.write(dstr)
fpwl.close()
print "Written %d points to %s"%(len(time),file)
return
#----------------------------------------------------------------------------------------------------
def runng(cirfile):
"""Runs ngspice and loads returned data"""
tmpcir = tempfile.mkstemp()
fcir = open(cirfile, 'r')
line = "*\n"
iswrite = False
while line:
line = fcir.readline()
if line[0] == '*':
continue
if line.find("write") != -1: #find where write directive is
iswrite = True
outfile = line.split()[1] #assume that write is first word
break
print "Running ngspice.... simulating %s"%cirfile
os.system("libprm -n %s %s"%(cirfile, tmpcir[1])) #get libs and subckts
os.system("ngspice %s"%tmpcir[1])
os.close(tmpcir[0])
os.remove(tmpcir[1])
if iswrite == True:
print "Reading results from %s"%outfile
out = fromng(outfile)
return out
print "No write directive found"
return
#----------------------------------------------------------------------------------------------------
if __name__ == "__main__":
dat = fromng("test.dat")
print dat.keys()
print len(dat)
_______________________________________________
geda-user mailing list
geda-user@xxxxxxxxxxxxxx
http://www.seul.org/cgi-bin/mailman/listinfo/geda-user