Three building blocks
A difference equation is a recipe: y[n] = 0.5·y[n−1] + 2·x[n] + x[n−1]. It only does three things: remember an old sample, multiply by a number, and add. A block diagram draws each one as a block.
The delay box, written z⁻¹, remembers one sample. On every clock tick it takes in the current value and hands out the one it stored a tick earlier. In: x[n]. Out: x[n−1]. In code, it is one variable.
A gain multiplies every sample that passes through it by a fixed number. This one is b₀ = 2: x[n] goes in, 2·x[n] comes out. In code, it is one multiplication.
An adder outputs the sum of every arrow that points into it. Here it adds 2·x[n] and x[n−1]. Using only present and past inputs like this gives an FIR filter (finite impulse response).
Direct form I
Now the y side. The output goes into a second delay, comes back one tick later as y[n−1], is scaled by a₁ = 0.5 and added in. Drawing each term of the equation like this is called direct form I.
Feed in the unit impulse: x[0] = 1, then zeros. Both delays start at 0. So y[0] = 2·1 + 0 + 0.5·0 = 2. On the tick, the delays store x[0] = 1 and y[0] = 2.
n = 1: the input is 0 now, but the delays still hold the past. y[1] = 2·0 + 1 + 0.5·2 = 2. Then the delays store 0 and 2.
From n = 2 the x side holds only zeros, but the loop keeps going: each output is half the one before. 1, 0.5, 0.25, 0.125 ... It never quite reaches zero: an IIR filter (infinite impulse response).
Direct form II
Both halves are linear and time-invariant, so their order can be swapped. Feedback first: w[n] = x[n] + 0.5·w[n−1]. Then the input part: y[n] = 2·w[n] + w[n−1]. Both halves now need the same w[n−1].
So one delay serves both: direct form II. Run the same impulse through it: y = 2, 2, 1, 0.5, 0.25, 0.125. The same output as before, with one delay instead of two. Fewer delays means less memory.
In Python, scipy.signal.lfilter([2, 1], [1, -0.5], x) runs this filter. SciPy writes the feedback with the opposite sign, so a₁ = 0.5 becomes −0.5. Inside, it uses a close cousin: the transposed direct form II.
The working
# step 1/11 · recipe y[n] = 0.5·y[n−1] + 2·x[n] + x[n−1] x[n] the input list (n = 0, 1, 2 ...) y[n] the output list x[n−1] the input one sample ago y[n−1] the output one sample ago only three operations appear: delay remember one old sample gain multiply by a fixed number adder add each one gets a block; arrows carry samples
The coefficients b₀ = 2, b₁ = 1 and a₁ = 0.5 are chosen example values; the input is the unit impulse. Every token, face value and stem in the figure is computed by stepping the two structures in model.mjs. One sample (one clock tick) is split into beats so the eye can follow it: the products, the sum, then the delays storing. Real hardware does it all within one sample period.