Tuesday, August 10, 2010

Handy FIR filter inherits from list:


class filter(list):

"""real valued F I R filter with memory"""

def __init__(self, coeffs, data=None):
list.__init__([float(item) for item in coeffs])
if data:
assert len(data) == len(coeffs)
self.data =[float(datum) for datum in data]
else:
self.data = len(self) * [0.]

def __call__(self,value=0.):
return self.filter(value)

def output(self):
return sum(map(mul, self.coeffs, self.data))

def update(self,value):
self.pop(0)
self.append(value)

def filter(self, value=0.):
value = float(value)
self.update(value)
return self.output()

def innovate(self):
return self.filter(white())

3 comments:

Michael Tobis said...

Aargh.

Michael Tobis said...

I went to some trouble to preserve the whitespace. It looked fine in the preview.

Michael Tobis said...

pre tag does it