0
$\begingroup$

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 Manipulate and ListLinePlot to 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.

$\endgroup$
  • 1
    $\begingroup$ You'll find an example of how to implement closures here. While not being familiar with the topic, after looking at RecurrenceFilter it does look like the thing you could use here (for good performance). $\endgroup$ – Szabolcs Nov 2 '16 at 7:44
  • 1
    $\begingroup$ Not entirely clear what is wanted; would be better if specific input AND desired outpur were provided. The above can be solved analytically by the way: recur[alf_, init_] := RSolveValue[{f[n + 1] == (1 - alf)*n + f[n]*alf, f[1] == init}, f[n], n] recur[.95, 7.5] -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) . More generally, RecurenceXX functions can be used regardless of whetehr an analytic solution can be found. $\endgroup$ – Daniel Lichtblau Nov 3 '16 at 17:01
  • $\begingroup$ RSolveValue looks 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?" $\endgroup$ – splicer Nov 4 '16 at 15:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.