{"id":1757,"date":"2016-06-09T17:23:14","date_gmt":"2016-06-09T22:23:14","guid":{"rendered":"https:\/\/blogs.mathworks.com\/cleve\/?p=1757"},"modified":"2016-06-09T17:26:09","modified_gmt":"2016-06-09T22:26:09","slug":"math-and-music","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/cleve\/2016\/06\/09\/math-and-music\/","title":{"rendered":"Math and Music"},"content":{"rendered":"<div class=\"content\"><!--introduction--><\/p>\n<p>What does $\\sqrt[12]{2}$ have to do with music? What are <i>equal temperament<\/i> and <i>just intonation<\/i>? How can the MATLAB function <tt>rats<\/tt> help tune a piano? (This post is based in part on the <i>Music<\/i> chapter in my online book, <i>Experiments in MATLAB<\/i>.)<\/p>\n<p><!--\/introduction--><\/p>\n<h3>Contents<\/h3>\n<div>\n<ul>\n<li><a href=\"#db502e87-458c-49e5-907e-11ab5182af07\">Octaves and semitones<\/a><\/li>\n<li><a href=\"#c216c3d4-0611-4ce1-97ef-c5cd52773e8e\">Keyboard<\/a><\/li>\n<li><a href=\"#d60f8c32-d345-4316-bfa5-cdbd1b56b8ea\">Equal Temperament<\/a><\/li>\n<li><a href=\"#003e235c-bf0b-4bb6-a9b8-0298e8fe4d70\">Just Intonation<\/a><\/li>\n<li><a href=\"#c55568f9-5408-46a8-8733-9ce944ab5973\">Do Re Mi<\/a><\/li>\n<li><a href=\"#2c53f657-299e-440c-8a4d-c878ea8c8af7\">Vibrations and Waves<\/a><\/li>\n<li><a href=\"#0b097b43-3af1-4930-ba44-f32c5795272d\">rat and rats<\/a><\/li>\n<li><a href=\"#b1667eb4-123e-4229-af73-a862fe54a469\">Rewriting Music History<\/a><\/li>\n<\/ul>\n<\/div>\n<h4>Octaves and semitones<a name=\"db502e87-458c-49e5-907e-11ab5182af07\"><\/a><\/h4>\n<p>In the theory of music, an <i>octave<\/i> is an interval with frequencies that range over a factor of two. In most Western music, an octave is divided into 12 <i>semitones<\/i> in a geometric progression. In other words the notes have equal frequency ratios. Since 12 semitones comprise a factor of 2, a single semitone is a factor of $\\sqrt[12]{2}$. And because this quantity occurs so often in this discussion, let<\/p>\n<p>$$ \\sigma = \\sqrt[12]{2} $$<\/p>\n<p>Our MATLAB programs use<\/p>\n<pre class=\"codeinput\">    format <span class=\"string\">short<\/span>\r\n    sigma = 2^(1\/12)\r\n<\/pre>\n<pre class=\"codeoutput\">sigma =\r\n    1.0595\r\n<\/pre>\n<p>Think of $\\sigma$ as an important mathematical constant, like $\\pi$ and $\\phi$.<\/p>\n<h4>Keyboard<a name=\"c216c3d4-0611-4ce1-97ef-c5cd52773e8e\"><\/a><\/h4>\n<p>Here is our miniature piano keyboard with 25 keys.<\/p>\n<pre class=\"codeinput\">     small_keyboard\r\n<\/pre>\n<p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/cleve\/files\/music_blog_01.png\" alt=\"\"> <\/p>\n<p>This keyboard has two octaves, with white keys labeled <b>C D &#8230; G A B<\/b>, plus another <b>C<\/b> key.  Counting both white and black, there are twelves keys in each octave. The frequency of each key is a semitone above or below its neighbors. Each black key can be regarded as either the <i>sharp<\/i> of the white below it or the <i>flat<\/i> of the white above it.  So the black key between <b>C<\/b> and <b>D<\/b> is both <b>C<\/b> $\\sharp$ and <b>D<\/b> $\\flat$. There are no <b>E<\/b> $\\sharp$ \/ <b>F<\/b> $\\flat$ or <b>B<\/b> $\\sharp$ \/ <b>C<\/b> $\\flat$.<\/p>\n<p>A conventional full piano keyboard has 88 keys.  Seven complete octaves account for $7 \\times 12 = 84$ keys.  There are three additional keys at the lower left and one additional key at the upper end. If the octaves are numbered 0 through 7, then a key letter followed by an octave number specifies a unique key.  In this notation, two important keys are <b>C4<\/b> and <b>A4<\/b>.  The <b>C4<\/b> key is near the center of the keyboard and so is also known as <i>middle C<\/i>. A piano is usually tuned so that the frequency of the <b>A4<\/b> key is 440 Hz.  <b>C4<\/b> is nine keys to the left of <b>A4<\/b> so its frequency is<\/p>\n<p>$$\\mbox{C4} = 440 \\sigma^{-9} \\approx 261.6256 \\mbox{Hz}$$<\/p>\n<p>Our miniature keyboard has the <b>A4<\/b> key colored green and the <b>C4<\/b> key colored blue.<\/p>\n<h4>Equal Temperament<a name=\"d60f8c32-d345-4316-bfa5-cdbd1b56b8ea\"><\/a><\/h4>\n<p>A piano is almost always tuned with <i>equal temperament<\/i>, the system used by musical instruments with fixed pitch. Starting with <b>A4<\/b> at 440 Hz the keys are tuned in strict geometric progression with ratio $\\sigma$.  Once this is done, a piano rarely needs adjustment.<\/p>\n<h4>Just Intonation<a name=\"003e235c-bf0b-4bb6-a9b8-0298e8fe4d70\"><\/a><\/h4>\n<p>The alternative system, <i>just intonation<\/i>, is defined by a vector of ratios involving small integers.<\/p>\n<pre class=\"codeinput\">   format <span class=\"string\">rat<\/span>\r\n   just = [1 16\/15 9\/8 6\/5 5\/4 4\/3 7\/5 3\/2 8\/5 5\/3 7\/4 15\/8 2]'\r\n<\/pre>\n<pre class=\"codeoutput\">just =\r\n       1       \r\n      16\/15    \r\n       9\/8     \r\n       6\/5     \r\n       5\/4     \r\n       4\/3     \r\n       7\/5     \r\n       3\/2     \r\n       8\/5     \r\n       5\/3     \r\n       7\/4     \r\n      15\/8     \r\n       2       \r\n<\/pre>\n<p>These ratios allow the generation of overtones, chords and harmony. More about that in a moment.  Many musical instruments can be tuned to the key of a particular composition just before playing it.  Singers, choirs, and barbershop quartets can naturally use just intonation.<\/p>\n<p>Here is a comparison of equal temperament and just intonation from a strictly numerical point of view. Equal temperament is defined by repeated powers of $\\sigma$. while just intonation is defined by a sequence of ratios.<\/p>\n<pre class=\"codeinput\">   sigma = 2^(1\/12);\r\n   k = (0:12)';\r\n   equal = sigma.^k;\r\n   num = [1 16 9 6 5 4 7 3 8 5 7 15 2]';\r\n   den = [1 15 8 5 4 3 5 2 5 3 4  8 1]';\r\n   just = num.\/den;\r\n   delta = (equal - just).\/equal;\r\n   T = [k equal num den just delta];\r\n   fprintf(<span class=\"string\">'   k       equal           just              delta\\n'<\/span>)\r\n   fprintf(<span class=\"string\">'%4d %12.6f %7d\/%d %11.6f %10.4f\\n'<\/span>,T')\r\n<\/pre>\n<pre class=\"codeoutput\">   k       equal           just              delta\r\n   0     1.000000       1\/1    1.000000     0.0000\r\n   1     1.059463      16\/15    1.066667    -0.0068\r\n   2     1.122462       9\/8    1.125000    -0.0023\r\n   3     1.189207       6\/5    1.200000    -0.0091\r\n   4     1.259921       5\/4    1.250000     0.0079\r\n   5     1.334840       4\/3    1.333333     0.0011\r\n   6     1.414214       7\/5    1.400000     0.0101\r\n   7     1.498307       3\/2    1.500000    -0.0011\r\n   8     1.587401       8\/5    1.600000    -0.0079\r\n   9     1.681793       5\/3    1.666667     0.0090\r\n  10     1.781797       7\/4    1.750000     0.0178\r\n  11     1.887749      15\/8    1.875000     0.0068\r\n  12     2.000000       2\/1    2.000000     0.0000\r\n<\/pre>\n<p>The last column in the table, <tt>delta<\/tt>, is the relative difference between the two.  We see that <tt>delta<\/tt> is less than one percent, except for <tt>k = 10<\/tt>.<\/p>\n<p>This is why the 12-note scale has proved to be so popular. Powers of $\\sqrt[12]{2}$ turn out to come very close to important rational numbers, especially 5\/4, 4\/3, and 3\/2.  A pair of pitches with a ratio of 3:2 is known as a <i>perfect fifth<\/i>; a ratio of 4:3 is a <i>perfect fourth<\/i>; a ratio of 5:4 is a <i>major third<\/i>.<\/p>\n<h4>Do Re Mi<a name=\"c55568f9-5408-46a8-8733-9ce944ab5973\"><\/a><\/h4>\n<p>One of the first songs you learned to sing was<\/p>\n<p><i>Do Re Mi Fa So La Ti Do<\/i><\/p>\n<p>If you start at middle C, you will be singing the <i>major scale<\/i> in the key of C.  This scale is played on a piano using only the white keys. The steps are not equally spaced.  Most of the steps skip over black keys and so are two semitones.  But the steps between <i>Mi<\/i> and <i>Fa<\/i> and <i>Ti<\/i> and <i>Do<\/i> are the steps from <b>E<\/b> to <b>F<\/b> and <b>B<\/b> to <b>C<\/b>.  There are no intervening black keys and so these steps are only one semitone. In terms of $\\sigma$, the C-major scale is<\/p>\n<p>$$\\sigma^0 \\ \\sigma^2 \\ \\sigma^4 \\ \\sigma^5 \\ \\sigma^7 \\ \\sigma^9<br \/>\n   \\ \\sigma^{11} \\ \\sigma^{12}$$<\/p>\n<p>The number of semitones between the notes is given by the vector<\/p>\n<pre class=\"codeinput\">    format <span class=\"string\">short<\/span>\r\n    diff([0 2 4 5 7 9 11 12])\r\n<\/pre>\n<pre class=\"codeoutput\">ans =\r\n     2     2     1     2     2     2     1\r\n<\/pre>\n<p>This sequence of frequencies in our most common scale is surprising.<\/p>\n<h4>Vibrations and Waves<a name=\"2c53f657-299e-440c-8a4d-c878ea8c8af7\"><\/a><\/h4>\n<p>Like everything else, it all comes down to <i>eigenvalues<\/i>. Musical instruments create sound through the action of vibrating strings or vibrating columns of air that, in turn, produce vibrations in the body of the instrument and the surrounding air. Mathematically, vibrations can be modeled by weighted sums of characteristic functions known as <i>modes<\/i> or <i>eigenfunctions<\/i>. Different modes vibrate at different characteristic frequencies or <i>eigenvalues<\/i>. These frequencies are determined by physical parameters such as the length, thickness and tension in a string, or the geometry of the air cavity. Short, thin, tightly stretched strings have high frequencies, while long, thick, loosely stretched strings have low frequencies.<\/p>\n<p>The simplest model is a one-dimensional vibrating string, held fixed at its ends. The units of the various physical parameters can be chosen so that the length of the string is $2 \\pi$. The modes are then simply the functions<\/p>\n<p>$$v_n(x) = \\sin{n x}, \\ \\, n = 1, 2, &#8230;$$<\/p>\n<p>Each of these functions satisfy the fixed end point conditions<\/p>\n<p>$$v_n(0) = v_n(2 \\pi) = 0$$<\/p>\n<p>The time-dependent modal vibrations are<\/p>\n<p>$$u_n(x,t) = \\sin{n x} \\sin{n t}, \\ \\, n = 1, 2, &#8230;$$<\/p>\n<p>and the frequency is simply the integer $n$. (Two- and three-dimensional models are much more complicated. This one-dimensional model is all we need here.)<\/p>\n<p>Our <a href=\"https:\/\/www.mathworks.com\/moler\/exm\/\">Experiments with MATLAB<\/a> program <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/37977-experiments-with-matlab-experiments-with-matlab\/content\/exm_2014\/vibrating_string.m\"><tt>vibrating_string<\/tt><\/a> provides a dynamic view. Here is a snapshot showing the first nine modes and the resulting wave traveling along the string.<\/p>\n<p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/cleve\/files\/vibes.png\" alt=\"\"> <\/p>\n<p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/cleve\/files\/wave.png\" alt=\"\"> <\/p>\n<p>The ratios in just intonation or equal temperament produce instruments with tunings that generate vibrations with these desired frequencies.<\/p>\n<h4>rat and rats<a name=\"0b097b43-3af1-4930-ba44-f32c5795272d\"><\/a><\/h4>\n<p>The MATLAB expression <tt>rat(X,tol)<\/tt> creates a truncated continued fraction approximation of <tt>X<\/tt> with an accuracy of <tt>tol<\/tt>.  The default <tt>tol<\/tt> is <tt>1.e-6*norm(X,1)<\/tt>.<\/p>\n<pre class=\"codeinput\">   rat(sigma.^(0:12)')\r\n<\/pre>\n<pre class=\"codeoutput\">ans =\r\n1                                                \r\n1 + 1\/(17 + 1\/(-5 + 1\/(-2)))                     \r\n1 + 1\/(8 + 1\/(6))                                \r\n1 + 1\/(5 + 1\/(4 + 1\/(-2)))                       \r\n1 + 1\/(4 + 1\/(-7 + 1\/(2 + 1\/(5))))               \r\n1 + 1\/(3 + 1\/(-74))                              \r\n1 + 1\/(2 + 1\/(2 + 1\/(2 + 1\/(2 + 1\/(2 + 1\/(2))))))\r\n1 + 1\/(2 + 1\/(147))                              \r\n2 + 1\/(-2 + 1\/(-2 + 1\/(-3 + 1\/(4 + 1\/(2)))))     \r\n2 + 1\/(-3 + 1\/(-7 + 1\/(-81)))                    \r\n2 + 1\/(-5 + 1\/(2 + 1\/(3 + 1\/(-2 + 1\/(-15)))))    \r\n2 + 1\/(-9 + 1\/(11))                              \r\n2                                                \r\n<\/pre>\n<p>These are unconventional continued fractions because they contain negative terms.  This algorithm is used for <tt>format rat<\/tt>.  The default tolerance is much too picky.  The equal temperament ratios do not give small integers.<\/p>\n<pre class=\"codeinput\">   format <span class=\"string\">rat<\/span>\r\n   sigma.^(0:12)'\r\n<\/pre>\n<pre class=\"codeoutput\">ans =\r\n       1       \r\n    1657\/1564  \r\n    1769\/1576  \r\n    1785\/1501  \r\n     635\/504   \r\n    3544\/2655  \r\n    1393\/985   \r\n    2655\/1772  \r\n    1008\/635   \r\n    3002\/1785  \r\n    1527\/857   \r\n    2943\/1559  \r\n       2       \r\n<\/pre>\n<p>This would not be a satisfactory way to try to tune an instrument. But if I&#8217;m much more tolerant and allow a 2 percent error, I get short continued fractions.<\/p>\n<pre class=\"codeinput\">   rat(sigma.^(0:12)',.02)\r\n<\/pre>\n<pre class=\"codeoutput\">ans =\r\n1                  \r\n1 + 1\/(17)         \r\n1 + 1\/(8)          \r\n1 + 1\/(5)          \r\n1 + 1\/(4)          \r\n1 + 1\/(3)          \r\n1 + 1\/(2 + 1\/(2))  \r\n1 + 1\/(2)          \r\n2 + 1\/(-2 + 1\/(-2))\r\n2 + 1\/(-3)         \r\n2 + 1\/(-5)         \r\n2 + 1\/(-9)         \r\n2                  \r\n<\/pre>\n<p>I can make <tt>format rat<\/tt> use these fractions by telling <tt>rats<\/tt> that it has only 6 columns to work with.  Now I get almost the same ratios as just intonation.<\/p>\n<pre class=\"codeinput\">   rats(sigma.^(0:12)',6)\r\n<\/pre>\n<pre class=\"codeoutput\">ans =\r\n   1   \r\n   1   \r\n  9\/8  \r\n  6\/5  \r\n  5\/4  \r\n  4\/3  \r\n  7\/5  \r\n  3\/2  \r\n  8\/5  \r\n  5\/3  \r\n  9\/5  \r\n 17\/9  \r\n   2   \r\n<\/pre>\n<h4>Rewriting Music History<a name=\"b1667eb4-123e-4229-af73-a862fe54a469\"><\/a><\/h4>\n<p>If any of the music theorists from ancient Greece or the Renaissance had access to MATLAB, they could have used our rational approximation to generate a tuning strategy.<\/p>\n<p><script language=\"JavaScript\"> <!-- \n    function grabCode_d8c46a672af145278c3eb001b876b263() {\n        \/\/ Remember the title so we can use it in the new page\n        title = document.title;\n\n        \/\/ Break up these strings so that their presence\n        \/\/ in the Javascript doesn't mess up the search for\n        \/\/ the MATLAB code.\n        t1='d8c46a672af145278c3eb001b876b263 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\n        t2='##### ' + 'SOURCE END' + ' #####' + ' d8c46a672af145278c3eb001b876b263';\n    \n        b=document.getElementsByTagName('body')[0];\n        i1=b.innerHTML.indexOf(t1)+t1.length;\n        i2=b.innerHTML.indexOf(t2);\n \n        code_string = b.innerHTML.substring(i1, i2);\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\n\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \n        \/\/ in the XML parser.\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\n        \/\/ doesn't go ahead and substitute the less-than character. \n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\n\n        copyright = 'Copyright 2016 The MathWorks, Inc.';\n\n        w = window.open();\n        d = w.document;\n        d.write('\n\n<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add copyright line at the bottom if specified.\r\n        if (copyright.length > 0) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\n\n\\n');\n\n        d.title = title + ' (MATLAB code)';\n        d.close();\n    }   \n     --> <\/script><\/p>\n<p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><a href=\"javascript:grabCode_d8c46a672af145278c3eb001b876b263()\"><span style=\"font-size: x-small;        font-style: italic;\">Get<br \/>\n      the MATLAB code <noscript>(requires JavaScript)<\/noscript><\/span><\/a><\/p>\n<p>      Published with MATLAB&reg; R2016a<\/p>\n<\/div>\n<p><!--\nd8c46a672af145278c3eb001b876b263 ##### SOURCE BEGIN #####\n%% Math and Music\n% What does $\\sqrt[12]{2}$ have to do with music?\n% What are _equal temperament_ and _just intonation_?\n% How can the MATLAB function |rats| help tune a piano?\n% (This post is based in part on the _Music_ chapter in\n% my online book, _Experiments in MATLAB_.)\n\n%% Octaves and semitones\n% In the theory of music, an _octave_ is an interval\n% with frequencies that range over a factor of two.\n% In most Western music, an octave is divided\n% into 12 _semitones_ in a geometric progression.\n% In other words the notes have equal frequency ratios.  \n% Since 12 semitones comprise a factor of 2,\n% a single semitone is a factor of $\\sqrt[12]{2}$.\n% And because this quantity occurs so often in this discussion, let\n% \n% $$ \\sigma = \\sqrt[12]{2} $$\n% \n% Our MATLAB programs use\n\n    format short\n    sigma = 2^(1\/12)\n\n%%    \n% Think of $\\sigma$ as an important mathematical constant,\n% like $\\pi$ and $\\phi$.\n\n%% Keyboard \n% Here is our miniature piano keyboard with 25 keys.\n\n     small_keyboard\n  \n%%\n% This keyboard has two octaves, with white keys labeled *C D ... G A B*,\n% plus another *C* key.  Counting both white and black,\n% there are twelves keys in each octave.\n% The frequency of each key is a semitone above or below its neighbors.\n% Each black key can be regarded as either the _sharp_ of the white\n% below it or the _flat_ of the white above it.  So the black key\n% between *C* and *D* is both *C* $\\sharp$ and *D* $\\flat$.\n% There are no *E* $\\sharp$ \/ *F* $\\flat$ or *B* $\\sharp$ \/ *C* $\\flat$.\n\n%%\n% A conventional full piano keyboard has 88 keys.  Seven complete octaves\n% account for $7 \\times 12 = 84$ keys.  There are three additional\n% keys at the lower left and one additional key at the upper end.\n% If the octaves are numbered 0 through 7, then a key letter followed by an\n% octave number specifies a unique key.  In this notation, two\n% important keys are *C4* and *A4*.  The *C4* key is near the center of\n% the keyboard and so is also known as _middle C_. A piano is usually\n% tuned so that the frequency of the *A4* key is 440 Hz.  *C4* is nine\n% keys to the left of *A4* so its frequency is\n% \n% $$\\mbox{C4} = 440 \\sigma^{-9} \\approx 261.6256 \\mbox{Hz}$$\n%\n% Our miniature keyboard has the *A4* key colored green and the *C4* key\n% colored blue.\n\n%% Equal Temperament\n% A piano is almost always tuned with _equal temperament_, the system\n% used by musical instruments with fixed pitch.\n% Starting with *A4* at 440 Hz the keys are tuned in strict geometric\n% progression with ratio $\\sigma$.  Once this is done, a piano rarely \n% needs adjustment.\n\n%% Just Intonation\n% The alternative system, _just intonation_, is defined by a vector of\n% ratios involving small integers.\n\n   format rat\n   just = [1 16\/15 9\/8 6\/5 5\/4 4\/3 7\/5 3\/2 8\/5 5\/3 7\/4 15\/8 2]'\n   \n%%\n% These ratios allow the generation of overtones, chords and harmony.\n% More about that in a moment.  Many musical instruments can be tuned\n% to the key of a particular composition just before playing it.  Singers,\n% choirs, and barbershop quartets can naturally use just intonation.\n\n%%\n% Here is a comparison of equal temperament and just intonation from a\n% strictly numerical point of view.\n% Equal temperament is defined by repeated powers of $\\sigma$.\n% while just intonation is defined by a sequence of ratios.\n\n   sigma = 2^(1\/12);\n   k = (0:12)';\n   equal = sigma.^k;\n   num = [1 16 9 6 5 4 7 3 8 5 7 15 2]';\n   den = [1 15 8 5 4 3 5 2 5 3 4  8 1]';\n   just = num.\/den;\n   delta = (equal - just).\/equal;\n   T = [k equal num den just delta];\n   fprintf('   k       equal           just              delta\\n')\n   fprintf('%4d %12.6f %7d\/%d %11.6f %10.4f\\n',T')\n   \n%%\n% The last column in the table, |delta|, is the relative difference\n% between the two.  We see that |delta| is less than one percent,\n% except for |k = 10|.\n\n%%\n% This is why the 12-note scale has proved to be so popular.\n% Powers of $\\sqrt[12]{2}$ turn out to come very close to important\n% rational numbers, especially 5\/4, 4\/3, and 3\/2.  A pair of pitches\n% with a ratio of 3:2 is known as a _perfect fifth_; a ratio of 4:3\n% is a _perfect fourth_; a ratio of 5:4 is a _major third_.\n\n%% Do Re Mi\n% One of the first songs you learned to sing was\n% \n% _Do Re Mi Fa So La Ti Do_\n% \n% If you start at middle C, you will be singing the _major scale_ in the\n% key of C.  This scale is played on a piano using only the white keys.\n% The steps are not equally spaced.  Most of the steps skip over\n% black keys and so are two semitones.  But the steps between\n% _Mi_ and _Fa_ and _Ti_ and _Do_\n% are the steps from *E* to *F* and *B* to *C*.  There are no\n% intervening black keys and so these steps are only one semitone.\n% In terms of $\\sigma$, the C-major scale is\n% \n% $$\\sigma^0 \\ \\sigma^2 \\ \\sigma^4 \\ \\sigma^5 \\ \\sigma^7 \\ \\sigma^9\n%    \\ \\sigma^{11} \\ \\sigma^{12}$$ \n%\n% The number of semitones between the notes is given by the vector\n% \n    format short\n    diff([0 2 4 5 7 9 11 12])\n\n%%\n% This sequence of frequencies in our most common scale is surprising.\n\n%% Vibrations and Waves\n% Like everything else, it all comes down to _eigenvalues_.\n% Musical instruments create sound through the action of vibrating strings\n% or vibrating columns of air that, in turn, produce vibrations in the \n% body of the instrument and the surrounding air.\n% Mathematically, vibrations can be modeled by weighted sums of \n% characteristic functions known as _modes_ or _eigenfunctions_.\n% Different modes vibrate at different characteristic frequencies\n% or _eigenvalues_. These frequencies are determined by physical \n% parameters such as the length,\n% thickness and tension in a string, or the geometry of the air cavity.\n% Short, thin, tightly stretched strings have high frequencies,\n% while long, thick, loosely stretched strings have low frequencies.\n\n%%\n% The simplest model is a one-dimensional vibrating string, \n% held fixed at its ends.\n% The units of the various physical parameters can be chosen so\n% that the length of the string is $2 \\pi$.\n% The modes are then simply the functions\n% \n% $$v_n(x) = \\sin{n x}, \\ \\, n = 1, 2, ...$$\n%\n% Each of these functions satisfy the fixed end point conditions\n% \n% $$v_n(0) = v_n(2 \\pi) = 0$$\n% \n% The time-dependent modal vibrations are\n% \n% $$u_n(x,t) = \\sin{n x} \\sin{n t}, \\ \\, n = 1, 2, ...$$\n% \n% and the frequency is simply the integer $n$.\n% (Two- and three-dimensional models are much more complicated.\n% This one-dimensional model is all we need here.)\n% \n% Our <https:\/\/www.mathworks.com\/moler\/exm\/\n% Experiments with MATLAB> program \n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/37977-experiments-with-matlab-experiments-with-matlab\/content\/exm_2014\/vibrating_string.m\n% |vibrating_string|> provides a dynamic view.\n% Here is a snapshot showing the first nine modes\n% and the resulting wave traveling along the string.\n%\n% <<vibes.png>>\n%\n% <<wave.png>>\n%\n\n%%\n% The ratios in just intonation or equal temperament produce instruments\n% with tunings that generate vibrations with these desired frequencies.\n\n%% rat and rats\n% The MATLAB expression |rat(X,tol)| creates a truncated continued\n% fraction approximation of |X| with an accuracy of |tol|.  The default\n% |tol| is |1.e-6*norm(X,1)|.\n\n   rat(sigma.^(0:12)')\n   \n%%   \n% These are unconventional continued fractions because they contain\n% negative terms.  This algorithm is used for |format rat|.  The \n% default tolerance is much too picky.  The equal temperament ratios\n% do not give small integers.  \n\n   format rat\n   sigma.^(0:12)'\n   \n%%\n% This would not be a satisfactory way to try to tune an instrument.\n% But if I'm much more tolerant and allow a 2 percent error, I get\n% short continued fractions.\n\n   rat(sigma.^(0:12)',.02)\n   \n%%\n% I can make |format rat| use these fractions by telling |rats|\n% that it has only 6 columns to work with.  Now I get almost the same\n% ratios as just intonation.\n\n   rats(sigma.^(0:12)',6)\n   \n%% Rewriting Music History\n% If any of the music theorists from ancient Greece or the Renaissance\n% had access to MATLAB, they could have used our rational approximation\n% to generate a tuning strategy.\n##### SOURCE END ##### d8c46a672af145278c3eb001b876b263\n--><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img src=\"https:\/\/blogs.mathworks.com\/cleve\/files\/vibes.png\" class=\"img-responsive attachment-post-thumbnail size-post-thumbnail wp-post-image\" alt=\"\" decoding=\"async\" loading=\"lazy\" \/><\/div>\n<p><!--introduction--><\/p>\n<p>What does $\\sqrt[12]{2}$ have to do with music? What are <i>equal temperament<\/i> and <i>just intonation<\/i>? How can the MATLAB function <tt>rats<\/tt> help tune a piano? (This post is based in part on the <i>Music<\/i> chapter in my online book, <i>Experiments in MATLAB<\/i>.)&#8230; <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/cleve\/2016\/06\/09\/math-and-music\/\">read more >><\/a><\/p>\n","protected":false},"author":78,"featured_media":1780,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[13,5,22],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/posts\/1757"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/users\/78"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/comments?post=1757"}],"version-history":[{"count":2,"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/posts\/1757\/revisions"}],"predecessor-version":[{"id":1759,"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/posts\/1757\/revisions\/1759"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/media\/1780"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/media?parent=1757"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/categories?post=1757"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/tags?post=1757"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}