# How to implement an integration filter on arrays?

I have the discrete integration filters:

$$y(n) = Ax(n) + By(n−p) , \text{A, B real constants, p positive integer}$$

EDIT: in the initial version of this question I focussed only on the case $$A=B=p=1$$

I want to apply it on an array representing an impulse signal [1, 0, 0, 0, 0, 0] (assuming $$x(n) = y(n) = 0$$ for every $$n < 0$$)

In the imperative style, using Python, I would write:

>>> x = [0]*5
>>> y = [0]*5
>>> impulse = [1]+[0]*5

>>> for i in impulse:
...   x += [i]
...   y += [i+y[-1]]
...

>>> x
[0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0]
>>> y
[0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1]


How can I obtain the same result with Mathematica? I saw the FoldList and MovingMap functions that had some potential to solve similar problems. But they seem to work only on one list, and I wasn't able to make them use both the $$x$$ and $$y$$ lists at the same time.

Pardon me if this is wildly off base, but is this useful?

RecurrenceFilter[{{1, 0, -B}, {A}}, {0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0}]

{0, 0, 0, 0, 0, A, 0, A B, 0, A B^2, 0}


RecurrenceFilter[{α,β},x] gives the response $$y$$ to the causal input $$x$$ by solving the recurrence equation:

$$\alpha_1 y_n + ... \alpha_i y_{n-i} = \beta_1 x_n + ... + \beta_j x_{n-j}$$

for $$n$$ from $$1$$ to $$Length[x]$$, where $$i$$ is $$Length[α]$$ and $$j$$ is $$Length[β]$$.

rf[n_, p_] :=
RecurrenceFilter[{Append[UnitVector[p, 1], -B], {A}}, UnitVector[n + 6, 6]]

rf[5, 2]

rf[8, 3]

{0, 0, 0, 0, 0, A, 0, A B, 0, A B^2, 0}

{0, 0, 0, 0, 0, A, 0, 0, A B, 0, 0, A B^2, 0, 0}

• Thanks for the help, Mr. Wizard. Could you elaborate a little on how RecurrenceFilter works? In particular, I don't how you plug the $p$ delay? – Sylvain Leroux Dec 7 '19 at 17:24
• I took the liberty to edit the question to add a link and quote the relevant part of the doc. Feel free to revert that edit if you disagree. – Sylvain Leroux Dec 7 '19 at 18:50
• @Sylvain I am glad this helped. Sorry that I wasn't around to answer the follow-up question, but I guess you figured that out yourself. The edit is fine. Thanks for the Accept. – Mr.Wizard Dec 8 '19 at 2:21
x = ConstantArray[0, 5];
y = ConstantArray[0, 5];
impulse = Join[{1}, ConstantArray[0, 5]];
IntegrationFilter[A_, B_, p_, impulse_, xsart_, ystart_] :=
RecurrenceFilter[
{Join[{1}, Table[0, p - 1], {-B}], {A}},
Join[xsart, impulse],
ystart
];


RecurrenceFilter ref : http://reference.wolfram.com/language/ref/RecurrenceFilter.html

IntegrationFilter[1, 0, 1, impulse, x, y]


{0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0}

IntegrationFilter[1, 1, 1, impulse, x, y]


{0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1}

One way to solve this is to use the recursive definition directly along with the initial condition(s):

Clear[y, x]
y[n_] := y[n] = a x[[n]] + b y[n - 1];
y[0] = 0;


To use this, you would need to define x:

x = ConstantArray[0, 20]; x[[3]] = x[[4]] = 1;


To "run" the recursion for the first few entries:

y /@ Range[7]

{0, 0, a, a + a b, b (a + a b), b^2 (a + a b), b^3 (a + a b)}


You can increase the delay (your p parameter) straightforwardly by changing y[n-1] to y[n-2] (for example), but then you also need to add another initial condition (in this case, a value for y[1]). Thanks to @OkkesDulgerci for the simplification.

• y /@ Range@7 is sufficient. – OkkesDulgerci Dec 8 '19 at 19:55