One of the next tasks I tackled was figuring out the scale of the fretboard. What do I mean by that? At first, I had the ultrasonic sensor measure the distance in a straightforward, linear way. However, the frets on a guitar neck become progressively smaller and, accordingly, the linear scale progressively more inaccurate. For my product, it would be great, if the sensor would not output the absolute distance but the actual fret of the current hand position. The first step in achieving this goal was to establish the mathematical relationship of the distances between the frets. Luckily, I found a YouTube video series that thoroughly explained the issue.
Apparently, the basis of it all is the (Western) chromatic scale and the 12-tone-equal temperament. The chromatic scale is a musical scale with twelve pitches, each a semitone, also known as a half-step, above or below its adjacent pitches. The 12-tone equal temperament on the other hand is the musical system that divides the octave into 12 parts, all of which are equally tempered (equally spaced) on a logarithmic scale, with a ratio equal to the 12th root of 2 (12√2 ≈ 1.05946). That resulting smallest interval, 1⁄12 the width of an octave, is called a semitone or half step.
As a result, in 12-tone equal temperament (the most common tuning in Western music), the chromatic scale covers all 12 of the available pitches.
After watching the video and making some experimental calculations, I came up with the following: The resulting formula to calculate the current length of the guitar length depending on the fret is as follows:
k = fret
Lk = string length at fret k
L0 = scale length
Using this formula, I then calculated the fret positions for my Charvel with a scale length of 648 mm or 64.8 cm on an Excel sheet. I double-checked the calculations by manually measuring several fret distances and compared them with the calculated ones – the math seems to be correct. As this formula gives me the string length AFTER the finger so from the finger/fret all the way to the bridge of the guitar, I subtracted the total scale length of 64.8 cm to get the distance from the sensor at the headstock to the hand.
I quickly realized however, that I needed the formula the other way round so to speak – I had to express the fret k depending on the distance measured. Consequently, I used all my summoned all my mathematical skills to transform the formula accordingly. I came up with:
k = fret
Dk = distance from finger to fret
L0 = scale length
Now I had (or still have) to find a way to implement this formula in my Arduino code and find out how to use it in the best way. Firstly, I had to find out another way to get distance measurements from the ultrasonic sensor because I used a library that only outputted integer values. However, the fret distances are not integer numbers at all so I used another way of programming the ultrasonic sensor, so it sends floating point values. With that done, I had to find a way to implement the formula in Arduino. Arduino knows the log function only (the logarithm with base 10) but my supervisor gave me the following formula to transform the base of my current formula (2 to the power of ½) to two log 10:
k = fret
Dk = distance from finger to fret
L0 = scale length
I then started a new, separate sketch to try out the formula and it works! I can input a certain distance and it returns me the corresponding fret.
However, I still have some issues to solve:
- The hand is always nearer to the sensor than the fret, so I need to establish certain fret ranges.
- Arduino then must be able to recognize which distances coming from the sensor fall into which fret range and then output the correct fret.
- It would be cool if one could calibrate the sensor by first giving him the distance of the first fret and then the 12th and it calculates the frets in between based on the above-mentioned formula. That way, the fret detection mechanism would be flexible and adaptable to each guitar scale length.
Sources:
Math of the Guitar Fretboard (Part 1) – YouTube
As well as the following parts