Maker Portal

View Original

Audio Processing in Python Part III: Guitar String Theory and Frequency Analysis

See this content in the original post

This is the third entry into the Audio Processing Series. For Part I, click here. For Part II, click here. If you’re up-to-speed on the previous parts, continue reading below.

See this content in the original post

See this content in the original post

When a string is plucked it is capable of emitting a range of frequencies based on its length and material properties. We can use the wave equation in one dimension to represent the behavior of a string under excitation:

See this content in the original post

where t is time, x is along the length of the string, u is the amplitude deflection of the string, and c is the speed of the wave along the string.

See this content in the original post

The T in the wave speed is an approximate tension, and μ is the mass per unit length of the string. And if we assume a solution of sinusoidal profile:

See this content in the original post

Upon plugging this solution back into the wave equation, we can find that the speed of the wave is related to the wave number and frequency by:

See this content in the original post

For a guitar, we can assume both ends of the string are fixed:

Figure 1: Fixed string that represents a stringed instrument. We will be suing this geometry to calculate modes of a vibrating string.

Using the wave solution above, we can simplify the unknowns in the equation by taking advantage of the properties of the string:

See this content in the original post

We can simplify a bit using rules of exponentials:

See this content in the original post

And introducing the cosines and sines using Euler’s relation:

See this content in the original post

Simplifying:

See this content in the original post

Taking only the real (physical) part:

See this content in the original post

where C has absorbed the other constants, k is the wavenumber, and ω is the angular frequency. From here we can impose another boundary condition on the right side of the string at x = l:

See this content in the original post

We can now use the relationship that we found earlier relating the wave speed to the frequency and wave number:

See this content in the original post

Which gives us the true definition of the standing wave frequency modes:

See this content in the original post

This states that with knowledge of the speed of the wave in the material and the length of the string, we can compute and predict the fundamental and harmonic modes of the system. Things get a little trickier, however, when discussing the speed of the wave. In order to predict the speed of the wave, we need to return to the definition of the speed of the wave, shown above:

See this content in the original post

Therefore, in the case of tuning a guitar, since we know roughly the length of the string, the mass per unit length of the string, and the target frequency (set by musical standards), we can predict the needed tension. A guitar also has the same length of string for each of its six strings, which means that the mass per unit length or tension need to change in order for the frequency to change. This is why we see different types of strings on a guitar.

Figure 2: The fundamental mode of a standing wave on a string. Using the equation derived above, we can calculate the fundamental modes of a guitar using the length of the string, the density of the string, and information about the tension on the string.

The fundamental mode is shown above. The fundamental is the lowest natural mode of vibration of the system. In the case of a guitar, we expect this frequency to be the open note of any of the six strings. The notes above the fundamental are called harmonics, and they can be calculated by increase the values of n in the equation above for frequency. Below I have plotted the first and second harmonics (second and third notes on the string).

Figure 3: First harmonic of the string (second note).

Figure 4: Second harmonic of the string (third note).

Harmonics are integer multiples (in most systems), so to find the harmonics in a recording, we can look at the fundamental and calculate if there are integer multiples of that note higher in the frequency spectrum. Now that we understand modes of a string a bit more, we can look at the strings on a guitar.

See this content in the original post

See this content in the original post

The guitar is a six-stringed instrument with the following frequency distribution (Wikipedia):

See this content in the original post

The frequencies above are for the standard tuning method of playing guitar. The lowest string on the guitar oscillates at about 82 Hz, and the highest string on the guitar oscillates at about 330 Hz. This means that when we take the Fast Fourier Transform of the guitar, we expect to see six large peaks when the guitar is strummed without any fingering. We may also see harmonics and other peaks depending on the shape and geometry of the guitar as well, though the shape of the body is a whole discuss itself and I will not in-depth about that type of behavior. We will assume all the dominant peaks will be fundamentals or harmonics, and will ignore the others.

See this content in the original post

When we press down a guitar string, we are effectively changing the length of the string, which results in a higher frequency (see frequency equation above). If we press a string down halfway between each endpoint, we can expect a frequency that is twice the fundamental. Using the change in string length and the frequency equation above, we can calculate every note on a guitar or any standing wave stringed instrument. From here, we can investigate and test the frequency content in a guitar by using the FFT function in Python.

See this content in the original post

See this content in the original post

In this section I will be using fairly advanced Python programming to do the following:

  1. Record 1 second of audio data using a USB mic [tutorial here]

  2. Subtract background noise in time and spectral domain

  3. Calculate FFT for guitar strum [tutorial here]

  4. Plot frequency spectra of guitar strum

  5. Annotate 6 peak frequencies related using a peak finding algorithm

I am using an acoustic guitar from Fender to produce the modes and as the stringed instrument for investigation. The guitar is tuned to a near-standard tuning configuration. The code for the algorithm outlined above is shown below:

See this content in the original post

The code above records background noise first so that the analysis can remove any noise in the measurement going forward. The algorithm also calculates and annotates the peak frequencies so that the user can pinpoint the modes of the system. For a more in-depth breakdown of the frequency methods or even the microphone methods used to calculate pressure (Pascals), see the previous section of this series [here].

Below is an example output of the code above. In the recording, I strummed the acoustic guitar with all six strings open. The results aren’t surprising, with the primary peaks being the six fundamental frequencies of the guitar (within some degree of error).

Figure 5: Open string strum showing the frequencies of the 6 strings on a standard-tuned guitar. We can see all six string and where the dominant energy lies in this specific acoustic guitar.

It is easy to see the six frequencies corresponding to the fundamental modes of the six guitar strings. It might also be notable to observe the distribution of energy between the six strings: the peak frequency is the A-string (111 Hz), and from there it’s the D-string (147 Hz), B-string (247 Hz), E-string (329 Hz), low-frequency E-string (83 Hz), and finally the G-string (197 Hz). This means that the G-string (197 Hz) has very little energy being generated at its fundamental frequency.

Upon investigating the G-string (197 Hz), we can see that there is plenty of energy in the string and its harmonics:

Figure 6: D-string vibration showing the fundamental frequency and its harmonics.

We can see how this is the case by looking at the frequency equation:

See this content in the original post

Additionally, if we strum a specific chord on the fretboard (C-chord), we measure the frequencies of specific chords. The fingering for a C-string is shown below:

In the C-chord, we can decompose the frequencies based on the shortening of each string. For example, we can consider each string as a group of 24 divisions (12th fret is an octave, roughly half of the string). This will give us a better idea of how to calculate frequencies based on fingering position:

See this content in the original post

This configuration gives us the ability to use the fundamental frequency of the open string to calculate the frequencies of specific fingerings at different frets. lc is the length of the string once the fret is pressed down, and l1 is the fundamental length. From here, if we say lc is some fraction of l1 , we can get rid of the length and calculate the fretted string frequency as a function of the open-string fundamental:

See this content in the original post

So we expect to see a change in frequency for the A, D, and B strings using the fret equation above. We expect the following frequencies:

See this content in the original post

Below is a frequency plot of a strummed C-chord. We can see roughly five independent frequencies (and also perhaps a pair of frequencies around 262 Hz - one independent and one harmonic). We also see another frequency, at 418 Hz, which is likely the harmonic of the dominant A-string on the third fret (127 Hz). We do not see the G-string, which is expected as in the above open strum spectrum its amplitude was much lower than the other strings.

Figure 7: C-chord strum showing the peaks of the chord. We are missing a few freqencies, and some of our predictions are off by a few Hertz, however, this type of prediction assumes perfect string length, whereas, in reality the length of the string may vary depending on the guitar, the player, and other factors as well. That being said, we can explain all of the peaks based on our frequency and string length equation, either with fundamentals or harmonics.


See this content in the original post

This entry into the audio processing tutorial is a culmination of three previous tutorials: Recording Audio on the Raspberry Pi with Python and a USB Microphone, Audio Processing in Python Part I: Sampling, Nyquist, and the Fast Fourier Transform, and Audio Processing in Python Part II: Exploring Windowing, Sound Pressure Levels, and A-Weighting Using an iPhone X. The goal of this tutorial and its series was to demonstrate the power of the Fast Fourier Transform and its significance to data processing and analysis. During this specific tutorial, I demonstrated how strings vibrate to produce the acoustic signals we recognize in musical instruments. I discussed the guitar and how to predict frequencies based on changing the length of the string via the fretboard. This type of frequency analysis can be used in a multitude of different applications ranging from music to image analysis, health monitoring of equipment and even applications in aerospace engineering. The FFT is an incredibly powerful tool, as is the understanding of how sampling and acoustics can be applied to real-world signals.

See this content in the original post

See More in Acoustics and Signal Processing:

See this gallery in the original post