{"id":6134,"date":"2019-05-12T21:48:04","date_gmt":"2019-05-13T01:48:04","guid":{"rendered":"http:\/\/pages.vassar.edu\/magnes\/?p=6134"},"modified":"2019-05-12T21:48:04","modified_gmt":"2019-05-13T01:48:04","slug":"guitar-simulation-using-the-karplus-strong-algorithm","status":"publish","type":"post","link":"https:\/\/pages.vassar.edu\/magnes\/2019\/05\/12\/guitar-simulation-using-the-karplus-strong-algorithm\/","title":{"rendered":"Guitar Simulation using the Karplus-Strong Algorithm"},"content":{"rendered":"<h1><span style=\"font-weight: 400\">Overview<\/span><\/h1>\n<p><span style=\"font-weight: 400\">The program I wrote attempted to simulate the sound of a guitar using MatLab code. \u00a0I used the Karplus-Strong algorithm to generate this sound. This algorithm is a method of string synthesis that uses a phase delay and a finite impulse response (FIR) filter to condition a random signal into a wave that can be audibly played to sound like notes and chords of a guitar in the key of G. \u00a0There were three stages to this program. The first was to analyze an open string, individually. The second was to introduce the use of frets to the strings. Frets are the divisions along the neck of a guitar where the player will place his or her fingers to change the pitch generated by the string. The third stage was to put these two together, and incorporate all six strings to generate chords.<\/span><\/p>\n<h1><span style=\"font-weight: 400\">Initializing Variables<\/span><\/h1>\n<p><span style=\"font-weight: 400\">The first thing we must do is define our variables. \u00a0We have our sampling frequency, \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0<\/span><span style=\"font-weight: 400\">F_s = 44100. \u00a0We then set the frequencies which the strings are tuned to. They are as follows:<\/span><\/p>\n<p><span style=\"font-weight: 400\">E = 105 Hz<\/span><\/p>\n<p><span style=\"font-weight: 400\">A = 110 Hz<\/span><\/p>\n<p><span style=\"font-weight: 400\">D = 115 Hz<\/span><\/p>\n<p><span style=\"font-weight: 400\">G = 120 Hz<\/span><\/p>\n<p><span style=\"font-weight: 400\">B = 124 Hz<\/span><\/p>\n<p><span style=\"font-weight: 400\">E2 = 129 Hz<\/span><\/p>\n<p><span style=\"font-weight: 400\">We also define the \u201coffset\u201d in the strings by finding the difference between a given string and the A string. (So for string X we take the frequencies X &#8211; A = Xoffset). \u00a0Next, we allocate a frequency vector and discretize time.<\/span><\/p>\n<h1><span style=\"font-weight: 400\">Open String<\/span><\/h1>\n<p>The Karplus-Strong algorithm works by taking an input of noise and channeling it into a delay line, then through the filter and outputs this sound.<span class=\"Apple-converted-space\">\u00a0 <\/span>The phase delay here can be calculated with the following equation, where F_s is the sampling frequency, or Nyquist frequency, and F_0 is the base frequency, or frequency of the string. <span class=\"Apple-converted-space\">\u00a0<\/span><\/p>\n<p>D = F_s \/ F_0<\/p>\n<p>We use the A string for the open string analysis, so we use the frequency of 110 Hz and our sampling frequency, 44100 Hz.<span class=\"Apple-converted-space\">\u00a0 <\/span>We can now create the FIR filter.<span class=\"Apple-converted-space\">\u00a0 <\/span>Using the MatLab code <i>firls<\/i>, we use a FIR filter with a least squares approximation of the noise signal to the desired harmonics of the A string.<span class=\"Apple-converted-space\">\u00a0 <\/span>We define our initial conditions for this by inputing poles into our filter which approximate these harmonics, so as the signal passes through the filter, it takes the shape of our desired harmonics, so when it is output, it sounds like the A string being plucked.<span class=\"Apple-converted-space\">\u00a0 <\/span>While a Fourier transform could also be used to filter the noisy input into a more coherent output, it does not achieve the same level of replication as the FIR filter does.<span class=\"Apple-converted-space\">\u00a0 <\/span>From here, we simply normalize the note and using the MatLab code <i>audioplayer, <\/i>we format it to be played aloud.<\/p>\n<div id=\"attachment_6136\" style=\"width: 635px\" class=\"wp-caption alignleft\"><a href=\"http:\/\/pages.vassar.edu\/magnes\/files\/2019\/05\/Screen-Shot-2019-05-02-at-2.35.53-AM.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-6136\" class=\"wp-image-6136 \" src=\"http:\/\/pages.vassar.edu\/magnes\/files\/2019\/05\/Screen-Shot-2019-05-02-at-2.35.53-AM.png\" alt=\"\" width=\"625\" height=\"228\" srcset=\"https:\/\/pages.vassar.edu\/magnes\/files\/2019\/05\/Screen-Shot-2019-05-02-at-2.35.53-AM.png 570w, https:\/\/pages.vassar.edu\/magnes\/files\/2019\/05\/Screen-Shot-2019-05-02-at-2.35.53-AM-300x109.png 300w\" sizes=\"auto, (max-width: 625px) 100vw, 625px\" \/><\/a><p id=\"caption-attachment-6136\" class=\"wp-caption-text\">Fig. 1<\/p><\/div>\n<p>Here, we see the harmonics of the A string from the generated note. \u00a0As desired, the harmonics are evenly spaced. \u00a0It can also be observed that there are certain harmonics have a greater magnitude than the rest, and those are the ones most audible to us.<\/p>\n<div id=\"attachment_6139\" style=\"width: 636px\" class=\"wp-caption alignleft\"><a href=\"http:\/\/pages.vassar.edu\/magnes\/files\/2019\/05\/Screen-Shot-2019-05-12-at-6.45.03-PM.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-6139\" class=\"wp-image-6139 \" src=\"http:\/\/pages.vassar.edu\/magnes\/files\/2019\/05\/Screen-Shot-2019-05-12-at-6.45.03-PM.png\" alt=\"\" width=\"626\" height=\"195\" srcset=\"https:\/\/pages.vassar.edu\/magnes\/files\/2019\/05\/Screen-Shot-2019-05-12-at-6.45.03-PM.png 549w, https:\/\/pages.vassar.edu\/magnes\/files\/2019\/05\/Screen-Shot-2019-05-12-at-6.45.03-PM-300x93.png 300w\" sizes=\"auto, (max-width: 626px) 100vw, 626px\" \/><\/a><p id=\"caption-attachment-6139\" class=\"wp-caption-text\">Fig. 2<\/p><\/div>\n<p>In this figure, we compare the harmonics of the A string, from above, to the harmonics of a D string. \u00a0It is apparent that the D string has a marginally larger harmonic than the A string, and that the difference perpetuates as we take into account more and more harmonics until they are entirely out of sync. \u00a0We can also note that the D harmonic has the same properties as the A harmonic in that it has significant peaks, and the the D string&#8217;s peaks are higher than that of the A string.<\/p>\n<div id=\"attachment_6140\" style=\"width: 1212px\" class=\"wp-caption alignleft\"><a href=\"http:\/\/pages.vassar.edu\/magnes\/files\/2019\/05\/Screen-Shot-2019-05-02-at-2.36.45-AM.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-6140\" class=\"size-full wp-image-6140\" src=\"http:\/\/pages.vassar.edu\/magnes\/files\/2019\/05\/Screen-Shot-2019-05-02-at-2.36.45-AM.png\" alt=\"\" width=\"1202\" height=\"746\" srcset=\"https:\/\/pages.vassar.edu\/magnes\/files\/2019\/05\/Screen-Shot-2019-05-02-at-2.36.45-AM.png 1202w, https:\/\/pages.vassar.edu\/magnes\/files\/2019\/05\/Screen-Shot-2019-05-02-at-2.36.45-AM-300x186.png 300w, https:\/\/pages.vassar.edu\/magnes\/files\/2019\/05\/Screen-Shot-2019-05-02-at-2.36.45-AM-768x477.png 768w, https:\/\/pages.vassar.edu\/magnes\/files\/2019\/05\/Screen-Shot-2019-05-02-at-2.36.45-AM-1024x636.png 1024w, https:\/\/pages.vassar.edu\/magnes\/files\/2019\/05\/Screen-Shot-2019-05-02-at-2.36.45-AM-624x387.png 624w\" sizes=\"auto, (max-width: 1202px) 100vw, 1202px\" \/><\/a><p id=\"caption-attachment-6140\" class=\"wp-caption-text\">Fig. 3<\/p><\/div>\n<p>Here we can compare the harmonics of all six strings individually, and then look at when all six are superimposed. \u00a0When we look at all six on the same graph, it is very easy to see how they all have a similar sized harmonic and how quickly they disperse as they multiply. \u00a0The lack of coincidence here is why it is not used as a chord in music. \u00a0Chords manipulate the strings using frets to create more coincidence in the harmonics so that certain frequencies stand out above the rest.<\/p>\n<h1><span style=\"font-weight: 400\">String with Frets<\/span><\/h1>\n<p>The second stage is to introduce the use of frets to our simulation.<span class=\"Apple-converted-space\">\u00a0 <\/span>Each fret that we move down the neck of the guitar alters the pitch by a half step.<span class=\"Apple-converted-space\">\u00a0 <\/span>That is, one fret changes the base frequency by a factor of 2^1\/12.<span class=\"Apple-converted-space\">\u00a0 <\/span>So, our new equation for the delay is:<\/p>\n<p>D = F_s \/ (F_0 * 2^(fret\/12))<\/p>\n<p>where \u201cfret\u201d is the number of the fret we are using, where 1 is nearest the top of the neck.<\/p>\n<div id=\"attachment_6142\" style=\"width: 659px\" class=\"wp-caption alignleft\"><a href=\"http:\/\/pages.vassar.edu\/magnes\/files\/2019\/05\/Screen-Shot-2019-05-02-at-2.36.15-AM.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-6142\" class=\"size-full wp-image-6142\" src=\"http:\/\/pages.vassar.edu\/magnes\/files\/2019\/05\/Screen-Shot-2019-05-02-at-2.36.15-AM.png\" alt=\"\" width=\"649\" height=\"192\" srcset=\"https:\/\/pages.vassar.edu\/magnes\/files\/2019\/05\/Screen-Shot-2019-05-02-at-2.36.15-AM.png 649w, https:\/\/pages.vassar.edu\/magnes\/files\/2019\/05\/Screen-Shot-2019-05-02-at-2.36.15-AM-300x89.png 300w, https:\/\/pages.vassar.edu\/magnes\/files\/2019\/05\/Screen-Shot-2019-05-02-at-2.36.15-AM-624x185.png 624w\" sizes=\"auto, (max-width: 649px) 100vw, 649px\" \/><\/a><p id=\"caption-attachment-6142\" class=\"wp-caption-text\">Fig. 4<\/p><\/div>\n<p>This figure compares the harmonics of the open A string, as we saw in the previous section, and compares it to the A string on the 1st fret. \u00a0The 1st fret, as previously mentioned, changes the pitch by a half step, and here we see how it affects the harmonics. \u00a0The highest peak of the 1st fret is not nearly as high in magnitude as that of the open string, but the frequency of the harmonic only slightly varies from that of the open string.<\/p>\n<div id=\"attachment_6145\" style=\"width: 1189px\" class=\"wp-caption alignleft\"><a href=\"http:\/\/pages.vassar.edu\/magnes\/files\/2019\/05\/Screen-Shot-2019-05-02-at-2.34.47-AM-1.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-6145\" class=\"wp-image-6145 size-full\" src=\"http:\/\/pages.vassar.edu\/magnes\/files\/2019\/05\/Screen-Shot-2019-05-02-at-2.34.47-AM-1.png\" alt=\"\" width=\"1179\" height=\"743\" srcset=\"https:\/\/pages.vassar.edu\/magnes\/files\/2019\/05\/Screen-Shot-2019-05-02-at-2.34.47-AM-1.png 1179w, https:\/\/pages.vassar.edu\/magnes\/files\/2019\/05\/Screen-Shot-2019-05-02-at-2.34.47-AM-1-300x189.png 300w, https:\/\/pages.vassar.edu\/magnes\/files\/2019\/05\/Screen-Shot-2019-05-02-at-2.34.47-AM-1-768x484.png 768w, https:\/\/pages.vassar.edu\/magnes\/files\/2019\/05\/Screen-Shot-2019-05-02-at-2.34.47-AM-1-1024x645.png 1024w, https:\/\/pages.vassar.edu\/magnes\/files\/2019\/05\/Screen-Shot-2019-05-02-at-2.34.47-AM-1-624x393.png 624w\" sizes=\"auto, (max-width: 1179px) 100vw, 1179px\" \/><\/a><p id=\"caption-attachment-6145\" class=\"wp-caption-text\">Fig. 5<\/p><\/div>\n<p>Here, we see the difference in the harmonics as we progress on the A string from the 1st fret to the 5th fret, and can compare them to each other and to the open A string. \u00a0Similarly to the open strings, we do not see much coincidence at any particular frequency. \u00a0This should change as we move to look at chords.<\/p>\n<h1><span style=\"font-weight: 400\">Chords\u00a0<\/span><\/h1>\n<p>The third stage is to put together what we have accomplished in the first to stages, and expand on it to generate chords. We must account for all six strings, and the use of frets.<span class=\"Apple-converted-space\">\u00a0 <\/span>So, instead, we have to make our single values for one string into vectors of length six for the six strings.<span class=\"Apple-converted-space\">\u00a0 <\/span>When we define our fret vector, it corresponds to finger positions on the strings while playing a chord.<span class=\"Apple-converted-space\">\u00a0 <\/span>Then, the delay equation will be the same as above in stage 2, but now since delay is a vector, it will depend on the fret vector.<span class=\"Apple-converted-space\">\u00a0 <\/span>We then define and use the filter again, but this time we use a <i>for<\/i> loop to incorporate all the strings.<span class=\"Apple-converted-space\">\u00a0 <\/span>The last step is to synthesize the notes that we have created with the loop into one coherent chord.<\/p>\n<div id=\"attachment_6173\" style=\"width: 619px\" class=\"wp-caption alignleft\"><a href=\"http:\/\/pages.vassar.edu\/magnes\/files\/2019\/05\/Screen-Shot-2019-05-02-at-3.09.29-AM.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-6173\" class=\" wp-image-6173\" src=\"http:\/\/pages.vassar.edu\/magnes\/files\/2019\/05\/Screen-Shot-2019-05-02-at-3.09.29-AM.png\" alt=\"\" width=\"609\" height=\"469\" srcset=\"https:\/\/pages.vassar.edu\/magnes\/files\/2019\/05\/Screen-Shot-2019-05-02-at-3.09.29-AM.png 527w, https:\/\/pages.vassar.edu\/magnes\/files\/2019\/05\/Screen-Shot-2019-05-02-at-3.09.29-AM-300x231.png 300w\" sizes=\"auto, (max-width: 609px) 100vw, 609px\" \/><\/a><p id=\"caption-attachment-6173\" class=\"wp-caption-text\">Fig. 6<\/p><\/div>\n<p>In looking at a G major chord, we should first understand how we manipulate the strings. \u00a0The fret vector for a G major (or 1 (I) in the key of G) chord looks like:<\/p>\n<p>fret = [3 2 0 0 0 2]<\/p>\n<p>So, the first (E) string is played on the third fret, second (A) string on the second fret, sixth (E2) string on the second fret, and the other strings left open with no manipulation. \u00a0So, the figure above (<em>Fig. 6<\/em>), shows the harmonics of a G major chord. \u00a0When we looked at the open A chord, where we played the six open strings together, there was no frequency where the harmonics of the strings coincided. \u00a0In this case, we see several examples of this, including around 200 Hz and 480 Hz, just to name a few. \u00a0These sounds &#8220;work&#8221; together because the harmonics of the individual strings are multiples of each other. \u00a0For example, the frequency of the first string with the shortest harmonic is 1\/2 the frequency of the fourth string, so they align. \u00a0These are considered to be an octave apart. \u00a0Also, the frequency of the sixth string is double that of the fourth string, so it aligns with both the first and fourth strings. \u00a0The second and fifth string have the same relationship as the first and fourth, but at different frequencies. \u00a0This sound is considered to be more consonant, whereas the open A chord is considered to be more dissonant, and that is due to these harmonics.<\/p>\n<div id=\"attachment_6183\" style=\"width: 617px\" class=\"wp-caption alignleft\"><a href=\"http:\/\/pages.vassar.edu\/magnes\/files\/2019\/05\/Screen-Shot-2019-05-02-at-3.09.44-AM.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-6183\" class=\" wp-image-6183\" src=\"http:\/\/pages.vassar.edu\/magnes\/files\/2019\/05\/Screen-Shot-2019-05-02-at-3.09.44-AM.png\" alt=\"\" width=\"607\" height=\"471\" srcset=\"https:\/\/pages.vassar.edu\/magnes\/files\/2019\/05\/Screen-Shot-2019-05-02-at-3.09.44-AM.png 517w, https:\/\/pages.vassar.edu\/magnes\/files\/2019\/05\/Screen-Shot-2019-05-02-at-3.09.44-AM-300x233.png 300w\" sizes=\"auto, (max-width: 607px) 100vw, 607px\" \/><\/a><p id=\"caption-attachment-6183\" class=\"wp-caption-text\">Fig. 7<\/p><\/div>\n<p>Here, we look at a C major chord, which has a fret vector:<\/p>\n<p>fret = [0 3 2 0 1 0]<\/p>\n<p style=\"text-align: left\">We see the same relationship between harmonics, where similar strings align with each other, but they are at different frequencies because they are at different notes. \u00a0Though every chord may not show the same exact string relationships as these two, they will have some relationship between the harmonics of the strings, as evident below in figure 8.<\/p>\n<div id=\"attachment_6186\" style=\"width: 1213px\" class=\"wp-caption alignleft\"><a href=\"http:\/\/pages.vassar.edu\/magnes\/files\/2019\/05\/Screen-Shot-2019-05-02-at-11.25.49-AM.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-6186\" class=\"size-full wp-image-6186\" src=\"http:\/\/pages.vassar.edu\/magnes\/files\/2019\/05\/Screen-Shot-2019-05-02-at-11.25.49-AM.png\" alt=\"\" width=\"1203\" height=\"762\" srcset=\"https:\/\/pages.vassar.edu\/magnes\/files\/2019\/05\/Screen-Shot-2019-05-02-at-11.25.49-AM.png 1203w, https:\/\/pages.vassar.edu\/magnes\/files\/2019\/05\/Screen-Shot-2019-05-02-at-11.25.49-AM-300x190.png 300w, https:\/\/pages.vassar.edu\/magnes\/files\/2019\/05\/Screen-Shot-2019-05-02-at-11.25.49-AM-768x486.png 768w, https:\/\/pages.vassar.edu\/magnes\/files\/2019\/05\/Screen-Shot-2019-05-02-at-11.25.49-AM-1024x649.png 1024w, https:\/\/pages.vassar.edu\/magnes\/files\/2019\/05\/Screen-Shot-2019-05-02-at-11.25.49-AM-624x395.png 624w\" sizes=\"auto, (max-width: 1203px) 100vw, 1203px\" \/><\/a><p id=\"caption-attachment-6186\" class=\"wp-caption-text\">Fig. 8<\/p><\/div>\n<p>The last thing to do was to create a way to play these sounds.<span class=\"Apple-converted-space\">\u00a0 <\/span>So, we can utilize user input, so the user can choose what note or chord to play.<span class=\"Apple-converted-space\">\u00a0 <\/span>By using nested <i>if <\/i>statements, we can correspond the sound the user wishes to play, via input, to the code for the associated sound.<span class=\"Apple-converted-space\">\u00a0 <\/span>This way, by typing in A, the program will play the A string.<span class=\"Apple-converted-space\">\u00a0 <\/span>In this case, as often used in music, we named the chords by numbers 1 through 7. \u00a0There was no way to export the MatLab sound, so please refer to the MatLab code,\u00a0<em>GuitarPlay.m,<\/em>\u00a0that I submitted for that. \u00a0To play a string, type &#8216;E, A, D, G, B, E2&#8217; (with caps), and to play a chord, type &#8216;1, 2, 3, 4, 5, 6, 7&#8217; when prompted.<\/p>\n<h1><span style=\"font-weight: 400\">Future Aspirations<\/span><\/h1>\n<p>In the future, I would like to improve the efficiency of the program by making the coding more elegant. \u00a0As far as specific plans for the program, I would like to work out a way to play chords sequentially, so I could include chord progressions, and potentially parts of a song. \u00a0I would also like to compare how it would compare if a note was played on one open string, and then the same note was played on a different fretted string. \u00a0I would also like to import the sound from a real guitar and analyze the sound and the harmonics side by side to this simulation.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Overview The program I wrote attempted to simulate the sound of a guitar using MatLab code. \u00a0I used the Karplus-Strong algorithm to generate this sound. This algorithm is a method of string synthesis that uses a phase delay and a finite impulse response (FIR) filter to condition a random signal into a wave that can [&hellip;]<\/p>\n","protected":false},"author":6448,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-6134","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/pages.vassar.edu\/magnes\/wp-json\/wp\/v2\/posts\/6134","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pages.vassar.edu\/magnes\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pages.vassar.edu\/magnes\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pages.vassar.edu\/magnes\/wp-json\/wp\/v2\/users\/6448"}],"replies":[{"embeddable":true,"href":"https:\/\/pages.vassar.edu\/magnes\/wp-json\/wp\/v2\/comments?post=6134"}],"version-history":[{"count":6,"href":"https:\/\/pages.vassar.edu\/magnes\/wp-json\/wp\/v2\/posts\/6134\/revisions"}],"predecessor-version":[{"id":6205,"href":"https:\/\/pages.vassar.edu\/magnes\/wp-json\/wp\/v2\/posts\/6134\/revisions\/6205"}],"wp:attachment":[{"href":"https:\/\/pages.vassar.edu\/magnes\/wp-json\/wp\/v2\/media?parent=6134"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pages.vassar.edu\/magnes\/wp-json\/wp\/v2\/categories?post=6134"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pages.vassar.edu\/magnes\/wp-json\/wp\/v2\/tags?post=6134"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}