Saturday, February 27, 2010

LT Spice model of Wierd Sound Generator with wav file output

Children seem to like loud annoying toys so I am building my son a Wierd Sound Generator. This thing is basically an electronic sound synthesizer, here is an example and another. It is a clever combination of Schmitt triggers and a filter.




Before releasing any solder smoke I judged it prudent to look at part of the circuit in LT Spice.  LT Spice has a model for the Schmidtt trigger allowing one to simulate part of the WSG. The predicted wave form can be exported and convert it into .wav or .mp3 file for listening pleasure.



LT Spice model

Sample sound

Note this is not particularity harmonious or pleasant to listen to in any way!

To export a voltage wave form from LT Spice click in the plot window and use File->Export. The venerable SciPy can be used to convert the LT Spice waveform .txt file into a .wav file.
from numpy import *
from scipy.io.wavefile import write 

lines = open('wsg1.txt', 'r').readlines()
t=[];v=[]
for line in lines[1:]:
   line.strip()
   tv, vv = line.split()
   t.append(float(tv))
   v.append(float(vv))
t = array(t)
v = array(v)
v -=v.min(); v/=v.max() # normalize
a = array(v*2**16, dtype=int16) # convert to integer
datarate = int(len(t)/t[-1])
print datarate, "samples per second"

#from pylab import *
#plot(t,a)
#show()

write('t.wav',datarate,a)
This depends on the latest version of SciPy (0.8.0). Another approach to generating a wav file not using SciPy can by found here