The wave equation tells you exactly how a plucked string moves. Turning that equation into code takes one key step: replace infinitely small differences with finite-sized ones. Everything else follows automatically.
A string under tension obeys Newton's second law at every point along it. The result is the 1D wave equation:
Here u(x, t) is the displacement of the string at position x and time t, and c is the wave speed — set by the string's tension and density. The equation says: wherever the string curves upward, it accelerates upward; wherever it curves downward, it accelerates down. That restoring force is what makes waves travel.
Replace the continuous string with N equally spaced nodes. Replace each derivative with the ratio of a small difference to a small step size. The second time-derivative becomes:
The second space-derivative (curvature) becomes:
Substitute both into c²·∂²x = ∂²t, then rearrange to isolate the only unknown — the future position u[t+1][i]:
The four-point pattern — 1 node from the past row, 3 from the present — is called the stencil. Each future node needs exactly these four neighbours. Step through the computation one node at a time and watch the stencil advance across the grid.
Run the same update loop 44 100 times per second. Read one node as the audio output. The inner loop — sweeping the stencil across all N nodes — produces one audio sample per iteration.
The FD simulation keeps three arrays of N values — past, present, future. But look at the update: to compute future[i] you only need past[i] and three present neighbours. If you sweep left to right and write the result back into the same array as you go, the "past" values for the nodes you've already passed become the "future" values for the ones ahead. One circular buffer is enough.
That single ring of N samples — with a read head that laps around once per fundamental period — is the Karplus-Strong delay line. The averaging filter (½·(current + next)) approximates the FD curvature term: a gentle low-pass that removes energy faster from high frequencies, just like a real string's stiffness damping.