Guitar Tuner Program

0 views
Skip to first unread message

Lajuana Paling

unread,
Aug 3, 2024, 10:49:35 AM8/3/24
to hercompdispli

Hello there! In this post we will program a guitar tuner with Python.This project is a pure software project, so there is no soldering or tinkering involved.You just need a computer with a microphone (or an audio interface) and Python.Of course the algorithms presented in the post are not bound to Python, so feel free to use any other language if you don't mind the addtional translation(however, I recommend to not use tcl as it is "the best-kept secret in the software industry" and we better keep it a secret, lol).

We will start with analyzing the problem we have which is probably a detuned guitar and then forward to solving this problem using math and algorithms.The focus of this post lies on understanding the methods we use and what their pros and cons are.For those who want to code a guitar tuner in under 60 seconds: my Github repo ;)

Let's start with some really basic introduction to music theory and guitars.First, we have to define some important musical terms as an exact distinction will avoid some ambiguities:

  • The frequency is defined as the reciprocal of the period duration of an repeating event. For example, if we have a sinusoidal signal with a period length of 2ms, the frequency is 500Hz.
  • Pitch is the perceived frequency of a sound. Thus, in contrast to frequency which is physical measure, the pitch is a psychoacoustical measure. This distinction is needed as there are cases where we hear frequencies which are physically not there (or don't hear frequencies which are actually there)! Don't worry, we will have a closer look on that subject later.
  • A note is just a pitch with a name. For example, the well known A4 is a pitch at 440Hz. It can also carry temporal information like whole notes or half notes, but this is rather uninteresting for us.
  • The term tone seems to be ambigous, so we rather try to avoid it. The only kind of tone which will be used is a pure tone. A pure tone is a sound with a sinusoidal waveform.
(Sources: [1],[2],[3])

With this defintions in mind we will now look at how a guitar works on a musical level.I guess most of you know this but the "default" guitar has 6 strings which are usually tuned in the standard tuning EADGBE.Whereby each note refers to one of the strings. For example, the lowest string is tuned to the note E2.This means that the string has a pitch of 82.41Hz, since this is how the tone E2 is defined.If it would have a pitch of 81Hz, our guitar is out-of-tune and we have to use the tuners on the headstock to get it back in tune.Of course all other notes can be assigned to a certain pitch as well:

Note, that for this post we assume an equal temperamentand a concert pitch of A4=440Hz which covers probably 99% of modern music.The cool thing about the equal temperament is that it defines the notes and pitches in half step fashion described by the following formula:$$f(i) = f_0 \cdot 2^i/12 $$So, if you have a pitch \(f_0\), for example A4 at 440Hz, and you want to increase it by one half step to an A#4 then you have to multiplythe pitch 440Hz with \(2^1/12\) resulting in 466.16Hz.
We can also derive an inverse formula which tells how many half steps are between the examined pitch \(f_i\) and a reference pitch \(f_o\).$$12 \cdot log_2 \left( \fracf_if_o \right) = i $$This also allows us to assign a pitch a note. Or at least a note which is close to the pitch.As you can imagine, this formula will be of particular interest for us.Because if we can extract the pich from a guitar recoding, we want to know the closest note and how far away it is.

After reading the following section you hopefully know what is meant by pitch detection and which algorithms are suited for this.As already mentioned above, pitch and frequencies are not the same.This might sound abstract at first, so let's "look" at an example.

As you can see the signal has a period length of roughly 2.27ms which corresponds to a frequency of 440Hz.So far so good.But you can also see that the signal is far away from being a pure tone. So, what is happening there?

To answer this question we need to make use of the so-called Discrete Fourier Transform (DFT).
It's basically the allround tool of any digital signal processing engineer.From a mathematical point of view it shows how a discrete signal can be decomposed as a set of cosine functionsoscillating at different frequecies.
Or in musical terms: the DFT shows which pure tones can be found in an audio recording.If you are interested in the mathematical details of the DFT, I recommend you to read my previouspost.But no worries, the most important aspects will be repeated in this post.
The cool thing about the DFT is that it provides us with a so called magnitude spectrum. For the given example it looks like this:

On the x-axis you can see the frequencies of the pure tones while the y-Axis displays their intensity.The spectrum reveals some interesting secrets which you couldn't see in the time domain.As expected there is a strong intensity of the pure tone at 440Hz.But there are other significant peaks at integer multiples of 440Hz. For example, 880Hz, 1320Hz, etc.If you are familiar with music you may know the name of these peaks: harmonics or overtones.

The reason for the overtones is quite simple. When you hit a guitar string you excite it to vibrate at certain frequencies.Especially frequencies which form standing waves can vibrate for a long time.These fulfill the boundary conditions that the string cannot move at the points where it is attached to the guitar (bridge and nut).Thus, multiple overtones are also excited which are all multiples of the fundamental frequency. The following GIF visualizes this:

The overall set of harmonics and how they are related is called timbre.A timbre is what makes your guitar sound like a guitar and not like any other instrument.This is pretty cool on the one hand, but it makes pitch detection a real challenge.Because at this point you might already had an idea for a guitar tuner: create a DFT spectrum, determine the frequency of the highest peak, done.Well, for the given spectrum about this might work, but there are many cases for which you will get wrong results.
The first reason is that the fundamental frequency does not always have to create the highest peak.Altough not beeing the highest peak the pitch is determined by it.This is the reason why pitch detection is not just a simple frequency detection!
The second reason is that the power of the guitar signal is distributed over a large frequency band.By selecting only the highest peak, the algorithm would be very prone to narrowband noise.In the example spectrum given about you can see a high peak at 50Hz which is caused by mains hum.Although the peak is relatively high, it does not determine the overall sound impression of the recording.Or did you feel like the 50Hz noise was very present?

The complexity of this problem has lead to a number of different pitch detection algorithms. In order to choose the right algorithm we have to think about what requirements a guitar tuner needs to fullfill.The most important requirements surely are:

  • Accuracy: According to [4], the human just-noticable difference for complex tones under 1000Hz is roughly 1Hz. So, our goal should roughly be a frequency resolution of 1Hz in a frequency range of ca. 80-400Hz.
  • Realtime capabability: When using the tuner we want to have a live feedback about which note we play. We therefore have to consider things like the runtime complexity of the algorithm and the hardware we are using.
  • Delay: If the results only popup 5 seconds after we played a string, tuning our guitar accurately will be pretty hard. I cannot provide you with any literature on that, but I guess a delay of lesser than 500ms sounds fair.
  • Robustness: Even in noisy environments a guitar tuner should be capable of doing its job. Especially the omnipresent mains hum at 50Hz (or 60 Hz depending on where you live) shouldn't be a problem.

In the following we will start with programming a simple maximum frequency peak algorithm.As already mentioned above, this method may not work pretty well since the fundamental frequency is not guarenteed to always have the highest peak.However, this method is quite simple and a gentle introduction to this subject.

Our first approach will be a simple guitar tuner using the DFT peak approach.Usually the DFT algorithm is applied to the whole duration of signal.However, our guitar tuner is a realtime application where there is no concept of a "whole signal".Furthermore, as we are going to play several different notes, only the last few seconds are relevant for pitch detection.So, instead we use the so called discrete Short-Time Fourier Transform (STFT) which is basically just the DFT applied for the most recent samples.You can imagine it as some kind of window where new samples push out the oldest samples:Note, that the spectrum is now a so-called spectrogram as it varies over time.

As a next point we look at the frequency resolution of the DFT which is(for details see my DFT post):$$ f_s / N \approx 1 / t_window [Hz]$$With \(N\) being the window size in samples, and \(t_window\) the window size in seconds.The resolution in Hertz is approximately the reciprocal of the window size in seconds.So, if we have a window of 500ms, then our frequency resolution is 2Hz.This is where things become tricky as a larger window results in a better frequency resolution but negatively affects the delay.If we consider frequency resolution more important up to a certaint extent than delay, a windows size of 1s sounds like a good choice.With this setting we achieve a frequency resolution of 1Hz.

If you tried to tune your guitar using this tuner you probably noticed that it doesn't work pretty well.As expected there main problem are harmonic errors as the overtones are often more intense than the actual fundamental frequency.A way to deal with is problem is using the Harmonic Product Spectrums as the next section will show.

c80f0f1006
Reply all
Reply to author
Forward
0 new messages