This was a fun project I initially undertook in October 2023. I was curious to see how vibrating strings looked under conditions actually found in musical instruments. You can find all the code for this at the GitHub repository: github.com/ELFREELAND/string_instrument
What is this?
When you watch someone play a guitar, it’s hard to make out exactly how the strings are vibrating – all you can really see is a blur. If you want to see what the strings are doing on the scale of milliseconds, you would need a really expensive camera. Alternatively, you can simulate the strings using some simple physics, and get a pretty good idea of what the strings are probably up to.
The Classical Wave Equation
Waves in a string are traditionally represented by the 1D wave equation:
![]()
Where
is the displacement of the string. The constant
is the propagation speed of a wave in the string, and it is equal to the square root of the tension divided by the linear density of the string:
![Rendered by QuickLaTeX.com \[c=\sqrt{\frac{T}{\rho}}\]](https://evan-freeland.com/wp-content/ql-cache/quicklatex.com-86c819ba126d11d0e9ff172b4f451ec1_l3.png)
For a string that is fixed at both ends, the boundary conditions are stated
, and solutions are of the form:
![]()
![]()
The fact that this is a linear PDE means that any two solutions can be added to get a third solution. The important takeaway from this is that any solution of the wave equation can be expressed as a sum of sine-wave-like solutions. Each possible value of
is called a harmonic.
If we introduce damping into the system, the wave equation becomes:
![]()
where
represents the strength of damping. The variation of
with
is important, since selectively damping the strings allows the musician to vary the tone of the instrument.
The System
I’m going to model the E string on my bass guitar, which looks like this:

The origin of the
-axis is at the nut, where the strings enter the neck. The string ends at the bridge, where
.
The oval-shaped protrusion to the left of the bridge is the pickup, an device which uses electromagnetic induction to convert the vibration of the strings into an electrical signal. We will get an “electrical signal” from our simulation by extracting
for some fixed
.
The fundamental frequency of a vibrating string,
, is related to the wavelength
and the wave speed
by:
![]()
this is known as Mersenne’s law. The fundamental frequency of my bass’s E string is 41.2 Hz, the string is 30 inches or 0.762 m long, and the manufacturer quotes the tension at 29.6 pounds or 131.67 Newtons when tuned correctly, which gives a linear density of 0.033 kg/m.
The Initial Condition
There are a number of ways to set a string in motion. The simplest of these involve pulling on the string at a single point and releasing it, meaning the initial condition of the freely-vibrating string looks like a triangle:

You can choose to pluck the string close to the bridge or closer to the middle, which will affect the timbre of the instrument:

Alternatively, instead of pulling and releasing the string, you can quickly strike it. This is a common technique among bass players, both upright and electric; and it’s also the mechanism behind the sound of a piano. This condition can be modeled by zero initial displacement, but a local spike in initial velocity:

Damping
The effect of damping on the string’s behavior is one of the more interesting things to investigate here. I’ll look at two important cases: palm muting and harmonics.
Palm muting is an important guitar technique, especially in rock and metal music, which involves the guitarist holding the side of their picking hand against the strings near the bridge to mute them. This can be approximated by a higher value of
right next to the bridge:

Playing harmonics is a technique used for any number of string instruments. By lightly muting the string at
, every harmonic will be damped except for the
th,
th,
th, etc., since those harmonics have a node at
. For example, by muting the string at position
:

you can isolate even-numbered harmonics. By muting at
:

you can isolate harmonics that are multiples of 3.
The Numerical Approach
Since I am dealing with complicated damping conditions, I am going to outsource all the thinking to a computer. In order for the computer to simulate the string, I need to discretize it.
Instead of a continuous string of constant linear density
, I’ll consider a massless string loaded with uniformly spaced point masses of mass
spaced distance
apart, such that
. The acceleration of each point mass is proportional to how far it is from each of its neighbors (provided the displacement isn’t too large), which gives a discrete version of the second derivative. For particle
:
![]()
The 2nd order PDE has been replaced by a system of
2nd order ODEs, where
is the number of point masses on the massless string. However, the solver I’m using only operates on 1st-order equations. To proceed further, I need to turn
2nd order equations into
1st order equations with the following substitution:
![]()
![]()
I have chosen to use 500 points, meaning the solver will have to chew through 1000 first-order ODEs.
Results
Waveforms
First, what do the waveforms look like? The figures below show the displacement of the string at
for one second after it’s plucked – this is like assuming the pickup is located at 80% of the length of the string. In order, they are an open string plucked near the bridge, an open string plucked near the neck, an open string hammered, a palm-muted string plucked near the bridge, a string muted to produce the 2nd harmonic, and a string muted to produce the 3rd harmonic.

The muted strings take some time to reach their final states – the palm-muted string’s waveform decays in amplitude and becomes smoother, while the harmonic-muted strings take some time to develop the higher frequency components.
The figures below show the displacement at the “pickup”, zoomed in, halfway through the simulation:

The plucked open strings have a sort of plateau-shaped waveform, the hammered string has a sort of jittery pulse shape, the palm muted string has a very smooth shape reminiscent of the string plucked near the neck, and the harmonic-muted strings have plateau-like waveforms at a higher frequency than the open strings.
Frequency spectra
The images below show the Fourier transform of the displacement data from the figures above:

The spikes at certain frequencies tell you that a sine wave at that frequency comprises a large part of the waveform. The first two spectra have large spikes at about 40 Hz, the fundamental. The first spectrum, plucked near the bridge, has larger amplitudes of the 2nd, 3rd, and 4th harmonics than the spectrum plucked near the neck. Plucking near the bridge is generally known to produce a “brighter”, “clankier” sound on a guitar or bass, and this corresponds to a higher amplitude of higher harmonics.
The spectrum of the hammered string is interesting: the fundamental is not the component with the highest amplitude; instead it is the 2nd harmonic. The 3rd, 4th, 6th, 7th, and 8th harmonics also have a substantial amplitude. As you’ll see later, the hammered string has a very interesting sound, likely influenced by this diverse frequency spectrum.
The palm-muted string has very low amplitudes of everything above the 4th harmonic – all higher harmonics have been silenced by the musician’s hand. This is kind of the point – palm muting gives a more subdued sound, allowing the instrument to sit lower in the ensemble.
Remember what is happening in the harmonic-muted strings: every harmonic that isn’t a multiple of 2 or 3 is muted. This is clearly visible in each spectrum: in the 2nd-harmonic-muted string, there are spikes at about
,
,
, and
. In the 3rd-harmonic-muted string, there are spikes around
,
,
, and
. There are still small spikes at the fundamental and the first couple harmonics, since these are present in the first few vibrations of the string.
Finally, note that in every spectrum, the 5th harmonic is absent or very nearly so. The 5th harmonic has nodes at
,
,
, and
. This happens to line up with where the pickup is sampling the string’s vibration, so the 5th harmonic is not detected. The 5th harmonic may well be present in the string’s vibration, but it doesn’t contribute to the signal we get.
Animation
But what does the string actually look like? I will use matplotlib.animation to create videos of the string in motion. I’ve highlighted the “pickup” with a dot.
Open string, picked near the bridge:

Open string, picked near the neck:

Open string, struck:

Palm-muted string:

String muted to produce 2nd harmonic:

String muted to produce 3rd harmonic:

I think the harmonic-muted strings are the most interesting here. You can see the initial wave reflect off of the muted point, then off the end of the string, and interfere with itself to produce the higher harmonics. Visualizing harmonics was actually the initial reason I started this project, so this was really cool to see.
Audio
Finally, I can use python’s built in wave library to output the displacement data to .wav files. The audio below contains all 6 waveforms (in the same order as before) played at a pitch of E1, then at a pitch of E4 (329 Hz), then a bassline from a song I like, created from the hammered open string and palm muted string sounds.
You can hear how the open string plucked near the bridge sounds “brighter” than the same string plucked near the neck, and the hammered string sounds “buzzy” or “clanky”. The palm muted string fades away quickly, giving a more subdued sound, and the harmonic-muted strings sound an octave higher and an octave plus a fifth higher than the others, respectively. When the harmonic-muted strings are played at E1, you can also hear how they take a few fractions of a second to develop the higher harmonics fully.
What else?
I still have a few questions that I didn’t end up getting to.
Can I use less than 500 points in the string and get similar results? like, can I use 50? 10?
The wave equation is accurate when the curvature of the string is small compared to its width. If the string is wide or the curvature is large, the bending moment of the string starts to contribute to the vibration, in addition to the tension, and the wave equation includes a
term. What effect does that have on the string?