Showing posts with label dsp. Show all posts
Showing posts with label dsp. Show all posts

Friday, February 11, 2011

Arbitrary voltage source in LTSpice

Here is a post I found when searching for a way to use some data as an input to LTSpice to test a filter.

http://ltspicelabs.blogspot.com/2006/10/using-wav-files-for-io-and-transient.html

This talks about taking a .wav file as input. Which is fine if you are doing audio. But what if you want to generate a .wav file based on, say, a mathematical model?

Python has a nice wave module. So you could write a short script like this:

# Send data to output .wave file
# filename = string
# fsys = integer system clock rate
# data = list of integers
def writewave(filename, fsys, data):
oversample_factor = 8
offset_factor = 16384
scale_factor = 60 # 8 bit in to ~15 bit out

w = wave.open(filename, 'wb')
w.setnchannels(1)
w.setsampwidth(2)
w.setframerate(fsys * oversample_factor) # need to oversample to generate the steppy DAC output
w.setnframes(len(data))
d = '' # string to hold byte values in little-endian order
for i in data:
ii = scale_factor * i + offset_factor
rl = int(ii) & 255
rh = (int(ii) >> 8) & 255
for j in xrange(oversample_factor):
d = d + chr(rl) + chr(rh) # little-endian
w.writeframes(d)
w.close()

This will write out your data to a .wav file that can then be used in LTSpice. The example here has a sample width of 2, which would be two bytes, or signed 16 bits. I was modeling a signed 8-bit DAC output, so I scaled it up and offset it to get what I wanted. Also I put in an oversampling factor to get a more step-wise response instead of a linear ramp between sample points.

Don't forget "import wave" in the Python script!

Monday, February 22, 2010

Python Filter Design

I've never really done DSP as part of my day job. I've been to a few training classes and have some texts that just scratch the surface of filter design, modulation, and so on. Never really touched Matlab either. But I've gotten into Python, so I found out there are some interesting modules that handle DSP functions and the related math.

So how hard would it be to come up with a FIR filter that meets certain performance requirements? Well, you can play around with the Python SciPy and NumPy libraries and check the performance of your filter in a few easy lines.


"""
Design a FIR filter and show the frequency response in
a few easy lines
"""

from scipy import signal
from pylab import *

"""
Window types: boxcar, triang, blackman, hamming,
hanning, bartlett, parzen, bohman, blackmanharris,
nuttall, barthann, kaiser (needs beta),
gaussian (needs std),
general_gaussian (needs power, width),
slepian (needs width)
"""

def dbPlot(w, h):
plot(w, 20 * log(abs(h)) / log(10))

# firwin(number of taps, cutoff relative to Nyquist
# rate, window type)
b = signal.firwin(31, 0.4, window='nuttall')

# freqz(list of zeroes, list of poles)
(w, h) = signal.freqz(b, 1)

plot(w, log(abs(h))/log(10)*20)
show()


This gives me a nice graphic:



You can play around with different windows, as shown in the code comments, or add taps as needed to get the right transition and attenuation.