Twelve frets, one octave

  • A fret shortens the speaking length of the string and raises the pitch
  • Twelve frets is one octave, and an octave doubles the frequency
  • Equal temperament uses the same ratio at every fret: the twelfth root of 2
  • On a log scale that repeated ratio is a straight line, and least squares is how the line gets chosen

The model on a log scale

\[\log_2(F_i) = \beta_0 + \beta_1 x_i + \varepsilon_i\]

  • \(x_i\) is the fret number, and the open string is fret 0
  • \(F_i\) is the frequency in hertz
  • \(\beta_1\) is the change in log frequency from one fret to the next
  • Equal temperament sets \(\beta_1 = 1/12\) and \(\beta_0 = \log_2(F_{\text{open}})\)
  • \(\varepsilon_i\) is the leftover, here a few cents of detuning around the tempered pitch

The line with the smallest squared miss

Score a candidate line by the residual sum of squares:

\[RSS(\beta_0,\beta_1) = \sum_{i=1}^{n} (y_i - \beta_0 - \beta_1 x_i)^2\]

The intercept and slope that minimize that sum are

\[\hat\beta_1 = \frac{\sum_{i=1}^{n}(x_i-\bar x)(y_i-\bar y)}{\sum_{i=1}^{n}(x_i-\bar x)^2}\]

\[\hat\beta_0 = \bar y - \hat\beta_1 \bar x\]

Here \(y_i = \log_2(F_i)\) and \(x_i\) is the fret. Squaring makes every miss count as a positive amount, and a larger miss counts more than a smaller one.

High E, frets 0 through 20

  • The open string is the standard high E, 329.63 Hz, which is \(440 \times 2^{-5/12}\)
  • The neck in the example runs from fret 0 to fret 20
  • Each frequency is the equal-temperament pitch moved by a random shift with mean 0 and standard deviation 8 cents
  • The seed is fixed (set.seed(12)), so the same notes come back every time the slides are built
  • The question for the fit: how close is the estimated slope to \(1/12\)?

Hertz bends upward

The curve is the fitted log-line, translated back into hertz. Frequency multiplies at each fret, so the raw pitch bends upward.

Log frequency is a straight line

The band is a 95% confidence band for the mean log frequency. An 8-cent wobble is small beside a jump of one semitone per fret, so the band sits tight against the line.

The fit recovers the semitone

  • Fitted line: \(\widehat{\log_2 F} = 8.359 + 0.08366\, x\)
  • One fret on the tempered scale is \(1/12 \approx 0.08333\) on the log2 scale
  • The estimated slope is 0.08366
  • Twelve times that slope is 1.0039, the number of octaves the fit gives to twelve frets
  • \(R^2 = 0.9999\): across this neck, the fret number accounts for nearly all of the variation in log frequency

Leftovers are a few cents

\[c_i = 1200 \times e_i\]

\(e_i\) is the residual in log2 frequency. Multiplying by 1200 writes that residual in cents, and 100 cents is one semitone.

The residual standard error is about 6.6 cents.

The best line sits at the bottom

Each point on the surface is one candidate pair \((\beta_0, \beta_1)\), scored by \(RSS\) on the log-frequency fit. The dark dot is \((\hat\beta_0, \hat\beta_1)\).

Code for the log-frequency plot

library(ggplot2)

set.seed(12)
fret <- 0:20
open_hz <- 440 * 2^(-5 / 12)
cents <- rnorm(length(fret), 0, 8)
log_freq <- log2(open_hz * 2^(fret / 12 + cents / 1200))
guitar <- data.frame(fret, log_freq)

ggplot(guitar, aes(fret, log_freq)) +
  geom_point(color = "#1F4E79", size = 2.4) +
  geom_smooth(
    method = "lm", formula = y ~ x, se = TRUE,
    color = "#8C1D40", fill = "#8C1D40"
  ) +
  labs(x = "Fret number", y = "log2(frequency in Hz)")

What to keep from the example

  • Equal temperament makes pitch multiply by the same ratio at every fret
  • \(\log_2(\text{frequency})\) turns that curve into a line with slope \(1/12\)
  • Least squares recovers that slope from the fretted notes, and \(12\hat\beta_1\) lands near one octave
  • The hertz plot shows the musical curve, the log plot shows the fitted line, and the bowl shows the squared-error score of every other line
  • The cents plot is the detuning left around that line, on the order of 7 cents