<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Fabulous Adventures In Coding : Science</title><link>http://blogs.msdn.com/ericlippert/archive/tags/Science/default.aspx</link><description>Tags: Science</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Absence of evidence is not evidence of absence</title><link>http://blogs.msdn.com/ericlippert/archive/2009/10/12/absence-of-evidence-is-not-evidence-of-absence.aspx</link><pubDate>Mon, 12 Oct 2009 13:51:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9905526</guid><dc:creator>Eric Lippert</dc:creator><slash:comments>15</slash:comments><comments>http://blogs.msdn.com/ericlippert/comments/9905526.aspx</comments><wfw:commentRss>http://blogs.msdn.com/ericlippert/commentrss.aspx?PostID=9905526</wfw:commentRss><description>&lt;DIV class=mine&gt;
&lt;P&gt;Today, two more subtly incorrect myths about C#.&lt;/P&gt;
&lt;P&gt;As you probably know, C# requires all local variables to be explicitly assigned before they are read, but assumes that all class instance field variables are initially assigned to default values. An explanation of why that is that I sometimes hear is "&lt;EM&gt;the compiler can easily prove that a local variable is not assigned, but it is much harder to prove that an instance&amp;nbsp;field is not assigned. And since the class's default constructor automatically assigns all instance fields to default values, you don't need to do the analysis for fields."&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;Both statements are subtly incorrect.&lt;/P&gt;
&lt;P&gt;The first statement is incorrect because the compiler in fact cannot and does not prove that a local variable is not assigned. Proving that is (1) impossible, and (2) does not give us any useful information we can act upon. It's impossible because proving that a given variable is assigned a value is equivalent to solving the Halting Problem:&lt;/P&gt;&lt;SPAN class=code&gt;
&lt;P&gt;int x;&lt;BR&gt;if (/*condition requiring solution of the halting problem here*/) x = 10;&lt;BR&gt;print(x);&lt;/P&gt;&lt;/SPAN&gt;
&lt;P&gt;If what we wanted to do was prove that x was &lt;EM&gt;unassigned&lt;/EM&gt; then we would have to &lt;EM&gt;at compile time&lt;/EM&gt; prove that the condition was false.&amp;nbsp;Our compiler is not that sophisticated! &lt;/P&gt;
&lt;P&gt;But the deeper point here is that we're not interested in proving for certain that x is &lt;EM&gt;unassigned&lt;/EM&gt;. We're interested in proving for certain that x is &lt;EM&gt;assigned&lt;/EM&gt;! If we can prove that for certain, then x is "definitely assigned". If we cannot prove that for certain then x is "not definitely assigned". We're only interested in "definitely unassigned" insofar as "definitely unassigned" is a stronger version of "not definitely assigned". If x is read from when it is "not definitely assigned", that's a bug. &lt;/P&gt;
&lt;P&gt;That is, we're attempting to prove that x is assigned, and our failure to prove that at every point where it is read is what motivates the error. That failure could be because of a bona fide bug in your program, or it could be because our flow analyzer is extremely conservative. For example:&lt;/P&gt;&lt;SPAN class=code&gt;
&lt;P&gt;int x, y = 0;&lt;BR&gt;if (0 * y == 0) x = 10;&lt;BR&gt;print(x);&lt;/P&gt;&lt;/SPAN&gt;
&lt;P&gt;You and I know that x is definitely assigned, but in C# 3 the compiler is &lt;EM&gt;deliberately&lt;/EM&gt; not smart enough to prove that. (Interestingly enough, it &lt;EM&gt;was&lt;/EM&gt; smart&amp;nbsp;enough in C# 2. &lt;A class="" href="http://blogs.msdn.com/ericlippert/archive/2006/03/29/the-root-of-all-evil-part-two.aspx" mce_href="http://blogs.msdn.com/ericlippert/archive/2006/03/29/the-root-of-all-evil-part-two.aspx"&gt;I broke that to bring the compiler into line with&amp;nbsp;the spec; being smarter but in violation of the spec is not necessarily a good thing&lt;/A&gt;.)&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This example again shows that we do not prove that x is unassigned; if we did prove that, then clearly our prover would contain an error, since you and I both know that x is definitely assigned. Rather, we fail to prove that x is assigned.&lt;/P&gt;
&lt;P&gt;This is an interesting twist on&amp;nbsp;the believers vs skeptics argument that goes like this: the skeptic says "there's no reliable evidence that bigfoot exists, therefore, bigfoot does not exist". The believer says&amp;nbsp;"absence of reliable evidence is not itself evidence of absence; and yes, bigfoot&amp;nbsp;does exist". In both cases, reasoning from a position of lacking reliable evidence is seldom good reasoning! But in our case, it is precisely &lt;EM&gt;because&lt;/EM&gt; we lack reliable evidence that we are coming to the conclusion that we do not know enough to allow you to read from x.&lt;/P&gt;
&lt;P&gt;(The relevant principle for tentatively concluding that bigfoot is mythical based on a lack of reliable evidence&amp;nbsp;is "extraordinary claims require extraordinary evidence". &lt;EM&gt;It&lt;/EM&gt; &lt;EM&gt;is reasonable to assume that&amp;nbsp;an extraordinary claim is false until reliable evidence is produced&lt;/EM&gt;.&amp;nbsp;When overwhelmingly reliable evidence is produced of an extraordinary claim -- say, the extraordinary claim that&amp;nbsp;time itself slows down when you move faster -- then it makes sense to believe the extraordinary claim. Overwhelming evidence has been provided for the theory of relativity, but not for the theory of bigfoot.)&lt;/P&gt;
&lt;P&gt;The second myth is that the default constructor of a class&amp;nbsp;initializes the fields to their default values. This can be shown to be false by several arguments. &lt;/P&gt;
&lt;P&gt;First, a class need not have a default constructor, and yet its fields are always observed to be initially assigned. If there is no default constructor, then something else must be initializing the fields. &lt;/P&gt;
&lt;P&gt;Second, even if a class does have a default constructor, there's no guarantee that it will be called. Some other constructor could be called.&lt;/P&gt;
&lt;P&gt;Third, the field initializers of a class run before any constructor body runs, therefore it cannot be the constructor body that does the initialization; that would be wiping out the results of the field initializers. &lt;/P&gt;
&lt;P&gt;Fourth, constructors can call other constructors; if each of those constructors was initializing the fields to zero, then that would be wasteful; we'd be unneccessarily re-initializing already-wiped-out fields. &lt;/P&gt;
&lt;P&gt;What actually happens is that the CLI memory allocator guarantees that the memory allocated for a given class instance will be initialized to all zeros before the constructor is called. By the time the constructors run the object is already freshly zeroed out&amp;nbsp;and ready to go.&lt;/P&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;&lt;/DIV&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9905526" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/ericlippert/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://blogs.msdn.com/ericlippert/archive/tags/Science/default.aspx">Science</category><category domain="http://blogs.msdn.com/ericlippert/archive/tags/Memory+Management/default.aspx">Memory Management</category><category domain="http://blogs.msdn.com/ericlippert/archive/tags/myths/default.aspx">myths</category><category domain="http://blogs.msdn.com/ericlippert/archive/tags/relativity/default.aspx">relativity</category><category domain="http://blogs.msdn.com/ericlippert/archive/tags/skepticism/default.aspx">skepticism</category><category domain="http://blogs.msdn.com/ericlippert/archive/tags/definite+assignment/default.aspx">definite assignment</category><category domain="http://blogs.msdn.com/ericlippert/archive/tags/constructors/default.aspx">constructors</category></item><item><title>What are the horns for?</title><link>http://blogs.msdn.com/ericlippert/archive/2009/03/05/what-are-the-horns-for.aspx</link><pubDate>Thu, 05 Mar 2009 20:40:53 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9460224</guid><dc:creator>Eric Lippert</dc:creator><slash:comments>10</slash:comments><comments>http://blogs.msdn.com/ericlippert/comments/9460224.aspx</comments><wfw:commentRss>http://blogs.msdn.com/ericlippert/commentrss.aspx?PostID=9460224</wfw:commentRss><description>&lt;div class="mine"&gt; &lt;p&gt;(Technology of a different sort today, just for a change of pace.)&lt;/p&gt; &lt;p&gt;&lt;a href="http://en.wikipedia.org/wiki/Falkirk_Wheel"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; margin: 0px 10px 0px 0px; border-right-width: 0px" height="175" alt="FalkirkWheel" src="http://blogs.msdn.com/blogfiles/ericlippert/WindowsLiveWriter/Whatarethehornsfor_8805/FalkirkWheel_3.jpg" width="244" align="left" border="0"&gt;&lt;/a&gt; The first time I saw a picture of the Falkirk Wheel -- the world's only rotating boat lift, in Scotland -- I thought that it must be a really nice computer-generated landscape. It looks like something you'd see in Halo.&lt;/p&gt; &lt;p&gt;But it's real; it lifts and lowers boats 24 metres between two canals. I was first sent photos of it by some engineer friends of mine, and we got into an email discussion of some of the interesting points in the design and implementation of such a device.&lt;/p&gt; &lt;p&gt;First off, does the weight of the boats in the rotating caissons matter? What if one caisson has a lot of heavy boats and the other has only light boats -- won't the imbalance put extra stress on the structure and the gears?&lt;/p&gt; &lt;p&gt;No! Boats displace their own weight in water -- at least, boats that still float do. Therefore when a boat of any tonnage enters a caisson, a mass of water equal to the mass of the boat leaves. As long as both caissons have the same volume and are filled to the same height, they always have the same mass no matter what boats are in them.&lt;/p&gt; &lt;p&gt;This means that it is straightforward to keep the imbalance between the two sides pretty small, just by using pumps to ensure that each caisson is filled to the same height. Because the system is so well balanced, and takes a good seven minutes to rotate, &lt;a href="http://www.gentles.milestonenet.co.uk/fcucanalweb/BEK/statistics.html"&gt;the power requirements are surprisingly low&lt;/a&gt;. In the six-to-twelve kilowatts range, which is only about one hundred bright light bulbs. (People are often surprised by how little power it takes to slowly rotate something balanced. For example, apparently the motor that powers the rotating restaurant in the Space Needle is a one-horsepower motor with a &lt;em&gt;huge&lt;/em&gt; gear ratio.)&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;a href="http://www.gentles.milestonenet.co.uk/fcucanalweb/technicaltour/technical.html"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; margin: 10px 10px 10px 0px; border-right-width: 0px" height="184" alt="FalkirkGear" src="http://blogs.msdn.com/blogfiles/ericlippert/WindowsLiveWriter/Whatarethehornsfor_8805/FalkirkGear_3.jpg" width="244" align="left" border="0"&gt;&lt;/a&gt;We then got into a lively discussion of what the horns on the sides of the Wheel rotor are for. One of my engineer friends thought that one of the horns might be deliberately heavier than the other, the resulting torque providing some mitigation of the "play" or "&lt;a href="http://en.wikipedia.org/wiki/Backlash_(engineering)"&gt;backlash&lt;/a&gt;" inherent in all gearing systems. But as you can see if you look carefully in the photo, the designers of the Wheel came up with a different solution to mitigate backlash in the planetary gears. &lt;strong&gt;The "teeth" of the lower gear are actually rollers.&lt;/strong&gt; The gears fit together the same way that a bicycle sprocket fits the rollers in the chain. Neat!&lt;/p&gt; &lt;p&gt;This still doesn't explain what the horns are for. After lots more speculation I decided to cut to the chase and emailed the public relations people at the Wheel. They cheerfully informed me that the horns were entirely decorative, served no engineering purpose, and were designed to be reminiscent of Celtic-style axes.&lt;/p&gt; &lt;p&gt;I don't believe them. I know what the horns are for. I mean, think about it. When the descending side of the Wheel strikes the water, &lt;strong&gt;the sharp horn breaks the surface tension&lt;/strong&gt; and allows the rest of the rotor to &lt;em&gt;smoothly&lt;/em&gt; enter the pool below. That's the only sensible explanation.&lt;/p&gt; &lt;p&gt;(Wikimedia Commons photo by Sean Mack, gear photo by James Gentles.)&lt;/p&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9460224" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/ericlippert/archive/tags/Science/default.aspx">Science</category><category domain="http://blogs.msdn.com/ericlippert/archive/tags/Non-computer/default.aspx">Non-computer</category></item><item><title>Talking About The Weather, Part Two</title><link>http://blogs.msdn.com/ericlippert/archive/2007/08/03/talking-about-the-weather-part-two.aspx</link><pubDate>Fri, 03 Aug 2007 17:25:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:4191068</guid><dc:creator>Eric Lippert</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/ericlippert/comments/4191068.aspx</comments><wfw:commentRss>http://blogs.msdn.com/ericlippert/commentrss.aspx?PostID=4191068</wfw:commentRss><description>&lt;DIV class=mine&gt;
&lt;P&gt;What we’re missing is a phenomenon that probably &lt;EM&gt;was&lt;/EM&gt; described correctly by your high school science teacher, namely, the phenomenon of “&lt;STRONG&gt;latent heat&lt;/STRONG&gt;” (which is what I was taught, though “enthalpy” would be the more modern term.)&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I said that the temperature of a substance is the average amount of energy in it.&lt;STRONG&gt; I lied; it is possible to add energy to water without changing the temperature.&lt;/STRONG&gt; Liquid water requires “extra” energy to vaporize that is not reflected in the temperature of the water. You may recall doing a science experiment which ended up with a &lt;A class="" href="http://www.sciencebyjones.com/heating_curve1.htm" mce_href="http://www.sciencebyjones.com/heating_curve1.htm"&gt;graph like this one&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;As you can see from the graph, when the water reaches the point where it is about to&amp;nbsp;melt or&amp;nbsp;boil&amp;nbsp;the temperature stops going up for a while, even though more and more energy is being added to the mix. The liquid water requires an extra little boost of energy to liberate the molecule from the liquid. That amount of energy is roughly 540 calories per gram of water thusly liberated.&lt;/P&gt;
&lt;P&gt;Let me be very clear on two points here. First, &lt;STRONG&gt;liquid water evaporates at any temperature, not just the boiling point&lt;/STRONG&gt;. Obviously puddles dry up even though they are not boiling! The boiling point is simply the point at which the liquid water has so much heat, and the vapor pressure is sufficiently low, that all of it is going to rapidly turn into vapor in short order. And second, with that in mind it should be clear that the 540 calories required per gram of vaporized water is 540 calories per gram &lt;STRONG&gt;no matter what temperature the water is at when it is vaporizing&lt;/STRONG&gt;. (There may be some small variation in the latent heat required based on the temperature, but it'll be on the near order of 540 calories per gram&amp;nbsp;in the kind of temperature ranges we're talking about here.)&lt;/P&gt;
&lt;P&gt;One way to think about the latent heat is this: liquid water has way more entropy – more randomness – than solid water. The position and speed of each molecule in a liquid is way more random than in a solid. And similarly, water vapor has way more entropy than liquid water. Adding heat makes entropy; it disorders a structure. If you want to make water more entropic by evaporating it, you’re going to have to add a whole load of heat to it to account for the increase in entropy.&amp;nbsp;540 calories per gram, in fact.&lt;/P&gt;
&lt;P&gt;But this process goes the other way too. When you&amp;nbsp;export entropy from&amp;nbsp;water vapor by turning it back into a liquid, those 540 calories per gram have to go somewhere. That entropy is exported from the water in the form of heat. When you put your finger in the steam rising from a boiling kettle, it's the enormous&amp;nbsp;latent heat of the vapor&amp;nbsp;condensing on your relatively cold finger&amp;nbsp;that really scalds you. Even if you happened to be in atmospheric conditions where the condensation happened to a much lower-temperature vapor, the latent heat would still hurt a lot.&lt;/P&gt;
&lt;P&gt;So now let’s revisit our look at a nice fluffy cloud. The sun adds 540 calories to a gram of surface water, liberating it into the atmosphere. That gram of water winds its way up into the sky where it hits a region of sufficiently low temperature and high pressure as that it flashes back into liquid form. &lt;STRONG&gt;Without changing the temperature of the water further, 540 calories is suddenly liberated into the bottom of the cloud, heating up the surrounding air.&lt;/STRONG&gt; Hot air rises. An updraft forms in the middle of the cloud, sucking some of the water droplets higher into the atmosphere. &lt;STRONG&gt;The cloud gets taller.&lt;/STRONG&gt; Eventually the updraft cools off and the cooler air moves outwards and slowly subsides.&amp;nbsp;You'll get a&amp;nbsp;sort of doughnut-shaped circulation of air&amp;nbsp;around the exterior of the cloud as the falling air then gets sucked back in the bottom.&lt;/P&gt;
&lt;P&gt;Meanwhile, the bottom of the cloud is getting hotter and hotter from the latent heat of more water evaporated from the lake surface turning back into liquid. The bottom of the cloud has an updraft in it, so it is pulling more and more water vapor from the wet atmosphere below it into the bottom of the cloud. The process is accelerating. If the cloud happens to be over a huge body of warm water then&amp;nbsp;this can accelerate very rapidly. &lt;/P&gt;
&lt;P&gt;The latent heat of fusion liberated when a gram of liquid water turns into ice is another 333 calories on top of the 540 calories that was from the latent heat of condensation. Imagine what happens to our cloud if the strong updraft makes the cloud so tall that the extremely cold atmosphere up there&amp;nbsp;glaciates the&amp;nbsp;lifted droplets into a cirrus cloud at the top. Cirrus clouds are made out of ice crystals.&amp;nbsp;The water (now in the form of ice) escapes from the cloud out the top and potentially stays up there for a long time.&amp;nbsp;The ice drifts slowly&amp;nbsp;away from the top of what is now probably looking like a pretty threatening cloud. Essentially the cirrus cloud is extracting all the latent heat from the water, depositing the heat&amp;nbsp;in the cloud, and exporting the resulting coldness out the top in the form of ice.&lt;/P&gt;
&lt;P&gt;Now imagine what happens if a bit of the water vapor in the cloud gets cold enough to freeze in the middle of the cloud. It liberates its latent heat of fusion into the updraft, making it stronger. The updraft&amp;nbsp;sucks the ice fragment up through hundreds of meters of cold wetness. When it gets to the top, maybe it is heavy enough to fall back down through more cold wetness in the outer part of the&amp;nbsp;cloud, only to get sucked up by the updraft again when it reaches the bottom. &lt;STRONG&gt;The ice fragment gets bigger and bigger, liberating more and more energy into the updraft as it goes around in vertical circles.&lt;/STRONG&gt; Eventually the updraft is not strong enough to hold it up and all that cold gets exported out the bottom of the cloud in the form of hail. &lt;/P&gt;
&lt;P&gt;And &lt;EM&gt;that&lt;/EM&gt; is how thunderclouds concentrate massive amounts of energy; they move&amp;nbsp;huge quantities of latent heat that the sun puts into warm surface water into their interiors to power the very updrafts that suck in more vapor from below, concentrating the heat even more. The average heat keeps going up because&amp;nbsp;coldness is exported in the form of cirrus clouds out the top and hail (or cold rain) out the bottom. &lt;/P&gt;
&lt;P&gt;Now imagine the effect of hundreds of such clouds forming over a relatively small area. The suction from the updrafts is immense, and easily moves around millions of tonnes of air that we then experience as high winds. And when the updrafts are insufficient to hold the water up, you get torrential rains and hail falling out of the sky. It’s an amazing phenomenon, and it is all thanks to the fact that our planet is almost entirely covered by a substance which has high latent heat and can be solid, liquid and gas in a relatively small temperature and pressure&amp;nbsp;range. &lt;BR&gt;&lt;/P&gt;&lt;/DIV&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=4191068" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/ericlippert/archive/tags/Science/default.aspx">Science</category><category domain="http://blogs.msdn.com/ericlippert/archive/tags/Weather/default.aspx">Weather</category><category domain="http://blogs.msdn.com/ericlippert/archive/tags/Non-computer/default.aspx">Non-computer</category><category domain="http://blogs.msdn.com/ericlippert/archive/tags/Best+Of+FAIC/default.aspx">Best Of FAIC</category></item><item><title>Talking About The Weather, Part One</title><link>http://blogs.msdn.com/ericlippert/archive/2007/08/02/talking-about-the-weather-part-one.aspx</link><pubDate>Thu, 02 Aug 2007 17:31:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:4178992</guid><dc:creator>Eric Lippert</dc:creator><slash:comments>7</slash:comments><comments>http://blogs.msdn.com/ericlippert/comments/4178992.aspx</comments><wfw:commentRss>http://blogs.msdn.com/ericlippert/commentrss.aspx?PostID=4178992</wfw:commentRss><description>&lt;DIV class=mine&gt;
&lt;P&gt;No technology today; just talking about the weather. &lt;/P&gt;
&lt;P&gt;I &lt;EM&gt;love&lt;/EM&gt; talking about the weather.&lt;/P&gt;
&lt;P&gt;I mentioned the other day that I had just returned from my ancestral homeland on the shores of Lake Huron, the great inland sea of southwestern Ontario. We got some rip-roaring thunderstorms this year. I love thunderstorms on the lake. They are extremely dramatic, particularly at sunset.&lt;/P&gt;
&lt;P&gt;Think about&amp;nbsp;this for a minute: even small thunderstorms must move around literally &lt;EM&gt;millions&lt;/EM&gt; of tonnes of air and water, often at high speed. Storms are &lt;EM&gt;massive&lt;/EM&gt; concentrations of energy. Where does the energy come from? How does it get so concentrated?&lt;/P&gt;
&lt;P&gt;Coincidentally, I&amp;nbsp;had just started reading&amp;nbsp;Frank Bethwaite’s book “&lt;A class="" href="http://www.amazon.com/High-Performance-Sailing-Frank-Bethwaite/dp/0070057990" mce_href="http://www.amazon.com/High-Performance-Sailing-Frank-Bethwaite/dp/0070057990"&gt;High Performance Sailing&lt;/A&gt;”. Bethwaite is not just an amazing sailboat designer, but also a professional meteorologist. As someone who knows next to nothing about the weather I found it fascinating&amp;nbsp;to learn just why it is that thunderstorms are so powerful.&lt;/P&gt;
&lt;P&gt;Understanding this phenomenon required me to first be disabused of an incorrect notion that I’ve held probably since the fourth grade. You were probably taught this too, in your elementary school or high school science classes: “&lt;EM&gt;the reason clouds form when moist air cools is because &lt;STRONG&gt;cold air can hold less water than warm air&lt;/STRONG&gt;&lt;/EM&gt;”. &lt;/P&gt;
&lt;P&gt;Nope. Not true. &lt;STRONG&gt;Your science teacher was wrong&lt;/STRONG&gt;. That statement, though arguably&amp;nbsp;not &lt;EM&gt;entirely&lt;/EM&gt; false, is sufficiently misleading as to work against a correct understanding of the weather. Nitrogen and oxygen, the gasses which make up most of the atmosphere, have no ability to “hold” anything. Their molecules are way too far apart to hold onto anything. Furthermore, phenomena such as cloud formation can be explained entirely without this idea, so just based solely upon Occam's razor we should dispense with the notion that the air has something to do with it.&lt;/P&gt;
&lt;P&gt;Ironically, the actual situation is not really very difficult to understand. First, consider the behaviour at the molecular level:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;A molecule of liquid water will vaporize when its energy is high enough. &lt;/LI&gt;
&lt;LI&gt;Conversely, a molecule of water vapor will condense into liquid when its energy is low enough.&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;Now consider the aggregate behaviour of zillions of individual molecules. We call the average energy of many molecules the “temperature”. When in gaseous form, that energy manifests itself as the molecules exerting force on their surroundings by slamming into things at high speed. We measure the number of molecules which inhabit a given region at a given temperature by measuring this force, which we call “pressure”. &lt;/P&gt;
&lt;P&gt;Since temperature is an average, obviously some of the molecules in the sample will have less energy than average, some will have more. If the temperature is high, then a higher percentage of the liquid molecules will have enough energy to vaporize and a lower percentage of vapor molecules will have low enough energy to condense. A warm puddle of water evaporates faster than a cold puddle of water for this reason.&amp;nbsp;And when you sweat, you cool down -- the warmest water molecules on your skin are evaporating and taking their heat with them, away from you.&lt;/P&gt;
&lt;P&gt;Since pressure (at a given temperature) is proportional to how many vapor molecules there are in a given amount of space, it should be equally clear that at high pressure, there are more molecules available to condense than at low pressure. So we should expect that, all else being equal, the higher the vapor pressure, the more condensation we’ll see. “Damp air” – air that has a lot of water vapor pressure - condenses onto a cold window a lot faster than dry air.&lt;/P&gt;
&lt;P&gt;With that simple molecular explanation, cloud formation can now be understood with no reference whatsoever to nonsense like “the water-holding capacity of the air”. Why should we mention the air at all? The air has nothing to do with it, aside from the fact that heating up the air also heats up the water vapor in it. Clouds would form equally well if our atmosphere was a mixture of helium and water vapor, or for that matter, just pure water vapor. &lt;STRONG&gt;All that matters is the temperature of the water and the vapor pressure in the region.&lt;/STRONG&gt; A cloud is exactly that portion of the atmosphere where the vapor pressure is high enough and the temperature is low enough that a significant quantity of water condenses.&lt;/P&gt;
&lt;P&gt;(To be nit-picky, yes, there are other factors. Foreign substances mixed into the liquid water, whether any of the water is in solid ice form, whether the liquid water is in round droplets or flat sheets, and so on, influence the rate of vaporization and condensation as well. My point though is simply that the "holding capacity" of the non-water part of the air is not relevant right now.)&lt;/P&gt;
&lt;P&gt;With that in mind let’s look at the larger picture of how a cumulus cloud forms. &lt;/P&gt;
&lt;P&gt;The sun heats the surface of a body of water.&amp;nbsp;Say, my beloved Lake Huron. The temperature of the surface rises, so the rate of vaporization increases.&amp;nbsp;Some warm vapor rises until it encounters a region of the atmosphere where the temperature is low enough and the vapor pressure is high enough that the water vapor turns back into liquid water. The liquid molecules combine into droplets which we can see. If the droplets get big enough then&amp;nbsp;they fall back to earth in the form of rain. &lt;/P&gt;
&lt;P&gt;That explains clouds and rain but does not explain the phenomenon I’m trying to understand here: why&amp;nbsp;thunderstorms are so energetic. It seems like what we’re doing is moving the energy from the sunlight into the lake surface, then into the atmosphere via evaporation, and then that energy is transferred randomly into to the rest of the atmosphere when the cloud cools and condenses. After all, that’s what “cooling” is – moving heat energy to somewhere else. If this explanation is right then clouds should be dispersing energy, not concentrating it.&amp;nbsp; Something else is going on here. What are we missing?&lt;/P&gt;
&lt;P&gt;Next time on FAIC: Entropy!&lt;/P&gt;&lt;/DIV&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=4178992" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/ericlippert/archive/tags/Science/default.aspx">Science</category><category domain="http://blogs.msdn.com/ericlippert/archive/tags/Weather/default.aspx">Weather</category><category domain="http://blogs.msdn.com/ericlippert/archive/tags/Non-computer/default.aspx">Non-computer</category><category domain="http://blogs.msdn.com/ericlippert/archive/tags/Best+Of+FAIC/default.aspx">Best Of FAIC</category></item><item><title>Through the Looking Glass</title><link>http://blogs.msdn.com/ericlippert/archive/2005/08/22/through-the-looking-glass.aspx</link><pubDate>Mon, 22 Aug 2005 23:00:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:454209</guid><dc:creator>Eric Lippert</dc:creator><slash:comments>17</slash:comments><comments>http://blogs.msdn.com/ericlippert/comments/454209.aspx</comments><wfw:commentRss>http://blogs.msdn.com/ericlippert/commentrss.aspx?PostID=454209</wfw:commentRss><description>&lt;FONT face="lucida sans unicode" color=purple size=2&gt;
&lt;P&gt;I'm back, I'm married, we had a fabulous time, and now I'm setting up new machines and figuring out what the heck I'm doing on the C# team. Today, we'll get back into it with some non-tech fun.&lt;/P&gt;
&lt;P&gt;A regular flat mirror seems like it ought to be perfectly symmetrical in its operations. So why is it that mirrors produce an image which reverse left and right, but not, say, top and bottom?&amp;nbsp; (A concave mirror, like the inside of a spoon, reverses top and bottom, but let’s worry about only flat mirrors for the duration of this article.) How does the mirror know which way is left-right?&lt;/P&gt;
&lt;P&gt;Think about that for a while before you read on. See if you can figure it out for yourself.&lt;/P&gt;
&lt;P&gt;Stuck?&amp;nbsp; Try this.&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Get a mirror.&amp;nbsp; 
&lt;LI&gt;Stand in the place where you live. 
&lt;LI&gt;Now face &lt;STRONG&gt;north&lt;/STRONG&gt;. 
&lt;LI&gt;Think about direction.&amp;nbsp; Wonder why you haven’t before. 
&lt;LI&gt;Hold the mirror out in front of you with your &lt;STRONG&gt;left&lt;/STRONG&gt; hand. 
&lt;LI&gt;Point &lt;STRONG&gt;east&lt;/STRONG&gt; with your &lt;STRONG&gt;right&lt;/STRONG&gt; hand. 
&lt;LI&gt;The “person in the mirror” is still pointing &lt;STRONG&gt;east&lt;/STRONG&gt;, and towards the &lt;STRONG&gt;right&lt;/STRONG&gt; side of the mirror (from your perspective), but doing so with their &lt;STRONG&gt;left&lt;/STRONG&gt; hand while facing &lt;STRONG&gt;south&lt;/STRONG&gt;.&lt;/LI&gt;&lt;/OL&gt;
&lt;P&gt;Wait a minute.&amp;nbsp; The mirror not only produces an image which reverses left and right but not up and down; apparently it also reverses north and south but not east and west! &lt;/P&gt;
&lt;P&gt;It’s more productive to think of a mirror as actually producing an image which reverses &lt;STRONG&gt;front and back&lt;/STRONG&gt;. If you’re facing north then “front and back” is the same as “north and south”.&amp;nbsp;If the mirror is on the floor&amp;nbsp;then “front and back” is the same as “up and down”. Note that if you took a movie of someone and flipped the movie film front-back when you showed it, you’d get the same effect.&lt;/P&gt;
&lt;P&gt;But why then do we all think of mirrors as reversing left and right, if in fact they reverse front-back?&amp;nbsp; Psychologically, humans see a front-back-reversed human as a left-right reversed human.&amp;nbsp; That image of a south-facing person smiling back at you is not an image of a &lt;EM&gt;human being&lt;/EM&gt; at all. Their DNA spirals the wrong way. All their body fat is made out of indigestible Olestra.&amp;nbsp; Their heart is on the wrong side of the body. If we could somehow create a real being who produced exactly that image, down to the front-back-reversed internal structure, there’s no way that they could produce viable offspring with a non-reversed human.&lt;/P&gt;
&lt;P&gt;But none of these is apparent at a glance. Since humans have almost perfect left-right symmetry it is extremely easy to interpret an image of a back-front-reversed human as a left-right reversed human. After all, if you were trying to act to look like the person in the mirror, that’s what you’d do – simply reverse your left and right behaviour.&amp;nbsp;You decide what is “slippable” and what isn’t.&amp;nbsp; If you are trying to look like the image in the mirror looks, you’d ignore all that stuff about your DNA and internal organs and reverse left and right, because that happens to produce the image that looks most similar. If humans had different symmetries then mirrors would not appear to reverse left-right at all, because our psychology would be different.&lt;/P&gt;
&lt;P&gt;Imagine a race of super-intelligent fishes that look like eels with a perfectly circular cross-section.&amp;nbsp; Our fishes have a blue fin on one side, a red fin on the other side, one eye in the very front of their face. These eely fishes can swim with their fins rotated in any direction they choose.&amp;nbsp; That’s a weird looking fish, but bear with me. Such a fish looking in a mirror would probably see the back-front reversed image as being not left-right reversed – because, what’s left-right to a symmetrical fish? – but rather as &lt;STRONG&gt;rotated 180 degrees&lt;/STRONG&gt;.&amp;nbsp; That’s what a fish would have to do in order to imitate the fish in the mirror.&amp;nbsp; The super-intelligent fish version of this blog entry would be “why do mirrors rotate images?”&lt;/P&gt;
&lt;P&gt;Similarly we could come up with bizarre creatures whose various lines of symmetry would cause them to see mirror images as top-bottom reversals. Even more bizarre, suppose human males and females were more or less exactly the same in their outward physical characteristics, except that men had both arms on the right side of their body, and women had both arms on the left side of their body.&amp;nbsp; We’d then be asking why mirrors change sex! And who knows what &lt;A href="http://www.memory-alpha.org/en/wiki/Let_That_Be_Your_Last_Battlefield"&gt;inhabitants of the planet Cheron&lt;/A&gt; would think?&lt;/P&gt;
&lt;P&gt;&lt;BR&gt;&amp;nbsp;&lt;/P&gt;&lt;/FONT&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=454209" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/ericlippert/archive/tags/Science/default.aspx">Science</category><category domain="http://blogs.msdn.com/ericlippert/archive/tags/Non-computer/default.aspx">Non-computer</category><category domain="http://blogs.msdn.com/ericlippert/archive/tags/Best+Of+FAIC/default.aspx">Best Of FAIC</category><category domain="http://blogs.msdn.com/ericlippert/archive/tags/Mirrors/default.aspx">Mirrors</category></item><item><title>Desafinado, Part Five: Getting Down Without Hitting The Bottom</title><link>http://blogs.msdn.com/ericlippert/archive/2005/04/18/desafinado-part-five-getting-down-without-hitting-the-bottom.aspx</link><pubDate>Mon, 18 Apr 2005 21:49:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:409291</guid><dc:creator>Eric Lippert</dc:creator><slash:comments>4</slash:comments><comments>http://blogs.msdn.com/ericlippert/comments/409291.aspx</comments><wfw:commentRss>http://blogs.msdn.com/ericlippert/commentrss.aspx?PostID=409291</wfw:commentRss><description>&lt;FONT face="lucida sans unicode" color=#800080 size=2&gt;
&lt;P&gt;Back in the 1960's a guy named Shepard published a paper which described a way to create a descending scale of twelve notes such that every consecutive pair was perceived as being two notes, the second one lower than the first. That's not hard -- every descending scale has that property! The kicker is that in a Shepard scale, &lt;B&gt;people also perceive the last note of the scale as higher than the first.&lt;/B&gt;&lt;/P&gt;
&lt;P&gt;Clearly that's totally impossible. If every note is lower than the one before then the twelfth cannot be higher than the first. And yet Shepard's Illusion is pretty strong. Another guy named Risset figured out a way to make the scale "continuous", so it sounds like one long note constantly getting lower but never hitting bottom.&lt;/P&gt;
&lt;P&gt;It's pretty tricky to pull off the illusion, and I haven't done a perfect job, but this at least illustrates it somewhat.&lt;/P&gt;
&lt;P&gt;The key to pulling off the illusion is to have the tone actually made up of many overtones and subtones of a primary tone. Take A=440Hz, for example. As I mentioned back in part one, for some reason humans seem to strongly associate perfect octaves. 220Hz and 880Hz sound like "the same note", only lower or higher respectively. It is hard to realize that this is not one tone, but actually many.&lt;/P&gt;
&lt;P&gt;What we'll do is play the three A's from the three different octaves simultaneously. If we played them all at the same volume, it's hard for the human ear to pick out which one is the "most important", but if we play the bottom one very softly, the middle one very loud, and the top one about medium, then it's easy to "lock on" to the mid-range component of the sound. &lt;/P&gt;
&lt;P&gt;Imagine someone playing this on the piano -- hitting three A's, the middle one loud and the bottom and top ones quietly.&lt;/P&gt;
&lt;P&gt;Now move all three notes UP eleven semitones, so that the A&amp;#9837; at the bottom is just below what was previously the middle A. Play the bottom note really loud but slightly softer than the previous loud A, the middle note slightly louder than the previous high A, and the new high note very soft indeed. The human ear does not perceive that as every note moving eleven semitones up, but rather that the loudest tone has moved down.&lt;/P&gt;
&lt;P&gt;Now take all three notes down a semitone. Every time you go down a semitone, make the bottom note a little quieter and the middle and top notes a little louder. Keep going until you get back to where you started, at which point go up eleven semitones again.&lt;/P&gt;
&lt;P&gt;That's the essence of the illusion. The human ear attunes itself to the loudest part of the tone, and therefore doesn't notice that the lows are dropping out of the bottom and being replaced by very faint notes at the top. As the top notes descend they get louder and louder, and eventually the ear switches from hearing the middle note as the primary to the bottom note as the primary, and then back to the middle later. &lt;/P&gt;
&lt;P&gt;Doing this as a continuously sliding tone requires some easy calculus. &lt;/P&gt;
&lt;P&gt;Consider just one component of the sound. We want to have a sine wave that is getting slower and slower over time. Say it starts at 440Hz and when we're done, it's going at 220Hz. How are we going to model this? Well, think "cycles per second". Take a wheel spinning on a fixed axle and put a mark on the edge. Ignore the side-to-side component of the mark as the wheel spins and just look at the up-and-down motion. That motion describes a sine wave. Spin the wheel at 440 cycles per second, and we'll get a 440 Hz sine wave out of the vertical component.&lt;/P&gt;
&lt;P&gt;How are we going to slow down from 440 Hz to 220 Hz, over, say, 16 seconds? We could use a linear model -- lose 14 Hz a second -- but that's not very sensible when we're thinking of sound. Remember, humans hear sounds based on ratios, not based on absolute numbers of cycles. Really what we want is for this to decay geometrically. We want the sound to have a "half life" in the frequency domain.&lt;/P&gt;
&lt;P&gt;It is convenient to measure the speed not in revolutions per second but in radians per second. There are 2&amp;pi; radians per revolution, so a wheel that is spinning at 440 Hz is spinning at 880&amp;pi; radians per second. Let's say that we're going to decay from our original frequency f=440 Hz down to f/2 in T seconds. What is the angular velocity at time t if it is an exponential decay?&lt;/P&gt;
&lt;P&gt;&amp;omega;(t) = (2&amp;pi;f) 2&lt;SUP&gt;-t/T&lt;/SUP&gt; radians per second&lt;/P&gt;
&lt;P&gt;Check that -- yep, that gives you 880&amp;pi; radians per second for t = 0, and 440&amp;pi; radians per second for t=T. &lt;/P&gt;
&lt;P&gt;Super. Now we need to determine the height of the mark at any given time. The angular position is easily determined from the angular velocity, and the height is the sine of the angle. Let's work out the angle:&lt;/P&gt;
&lt;P&gt;&amp;Theta;(t) = &amp;int;&amp;omega;(t) dt = (-2fT&amp;pi;/ln 2) 2&lt;SUP&gt;-t/T&lt;/SUP&gt; + C&lt;/P&gt;
&lt;P&gt;for some constant C which we'll just set to zero arbitrarily -- we do not care about the phase, just the frequency. &lt;/P&gt;
&lt;P&gt;Take the sine of the angle, and we're all set, we've got our decaying wave. &lt;/P&gt;
&lt;P&gt;That takes care of the frequency decay. What about the change in volume of each component?&lt;/P&gt;
&lt;P&gt;We'll do a rough approximation of a bell curve. We want the very highest highs to come in very quietly. We want the very lowest lows to go out very quietly. And we want most of the sound energy in the middle, so that that's what you hear. Rather than muck around with computing a real bell curve, we'll just do a simple linear approximation of one.&lt;/P&gt;
&lt;P&gt;I find the illusion to be strongest when the primary note is pretty low. Let's try 110 Hz, two octaves below concert A. We'll make some simple modifications to yesterday's program. We'll decay over 16 seconds, and we'll do three decays.  And we'll use eight full octaves.&lt;/P&gt;
&lt;FONT face="Lucida Console" color=#333399 size=2&gt;&lt;BR&gt;namespace Wave { &lt;BR&gt;&amp;nbsp; using System; &lt;BR&gt;&amp;nbsp; using System.IO; &lt;BR&gt;&amp;nbsp; class MainClass { &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public static void Main(String[] args) { &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; FileStream stream = new FileStream("test.wav", FileMode.Create);&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; BinaryWriter writer = new BinaryWriter(stream);&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; int RIFF = 0x46464952;&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; int WAVE = 0x45564157;&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; int formatChunkSize = 16;&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; int headerSize = 8;&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; int format = 0x20746D66;&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; short formatType = 1;&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; short tracks = 1;&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; int samplesPerSecond = 44100;&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; short bitsPerSample = 16;&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; short frameSize = (short)(tracks * ((bitsPerSample + 7)/8));&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; int bytesPerSecond = samplesPerSecond * frameSize;&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; int waveSize = 4;&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; int data = 0x61746164;&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; int T = 16;&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; int reps = 3;&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; int samplesperrep = samplesPerSecond * T;&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; int dataChunkSize = samplesperrep * reps * frameSize;&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; int fileSize = waveSize + headerSize + formatChunkSize + headerSize + dataChunkSize;&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; writer.Write(RIFF);
&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; writer.Write(fileSize);&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; writer.Write(WAVE);&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; writer.Write(format);&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; writer.Write(formatChunkSize);&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; writer.Write(formatType);&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; writer.Write(tracks);&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; writer.Write(samplesPerSecond);&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; writer.Write(bytesPerSecond);&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; writer.Write(frameSize);&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; writer.Write(bitsPerSample);&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; writer.Write(data);&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; writer.Write(dataChunkSize);&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; double fundamental = 110.0;&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; double ampl = 10000;&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for (int j = 0 ; j &amp;lt; reps ; ++j)&amp;nbsp;{&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for (int i = 0; i &amp;lt; samplesperrep; i++)&amp;nbsp;{&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; double t = (double)i / (double)samplesPerSecond;&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; double amp0 = (ampl/8) * t / T;&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; double amp1 = (ampl/8) + (ampl/8) * t/T;&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; double amp2 = (ampl/4) + (ampl/4) * t/T;&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; double amp3 = (ampl/2) + (ampl/2) * t/T;&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; double amp4 = (ampl/1) - (ampl/2) * t/T;&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; double amp5 = (ampl/2) - (ampl/4) * t/T;&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; double amp6 = (ampl/4) - (ampl/8) * t/T;&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp;&amp;nbsp;double amp7 = (ampl/8) - (ampl/8) * t/T;&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; double theta = -fundamental * 2 * Math.PI * T * Math.Pow(2, -t/T) / Math.Log(2);&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; short s = (short)(&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; amp0*Math.Sin(theta*16)+amp1*Math.Sin(theta*8)+&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; amp2*Math.Sin(theta*4)+amp3*Math.Sin(theta*2)+&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; amp4*Math.Sin(theta*1)+amp5*Math.Sin(theta/2)+&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; amp6*Math.Sin(theta/4)+amp7*Math.Sin(theta/8)&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; );&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; writer.Write(s);&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;} &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; } &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; writer.Close(); &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; stream.Close(); &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; } &lt;BR&gt;&amp;nbsp; } &lt;BR&gt;} &lt;BR&gt;&lt;/FONT&gt;
&lt;P&gt;The brain at some point stops thinking that the lower midrange frequency is the interesting one, and switches to the higher. If you listen attentively you can notice when your brain makes the switch, and the illusion is then somewhat dispelled.&lt;/P&gt;
&lt;P&gt;Pretty weird, eh? &lt;/P&gt;
&lt;P&gt;Coming up soon: back to wackiness in scripting!&lt;/P&gt;&lt;/FONT&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=409291" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/ericlippert/archive/tags/Rarefied+Heights/default.aspx">Rarefied Heights</category><category domain="http://blogs.msdn.com/ericlippert/archive/tags/Science/default.aspx">Science</category><category domain="http://blogs.msdn.com/ericlippert/archive/tags/Music/default.aspx">Music</category></item><item><title>Desafinado, Part Four: Rolling Your Own WAV Files</title><link>http://blogs.msdn.com/ericlippert/archive/2005/04/15/desafinado-part-four-rolling-your-own-wav-files.aspx</link><pubDate>Fri, 15 Apr 2005 21:29:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:408632</guid><dc:creator>Eric Lippert</dc:creator><slash:comments>8</slash:comments><comments>http://blogs.msdn.com/ericlippert/comments/408632.aspx</comments><wfw:commentRss>http://blogs.msdn.com/ericlippert/commentrss.aspx?PostID=408632</wfw:commentRss><description>&lt;FONT face="Lucida sans unicode"&gt;&lt;FONT color=#800080 size=2&gt;
&lt;P&gt;We’ve established why every just about piano in the world -- in fact, every concert-pitched musical instrument in the world -- is slightly out of tune. No one actually plays perfect fifths; every fifth interval is slightly flat. Why don't we hear the difference? Is the difference even perceptible?&lt;/P&gt;
&lt;P&gt;It is &lt;I&gt;very&lt;/I&gt; hard to hear unless you compare and contrast. So let's do that. Here's a little C# program I just whipped up. This program creates a WAV file that first plays two seconds of E a perfect fifth above concert A=220Hz, and then two seconds of E a slightly flattened fifth above A. &lt;/P&gt;
&lt;P&gt;Can you hear the difference?&amp;nbsp;I can't hear the difference between the two E's at all.&lt;/P&gt;
&lt;P&gt;However, you can REALLY hear the difference in the next section. The file then plays two seconds of E and a "perfect B" above it together, and then two seconds of E and a "concert B" above it. &lt;/P&gt;
&lt;P&gt;Now it is obvious -- with such clean, perfect waves you can really strongly hear it when it goes out of tune. You get a sort of ringing "wah wah wah" effect as the waves go in and out of sync with each other. The number of&amp;nbsp;wahs,&amp;nbsp;or, as piano tuners call them, &lt;STRONG&gt;beats&lt;/STRONG&gt;&amp;nbsp;per second tells you how close to a perfect fifth the notes are -- the slower the beats, the more in tune.&amp;nbsp; Experienced piano tuners can easily hear when the number of beats per second&amp;nbsp;is just right for the piano to be exactly out of tune enough to be evenly tempered. &lt;/P&gt;
&lt;P&gt;This code could use some explanation. &lt;/P&gt;
&lt;P&gt;The basic WAV file format follows the Interchange File Format specification. An IFF file consists of a series of "chunks" where chunks can contain other chunks. Each chunk starts with an eight byte header: four bytes describing the chunk, followed by four bytes giving the size of the chunk (not counting the eight byte header). The header is followed by the given number of bytes of data in a chunk-specific format. A WAV file consists of one main chunk called RIFF that contains three things: the string "WAVE", a "format" chunk that describes the sample rate, etc, and a "data" chunk that contains the sampled waveform.&lt;/P&gt;
&lt;P&gt;We won't mess around with any advanced WAV file features like cue points or playlists or compression. We'll just dump out some data and play it with the WAV file player of your choice. We'll use CD quality audio -- 44100 samples per second, each one with 16 bits per sample. (Unlike a CD, we'll do this in mono, not stereo.)&lt;/P&gt;&lt;/FONT&gt;&lt;FONT face="Lucida Console" color=#333399 size=2&gt;
&lt;P dir=ltr&gt;namespace Wave&lt;BR&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;using System;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;using System.IO;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;class MainClass {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public static void Main()&amp;nbsp;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;FileStream stream = new FileStream("test.wav", FileMode.Create);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;BinaryWriter writer = new BinaryWriter(stream);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;int RIFF = 0x46464952;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;int WAVE = 0x45564157;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;int formatChunkSize = 16;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;int headerSize = 8;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;int format = 0x20746D66;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;short formatType = 1;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;short tracks = 1;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;int samplesPerSecond = 44100;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;short bitsPerSample = 16;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;short frameSize = (short)(tracks * ((bitsPerSample + 7)/8));&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;int bytesPerSecond = samplesPerSecond * frameSize;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;int waveSize = 4;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;int data = 0x61746164;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;int samples = 88200 * 4;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;int dataChunkSize = samples * frameSize;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;int fileSize = waveSize + headerSize + formatChunkSize + headerSize + dataChunkSize;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;writer.Write(RIFF);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;writer.Write(fileSize);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;writer.Write(WAVE);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;writer.Write(format);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;writer.Write(formatChunkSize);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;writer.Write(formatType);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;writer.Write(tracks);&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;writer.Write(samplesPerSecond);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;writer.Write(bytesPerSecond);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;writer.Write(frameSize);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;writer.Write(bitsPerSample);&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;writer.Write(data);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;writer.Write(dataChunkSize);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;double aNatural = 220.0;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;double ampl = 10000;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;double perfect = 1.5;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;double concert = 1.498307077;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;double freq = aNatural * perfect;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;for (int&amp;nbsp;i = 0; i &amp;lt; samples / 4; i++) {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;double t = (double)i / (double)samplesPerSecond;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;short s = (short)(ampl * (Math.Sin(t * freq * 2.0 * Math.PI)));&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;writer.Write(s);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;freq = aNatural * concert;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;for (int i = 0; i &amp;lt; samples / 4; i++)&amp;nbsp;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;double t = (double)i / (double)samplesPerSecond;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;short s = (short)(ampl * (Math.Sin(t * freq * 2.0 * Math.PI)));&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;writer.Write(s);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;for (int i = 0; i &amp;lt; samples / 4; i++) {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;double t = (double)i / (double)samplesPerSecond;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;short s = (short)(ampl * (Math.Sin(t * freq * 2.0 * Math.PI) + Math.Sin(t * freq * perfect * 2.0 * Math.PI)));&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;writer.Write(s);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;for (int i = 0; i &amp;lt; samples / 4; i++) {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;double t = (double)i / (double)samplesPerSecond;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;short s = (short)(ampl * (Math.Sin(t * freq * 2.0 * Math.PI) + Math.Sin(t * freq * concert * 2.0 * Math.PI)));&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;writer.Write(s);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;writer.Close();&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;stream.Close();&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;BR&gt;}&lt;/P&gt;&lt;/FONT&gt;&lt;FONT color=#800080 size=2&gt;
&lt;P&gt;Compile this guy up, run it, and listen to test.wav. Pretty cool eh?&lt;/P&gt;
&lt;P&gt;Next time, we'll wrap up with one of the most interesting psychological effects you can get in music -- a tone that goes down, but never hits bottom.&lt;/P&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=408632" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/ericlippert/archive/tags/Rarefied+Heights/default.aspx">Rarefied Heights</category><category domain="http://blogs.msdn.com/ericlippert/archive/tags/Science/default.aspx">Science</category><category domain="http://blogs.msdn.com/ericlippert/archive/tags/Music/default.aspx">Music</category></item><item><title>Desafinado, Part Three: Too Many Fifths</title><link>http://blogs.msdn.com/ericlippert/archive/2005/04/13/desafinado-part-three-too-many-fifths.aspx</link><pubDate>Wed, 13 Apr 2005 22:27:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:407908</guid><dc:creator>Eric Lippert</dc:creator><slash:comments>4</slash:comments><comments>http://blogs.msdn.com/ericlippert/comments/407908.aspx</comments><wfw:commentRss>http://blogs.msdn.com/ericlippert/commentrss.aspx?PostID=407908</wfw:commentRss><description>&lt;FONT face="lucida sans unicode" color=purple size=2&gt;
&lt;P&gt;Last time we established the diatonic scale which has the nice property that there are five tone intervals and six fifths:&lt;/P&gt;
&lt;TABLE class=""&gt;
&lt;TBODY&gt;
&lt;TR align=middle&gt;
&lt;TH class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;Note&lt;/FONT&gt;&lt;/TH&gt;
&lt;TH class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;Frequency&lt;/FONT&gt;&lt;/TH&gt;&lt;/TR&gt;
&lt;TR align=middle&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;A&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;220.000&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR align=middle&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;B&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;247.500&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR align=middle&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;C&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;260.741&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR align=middle&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;D&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;293.333&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR align=middle&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;E&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;330.000&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR align=middle&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;F&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;347.654&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR align=middle&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;G&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;391.111&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;
&lt;P&gt;and then double for the next octave up and so on.&lt;/P&gt;
&lt;P&gt;But it's a little weird in that B doesn't have a fifth above it, just E a fifth below it. Similarly, F has no fifth below it. If there were any justice in this world, B and F ought to be fifth's of each other. But they're not quite right! A fifth above B is smack between F and G. Similarly, a fifth below F is between A and B. &lt;/P&gt;
&lt;P&gt;We stuck E and B between two other notes. So let’s do it again. The fifth below F is a little bit below B, so we'll call it "B flat", or B♭. A fifth above B is a little bit above F, so we'll call it "F sharp", or F♯ In our first octave we have&lt;/P&gt;
&lt;TABLE class=""&gt;
&lt;TBODY&gt;
&lt;TR align=middle&gt;
&lt;TH class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;Note&lt;/FONT&gt;&lt;/TH&gt;
&lt;TH class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;Frequency&lt;/FONT&gt;&lt;/TH&gt;&lt;/TR&gt;
&lt;TR align=middle&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;A&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;220.000&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR align=middle&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;B♭&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;231.769&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR align=middle&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;B&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;247.500&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR align=middle&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;C&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;260.741&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR align=middle&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;D&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;293.333&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR align=middle&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;E&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;330.000&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR align=middle&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;F&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;347.654&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR align=middle&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;F♯&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;371.250&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR align=middle&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;G&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;391.111&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;
&lt;P&gt;Gah. This doesn't solve anything. &lt;B&gt;Now we've got two more notes missing a fifth&lt;/B&gt;. Well, let's add them. A fifth below B♭ is E♭, a fifth above F♯ is C♯. And &lt;B&gt;again&lt;/B&gt;, we've got the same problem! A fifth below E♭ is A♭, a fifth above C♯ is G♯, and our first octave and a bit now looks like this:&lt;/P&gt;
&lt;TABLE class=""&gt;
&lt;TBODY&gt;
&lt;TR align=middle&gt;
&lt;TH class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;Note&lt;/FONT&gt;&lt;/TH&gt;
&lt;TH class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;Frequency&lt;/FONT&gt;&lt;/TH&gt;&lt;/TR&gt;
&lt;TR align=middle&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;A♭&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;206.017&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR align=middle&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;A&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;220.000 &lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR align=middle&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;B♭&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;231.769 &lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR align=middle&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;B&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;247.500 &lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR align=middle&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;C&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;260.741&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR align=middle&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;C♯&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;278.438 &lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR align=middle&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;D&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;293.333 &lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR align=middle&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;E♭&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;309.025&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR align=middle&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;E&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;330.000 &lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR align=middle&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;F&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;347.654 &lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR align=middle&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;F♯&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;371.250&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR align=middle&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;G&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;391.111&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR align=middle&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;A♭&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;412.033&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR align=middle&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;G♯&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;417.657&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR align=middle&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;A&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;440.000&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;
&lt;P&gt;Hold on a minute here. This is getting ridiculous. A♭ and G♯ are less than 2% apart, and seem to have gotten out of order -- how is it that A♭ is lower than G♯? 
&lt;P&gt;This is a mess. In two octaves our stringed instrument now has over 25 strings and we don't seem to be slowing down at all as far as adding new ones goes!&lt;/P&gt;&lt;B&gt;
&lt;P&gt;We could keep on doing this literally forever&lt;/B&gt;. Why? Because as the Pythagoreans discovered, &lt;B&gt;there are no whole number ratios that can be squared, cubed, or put to any other power such that you eventually end up with two&lt;/B&gt;. We are &lt;B&gt;never&lt;/B&gt; going to multiply &lt;B&gt;any&lt;/B&gt; new note by any combination of 2/1, 1/2, 3/2 or 2/3 and end up with a note that we've already got in any other octave. The system is not closed.&lt;/P&gt;
&lt;P&gt;Look at how close A♭ and G♯ are. Wouldn't it be tempting to just "split the difference", set them equal to each other, and be done with it? &lt;/P&gt;
&lt;P&gt;Hmm.&lt;/P&gt;
&lt;P&gt;Let's throw out that A♭ in there and look at the ratios of successive notes -- not as whole number ratios, but as percentages.&lt;/P&gt;
&lt;TABLE class=""&gt;
&lt;TBODY&gt;
&lt;TR align=middle&gt;
&lt;TH class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;Note&lt;/FONT&gt;&lt;/TH&gt;
&lt;TH class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;Increase&lt;/FONT&gt;&lt;/TH&gt;&lt;/TR&gt;
&lt;TR align=middle&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;A♭&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;7.0%&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR align=middle&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;A&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;5.3%&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR align=middle&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;B♭&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;6.8%&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR align=middle&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;B&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;5.3%&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR align=middle&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;C&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;6.8%&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR align=middle&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;D&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;5.1% &lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR align=middle&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;E♭&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;7.0%&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR align=middle&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;E&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;5.3%&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR align=middle&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;F&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;6.8%&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR align=middle&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;F♯&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;5.3%&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR align=middle&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;G&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;6.8%&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR align=middle&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;G♯&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;5.3%&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;
&lt;P&gt;Each note on this scale is between 5.1% and 7.0% higher than the previous note. &lt;B&gt;What if we made every interval the same percentage?&lt;/B&gt; What percentage would it be? We need to double in frequency over an octave, and have twelve steps to do it in. The twelfth root of two is about 1.05946. Suppose we redo this twelve-note scale so that every note is 5.946% higher than the previous. What do we get?&lt;/P&gt;
&lt;TABLE class=""&gt;
&lt;TBODY&gt;
&lt;TR align=middle&gt;
&lt;TH class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;Note&lt;/FONT&gt;&lt;/TH&gt;
&lt;TH class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;Frequency&lt;/FONT&gt;&lt;/TH&gt;
&lt;TH class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;Difference&lt;/FONT&gt;&lt;/TH&gt;&lt;/TR&gt;
&lt;TR align=middle&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;A&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;220.000&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;0.00%&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR align=middle&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;A♯/B♭&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;233.082&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;0.57%&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR align=middle&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;B&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;246.942&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;-0.23%&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR align=middle&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;C&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;261.625&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;0.34%&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR align=middle&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;C♯/D♭&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;277.183&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;-0.45%&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR align=middle&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;D&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;293.664&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;-0.11%&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR align=middle&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;D♯/E♭&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;311.127&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;0.68%&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR align=middle&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;E&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;329.627&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;-0.11%&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR align=middle&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;F&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;349.228&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;0.45%&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR align=middle&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;F♯/G♭&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;369.995&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;-0.34%&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR align=middle&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;G&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;391.995&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;0.23%&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR align=middle&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;G♯/A♭&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;415.305&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;-0.56%&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR align=middle&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;A&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;440.000&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD class=""&gt;&lt;FONT face="lucida sans unicode" color=purple size=2&gt;0.00%&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;
&lt;P&gt;This is pretty darn close to the scale we deduced from Pythagorean principles. And we have successfully split the difference -- we say that A♭ and G♯ are the same frequency. Similarly, we add D♭ = C♯ and so on, so that &lt;B&gt;every note has a fifth below and a fifth above.&lt;/P&gt;&lt;/B&gt;
&lt;P&gt;This twelve-note scale is called the &lt;B&gt;equally-tempered chromatic scale&lt;/B&gt;, and it is the scale that pianos and other musical instruments are actually tuned to. &lt;/P&gt;
&lt;P&gt;This scale is &lt;B&gt;a compromise between flexibility and tonal perfection&lt;/B&gt;. The price you pay for having a closed system is that instead of the frequency of the higher note being 1.5 times the frequency of the lower note, it's actually 1.4983 times higher -- &lt;B&gt;every fifth interval is slightly flat&lt;/B&gt;.&lt;B&gt; &lt;/P&gt;&lt;/B&gt;
&lt;P&gt;And in fact that's how piano tuners do it "by ear". They tune one string to a tuning fork, then tune a fifth above it to a &lt;B&gt;perfect&lt;/B&gt; 3:2 fifth, and then flatten the upper string just very slightly (or, equivalently, slightly sharpen the lower string if they are tuning a fifth below). Then they use that as the reference to tune the next note above it to a slightly flattened fifth, and so on until all twelve notes in one octave are tuned. Every other note can then be tuned to those reference notes. The process of getting the middle octave of the piano tuned is called "setting the temperament", which explains my little pun in part one. &lt;/P&gt;
&lt;P&gt;Bach's famous set of preludes and fugues, one in all twelve major and minor keys, is called "The Well-Tempered Clavier". Though it sounds good when played in an equal temperament, Bach actually designed the pieces to be played on a "Well Tempered" instrument. In that temperament certain intervals are kept pure while others are significantly off, and must be avoided by the composer.&lt;/P&gt;
&lt;P&gt;Next time, we get computers back into the picture. We'll write some programs that illustrate this fact, and thereby show how piano tuners can tune pianos by ear. Then in my final installment we'll write a program that illustrates a psychoacoustic oddity called Shepard's Illusion.&lt;/P&gt;&lt;/FONT&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=407908" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/ericlippert/archive/tags/Rarefied+Heights/default.aspx">Rarefied Heights</category><category domain="http://blogs.msdn.com/ericlippert/archive/tags/Science/default.aspx">Science</category><category domain="http://blogs.msdn.com/ericlippert/archive/tags/Music/default.aspx">Music</category></item><item><title>Desafinado, Part Two: A Perfect Pythagorean Tuning</title><link>http://blogs.msdn.com/ericlippert/archive/2005/04/11/desafinado-part-two-a-perfect-pythagorean-tuning.aspx</link><pubDate>Mon, 11 Apr 2005 21:22:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:407252</guid><dc:creator>Eric Lippert</dc:creator><slash:comments>3</slash:comments><comments>http://blogs.msdn.com/ericlippert/comments/407252.aspx</comments><wfw:commentRss>http://blogs.msdn.com/ericlippert/commentrss.aspx?PostID=407252</wfw:commentRss><description>&lt;FONT face="Lucida Sans Unicode" color=purple size=2&gt;
&lt;P&gt;Last time we talked about the Pythagorean's discovery that sounds with vibrations in the ratios 1:2 and 2:3 sound consonant to the human ear. Let's explore the consequences of that a bit.&lt;/P&gt;
&lt;P&gt;Suppose we're building a stringed instrument from scratch and we want to tune it so that it sounds good to human ears.&amp;nbsp; Let's tune the first string so that it vibrates at 440 vibrations per second.&amp;nbsp; The unit of vibrations per second is the Hertz, so this is a 440Hz string.&amp;nbsp; Call that string "A2".&lt;/P&gt;
&lt;P&gt;There's no particularly good reason to pick 440Hz versus any other frequency, but we've got to start somewhere. Also, note that I'm assuming here that we have some magical way to exactly tune a string to a particular number of vibrations.&amp;nbsp;The practical details of piano tuning in the absense of electronic measuring equipment are interesting, but before we get into that, I want to have a theoretical basis for what the ideal tuning ought to be.&lt;/P&gt;
&lt;P&gt;Anyway, we've labeled this string "A2", and it's 440Hz.&amp;nbsp; We'll add&amp;nbsp;two more strings, an&amp;nbsp;octave above and below that: A1=220Hz and A3=880Hz.&lt;/P&gt;
&lt;P&gt;If we want to do nice harmonies and melodies, we have an intuition from Pythagorus that fifths are a good idea.&amp;nbsp;What note below A2 makes a perfect fifth with A2?&amp;nbsp; That is, what to 440Hz makes a 2:3 ratio?&amp;nbsp; Easy -- 293.333Hz.&amp;nbsp; Call that string D1, and make a string D2 an octave above it.&amp;nbsp; So far we've got&lt;/P&gt;&lt;FONT face="Lucida Console"&gt;
&lt;P&gt;Note&amp;nbsp;Frequency&lt;BR&gt;&amp;nbsp;A1&amp;nbsp;&amp;nbsp; 220.000&lt;BR&gt;&amp;nbsp;D1&amp;nbsp;&amp;nbsp; 293.333&lt;BR&gt;&amp;nbsp;A2&amp;nbsp;&amp;nbsp; 440.000&lt;BR&gt;&amp;nbsp;D2&amp;nbsp;&amp;nbsp; 586.666&lt;BR&gt;&amp;nbsp;A3&amp;nbsp;&amp;nbsp; 880.000&lt;/P&gt;&lt;/FONT&gt;
&lt;P&gt;A now has a fifth below, D has a fifth above. Let's make a fifth below D. What to D2 makes a 2:3 ratio?&amp;nbsp; Call that G1, and add an octave G2 above it:&lt;/P&gt;&lt;FONT face="Lucida Console"&gt;
&lt;P&gt;Note Frequency&lt;BR&gt;&amp;nbsp;A1&amp;nbsp; &amp;nbsp;220.000&lt;BR&gt;&amp;nbsp;D1&amp;nbsp;&amp;nbsp; 293.333&lt;BR&gt;&amp;nbsp;G1&amp;nbsp;&amp;nbsp; 391.111&lt;BR&gt;&amp;nbsp;A2&amp;nbsp;&amp;nbsp; 440.000&lt;BR&gt;&amp;nbsp;D2&amp;nbsp;&amp;nbsp; 586.666&lt;BR&gt;&amp;nbsp;G2&amp;nbsp;&amp;nbsp; 782.222&lt;BR&gt;&amp;nbsp;A3&amp;nbsp;&amp;nbsp; 880.000&lt;/P&gt;&lt;/FONT&gt;
&lt;P&gt;Now do the same thing to G2 to make C1 and C2, and the same thing to C2 to make F1 and F2, and we end up with a stringed instrument that looks like this in the first octave: (the second octave is all these doubled, so I'm going to stop calling out which octave we're in when I name the note.&amp;nbsp; It'll be clear from context.)&lt;/P&gt;&lt;FONT face="Lucida Console"&gt;
&lt;P&gt;Note Frequency&lt;BR&gt;&amp;nbsp;A&amp;nbsp;&amp;nbsp; &amp;nbsp;220.000&lt;BR&gt;&amp;nbsp;C&amp;nbsp;&amp;nbsp;&amp;nbsp; 260.741&lt;BR&gt;&amp;nbsp;D&amp;nbsp;&amp;nbsp;&amp;nbsp; 293.333&lt;BR&gt;&amp;nbsp;F&amp;nbsp;&amp;nbsp;&amp;nbsp; 347.654&lt;BR&gt;&amp;nbsp;G&amp;nbsp;&amp;nbsp;&amp;nbsp; 391.111&lt;/P&gt;
&lt;P&gt;&lt;/FONT&gt;We'd then have an eleven-stringed instrument on which you could play traditional Asian music.&amp;nbsp; This is called the &lt;B&gt;pentatonic scale&lt;/B&gt; because it has five notes before it repeats.&lt;/P&gt;
&lt;P&gt;Something that's interesting about this scale is the &lt;STRONG&gt;ratios between successive notes' frequencies:&lt;/STRONG&gt;&lt;/P&gt;&lt;FONT face="Lucida Console"&gt;
&lt;P&gt;A : C = 27 : 32&lt;BR&gt;C : D =&amp;nbsp; 8 : 9&lt;BR&gt;D : F = 27 : 32&lt;BR&gt;F : G =&amp;nbsp; 8 : 9 &lt;BR&gt;G : A =&amp;nbsp; 8 : 9&lt;/P&gt;&lt;/FONT&gt;
&lt;P&gt;Clearly there is something interesting going on with this 8:9 ratio.&amp;nbsp; Therefore, we'll give it a name. Just as the interval between two notes that are 2:3 is called a "fifth", the interval between two notes that are 8:9 is called a "tone".&lt;/P&gt;
&lt;P&gt;There are two notable things here. First, the gaps from A to C and&amp;nbsp;D to F are awfully big, quite a bit bigger than a tone.&amp;nbsp; Second, though D:A is a fifth, there is no string such that A:x forms a fifth.&amp;nbsp; We forgot to add a fifth above A.&lt;/P&gt;
&lt;P&gt;In keeping with our intuition that the 8:9 ratio is important, let's insert tones above A and D.&amp;nbsp; We'll add notes in an&amp;nbsp;8:9 ratio,&amp;nbsp;call them B and E, and then work out the ratios of each string to its neighbour again:&lt;/P&gt;&lt;FONT face="Lucida Console"&gt;
&lt;P&gt;Note &amp;nbsp;Frequency Ratio to next&lt;BR&gt;&amp;nbsp;A&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 220.000&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 8 : 9&lt;BR&gt;&amp;nbsp;B&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 247.500&amp;nbsp;&amp;nbsp;&amp;nbsp; 243 : 256&lt;BR&gt;&amp;nbsp;C&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 260.741&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;8 : 9&lt;BR&gt;&amp;nbsp;D&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 293.333&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 8 : 9&lt;BR&gt;&amp;nbsp;E&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 330.000&amp;nbsp;&amp;nbsp;&amp;nbsp; 243 : 256&lt;BR&gt;&amp;nbsp;F&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 347.654&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 8 : 9&lt;BR&gt;&amp;nbsp;G&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 391.111&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 8 : 9&lt;/P&gt;&lt;/FONT&gt;
&lt;P&gt;Hey, check it out -- this has the nice additional property that E is a fifth above A! Coolness. Two birds with one stone and all that.&lt;/P&gt;
&lt;P&gt;This is called the &lt;B&gt;diatonic scale&lt;/B&gt;, and is the standard scale for western music.&amp;nbsp; These are the white keys on a piano.&amp;nbsp; &lt;/P&gt;
&lt;P&gt;Interestingly, the ancient Greeks developed this standard scale, but different groups wrote music in modes that emphasized different starting notes. If you play the scale CDEFGABC, we'd call that a "major" scale, but the Greeks would have called it the "Ionian Mode".&amp;nbsp; The Aeolian Mode started on A, and we'd call that the "natural minor" scale.&amp;nbsp; The Mixolydians started on G. (The Beatles wrote several songs in Mixolydian Mode, which is why songs like "Love Me Do" and "A Hard Day's Night" have a kind of odd sound to their chord progressions.)&amp;nbsp; There is a different Greek tribe associated with each possible starting note, but they all used this scale.&lt;/P&gt;
&lt;P&gt;Now we see where the terms "octave" and "fifth" come from. There are &lt;B&gt;eight&lt;/B&gt; diatonic notes between (inclusive) any two notes an octave apart, and for every note &lt;B&gt;except B and F&lt;/B&gt;, there are &lt;B&gt;five&lt;/B&gt; notes between two notes a fifth apart. B has no fifth above, F has no fifth below.&lt;/P&gt;
&lt;P&gt;The diatonic scale has many nice properties -- it has a large number of pairs of notes that sound good together. &lt;STRONG&gt;The greatest number of consonant pairs of any&amp;nbsp;seven note scale, in fact.&lt;/STRONG&gt;&amp;nbsp; (The proof is left as an exercise to the reader.) A consequence of this fact is that if we want &lt;STRONG&gt;more&lt;/STRONG&gt; consonant pairs, we're going to have to add &lt;STRONG&gt;more notes&lt;/STRONG&gt;. Let's do that. After all, so far everything is working out great! &lt;/P&gt;
&lt;P&gt;Next time: fixing that problem with B and F not having one of their fifths is going to screw &lt;EM&gt;everything&lt;/EM&gt; up.&lt;/P&gt;
&lt;P&gt;(Foreshadowing -- your sign of a quality blog.)&lt;/P&gt;&lt;/FONT&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=407252" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/ericlippert/archive/tags/Rarefied+Heights/default.aspx">Rarefied Heights</category><category domain="http://blogs.msdn.com/ericlippert/archive/tags/Science/default.aspx">Science</category><category domain="http://blogs.msdn.com/ericlippert/archive/tags/Music/default.aspx">Music</category></item><item><title>Desafinado, Part One: Eric Continues His Obsession With The Greeks</title><link>http://blogs.msdn.com/ericlippert/archive/2005/04/08/desafinado-part-one-eric-continues-his-obsession-with-the-greeks.aspx</link><pubDate>Fri, 08 Apr 2005 22:02:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:406605</guid><dc:creator>Eric Lippert</dc:creator><slash:comments>8</slash:comments><comments>http://blogs.msdn.com/ericlippert/comments/406605.aspx</comments><wfw:commentRss>http://blogs.msdn.com/ericlippert/commentrss.aspx?PostID=406605</wfw:commentRss><description>&lt;FONT face="lucida sans unicode" color=purple size=2&gt;
&lt;P&gt;Whew.&amp;nbsp; That's enough wacky VBScript for a while.&lt;/P&gt;
&lt;P&gt;As I said a long, long time ago now, I came up with the idea of doing a little "wacky VBScript" quiz in order to illustrate some of the weird corner cases in language design.&amp;nbsp; I want to do a long -- potentially very long -- series on some of the more computer sciency fundamental issues in the theory of programming languages and what "computation" means. These quirks are just the fun corner cases.&lt;/P&gt;
&lt;P&gt;But I feel like changing gears for a bit before I get into that heavy lifting, if you'll pardon the mixed metaphor.&lt;/P&gt;
&lt;P&gt;So for the next little while, music! No computers!&lt;/P&gt;
&lt;P&gt;Well, ok, maybe &lt;EM&gt;some&lt;/EM&gt; computers. In parts one through three, we'll build up some musical theory starting from the Ancient Greeks. In Part Four, we'll write a program that illustrates what I'm talking about. In Part Five, weird psychoacoustics!&lt;/P&gt;
&lt;P&gt;*********************************************&lt;/P&gt;
&lt;P&gt;One of my many dilletantish hobbies is playing the piano.&amp;nbsp; I've got an upright at home with a lovely case and a terrible action, and it's not really in tune.&amp;nbsp; I have all the equipment I need to tune it myself, I just don't have the time right now, or the temperament.&lt;/P&gt;
&lt;P&gt;Ha ha ha ha, little piano tuner joke there.&amp;nbsp; It'll become clear why that's so funny in a bit.&lt;/P&gt;
&lt;P&gt;The ironic thing about tuning a piano is this: &lt;B&gt;all pianos are out of tune. On purpose.&lt;/P&gt;&lt;/B&gt;
&lt;P&gt;In fact, almost all music you've ever heard on all instruments has been deliberately out of tune.&lt;/P&gt;
&lt;P&gt;How is that possible?&amp;nbsp; Surely you'd have noticed!&lt;/P&gt;
&lt;P&gt;To explain, we have to go way, way back to the time of Pythagoras, that mystic ancient Greek whose dour band of crazy math geeks violently suppressed information about dodecahedra and the irrationality of root two.&amp;nbsp; Amongst the Pythagoreans' many fundamental discoveries in mathematics is that if you pluck tightened strings of &lt;STRONG&gt;equal tension and different lengths&lt;/STRONG&gt;, the sound is most &lt;B&gt;consonant&lt;/B&gt; -- pleasing to the ear&amp;nbsp;-- when the ratio between the string lengths is a &lt;B&gt;ratio of small whole numbers&lt;/B&gt;.&lt;/P&gt;
&lt;P&gt;For instance, take a string under tension and press down on it like you would on a guitar,&amp;nbsp;dividing it into two strings, one twice as long as the other.&amp;nbsp;&amp;nbsp;Or, equivalently, get two strings under the same tension, one twice as long as the other.&amp;nbsp; If you&amp;nbsp;pluck them&amp;nbsp;at the same time then&amp;nbsp;you get a very pleasing sound.&amp;nbsp; The ratio is 1:2, and this is a "perfect octave". &amp;nbsp;&amp;nbsp;Find yourself a piano and hit middle C and the next higher or lower C and you'll hear what I mean.&amp;nbsp; In some very strong sense the human ear hears these two tones as extremely similar. We hear it as "the same tone, only higher".&lt;/P&gt;
&lt;P&gt;Clearly&amp;nbsp;"octave" has something to do with the number eight -- what exactly will become clear later.&lt;/P&gt;
&lt;P&gt;Why it is that humans find small ratios pleasant is an interesting question that I might explore in another post.&amp;nbsp; But for now, just accept that small whole number ratios sound really good.&lt;/P&gt;
&lt;P&gt;If 1:2 sounds good, what about other small-number ratios?&amp;nbsp; Doubling/halving the string length gives you an octave. What about making the string 50% longer -- a ratio of 2:3?&amp;nbsp; Yes, that also sounds really good.&amp;nbsp; If you play C on a piano and then hit the G below it, that's a ratio of 2:3 in terms of string length (again, assuming that the strings are under the same tension, etc.)&amp;nbsp; &lt;/P&gt;
&lt;P&gt;This is the ratio used in Gregorian chants.&amp;nbsp; This ratio is called a "fifth", again, for reasons which will become clear later.&lt;/P&gt;
&lt;P&gt;We know nowadays something that the Pythagoreans did not: that what really matters is not the &lt;B&gt;lengths of the strings&lt;/B&gt;, but the &lt;B&gt;speed at which they vibrate&lt;/B&gt;.&amp;nbsp; It just happens that a string that is twice as long vibrates half as slowly, all other things being equal, so basically the ratios are the same whether you're talking about string length or vibration frequency, they're just opposite in order. &lt;/P&gt;
&lt;P&gt;From now on we'll talk only about ratios in terms of their vibrations per second, and stop worrying about &lt;STRONG&gt;string length&lt;/STRONG&gt;. A piano has eight octaves. If the longest string had to be 128 times longer than the shortest string then&amp;nbsp;you'd need a big room!&amp;nbsp; Therefore we vary the thickness, weight and tension of the strings so that they can be a reasonable length and yet vibrate at the right frequency. It's the vibrations that matter.&amp;nbsp;(The variance in thickness of the strings leads to practical problems with inharmonicity in the short strings, but we probably won't get into that level of detail in this series.)&lt;/P&gt;
&lt;P&gt;Next time we'll mentally&amp;nbsp;build a stringed instrument from scratch and figure out how to tune it to perfect Pythagorean pitches.&lt;/P&gt;&lt;/FONT&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=406605" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/ericlippert/archive/tags/Rarefied+Heights/default.aspx">Rarefied Heights</category><category domain="http://blogs.msdn.com/ericlippert/archive/tags/Science/default.aspx">Science</category><category domain="http://blogs.msdn.com/ericlippert/archive/tags/Music/default.aspx">Music</category><category domain="http://blogs.msdn.com/ericlippert/archive/tags/Ancient+Greeks/default.aspx">Ancient Greeks</category></item><item><title>That's a Big Transistor</title><link>http://blogs.msdn.com/ericlippert/archive/2005/01/21/that-s-a-big-transistor.aspx</link><pubDate>Sat, 22 Jan 2005 01:37:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:358512</guid><dc:creator>Eric Lippert</dc:creator><slash:comments>24</slash:comments><comments>http://blogs.msdn.com/ericlippert/comments/358512.aspx</comments><wfw:commentRss>http://blogs.msdn.com/ericlippert/commentrss.aspx?PostID=358512</wfw:commentRss><description>&lt;font face="Lucida Sans Unicode"&gt;&lt;font color="#800080" size="2"&gt; &lt;p&gt;Here's some fun for a Friday.&lt;/p&gt; &lt;p&gt;A few years back a bunch of my coworkers and I got to discussing the space program over lunch. Someone asked why it is that we continue to launch devices into orbit by strapping a big old tank full of liquid oxygen to the device and then set it on fire. Why haven't we developed better technology using magnets or something? &lt;/p&gt; &lt;p&gt;I did some&amp;nbsp;poking around the web&amp;nbsp;and wrote up a little summary of the analysis, which I present here for your amusement.&lt;/p&gt; &lt;p&gt;*****************************&lt;/p&gt; &lt;p&gt;Suppose you've got a 1000 kilogram object that you'd like to be an arbitrary height above the surface of the earth. In order to get an object to an arbitrary height, we need to accelerate it to the escape velocity of the earth, which is 11 kilometres per second. How much energy does that take? The kinetic energy of a projectile is half the mass times the square of the velocity:&lt;/p&gt;&lt;/font&gt;&lt;font color="#000080" size="2"&gt; &lt;p&gt;Kinetic Energy = 0.5 x 1000 kg x (11000 m/s)&lt;sup&gt;2&lt;/sup&gt; = about 60 gigajoules&lt;/p&gt;&lt;/font&gt;&lt;font color="#800080" size="2"&gt; &lt;p&gt;Sixty billion Joules of energy sounds like rather a lot, but really it isn't. The electrical power for your house is measured in Joules, but because Joules are so tiny, the bill comes in kilowatt hours. One Watt is a power consumption of one Joule of energy per second, so a kilowatt hour is a power consumption of 1000 Joules per second for 3600 seconds = 3.6 million Joules. That costs about ten cents. So that sixty billion Joules would only cost about $1700 --&amp;nbsp;near what&amp;nbsp;it costs to keep 20 one hundred Watt light bulbs burning for a solid year. And power costs less if you buy it in bulk.&lt;/p&gt; &lt;p&gt;$1700 to put a tonne in orbit is&amp;nbsp;incredibly cheap. Why doesn't NASA use electricity instead of chemical power? Turning the electricity into acceleration quickly is harder than turning it into heat and light slowly, but surely we can figure something out.&lt;/p&gt; &lt;p&gt;One way to do it would be to build a coil gun. Coil guns are very easy to build -- in fact, I've built one myself. A coil gun works like this: you have a metal projectile sitting in a tube. One end of the tube has a coil of wire wrapped around it. When an electric current is applied to the wire, it turns the coil into an electromagnet, which pulls the projectile towards it. &lt;/p&gt; &lt;p&gt;Of course, the current has to shut off before the projectile passes entirely through the coil, otherwise the electromagnet will be pulling the object back towards it, slowing it down.&amp;nbsp; The projectile sails on through the coil, out into space.&lt;/p&gt; &lt;p&gt;You can build multi-stage coil guns. The first coil gets the projectile moving. As&amp;nbsp;the projectile&amp;nbsp;leaves the first coil, that triggers a switch that turns on the next coil, and so on. Each coil makes the projectile a little bit faster.&lt;/p&gt; &lt;p&gt;So a coil gun stage&amp;nbsp;has three basic parts: the coil itself, something which stores the electricity -- usually a capacitor of some kind -- and a switch to connect the power source to the coil at exactly the right moment.&lt;/p&gt; &lt;p&gt;I once&amp;nbsp;had an old television set that didn't work anymore. Before I took it to the dump I ransacked it for capacitors. I had everything I needed around the house -- a few capacitors, a diode, some old telephone wire, a piece of spare kite spar tubing, and a light switch. Run wall power through the diode to charge the capacitors, run power from the capacitors to the coil, interrupted by a switch. Put a nail in the tube, hit the switch, and the nail goes flying. Cool!&amp;nbsp; (Note: large capacitors can kill or wound you when charged.&amp;nbsp; Handle with care.)&lt;/p&gt; &lt;p&gt;The problem is that none of these pieces scale up.&lt;/p&gt; &lt;p&gt;Suppose we've got a really huge multi-stage coil gun. Just to keep the math easy, let's say we have ten thousand coils, each a metre long with which we're going to accelerate our projectile to 10000m/s by applying a uniform acceleration of 5000m/s&lt;sup&gt;2&lt;/sup&gt;. That acceleration&amp;nbsp;would squash humans like bugs, but we could use this thing to move large quantities of equipment into orbit. When the on switch is hit, a mere two seconds later the projectile will be heading into space at 10000m/s.&lt;/p&gt; &lt;p&gt;Consider only the last coil of the ten thousand. The projectile is going to be moving at almost 10km/s, so it will only be in the coil for about 100 microseconds. We can assume that this is about how long the last coil is going to be energized. &lt;/p&gt; &lt;p&gt;It is &lt;strong&gt;vitally important that the pulse of electricity delivered to the coil be short&lt;/strong&gt;, for three reasons. First, as I mentioned before, the magnet had better be off by the time the&amp;nbsp;projectile reaches the far side, otherwise the magnet will be slowing the projectile down. Second, the farther the&amp;nbsp;projectile is from the coil when the coil is energized, the weaker the magnetic pull will be; you don't want to waste power by turning the magnet on while the object is too far away to&amp;nbsp;get much&amp;nbsp;oomph from it. And most important, &lt;b&gt;the magnetic field strength of an electromagnet is proportional to the electric current&lt;/b&gt;. Current is electrons moved per second, so if you want high current you have to either make the number of electrons moved larger, or the amount of time you spend moving them smaller, or preferably both.&lt;/p&gt; &lt;p&gt;For all these reasons we can assume that the pulse is going to be extremely short, on the order of 100 microseconds.&lt;/p&gt; &lt;p&gt;Current is voltage divided by resistance, power is voltage times current, and the heat produced by resistance is a function of current.&amp;nbsp;&amp;nbsp;(That's why we have high-voltage power lines to deliver power: we step the power up to high voltage so that it moves&amp;nbsp;lots of power without&amp;nbsp;creating too much heat. We then step it back down to more useful low voltage/high current at local transformer stations.) The resistance in copper wire is going to be non-trivial, and worse, the magnetic fields set up in the electromagnet are going to themselves push against the electrons moving through the coil -- we're going to get inductance on the line. The closer together the coils, the stronger the electromagnet will be, but the more impedance will be produced.&amp;nbsp;In order to get enough current, we're going to need huge voltages. And no matter how you slice it, the coils are going to generate heat, lots of it. But that's OK. We can cool the coils, and make sure that we don't pump so much power into each coil that the copper wire melts.&lt;/p&gt; &lt;p&gt;But there is another place where heat is produced. 100% of the current for each coil has to pass through a switch. A conventional switch, where you have two pieces of metal with a gap between them, and you remove the gap, isn't going to work for the kind of power we're talking about. We're talking about currents way larger than arc welders use, so basically the switch will weld itself shut. And as the switch closes,&amp;nbsp;the high voltages&amp;nbsp;will cause current to leak across the channel when the switch is half closed, thereby increasing the amount of time that electrons are flowing. That will weaken the&amp;nbsp;magnetic field.&amp;nbsp;Mechanical switches just aren't going to cut it.&lt;/p&gt; &lt;p&gt;Fortunately we have a very fast, very precisely controllable switching technology: transistors. A transistor is a chunk of silicon which has been carefully constructed so that it can act as either an electrical insulator or an electrical conductor. They are incredibly high-speed switches&amp;nbsp;-- that's why we make computers out of them. Let's just build ten thousand transistors, one for each coil switch. The switch for a coil will be triggered by the projectile interrupting a laser beam crossing the previous coil.&lt;/p&gt; &lt;p&gt;Hold on, a minute though. Transistors are semiconductors -- they do not transmit electricity perfectly. They're maybe 90% efficient. About 10% of the power transferred through the switch is going to be turned into heat inside the switch. How much heat can a transistor take before its wrecked? Anyone who has overclocked a Pentium knows what I'm talking about! Transistors get real hot real fast when you try to push a lot of power through them.&lt;/p&gt; &lt;p&gt;How much power?&lt;/p&gt;&lt;/font&gt;&lt;font color="#000080" size="2"&gt; &lt;p&gt;Power&amp;nbsp;= mass x acceleration x distance / time&lt;/p&gt;&lt;/font&gt;&lt;font color="#800080" size="2"&gt; &lt;p&gt;By the time we hit the last coil, the 1000kg projectile will be moving at about 9999.5m/s. We need to accelerate it up to 10000m/s, we have 100 microseconds in which to do so, and one metre. The acceleration is 5000m/s&lt;sup&gt;2&lt;/sup&gt;. If we work it out, the total power required by the last stage is &lt;strong&gt;fifty billion Watts&lt;/strong&gt;.&amp;nbsp;The five million Joules cost about a dime; it's&amp;nbsp;getting them&amp;nbsp;where they need to be&amp;nbsp;in 0.0001 seconds that's the hard part.&lt;/p&gt; &lt;p&gt;We're going to turn 10% of that power&amp;nbsp;directly into heat in the switch, so that's five billion Watts of heat to dissipate. Imagine the heat of ten million 500 Watt spotlights concentrated in a small area, and all turned on for 100 microseconds.&amp;nbsp; It'll be hot.&lt;/p&gt; &lt;p&gt;Note that we're assuming that 100% of the force generated by the magnetic field is translated into motive force on the projectile. In reality, only about&amp;nbsp;25% of the magnetic force actually turns into acceleration.&amp;nbsp; We'd need to up the power by at least a factor of&amp;nbsp;four,&amp;nbsp;so really we've got more like 20 billion Watts of heat to dissipate. But let's ignore that for now.&lt;/p&gt; &lt;p&gt;Let's assume that the transistor is extremely thin and therefore the transistor is equally hot everywhere. An extremely thin transistor is also a good idea because a thin transistor has large surface area. The larger the surface area an object has, the faster you can cool it. Let's wrap our transistor in a huge copper heat sink. &lt;/p&gt; &lt;p&gt;Slabs of copper do not diffuse heat infinitely quickly. The temperature of the surface of the heat sink that is touching the transistor will rise based on many factors -- higher power consumption, smaller surface area and longer application of heat by the transistor all cause a larger temperature rise at the interface between the transistor and the sink. The heat capacity and thermal diffusion of copper are well known, and from these facts we can work out that:&lt;/p&gt; &lt;p&gt;&lt;/font&gt;&lt;font color="#000080" size="2"&gt;temperature rise at surface in &lt;font size="2"&gt;℃&lt;/font&gt; = 0.6 x (&amp;nbsp;Watts&amp;nbsp;/ cm&lt;sup&gt;2&lt;/sup&gt;) x &lt;font size="4"&gt;√&lt;/font&gt;seconds &lt;/p&gt;&lt;/font&gt;&lt;font color="#800080" size="2"&gt; &lt;p&gt;Clearly if the heat sink is not sucking heat out fast enough, the heat sink is going to &lt;i&gt;itself&lt;/i&gt; get hot enough to wreck the transistor. Let's suppose that the transistor can take a rise of 100 degrees Celcius before it is destroyed. We have 5 billion Watts of heat, and 100 microseconds to get rid of as much of it as possible. How much area do we need to ensure that the surface of the heat sink rises only 100 degrees?&lt;/p&gt; &lt;p&gt;Solve the above equation for area and you get 300000 square centimeters. That's a square transistor 5.5 metres on a side, about the size of a typical&amp;nbsp;kitchen floor.&lt;/p&gt; &lt;p&gt;Transistor-grade wafer-thin silicon costs about $2 for a square centimeter, so the transistor for the last stage alone will cost us about $600000 dollars. And that's for the stage that only adds 0.01% of the total oomph, for a device that can only launch a one-tonne projectile, and we've neglected inefficiency in the coil. Clearly to build the whole device would cost &lt;strong&gt;multiple billions of dollars for the on switch alone&lt;/strong&gt;. That's an expensive on switch!&lt;/p&gt; &lt;p&gt;Of course, we could solve these problems by inventing new magic materials. If we had cheap superconductors that conducted electricity and heat with 100% efficiency at reasonable temperatures, supported large currents and fast switching, then sure, we could build mass drivers that put objects into orbit at low cost. Tragically, we do not have magic materials.&lt;/p&gt; &lt;p&gt;And of course I haven't even mentioned the difficulties of generating and storing enough power to light Seattle and then discharging it all in two seconds. Generating large quantities of power spread out over lots of time and space is easy. Concentrating that power into a very tiny time and space is hard. Fifty gigawatts for the last stage alone -- we'd need the Mr. Fusion from Back To The Future!&amp;nbsp;&amp;nbsp;(Actually, we'd need around 40 of them, as Mr. Fusion generated the mere&amp;nbsp;1.21&amp;nbsp;gigawatts required to power the flux capacitor.)&amp;nbsp;I've also glossed over many serious difficulties in the implementation of the coils; dealing with self-inductance, magnetic eddys and other issues is non-trivial.&lt;/p&gt; &lt;p&gt;Until there are radical new advances in material science and power management we're going to be stuck with strapping big tanks of liquid oxygen onto the sides of projectiles if we want to get them into space.&lt;/p&gt; &lt;p&gt;****************************&lt;/p&gt; &lt;p&gt;Further reading:&lt;/p&gt; &lt;p&gt;I stole this argument from this excellent, more technical description of the switching problem: &lt;/font&gt;&lt;a href="http://www.islandone.org/LEOBiblio/SPBI1SI.HTM"&gt;&lt;u&gt;&lt;font color="#0000ff" size="2"&gt;http://www.islandone.org/LEOBiblio/SPBI1SI.HTM&lt;/u&gt;&lt;/font&gt;&lt;/a&gt;&lt;/p&gt;&lt;font color="#800080" size="2"&gt; &lt;p&gt;This site is a good one if you're interested in exotic orbital technologies: &lt;/font&gt;&lt;a href="http://www.islandone.org/LEOBiblio/"&gt;&lt;u&gt;&lt;font color="#0000ff" size="2"&gt;http://www.islandone.org/LEOBiblio/&lt;/u&gt;&lt;/font&gt;&lt;/a&gt;&lt;/p&gt;&lt;font color="#800080" size="2"&gt; &lt;p&gt;This guy builds &lt;/font&gt;&lt;a href="http://www.powerlabs.org/coilgun.htm"&gt;&lt;u&gt;&lt;font color="#0000ff" size="2"&gt;coil guns with switches rated to 14000 amps at 1300 Volts&lt;/u&gt;&lt;/font&gt;&lt;/a&gt;&lt;font color="#800080" size="2"&gt;. I would imagine that these are not cheap.&lt;/p&gt; &lt;p&gt;This page has some great explanations of the &lt;/font&gt;&lt;a href="http://www.coilgun.com/"&gt;&lt;u&gt;&lt;font color="#0000ff" size="2"&gt;magnetic and electric physics involved in coil guns&lt;/u&gt;&lt;/font&gt;&lt;/a&gt;&lt;font color="#800080" size="2"&gt;.&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;/font&gt;&lt;/font&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=358512" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/ericlippert/archive/tags/Rarefied+Heights/default.aspx">Rarefied Heights</category><category domain="http://blogs.msdn.com/ericlippert/archive/tags/Science/default.aspx">Science</category><category domain="http://blogs.msdn.com/ericlippert/archive/tags/Non-computer/default.aspx">Non-computer</category></item></channel></rss>