Following Excercise 1.10 and Section 5.1.1.2 of [MÃ¼ller, FMP, Springer 2015], we discuss in this notebook the tuning system introduced by Pythagoras as well as the Pythagorean comma.

The oldest known tuning system was introduced by the Greek philosopher and mathematician Pythagoras (sixth century BC). **Pythagorean tuning** is a system of musical tuning in which the frequency ratios of all intervals are based on the ratio $3:2$ as found in the harmonic series. This ratio is also known as the **perfect fifth**. We now construct a scale starting with the center frequency $\omega$ of a root note (corresponding to the frequency ratio $1$). Then, we successively multiply the frequency value by a factor of $3/2$, and if necessary, divide it by two such that all frequency values lie between $\omega$ and $2\cdot\omega$ (corresponding to frequency ratios between $1$ and $2$). We repeat this procedure to produce $13$ frequency values (and $13$ frequency ratios). The last (the 13$^\mathrm{th}$) frequency ratio is also known as the **Pythagorean comma**, which indicates the degree of inconsistency when trying to define a twelve-tone scale using only perfect fifths.

In the following code example, we construct the thirteen frequency ratios. Furthermore, these frequency ratios are compared with the one obtained from equal-tempered scale (the difference is specified in cents).

In [1]:

```
import numpy as np
import IPython.display as ipd
import sys
sys.path.append('..')
import libfmp.c1
def compute_ratio(num3, num2):
return (3 ** num3) / (2 ** num2)
num3 = 0
num2 = 0
note = 0
diff = 0
s = np.zeros((13, 6))
s[0] = [0, note, num3, num2, compute_ratio(num3, num2), diff]
for m in range(1, 13):
note = (note + 7) % 12
if note == 0: note = 12
num3 = num3 + 1
num2 = num2 + 1
ratio = compute_ratio(num3, num2)
if ratio > 2:
num2 = num2 + 1
ratio = compute_ratio(num3, num2)
diff = (np.log2(ratio) - 1 / 12) * 1200
diff = np.remainder(diff, 100)
s[m] = [m, note, num3, num2, compute_ratio(num3, num2), diff]
for m in range(13):
print('m = %2i, note = %2i, num3 = %2i, num2 = %2i, ratio = %6i:%6i = %7.4f, diff = %+6.2f'
% (s[m, 0], s[m, 1], s[m, 2], s[m, 3], 3 ** s[m, 2], 2 ** s[m, 3], s[m, 4], s[m, 5]))
print('Pythagorean comma: %7.4f (%+6.2f cents)' % (s[12, 4], s[12, 5]))
dur = 4 # seconds
Fs = 4000 # sampling rate
freq = 440
x, t = libfmp.c1.generate_sinusoid(dur=dur, Fs=Fs, freq=freq)
freq_pyt_comma = freq * s[12, 4]
x_pyt_comma, t = libfmp.c1.generate_sinusoid(dur=dur, Fs=Fs, freq=freq_pyt_comma)
print()
print('Sinsoid of 440 Hz (A4):')
ipd.display(ipd.Audio(data=x, rate=Fs))
print('Sinsoid with %5.4f * 440 = %7.4f Hz:' % (s[12, 4], freq_pyt_comma))
ipd.display(ipd.Audio(data=x_pyt_comma, rate=Fs))
```

The oldest known tuning system was introduced by the Greek philosopher and mathematician Pythagoras (sixth century BC). As said before, the geometrically motivated **Pythagorean tuning** is based only on the frequency ratio $2:1$ of the octave and the ratio $3:2$ of the fifth. When only adding fifths and possibly subtracting octaves, one obtains the scale defined above. When allowing adding and subtracting fifths and octaves, one obtains the **Pythagorean scale**. This results in intervals that can be expressed by frequency ratios that involve only powers of two or powers of three.

In the following code, we consider the chromatic scale starting C4 and ending with C5. For all 13 notes, we produces sinusoids once using the center frequencies of the equal-tempered scale and once the frequencies obtained by the Pythagorean intervals.

In [2]:

```
import pandas as pd
from collections import OrderedDict
# Computation of frequencies and differences
pyt_frac = ['1:1', '$2^8:3^5$', '$3^2:2^3$', '$2^5:3^3$', '$3^4:2^6$', '$2^2:3$', '$3^6:2^9$',
'$3:2$', '$2^7:3^4$', '$3^3:2^4$', '$2^4:3^2$', '$3^5:2^7$', '$2:1$']
pyt_ratio = np.asarray([1, 256/243, 9/8, 32/27, 81/64, 4/3, 729/512, 3/2, 128/81, 27/16, 16/9, 243/128, 2])
p = 60
freq = libfmp.c1.f_pitch(p)
freq_pyt = pyt_ratio * freq
notes = np.asarray(range(p, p + 13))
freq_center = libfmp.c1.f_pitch(notes)
freq_deviation_cents = libfmp.c1.difference_cents(freq_pyt, freq_center)
# Generation of sinusoids
dur = 4 #seconds
Fs = 4000 #sampling rate
sinusoid_freq_center = []
for freq in freq_center:
x, t = libfmp.c1.generate_sinusoid(dur=dur, Fs=Fs, freq=freq)
sinusoid_freq_center.append(x)
sinusoid_freq_pyt = []
for freq in freq_pyt:
x, t = libfmp.c1.generate_sinusoid(dur=dur, Fs=Fs, freq=freq)
sinusoid_freq_pyt.append(x)
# Generation of html table
audio_tag_html_center = []
for i in range(len(freq_center)):
audio_tag = ipd.Audio( sinusoid_freq_center[i], rate=Fs)
audio_tag_html = audio_tag._repr_html_().replace('\n', '').strip()
audio_tag_html = audio_tag_html.replace('<audio ', '<audio style="width: 100px; "')
audio_tag_html_center.append(audio_tag_html)
audio_tag_html_pyt = []
for i in range(len(freq_pyt)):
audio_tag = ipd.Audio(sinusoid_freq_pyt[i], rate = Fs)
audio_tag_html = audio_tag._repr_html_().replace('\n', '').strip()
audio_tag_html = audio_tag_html.replace('<audio ', '<audio style="width: 100px; "')
audio_tag_html_pyt.append(audio_tag_html)
pd.set_option('display.max_colwidth', None)
df = pd.DataFrame(OrderedDict([('Note', ['C4', 'C$^\sharp$4', 'D4', 'D$^\sharp$4', 'E4', 'F4',
'F$^\sharp$4', 'G4', 'G$^\sharp$4', 'A4', 'A$^\sharp$4',
'B4', 'C4']),
('ET Freq. (Hz)', freq_center),
('ET Sinusoid', audio_tag_html_center),
('Pyt. Ratio ', pyt_frac),
('Pyt. Freq. (Hz)', freq_pyt),
('Pyt. Sinusoid', audio_tag_html_pyt),
('Difference (Cents)', freq_deviation_cents)]))
df.index = np.arange(1, len(df) + 1)
ipd.HTML(df.to_html(escape=False, float_format='%.2f'))
```

Out[2]: