{"id":275,"date":"2013-04-16T13:19:20","date_gmt":"2013-04-16T02:19:20","guid":{"rendered":"http:\/\/x37v.com\/x37v\/?p=275"},"modified":"2013-06-14T16:40:01","modified_gmt":"2013-06-14T05:40:01","slug":"simple-karplus-strong-synthesis-and-note-length","status":"publish","type":"post","link":"https:\/\/x37v.com\/x37v\/writing\/simple-karplus-strong-synthesis-and-note-length\/","title":{"rendered":"Simple Karplus-Strong Synthesis and Note Length"},"content":{"rendered":"<p>Ever waste a whole bunch of time trying to understand something, where the only way to feel less guilty about wasting so much time on it is to tell someone about it? Me too\u2026\u00a0here goes.<\/p>\n<p>In between more important things, I was thinking about Karplus-Strong (KS) plucked string synthesis again the other day\u00a0\u2014\u00a0more than just the general concept of a filtered feedback network creating something that sounds like a plucked string \u2014 specifically, how high notes are quick to decay, and how low notes sustain for longer.<\/p>\n<p>The reason this happens is because of the <em>amplitude scaling<\/em> that is applied through each feedback recursion. Each feedback recursion multiplies the amplitude by some value less than (but close to) 1. Essentially, the note lasts as long as the recursive geometric sequence doesn&#8217;t tend to zero. Because the recursive geometric sequence terminates at zero (well, at least at some point in a fixed bit number system anyway), the <em>number of times<\/em> a grain repeats before fading to silence is exactly the same whether it&#8217;s a low or high note. Since the high notes have a short grain-length, they die quicker.<\/p>\n<p>I set out to figure out a way for one to specify note length, and have the algorithm figure out the scaling factor based on the desired pitch (as\u00a0the geometric sequence should occur more often, the closer it gets to 1). Not sure if someone has written about this before (there&#8217;s probably plenty published about it, and to be honest, I didn&#8217;t look), but it was an excuse to kick around a couple of under-used neurons in my brain.<\/p>\n<h3>Where to begin\u2026<\/h3>\n<p>When we think about sound in a medium (eg. in air, water, or earth), the relationship between pitch and wavelength is that they are inversely proportional about the speed of sound in the medium. The pitch is the number of oscillations per second, and the wavelength would be some value specified in metres, feet, etc. If something has a frequency <em>x<\/em>, the length of a cycle of the wave is <em>speed of sound in the medium<\/em> divided by frequency <em>x<\/em> \u2014 so if sound travels 343 feet per second in air, something with a frequency of 343 Hz would have a wavelength of 1 foot. A frequency of 440Hz would have a wavelength of 0.78 feet, etc. etc.<\/p>\n<p><!-- Bored? Me too. --><\/p>\n<p>It&#8217;s more useful to think of wavelength in the temporal sense though, when it comes to KS synthesis.<br \/>\n&#8216;Wavelength&#8217; in KS synthesis is really just &#8216;time in ms&#8217; of a repeating grain\/cycle. I&#8217;ll refer to this from here onwards as cycle-time, or grain-width\u2026 ehhh, I can&#8217;t decide. Grain-width is probably better. Essentially, in KS synthesis, we&#8217;re more interested in the time it takes for a cycle of the wave to complete. Therefore, in the KS algorithm, the delay-line length of the repeating wave (the grain-width) in ms is 1000 \/ frequency.<\/p>\n<p>Why? Because the frequency is the number of time it repeats each second. For example, a frequency of 440 Hz would have a grain-width of 1000\/ 440 = ~2.27ms.  A frequency of 343 Hz would have a cycle-time\u2026\u00a0I mean <em>grain-width<\/em>\u2026 of ~2.915ms. This is the &#8216;length&#8217; of the repeated noise burst.<\/p>\n<h3>Calculating the Scaling Factor<\/h3>\n<p>Firstly, let&#8217;s lock the desired length of the plucked sound to be 2 seconds. In order for something to play for 2 seconds, we need to know the pitch we want to hear, and consequently adjust the multiplication factor so that for a given cycle-time\/grain-width, a repeated wave will terminate after a specific number of recursions.<\/p>\n<p>OK\u2026 so let&#8217;s say we want to play a 440 Hz note. The cycle-time is 2.27ms. The sound should repeat 880 times (given that it repeats 440 times per second).<\/p>\n<p>It wasn&#8217;t immediately obvious to me how I should figure out how to set a multiplication value such that a geometric sequence (starting at 1., ending at 0.) should terminate after 880 recursions.  The answer to this question might be fairly straightforward to someone out there with a background in number manipulation (or quantisation) in fixed bit number bit systems, but it seemed like the only way for me to understand this was to plot out the &#8216;recursions&#8217; given a series of fixed scaling factors.<\/p>\n<p>So, to figure this out, I did something really dull\u2026 (I&#8217;ll call it brute-force &#8220;modelling&#8221; to make it sound more interesting than it is). I set up a really basic system to count the number of times that a number (1.) is recursively scaled (with values &lt; 1.) until it was less than 0.000001*. At an amplitude of 0.000001, I figured the sound was essentially silent (for 16-bit audio anyway). The way I did it was pretty rough, but that didn&#8217;t matter. I just wanted to glean a sense of how the scaling factor affected the number of recursions.<\/p>\n<blockquote style=\"border: 2px solid black; padding: 5px 10px; background-color: #f9f9f9;\"><p>*Disclaimer: Having a poor understanding of number representation in the computer, I couldn&#8217;t count the number of times the function &#8216;actually&#8217; happened because if I set the counter function to stop when the geometric sequence &#8216;equalled&#8217; zero, it kept counting. I figured it had something to do with <a href=\"http:\/\/en.wikipedia.org\/wiki\/Denormal_number\" target=\"_blank\">subnormal numbers<\/a> \u2014\u00a0or that the internal mechanics of the &#8216;accum&#8217; object had a finer resolution than the upper world of Max (or maybe it&#8217;s that I just upgraded to Max 6.1, where it appears that objects are working internally with 64bit precision\u2026\u00a0I think).<\/p>\n<p>I therefore used an arbitrary small value, namely 0.000001 (which seemed to be the smallest number in magnitude that I could use as an argument in an object or write in a message box).<\/p><\/blockquote>\n<h3>This is what I learned:<\/h3>\n<p>Starting with 1, the following values were less than 0.000001 after being recursively multiplied <em>n<\/em> times:<\/p>\n<table cellspacing=\"0\" cellpadding=\"3\">\n<tbody>\n<tr>\n<td valign=\"middle\">0.90 = 131 recursions<\/td>\n<td valign=\"middle\">0.990 = 1374 recursions<\/td>\n<td valign=\"middle\">0.9990 = 13808 recursions<\/td>\n<\/tr>\n<tr>\n<td valign=\"middle\">0.91 = 146 recursions<\/td>\n<td valign=\"middle\">0.991 = 1528 recursions<\/td>\n<td valign=\"middle\">0.9991 = 15343 recursions<\/td>\n<\/tr>\n<tr>\n<td valign=\"middle\">0.92 = 165 recursions<\/td>\n<td valign=\"middle\">0.992 = 1720 recursions<\/td>\n<td valign=\"middle\">0.9992 = 17262 recursions<\/td>\n<\/tr>\n<tr>\n<td valign=\"middle\">0.93 = 190 recursions<\/td>\n<td valign=\"middle\">0.993 = 1966 recursions<\/td>\n<td valign=\"middle\">etc.<\/td>\n<\/tr>\n<tr>\n<td valign=\"middle\">0.94 = 223 recursions<\/td>\n<td valign=\"middle\">0.994 = 2295 recursions<\/td>\n<td valign=\"middle\"><\/td>\n<\/tr>\n<tr>\n<td valign=\"middle\">0.95 = 269 recursions<\/td>\n<td valign=\"middle\">0.995 = 2756 recursions<\/td>\n<td valign=\"middle\"><\/td>\n<\/tr>\n<tr>\n<td valign=\"middle\">0.96 = 338 recursions<\/td>\n<td valign=\"middle\">0.996 = 3446 recursions<\/td>\n<td valign=\"middle\"><\/td>\n<\/tr>\n<tr>\n<td valign=\"middle\">0.97 = 453 recursions<\/td>\n<td valign=\"middle\">0.997 = 4598 recursions<\/td>\n<td valign=\"middle\"><\/td>\n<\/tr>\n<tr>\n<td valign=\"middle\">0.98 = 683 recursions<\/td>\n<td valign=\"middle\">0.998 = 6900 recursions<\/td>\n<td valign=\"middle\"><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>The pattern I spotted was that with more precision (as we tend toward 1.), the resolution jumps tenfold. Seems somewhat straightforward. I thought this over, and came to the conclusion that we can model this &#8216;precision&#8217; by:<\/p>\n<p>subtracting the scaling factor from 1, and then dividing 1 by that number.<\/p>\n<blockquote><p>eg. for 0.98:<\/p>\n<p>1. &#8211; 0.98 = 0.02<br \/>\n1. \/ 0.02 = 50.<\/p>\n<p>and 0.998:<\/p>\n<p>1. &#8211; 0.998 = 0.002<br \/>\n1. \/ 0.002 = 500.<\/p><\/blockquote>\n<p>This produced values that shared the same ratios as the figures I got from my tests, but the numbers themselves were proportionally out by another value. Dividing the numbers produced by my recursive scaling tests by these precision &#8216;ratios&#8217; I derived, I get a value somewhere around 13.75. I don&#8217;t know what this means\u2026 I&#8217;ll try to understand this later.<\/p>\n<p>Firstly though, in order for me to calculate the scaling factor for a given pitch, I should do some kind of lookup (or reversal of the above process): find a value that terminates after <em>n<\/em> recursions, and set that as the scaling factor.<\/p>\n<p>So, back to the example of making a note ring on for two seconds, lets pick a pitch and try it out\u2026 Say 440Hz.<\/p>\n<p>OK. 440Hz will recur 880 times in two seconds.<\/p>\n<p>If we want 880 recursions, our scaling factor should be somewhere between 0.98 and 0.99. Given that 0.98 produces 683 recursions, and 0.99 produces 1374, there&#8217;s quite a bit of leeway there.<\/p>\n<p>Let&#8217;s try using the reverse of the formula (with the mystical ~13.75 ) to try to derive the scaling factor. [Strangely, I only just noticed the 1374 on the line above\u2026\u00a0maybe it&#8217;s just a coincidence.]<\/p>\n<blockquote><p>880 (number of recursions) \/ 13.75 = 64<\/p>\n<p>1 \/ 64 = 0.015625<\/p>\n<p>1 &#8211; 0.015625 = 0.984375 (Fits the conditions of being between 0.98 and 0.99. Good).<\/p><\/blockquote>\n<p>The formula to calculate scaling factor therefore appears to be,<\/p>\n<blockquote><p>1 minus the inverse of ((pitch * length in seconds) \/ 13.75)<\/p>\n<p>or (since 13.75 is just a constant)<\/p>\n<p>1. &#8211; (1. \/ ((frequency * length in seconds) \/ magicNumber))<\/p><\/blockquote>\n<p>It&#8217;s not perfect, but notes now seem to sustain for similar lengths, whether played low or high.<\/p>\n<h3>Other thoughts<\/h3>\n<p>Up to this point (in order to remove variables) I&#8217;d been working with an ideal situation, where no low-pass filter existed in the KS feedback network. Depending on the cutoff (or amount that is attenuated), the filter plays a big part in how long the note appears to resonate. Notes down the bottom still start with a broader spectrum, so a proportional filter cut-off (based on the fundamental of the synthesised note) might be a good idea too. I&#8217;ll think about that another day. After adding a fixed cut-off it doesn&#8217;t sound bad as it is though.<\/p>\n<h3>What have I learned from this?<\/h3>\n<ol>\n<li>If something is percolating away in your brain, it&#8217;s faster to go about something in a laborious way and put pen to paper with the data you acquire, than it is for something to become self-evident.<\/li>\n<li>That producing something useful with the Karplus-Strong algorithm is much more complicated than scaling\/filtering feedback of noise.<\/li>\n<\/ol>\n<h3>What am I still to learn?<\/h3>\n<p>With regard to different scaling factors, I haven&#8217;t yet looked in depth at how different pitches decay with different values (basically, whether a geometric sequence is the same at different scales). From some basic tests in a spectrogram, the geometric sequence appears to be similar at different scales. Notes seem to decay at similar rates when the grain is smaller (and the scaling factor is consequently closer to 1). It isn&#8217;t immediately obvious to me that it should or shouldn&#8217;t be this way.<\/p>\n<p>At the end of this all\u2026 what&#8217;s with the number <em>magicNumber<\/em> constant (~13.75)? I still have no idea. Ultimately, though, the ~13.75 is irrelevant. I arrived at this number from the data I acquired just by counting the number of times 1 (when recursively multiplied by a value less than 1) tended to 0. In the actual synthesis implementation, setting it around 4.5 with a low-pass filter around 8000Hz makes notes appear to last around the desired length. It seems somewhat related to the filter cutoff-value in adjusting note length. (eg. a lower filter value causes the note to appear to fade more quickly, as we&#8217;re filtering out a lot of the note&#8217;s energy). It&#8217;s a little bit subjective though, and I only timed the length of plucked synth note by ear (and on laptop speakers).<\/p>\n<h3>Patch<\/h3>\n<p>Here&#8217;s the patch. Adjust the note length number box to produce longer or shorter notes. Make sure you switch between the &#8216;enable\/disable scaling adjustment&#8217; to hear the difference in note sustain lengths\u00a0(ie. compensating for different grain-widths to produce similar decay lengths).<\/p>\n<pre><code>\r\n----------begin_max5_patcher----------\r\n2143.3oc0bksbaaCE8Y6uBTN9g3TGEhMtjY5CY5zz9T9ApyjghBRlIbQkjJ1\r\nMYh+1KVn1rDAgkHgYGOVxjhB7hCt2ycCz+3xKblV7.qxA7NveCt3heb4EWHO\r\nk3DWzb7ENYQODmFUIuLmb18ES+hyMpOpl8Ps7zoEQyxXUU.2Iggq+z7UYI4o\r\nrZ42D1bx4E40UIemIOGZh61qsXU8Su3kQ0w2kju3ykr3ZkX5QI7uD.EREuED\r\nJOvchK3SMemjYRIhKkuIv0Ym6ZdTl7t579xjnz0excIylwx28lpji5+cIScG\r\ncb.eR7I+7xKEubigHUbQVFKu9.n5Oxillxd6rjJw6fp3nT9LDDM6Kqpp28a7\r\nbfukkrJ9WMpNoHeGvhDPlfn2.vdRzxs4kMX09n95Aa5pobIqi0ARHdBzyyKv\r\nWL7p0DW0pA43qF9AcuZLubwTonLwsGg7zh6WxuFv7jzZVIHdUcw74fjbve88\r\ndErgS74fMwE26fc.VtN1LzPOWs.s2KEPmEkuJJciN87n35hx9DhoHID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D1Z.t4xicHr8vlHO\r\nH64.wDmxjQUHczP6QmYRHcT6JN3NDGruckGTWJyA1Ud5JgIjEWuLgsWtdYGi\r\nchQQKDXO4wDqK64qvDwgXOxGrQttnVqbDl34BFZMkmmvqnAdfimJyQBFWoai\r\nrXkKMwXGNtpVCzdvCzD0Yn8xrD5Yp4tkjGSXmg3mWpShpGI+mNi7A3bcsjjG\r\nN.4FAwlVuaKAon9.RaSdTsBJZ4xuwJqZFSon3jE8khRwgd2bo5esRpCkcuwo\r\nj8sj0Wu5LQkw2kTyhqWUpZc2CAdNWJtO+7x+CLx3iHB\r\n-----------end_max5_patcher-----------\r\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Ever waste a whole bunch of time trying to understand something, where the only way to feel less guilty about wasting so much time on it is to tell someone about it? Me too\u2026\u00a0here goes. In between more important things, I was thinking about Karplus-Strong (KS) plucked string synthesis again the other day\u00a0\u2014\u00a0more than just [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[13,1],"tags":[],"class_list":["post-275","post","type-post","status-publish","format-standard","hentry","category-max","category-writing"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p4SgL0-4r","_links":{"self":[{"href":"https:\/\/x37v.com\/x37v\/wp-json\/wp\/v2\/posts\/275","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/x37v.com\/x37v\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/x37v.com\/x37v\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/x37v.com\/x37v\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/x37v.com\/x37v\/wp-json\/wp\/v2\/comments?post=275"}],"version-history":[{"count":23,"href":"https:\/\/x37v.com\/x37v\/wp-json\/wp\/v2\/posts\/275\/revisions"}],"predecessor-version":[{"id":311,"href":"https:\/\/x37v.com\/x37v\/wp-json\/wp\/v2\/posts\/275\/revisions\/311"}],"wp:attachment":[{"href":"https:\/\/x37v.com\/x37v\/wp-json\/wp\/v2\/media?parent=275"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/x37v.com\/x37v\/wp-json\/wp\/v2\/categories?post=275"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/x37v.com\/x37v\/wp-json\/wp\/v2\/tags?post=275"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}