Here's example of what I would typically do in Python to play around with system of custom filters:
import math # returns a simple lowpass filter function, based on the specified parameters def filterA(alpha, initial): def f(x): f.prev = x * (1.0 - alpha) + f.prev * alpha return f.prev f.prev = initial return f # create some test signal testSignal = [math.sin(0.25 * x) for x in range(20)] # see the result of applying two of these filters, chained together, on the # test signal print(map(filterA(alpha=0.95, initial=7.5), map(filterA(alpha=0.99, initial=-1.5), testSignal)))
I would like to express something like this in Mathematica so that I can
ListLinePlotto gain intuition on the effect of tweaking filter parameters
FullSimplify(and other functions) to see if a chain of filters can be combined into a single filter, or perhaps simplify the math in other ways
- perform symbolic evaluation of passing various signals through the filter chain (e.g. how does the system respond to a unit impulse function)?
RecurrenceFilter be helpful here? I've been trying to grok them for a few days now and can't figure out how to use them for something like this.
Does Mathematica have something like a closure, where each successive call to a function can update state?
Please note, the exponential moving average I implemented above was just an example – the filters I'm working with are typically more complex.