A rule, one sample at a time
A difference equation is a rule a system follows once per sample. Ours: y[n] = 0.5·y[n−1] + 0.5·x[n]. The new output is half the new input plus half the last output.
The delay box (written z⁻¹ in books) holds one number for one tick: the last output. Before the input starts, nothing has happened yet, so it holds y[−1] = 0. This start is called initial rest.
Running the rule
The input jumps to 8. x[0] = 8 is multiplied by 0.5: 4. The delay box gives 0, and 0.5 × 0 = 0. The adder sums them: y[0] = 4 + 0 = 4.
The new output is copied into the delay box. One tick later it comes back as y[n−1]. This loop from the output back into the adder is called feedback.
x[1] = 8 gives 4 again. The delay box returns y[0] = 4, and half of it is 2. So y[1] = 4 + 2 = 6. The output now depends on the past, not only on the input.
The same rule, again and again: y[2] = 4 + 3 = 7, y[3] = 4 + 3.5 = 7.5, y[4] = 4 + 3.75 = 7.75. Each tick closes half of the gap to 8.
If the output stops changing, y = 0.5·y + 0.5·8, so y = 8. The output creeps up to the input instead of jumping. That makes this rule a smoother, known as exponential smoothing.
Two views of one system
Now feed a single 8, then zeros: 8·δ[n]. Out come 4, 2, 1, 0.5 ... Each output is half the last and never reaches exactly 0. Feedback gives an infinite impulse response (IIR): h[n] = 0.5ⁿ⁺¹.
Convolving the step with h[n] gives the same 4, 6, 7, 7.5 ... But h[n] never ends, so that sum grows with n. The rule needs only 2 multiplies, 1 add and one stored number per sample.
In code
In Python 3, the rule is a loop. prev is the delay box: it starts at 0 and is updated once per sample. scipy.signal.lfilter([0.5], [1, -0.5], x) runs the same rule and gives the same numbers.
The working
# step 1/10 · rule y[n] = 0.5·y[n−1] + 0.5·x[n] x[n] the input, sample number n y[n] the output, sample number n y[n−1] the output one sample earlier four parts do the work: ×0.5 multiply the new input + the adder delay remembers the last output ×0.5 multiply that old output
The weights 0.5 and 0.5 and the step height 8 are chosen so every output is a short, exact number. Every value in the figure is computed from this model. "Initial rest" (input zero before n = 0 means output zero before n = 0) is the start condition Oppenheim and Schafer use to make a difference equation describe one linear, time-invariant system; scipy.signal.lfilter assumes it when zi is not given.