{"id":8061,"date":"2024-05-02T21:49:25","date_gmt":"2024-05-02T19:49:25","guid":{"rendered":"http:\/\/www.tonmeister.ca\/wordpress\/?p=8061"},"modified":"2024-05-03T07:15:44","modified_gmt":"2024-05-03T05:15:44","slug":"excruciating-minutiae-part-3","status":"publish","type":"post","link":"http:\/\/www.tonmeister.ca\/wordpress\/2024\/05\/02\/excruciating-minutiae-part-3\/","title":{"rendered":"Excruciating minutiae: Part 3"},"content":{"rendered":"\n<p>In <a href=\"http:\/\/www.tonmeister.ca\/wordpress\/2024\/05\/01\/excruciating-minutiae-part-2\/\" data-type=\"post\" data-id=\"8051\">Part 2<\/a> of this series, I wrote the following sentence:<\/p>\n\n\n\n<p class=\"has-text-align-center\"><em>The easiest (and possibly best) way to do this is to create white noise with a triangular probability distribution function and a peak-to-peak amplitude of \u00b1 1 quantisation level.<\/em><\/p>\n\n\n\n<p>That&#8217;s a very busy sentence, so let&#8217;s unpack it a little.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Rolling the dice<\/h3>\n\n\n\n<p>If you roll one die, you have an equal probability of rolling any number between 1 and 6 (inclusive). Let&#8217;s roll one die 100 times counting the number of times we get a 1, or a 2, or a 3, and so on up to 6.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>Number rolled<\/td><td>Number of times the number was rolled<\/td><td>Percentage of times the number was rolled<\/td><\/tr><tr><td>1<\/td><td>17<\/td><td>17<\/td><\/tr><tr><td>2<\/td><td>14<\/td><td>14<\/td><\/tr><tr><td>3<\/td><td>15<\/td><td>15<\/td><\/tr><tr><td>4<\/td><td>15<\/td><td>15<\/td><\/tr><tr><td>5<\/td><td>21<\/td><td>21<\/td><\/tr><tr><td>6<\/td><td>18<\/td><td>18<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>(Note that the percentage of times each number was rolled is the same as the number of times each number was rolled only because I rolled the die 100 times.)<\/p>\n\n\n\n<p>If I plot those results, it looks like Figure 1.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"768\" src=\"http:\/\/www.tonmeister.ca\/wordpress\/wp-content\/uploads\/pt03_fig01-1024x768.png\" alt=\"\" class=\"wp-image-8062\" srcset=\"http:\/\/www.tonmeister.ca\/wordpress\/wp-content\/uploads\/pt03_fig01-1024x768.png 1024w, http:\/\/www.tonmeister.ca\/wordpress\/wp-content\/uploads\/pt03_fig01-300x225.png 300w, http:\/\/www.tonmeister.ca\/wordpress\/wp-content\/uploads\/pt03_fig01-768x576.png 768w, http:\/\/www.tonmeister.ca\/wordpress\/wp-content\/uploads\/pt03_fig01.png 1120w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption class=\"wp-element-caption\">Figure 1. The results of rolling 1 die 100 times.<\/figcaption><\/figure>\n<\/div>\n\n\n<p>It may be weird, but I&#8217;ve plotted the number of times I rolled -5 or 13 (for example). These are 0 times because it&#8217;s impossible to get those numbers by rolling one die. But the reason I put those results in there will make more sense later.<\/p>\n\n\n\n<p>Let&#8217;s keep rolling the die. If I do it 1,000,000 times instead of 100, I get these results:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>ed<\/td><td>Number of times the number was rolled<\/td><td>Percentage of times the number was rolled<\/td><\/tr><tr><td>1<\/td><td>166225<\/td><td>16.6225<\/td><\/tr><tr><td>2<\/td><td>166400<\/td><td>16.6400<\/td><\/tr><tr><td>3<\/td><td>166930<\/td><td>16.6930<\/td><\/tr><tr><td>4<\/td><td>167055<\/td><td>16.7055<\/td><\/tr><tr><td>5<\/td><td>166501<\/td><td>16.6501<\/td><\/tr><tr><td>6<\/td><td>166889<\/td><td>16.6889<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Now, since I rolled many, many, more times, it&#8217;s more obvious that the six results have an equal probability. The more I roll the die, the more those numbers get closer and closer to each other.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"768\" src=\"http:\/\/www.tonmeister.ca\/wordpress\/wp-content\/uploads\/pt03_fig02-1024x768.png\" alt=\"\" class=\"wp-image-8063\" srcset=\"http:\/\/www.tonmeister.ca\/wordpress\/wp-content\/uploads\/pt03_fig02-1024x768.png 1024w, http:\/\/www.tonmeister.ca\/wordpress\/wp-content\/uploads\/pt03_fig02-300x225.png 300w, http:\/\/www.tonmeister.ca\/wordpress\/wp-content\/uploads\/pt03_fig02-768x576.png 768w, http:\/\/www.tonmeister.ca\/wordpress\/wp-content\/uploads\/pt03_fig02.png 1120w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption class=\"wp-element-caption\">Figure 2.<\/figcaption><\/figure>\n<\/div>\n\n\n<p>Take a look at the shape of the plot above. The area under the line from 1 to 6 (inclusive) is almost a rectangle because the six numbers are all almost the same.<\/p>\n\n\n\n<p>The shape of that plot shows us the probability of rolling the six numbers on the die, so we call it a <em>probability density function<\/em> or <em>PDF<\/em>. In this case, we see a <em>rectangular PDF<\/em>.<\/p>\n\n\n\n<p>But what happens if we roll two dice instead? Now things get a little more complicated, since there is more than one way to get a total result, as shown in the table below.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>Total<\/td><td><\/td><td><\/td><td><\/td><td><\/td><td><\/td><td><\/td><\/tr><tr><td>2<\/td><td>1+1<\/td><td><\/td><td><\/td><td><\/td><td><\/td><td><\/td><\/tr><tr><td>3<\/td><td>1+2<\/td><td>2+1<\/td><td><\/td><td><\/td><td><\/td><td><\/td><\/tr><tr><td>4<\/td><td>1+3<\/td><td>2+2<\/td><td>3+1<\/td><td><\/td><td><\/td><td><\/td><\/tr><tr><td>5<\/td><td>1+4<\/td><td>2+3<\/td><td>3+2<\/td><td>4+1<\/td><td><\/td><td><\/td><\/tr><tr><td>6<\/td><td>1+5<\/td><td>2+4<\/td><td>3+3<\/td><td>4+2<\/td><td>5+1<\/td><td><\/td><\/tr><tr><td>7<\/td><td>1+6<\/td><td>2+5<\/td><td>3+4<\/td><td>4+3<\/td><td>5+2<\/td><td>6+1<\/td><\/tr><tr><td>8<\/td><td><\/td><td>2+6<\/td><td>3+5<\/td><td>4+4<\/td><td>5+3<\/td><td>6+2<\/td><\/tr><tr><td>9<\/td><td><\/td><td><\/td><td>3+6<\/td><td>4+5<\/td><td>5+4<\/td><td>6+3<\/td><\/tr><tr><td>10<\/td><td><\/td><td><\/td><td><\/td><td>4+6<\/td><td>5+5<\/td><td>6+4<\/td><\/tr><tr><td>11<\/td><td><\/td><td><\/td><td><\/td><td><\/td><td>5+6<\/td><td>6+5<\/td><\/tr><tr><td>12<\/td><td><\/td><td><\/td><td><\/td><td><\/td><td><\/td><td>6+6<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>As can be (hopefully) seen in the table, there is only one way to roll a 2, and there&#8217;s only one way to roll a 12. But there are 6 different ways to roll a 7. Therefore, if you&#8217;re rolling two dice, it&#8217;s 6 times more likely that you&#8217;ll roll a 7 than a 12, for example.<\/p>\n\n\n\n<p>If I were to roll two dice 1,000,000 times, I would get a PDF like the one shown in Figure 3.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"768\" src=\"http:\/\/www.tonmeister.ca\/wordpress\/wp-content\/uploads\/pt03_fig03-1024x768.png\" alt=\"\" class=\"wp-image-8064\" srcset=\"http:\/\/www.tonmeister.ca\/wordpress\/wp-content\/uploads\/pt03_fig03-1024x768.png 1024w, http:\/\/www.tonmeister.ca\/wordpress\/wp-content\/uploads\/pt03_fig03-300x225.png 300w, http:\/\/www.tonmeister.ca\/wordpress\/wp-content\/uploads\/pt03_fig03-768x576.png 768w, http:\/\/www.tonmeister.ca\/wordpress\/wp-content\/uploads\/pt03_fig03.png 1120w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption class=\"wp-element-caption\">Figure 3.<\/figcaption><\/figure>\n<\/div>\n\n\n<p>I won&#8217;t explain why this would be considered to be a <em>triangular PDF<\/em>.<\/p>\n\n\n\n<p>Whether you roll one die or two dice, the number you get is random. In other words, you can&#8217;t use the past results to predict what the next number will be. However, if you are rolling one die, and you bet that you&#8217;ll roll a 6 every time, you&#8217;ll be right about 16.7% of the time. If you&#8217;re rolling two dice and you bet that you&#8217;ll roll a 12 every time, you&#8217;ll only be right about 2.8% of the time.<\/p>\n\n\n\n<p>Let&#8217;s take two dice of different colours, say, one red die and one blue die. We&#8217;ll roll both dice again, but instead of adding the two values, we&#8217;ll subtract the blue value from the red one. If we do this 1,000,000 times, we&#8217;ll get something like the results shown below in Figure 4.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"768\" src=\"http:\/\/www.tonmeister.ca\/wordpress\/wp-content\/uploads\/pt03_fig04-1024x768.png\" alt=\"\" class=\"wp-image-8065\" srcset=\"http:\/\/www.tonmeister.ca\/wordpress\/wp-content\/uploads\/pt03_fig04-1024x768.png 1024w, http:\/\/www.tonmeister.ca\/wordpress\/wp-content\/uploads\/pt03_fig04-300x225.png 300w, http:\/\/www.tonmeister.ca\/wordpress\/wp-content\/uploads\/pt03_fig04-768x576.png 768w, http:\/\/www.tonmeister.ca\/wordpress\/wp-content\/uploads\/pt03_fig04.png 1120w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption class=\"wp-element-caption\">Figure 4.<\/figcaption><\/figure>\n<\/div>\n\n\n<p>Notice that the probability density function keeps the same shape, it&#8217;s just moved down to a range of \u00b15 instead of 2 to 12.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Generating noise<\/h3>\n\n\n\n<p>In audio, noise is a sound that is completely random. In other words, just like the example with the dice, in a digital audio signal, you can&#8217;t predict what the next sample value will be based on the past sample values. However, there are many different ways of generating that random number and manipulating its characteristics.<\/p>\n\n\n\n<p>Let&#8217;s start with a computer algorithm that can generate a random number between 0 and 1 (inclusive) with a rectangular PDF. We&#8217;ll then ask the algorithm to spit out 1,000,000 values. If the numbers really are random, and the computer has infinite precision, then we&#8217;ll probably get 1,000,000 different numbers. However, we&#8217;re not really interested in the numbers themselves &#8211; we&#8217;re interested in how they&#8217;re distributed between 0.00 and 1.00. Let&#8217;s say we divide up that range into 100 steps (or &#8220;buckets&#8221;) that are 0.01 wide and count how many of our random numbers fall into each group. So, we&#8217;ll count how many are between 0.0 and 0.01, between 0.01 and 0.02, and so on up to 0.99 to 1.00. We&#8217;ll get something like Figure 5.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"768\" src=\"http:\/\/www.tonmeister.ca\/wordpress\/wp-content\/uploads\/pt03_fig05-1024x768.png\" alt=\"\" class=\"wp-image-8067\" srcset=\"http:\/\/www.tonmeister.ca\/wordpress\/wp-content\/uploads\/pt03_fig05-1024x768.png 1024w, http:\/\/www.tonmeister.ca\/wordpress\/wp-content\/uploads\/pt03_fig05-300x225.png 300w, http:\/\/www.tonmeister.ca\/wordpress\/wp-content\/uploads\/pt03_fig05-768x576.png 768w, http:\/\/www.tonmeister.ca\/wordpress\/wp-content\/uploads\/pt03_fig05.png 1120w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption class=\"wp-element-caption\">Figure 5.<\/figcaption><\/figure>\n<\/div>\n\n\n<p>I&#8217;ve only plotted the probabilities of the possible values: 0 to 1, which winds up showing only the top of the rectangle in the rectangular PDF.<\/p>\n\n\n\n<p>If I generate 1,000,000 random numbers with that algorithm, and then subtract 1,000,000 other random numbers, one by one, and find the probabilities of the result, the answer will be familiar.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"768\" src=\"http:\/\/www.tonmeister.ca\/wordpress\/wp-content\/uploads\/pt03_fig06-1024x768.png\" alt=\"\" class=\"wp-image-8068\" srcset=\"http:\/\/www.tonmeister.ca\/wordpress\/wp-content\/uploads\/pt03_fig06-1024x768.png 1024w, http:\/\/www.tonmeister.ca\/wordpress\/wp-content\/uploads\/pt03_fig06-300x225.png 300w, http:\/\/www.tonmeister.ca\/wordpress\/wp-content\/uploads\/pt03_fig06-768x576.png 768w, http:\/\/www.tonmeister.ca\/wordpress\/wp-content\/uploads\/pt03_fig06.png 1120w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption class=\"wp-element-caption\">Figure 6.<\/figcaption><\/figure>\n<\/div>\n\n\n<p>So, this is how we make the noise that&#8217;s added to the signal. If, for each sample, you generate two random numbers (making sure that your algorithm has a rectangular PDF) and subtract one from the other, you have the dither signal that will have a maximum level of \u00b11 quantisation level. <\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The signal (with a maximum range of \u00b11) is scaled up by multiplying it by 2<sup>(NumberOfBits-1)<\/sup>-2<\/li>\n\n\n\n<li>then you add the result of the dither generator<\/li>\n\n\n\n<li>then the total is rounded to the nearest integer value<\/li>\n\n\n\n<li>and then the result is scaled back down by a factor of 2<sup>(NumberOfBits-1)<\/sup> to bring its back down to a range of \u00b11 to get it ready for exporting to a standard audio file format like .wav or .flac.<\/li>\n<\/ul>\n\n\n\n<p>In other words, assuming that you have an audio signal called &#8220;Signal&#8221; that has a range of \u00b11 and consists of floating point values:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ScaleUp = 2^(Bitdepth-1)-2\nScaleDown = 2^(Bitdepth-1)\n\nTpdfDither = rand(LengthOfSignal) - rand(LengthOfSignal) \n\nQuantisedDitheredSignal = round(Signal * ScaleUp + TpdfDither) \/ ScaleDown;<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In Part 2 of this series, I wrote the following sentence: The easiest (and possibly best) way to do this is to create white noise with a triangular probability distribution function and a peak-to-peak amplitude of \u00b1 1 quantisation level. That&#8217;s a very busy sentence, so let&#8217;s unpack it a little. Rolling the dice If [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","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":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[63,4,59,43,42,65,58],"tags":[],"class_list":["post-8061","post","type-post","status-publish","format-standard","hentry","category-analysis","category-audio","category-digital-audio","category-dsp","category-math","category-measurements","category-noise"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p48hIM-261","_links":{"self":[{"href":"http:\/\/www.tonmeister.ca\/wordpress\/wp-json\/wp\/v2\/posts\/8061","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.tonmeister.ca\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.tonmeister.ca\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.tonmeister.ca\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.tonmeister.ca\/wordpress\/wp-json\/wp\/v2\/comments?post=8061"}],"version-history":[{"count":6,"href":"http:\/\/www.tonmeister.ca\/wordpress\/wp-json\/wp\/v2\/posts\/8061\/revisions"}],"predecessor-version":[{"id":8075,"href":"http:\/\/www.tonmeister.ca\/wordpress\/wp-json\/wp\/v2\/posts\/8061\/revisions\/8075"}],"wp:attachment":[{"href":"http:\/\/www.tonmeister.ca\/wordpress\/wp-json\/wp\/v2\/media?parent=8061"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.tonmeister.ca\/wordpress\/wp-json\/wp\/v2\/categories?post=8061"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.tonmeister.ca\/wordpress\/wp-json\/wp\/v2\/tags?post=8061"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}