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
- use
ManipulateandListLinePlotto gain intuition on the effect of tweaking filter parameters - use
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)?
Could RecurrenceTable or 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.
RecurrenceFilterit does look like the thing you could use here (for good performance). – Szabolcs Nov 02 '16 at 07:44-0.05 21.0526315789^(-1. n) (-400. 1.05263157895^(1. n) 19.^n + 400. 1.05263157895^(1. n) 20.^n - 157.894736842 20.^(1. n) - 20. 1.05263157895^(1. n) 20.^n n)
– Daniel Lichtblau Nov 03 '16 at 17:01. More generally,RecurenceXX` functions can be used regardless of whetehr an analytic solution can be found.RSolveValuelooks promising. Thanks! I'm not looking for a specific input/output solution. I suppose what I'm asking is "how do people experienced Mathematica users work with recurrence relations in the context of signal processing experimentation?" – splicer Nov 04 '16 at 15:49