]> Sound and music - The Karplus-Strong Algortihm

## The Karplus-Strong Algortihm

In 1983, Alex Strong and Kevin Karplus published a simple but effective algorithm for synthesizing the sound of a plucked string.

Pick the period $N$. Then:

1. The first $N$ outputs $y\left[0\right],...,y\left[N-1\right]$ are random.

2. For $n\ge N$, output $y\left[n\right]=\left(y\left[n-N\right]+y\left[n-\left(N+1\right)\right]\right)/2$. (By convention $y\left[-1\right]=0$.)

If played at the frequency ${f}_{s}$, this sequence sounds like a string being plucked at frequency ${f}_{s}/\left(N+1/2\right)$

### Explanation

The Karplus-Strong algorithm is an example of digital waveguide synthesis. An instrument is physically modeled and simulated. In this case, the random samples crudely represents the initial pluck: each part of the string is in a random position moving at a random velocity.

The delay and feedback cause the waveform to repeat itself, oscillating as a string would. If we just had $y\left[n\right]=y\left[n-N\right]$, we would have a waveform that repeats with frequency ${f}_{s}/N$.

Instead, taking the average of two consecutive samples acts as a one-zero low-pass filter, mimicking dampening effects of a real string as it vibrates. Higher frequency oscillations lose energy quicker than lower frequency oscillations.

The filter $y\left[n\right]=\left(x\left[n\right]+x\left[n-1\right]\right)/2$ has the transfer function $H\left(z\right)=\left(1+{z}^{-1}\right)/2$. When $z={e}^{ia}$, this is ${e}^{-ia/2}\left({e}^{ia/2}+{e}^{-ia/2}\right)/2={e}^{-ia/2}\mathrm{cos}a/2$.

Thus an input ${e}^{ian}$ comes out as ${e}^{ia\left(n-1/2\right)}$, explaining why we divide the sampling frequency by $N+1/2$ to arrive at the frequency of the plucked string.

### Extensions

Although the basic algorithm produces surprisingly good results, we can do better.

At higher frequencies, rounding ${f}_{s}/\left(N+1/2\right)$ to the nearest integer is too crude. We can correct for the error by introducing an allpass filter in the loop: $y\left[n\right]=Cx\left[n\right]+x\left[n-1\right]-Cy\left[n-1\right]$.

At lower frequencies, the sound decays too slowly. We can shorten the decay by introducing a loss factor $\rho <1$, and set $y\left[n\right]=\rho \left(y\left[n-N\right]+y\left[n-\left(N+1\right)\right]\right)/2$.

At higher frequencies, we have the opposite problem. We can stretch the decay by weighting the average. Pick some $0 and set $y\left[n\right]=\left(\left(1-S\right)y\left[n-N\right]+Sy\left[n-\left(N+1\right)\right]\right)/2$. This changes the phase delay; see Jaffe and Smith for the exact formula (or derive it yourself).

When a real string is plucked harder, the waveform contains more high frequency components. Thus by putting the output through an appropriate low-pass filter we change the loudness of the output. One possible dynamics filter is $y\left[n\right]=\left(1-R\right)x\left[n\right]+Ry\left[n-1\right]$ for some $0 that depends on the frequency and desired loudness.

To simulate string muting, we can introduce a loss factor when a note ends.

Slurs can be simulated by using a new value of N on the fly. Similarly, glissandi can be simulated by changing N gradually.

### References

• Kevin Karplus, Alex Strong, “Digital Synthesis of Plucked String and Drum Timbres”, Computer Music Journal (MIT Press) 7(2), 1983.