## Piano Tuning

You have been given a keyboard. Inconveniently, this keyboard has not been tuned. You'd like to play it, and therefore decide to tune it yourself. Luckily, the mechanics of tuning this keyboard are easy... you just need to work out which frequency to assign to each note...

### The octave doubling rule

You try to recollect any relevant info you might have picked up over the years.. and remember the following:

The note an octave above, has double the frequency.

In other words, if note C5 has frequency f, then note C6 will have frequency 2*f. As far as you remember, this should apply to all the notes on the keyboard.

It follows from this that the note an octave below must have half the frequency. So C4 would have a frequency of f/2.

Great! But where to go from here?..

### The first frequency

You allow your eyes to wander up and down the 88 key keyboard...

You realise that you could pick a relatively middling key on the keyboard, and assign to this a relatively middling frequency.

You choose A4 to be your middling key. You then play around with a tone generator, and decide that you perceive ~440Hz to be a fairly middling frequency.

You turn on your piano, and program in freq(A4) = 440Hz.

Great! You have now tuned one note!

### All the As

Now that you have decided on a frequency for A4, you realise that you can work out the frequencies of all the other As, using the octave doubling rule:

A4 <- 440 Hz

A5 <- 2*440 Hz

A6 <- 4*440 Hz

etc.

And:

A3 <- 440/2 Hz

A2 <- 440/4 Hz

etc.

So you program these in...

Great! Now you have tuned all of your As!

### Join the dots

You have all the As tuned, but you're not sure what to do about the other notes.

You wonder if a graph might help, so you painstakingly draw out the following:

Looking at this graph, you realise that drawing a curve through the points would assign a frequency to each key on the keyboard. However, drawing the curve looks somewhat difficult..

On a whim, you decide to plot the points again, but this time using logarithmic graph paper:

Aha! Now you can use a ruler to draw a straight line through the points:

If you want to find the frequency of a certain key, you can read it off the graph!

You wonder if the octave doubling rule still applies to any note, or just to the As. You read a few values from the graph, and it seems like it does apply to all notes! "Hurrah!" you shout.

Great! By reading from the graph, you can now tune all the notes on your piano!

### Finding the expression

Unfortunately, you don't enjoy reading values from the graph. It's repetitive, inaccurate, and slow.

"If only I had a mathematical expression for this curve", you think to yourself, "then I could accurately compute the frequencies for this keyboard!"...

You write out the frequencies of the A keys on your keyboard, and re-number them:

k |
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
---|---|---|---|---|---|---|---|---|

freq |
27.5 | 55 | 110 | 220 | 440 | 880 | 1760 | 3520 |

You notice that 880 is 440 * 2^{1}, and that 1760 is 440 *2^{2}, and that 3520 is 440
*2^{3}...
And realise that you can write the exponent in terms of k...

freq = 440 * 2^{k-5}

You double check that this works for k=4, k=3 and k=1... and it does!

Now you write a table, showing how the 'k' number maps to the keyboard number (e.g. A4 is note number 49 on the keyboard, A5 is 61, etc.):

k |
1 | 2 | 3 | 4 | 5 | 6 |
---|---|---|---|---|---|---|

n |
1 | 13 | 25 | 37 | 49 | 61 |