<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Gerry Beauregard</title>
	<atom:link href="http://gerrybeauregard.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://gerrybeauregard.wordpress.com</link>
	<description>Programming, investing, and random stuff.</description>
	<lastBuildDate>Mon, 30 Jan 2012 22:57:40 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='gerrybeauregard.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Gerry Beauregard</title>
		<link>http://gerrybeauregard.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://gerrybeauregard.wordpress.com/osd.xml" title="Gerry Beauregard" />
	<atom:link rel='hub' href='http://gerrybeauregard.wordpress.com/?pushpress=hub'/>
		<item>
		<title>A Flash audio output for real-time Javascript audio generation</title>
		<link>http://gerrybeauregard.wordpress.com/2011/11/14/a-flash-audio-output-for-real-time-javascript-audio-generation/</link>
		<comments>http://gerrybeauregard.wordpress.com/2011/11/14/a-flash-audio-output-for-real-time-javascript-audio-generation/#comments</comments>
		<pubDate>Mon, 14 Nov 2011 07:57:59 +0000</pubDate>
		<dc:creator>Gerry Beauregard</dc:creator>
				<category><![CDATA[Audio]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://gerrybeauregard.wordpress.com/?p=249</guid>
		<description><![CDATA[In the not-too-distant future, it seems likely that all major browsers will provide built in support for real-time audio generation in Javascript. Standardization for the Web Audio API is well under way. Looks like it&#8217;s already supported in shipping versions &#8230; <a href="http://gerrybeauregard.wordpress.com/2011/11/14/a-flash-audio-output-for-real-time-javascript-audio-generation/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gerrybeauregard.wordpress.com&amp;blog=14994846&amp;post=249&amp;subd=gerrybeauregard&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>In the not-too-distant future, it seems likely that all major browsers will provide built in support for real-time audio generation in Javascript. <a href="https://dvcs.w3.org/hg/audio/raw-file/tip/webaudio/specification.html" title="W3 page on Web Audio specification.">Standardization for the Web Audio API</a> is well under way. Looks like it&#8217;s already supported in shipping versions of Chrome.</p>
<p>In the meantime though, if you want to do real-time audio synthesis in Javascript without requiring Chrome or special beta plugins for other browsers, one alternative is to use a Flash SWF that calls out to Javascript to do the audio generation. This post describes such a SWF.</p>
<p>Here&#8217;s the general idea: the ActionScript 3 code for the SWF uses the Sound class&#8217; SampleDataEvent mechanism to periodically request sample data. The SampleDataEvent handler uses the ExternalInterface class to call a generateAudio() function implemented in Javascript; that function computes the audio sample values, and returns them to the SWF.</p>
<p>Code for a proof-of-concept can be found below. The full source code for the SWF is in one class, FlashAudioOutput. The Javascript code just generates a sine tone, but in principle you could of course generate whatever audio data you like.</p>
<p>The code is generally pretty obvious. The only tricky bit is how to transfer a block of audio sample data from Javascript to ActionScript. The most obvious way would be to use an Array, but this turns out to be horrendously inefficient. Incredible as it may seem, it&#8217;s actually more efficient to pass the data as a String!</p>
<p>That&#8217;s accomplished using Javascript code that converts an Array of normalized (-1,1) floating point numbers to a very long string of 16-bit integers represented in hexadecimal. That&#8217;s done in function convertToHexString() below. In the ActionScript code, the SampleDataEvent handler does the reverse operation of converting the string of hexadecimal values back to normalized float-point values.</p>
<p>Here&#8217;s the ActionScript code for the FlashAudioOutput class:</p>
<p><pre class="brush: as3; wrap-lines: false;">
package
{
	import flash.display.Sprite;
	import flash.events.SampleDataEvent;
	import flash.external.ExternalInterface;
	import flash.media.Sound;
	
	/**
	 * A class to allow real-time audio generation in Javascript in browsers that
	 * don't support the HTML5 real-time audio API. Calls out to a generateAudio
	 * function at regular intervals to get a buffer of audio in the form of
	 * a string of 16-bit signed hexadecimal interleaves (LRLR...) samples.
	 */
	public class FlashAudioOutput extends Sprite
	{
		private const NUM_CHANNELS:uint = 2;			// Audio output is stereo
		private const CHUNK_LEN:uint = 8192;			// Number of stereo sample pairs per SampleDataEvent
		private const SAMPLE_RATE:Number = 44100;		// Sample rate (Hz)
		
		public function FlashAudioOutput()
		{
			var isAvailable:Boolean = ExternalInterface.available;
			if (!isAvailable)
			{
				trace(&quot;ExternalInterface not available!&quot;);
				return;
			}
			
			// Create Sound object for and start real-time audio 'callbacks'
			var snd:Sound = new Sound;
			snd.addEventListener(SampleDataEvent.SAMPLE_DATA, onSampleData);
			snd.play();
		}
		
		
		/**
		 * Called at regular intervals to request new audio
		 * data to be sent to the output.
		 */
		private function onSampleData(ev:SampleDataEvent):void
		{
			const BITS_PER_SAMPLE:uint = 16;		// Number of bits per sample
			const HEX_DIGITS_PER_SAMPLE:uint = 4;	// Number of hexadecimal digits per 16-bit sample
			const SHRT_MAX:Number = 32767.0;		// Maximum signed 16-bit number
			const SCALE:Number = 1.0/SHRT_MAX;		// For scaling from 16-bit signed int to normalized [-1,1] range
			const TWO_TO_16TH:Number = 65536;		// 2^16. For two's complement conversion

			
			// Call out to the Javascript audio generation function
			var hexBuf:String = ExternalInterface.call(&quot;generateAudio&quot;, CHUNK_LEN);
			
			var expectedLength:uint = NUM_CHANNELS * CHUNK_LEN * HEX_DIGITS_PER_SAMPLE;
			if (hexBuf.length != expectedLength)
				trace(&quot;hexString not expected length&quot;, hexBuf.length, expectedLength);
			
			var pos:uint = 0;
			for (var i:uint = 0; i &lt; NUM_CHANNELS*CHUNK_LEN; i++)
			{
				// Get next 16-bit, 4 digit hex number
				var sub:String = hexBuf.substr(pos, HEX_DIGITS_PER_SAMPLE);
				pos += HEX_DIGITS_PER_SAMPLE;
				
				// Convert from 16-bit two's complement hexadecimal
				// to floating point number in range [-1,1]
				var val:Number = parseInt(sub, BITS_PER_SAMPLE);
				if (val &gt; SHRT_MAX)
					val -= TWO_TO_16TH;
				val *= SCALE;
				
				// Write to output
				ev.data.writeFloat(val);
			}
		}
	}
}
</pre></p>
<p>And here&#8217;s the relevant bit of Javascript code:</p>
<p><pre class="brush: jscript; wrap-lines: false;">
			var phase = 0.0;
			var phaseInc = 2*Math.PI*440/44100;
			
			// Audio generation function. Called at regular intervals by the audio output SWF.
			function generateAudio(n)
			{
				// Generate a sine tone
				// Exercise for developer: make more interesting sound :-)
				var x = new Array(2*n);
				var pos = 0;
				for (i = 0; i &lt; n; i++)
				{
					var val = 0.1*Math.sin(phase);
					x[pos++] = val;	// Left
					x[pos++] = val;	// Right
					phase += phaseInc;
				}

				return convertToHexString(x);
			}

			// 
			// Converts an array of floating point sample values into 16-bit hexadecimal
			// two's complement representation. Input values are expected to be in
			// [-1,1] range and are clipped if they exceed it.		
			function convertToHexString(x)
			{
				SHRT_MAX = 32767.0;
				TWO_TO_16TH = 65536;
				BITS_PER_SAMPLE = 16;
				HEX_DIGITS_PER_SAMPLE = 4;
			
				n = x.length;
				hexBuf = &quot;&quot;;
	
				// For each sample			
				for (i = 0; i &lt; n; i++)
				{
					val = x[i];
				
					// Clip to range [-1,1]
					if (val &gt; 1)
						val = 1;
					else if (val &lt; -1)
						val = -1;
				
					// Scale and convert to two's complement form						
					val = Math.floor(SHRT_MAX*val);
					if (val &lt; 0)
						val += TWO_TO_16TH;
					
					// Convert to hexadecimal
					hex = val.toString(BITS_PER_SAMPLE);

					// Append to string, adding leading zeros if necessary
					pad = HEX_DIGITS_PER_SAMPLE - hex.length;
					while (pad--)
						hexBuf += &quot;0&quot;;
					hexBuf += hex;
				}
				return hexBuf;
			}
</pre></p>
<p>A running implementation of the above can be found <a href="http://www.audiostretch.com/flashaudiooutput/">here</a>.</p>
<p>Have fun with the code. Feel free to use it however you like. Comments are always welcome.</p>
<p>Note: to copy the code without the line numbers, just mouse over the top-right part are of each listing. Some icons will appear, one of which is for &#8220;copy to clipboard&#8221;. Click that, then paste into whatever editor you use for Flash development.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/gerrybeauregard.wordpress.com/249/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/gerrybeauregard.wordpress.com/249/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/gerrybeauregard.wordpress.com/249/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/gerrybeauregard.wordpress.com/249/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/gerrybeauregard.wordpress.com/249/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/gerrybeauregard.wordpress.com/249/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/gerrybeauregard.wordpress.com/249/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/gerrybeauregard.wordpress.com/249/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/gerrybeauregard.wordpress.com/249/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/gerrybeauregard.wordpress.com/249/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/gerrybeauregard.wordpress.com/249/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/gerrybeauregard.wordpress.com/249/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/gerrybeauregard.wordpress.com/249/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/gerrybeauregard.wordpress.com/249/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gerrybeauregard.wordpress.com&amp;blog=14994846&amp;post=249&amp;subd=gerrybeauregard&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://gerrybeauregard.wordpress.com/2011/11/14/a-flash-audio-output-for-real-time-javascript-audio-generation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/601d233914f6e10f862be379fbe76b6e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">gerrybeauregard</media:title>
		</media:content>
	</item>
		<item>
		<title>AudioStretch &#8211; real-time audio time-stretching and pitch-shifting in ActionScript</title>
		<link>http://gerrybeauregard.wordpress.com/2011/10/13/audiostretch-real-time-audio-time-stretching-and-pitch-shifting-in-actionscript/</link>
		<comments>http://gerrybeauregard.wordpress.com/2011/10/13/audiostretch-real-time-audio-time-stretching-and-pitch-shifting-in-actionscript/#comments</comments>
		<pubDate>Thu, 13 Oct 2011 14:28:19 +0000</pubDate>
		<dc:creator>Gerry Beauregard</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://gerrybeauregard.wordpress.com/?p=239</guid>
		<description><![CDATA[In my very first post, I mentioned that the FFT has many applications in audio signal processing. In fact, the main reason I ported my old C++ FFT to ActionScript is so that I could implement AudioStretch, a real-time time-stretcher &#8230; <a href="http://gerrybeauregard.wordpress.com/2011/10/13/audiostretch-real-time-audio-time-stretching-and-pitch-shifting-in-actionscript/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gerrybeauregard.wordpress.com&amp;blog=14994846&amp;post=239&amp;subd=gerrybeauregard&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>In my <a href="http://gerrybeauregard.wordpress.com/2010/08/03/an-fft-in-as3/" title="An FFT in AS3">very first post</a>, I mentioned that the FFT has many applications in audio signal processing. In fact, the main reason I ported my old C++ FFT to ActionScript is so that I could implement <a href="http://www.audiostretch.com/" title="AudioStretch" target="_blank">AudioStretch</a>, a real-time time-stretcher / pitch-shifter that runs in Flash, in your web browser.</p>
<p>I first put AudioStretch online in late 2009, and I got a lot of traffic for a while, especially after <a href="http://www.andre-michelle.com/" title="André-Michelle site" target="_blank">André-Michelle</a> tweeted about it. In retrospect, though, it really didn&#8217;t sound very good. I think people were just intrigued that stuff like this could be done at all in Flash.</p>
<p>Since then, I&#8217;ve made some huge improvements to the sound quality &#8211; better bass response, better transient handling, vastly better inter-channel phase coherence.  I&#8217;ve added more speed options as well &#8211; you can even go to 0% speed, which can be pretty cool for working out notes of chords. (The UI is still really basic. Improvements coming soon!).  So if you haven&#8217;t tried it in a while, or if you&#8217;ve never tried it at all, I invite you to give it a go:</p>
<p><a href="http://www.audiostretch.com/" title="AudioStretch" target="_blank">Try AudioStretch Now!</a></p>
<p>Let me know what you think!</p>
<p>Incidentally, I have no plans to publicly release the code for AudioStretch. If you&#8217;re wondering how it works, here&#8217;s what I can tell you: it&#8217;s loosely based on the <a href="http://en.wikipedia.org/wiki/Phase_vocoder" title="Wikipedia Phase Vocoder article">phase vocoder</a> technique, but also embodies loads of proprietary tricks which I might write up one day&#8230;</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/gerrybeauregard.wordpress.com/239/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/gerrybeauregard.wordpress.com/239/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/gerrybeauregard.wordpress.com/239/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/gerrybeauregard.wordpress.com/239/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/gerrybeauregard.wordpress.com/239/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/gerrybeauregard.wordpress.com/239/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/gerrybeauregard.wordpress.com/239/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/gerrybeauregard.wordpress.com/239/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/gerrybeauregard.wordpress.com/239/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/gerrybeauregard.wordpress.com/239/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/gerrybeauregard.wordpress.com/239/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/gerrybeauregard.wordpress.com/239/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/gerrybeauregard.wordpress.com/239/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/gerrybeauregard.wordpress.com/239/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gerrybeauregard.wordpress.com&amp;blog=14994846&amp;post=239&amp;subd=gerrybeauregard&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://gerrybeauregard.wordpress.com/2011/10/13/audiostretch-real-time-audio-time-stretching-and-pitch-shifting-in-actionscript/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/601d233914f6e10f862be379fbe76b6e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">gerrybeauregard</media:title>
		</media:content>
	</item>
		<item>
		<title>Soundflower &#8211; great tool for recording the Mac&#8217;s audio output</title>
		<link>http://gerrybeauregard.wordpress.com/2011/08/31/soundflower-great-tool-for-recording-the-macs-audio-output/</link>
		<comments>http://gerrybeauregard.wordpress.com/2011/08/31/soundflower-great-tool-for-recording-the-macs-audio-output/#comments</comments>
		<pubDate>Wed, 31 Aug 2011 05:23:48 +0000</pubDate>
		<dc:creator>Gerry Beauregard</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://gerrybeauregard.wordpress.com/?p=234</guid>
		<description><![CDATA[I just came across a really good solution to a problem I&#8217;ve often encountered: how to record the sound output from a Mac. For example, I sometimes need to record the audio output from a web page that has a &#8230; <a href="http://gerrybeauregard.wordpress.com/2011/08/31/soundflower-great-tool-for-recording-the-macs-audio-output/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gerrybeauregard.wordpress.com&amp;blog=14994846&amp;post=234&amp;subd=gerrybeauregard&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I just came across a really good solution to a problem I&#8217;ve often encountered: how to record the sound output from a Mac. For example, I sometimes need to record the audio output from a web page that has a <a title="Sonoport ActionScript Interactive Audio" href="www.sonoport.com">Sonoport</a> dynamic sound model running on it.</p>
<p>Unlike some PC sound cards, the Mac doesn&#8217;t have an audio loopback feature to send the output to the input. It&#8217;s possible to physically route the output to the input using a 1/8&#8243; to 1/8&#8243; inch stereo cable connecting the audio output jack to the audio input jack, but that&#8217;s really crude, and isn&#8217;t digital.</p>
<p>The solution I found is <a title="Soundflower" href="http://cycling74.com/soundflower-landing-page/">Soundflower</a>, a free download from <a title="Cycling 74" href="http://cycling74.com/">Cycling 74</a>, the makers of Max. It provides a virtual sound output, to which you can route the Mac&#8217;s audio output by going to System Preferences / Sound.</p>
<p>Whatever audio is sent to that output is routed to Soundflower&#8217;s virtual sound <em>input</em>, which can be set as your Mac&#8217;s default sound input (also via System Preferences / Sound).</p>
<p>Using Soundflower, I can record the sound output from my web browser (and whatever else I&#8217;m running) to a sound recording app such as Audacity or the QuickTime Player (which despite its name, can also do recording).</p>
<p>Note that if you route audio to Soundflower, you won&#8217;t hear it out your headphones or speakers. The workaround is to route your sound recorder&#8217;s output to your Mac&#8217;s (real, physical) audio output, and turn on the record monitoring function. In Audacity, you can set the former from the Playback menu in Audacity / Preferences / Devices; and the latter by checking the &#8220;Software Playthrough&#8221; checkbox in Audacity / Preferences / Recording.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/gerrybeauregard.wordpress.com/234/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/gerrybeauregard.wordpress.com/234/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/gerrybeauregard.wordpress.com/234/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/gerrybeauregard.wordpress.com/234/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/gerrybeauregard.wordpress.com/234/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/gerrybeauregard.wordpress.com/234/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/gerrybeauregard.wordpress.com/234/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/gerrybeauregard.wordpress.com/234/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/gerrybeauregard.wordpress.com/234/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/gerrybeauregard.wordpress.com/234/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/gerrybeauregard.wordpress.com/234/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/gerrybeauregard.wordpress.com/234/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/gerrybeauregard.wordpress.com/234/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/gerrybeauregard.wordpress.com/234/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gerrybeauregard.wordpress.com&amp;blog=14994846&amp;post=234&amp;subd=gerrybeauregard&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://gerrybeauregard.wordpress.com/2011/08/31/soundflower-great-tool-for-recording-the-macs-audio-output/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/601d233914f6e10f862be379fbe76b6e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">gerrybeauregard</media:title>
		</media:content>
	</item>
		<item>
		<title>Do Credit Ratings Accurately Predict Risk of Default?</title>
		<link>http://gerrybeauregard.wordpress.com/2011/08/10/do-credit-ratings-accurately-predict-risk-of-default/</link>
		<comments>http://gerrybeauregard.wordpress.com/2011/08/10/do-credit-ratings-accurately-predict-risk-of-default/#comments</comments>
		<pubDate>Tue, 09 Aug 2011 23:17:43 +0000</pubDate>
		<dc:creator>Gerry Beauregard</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://gerrybeauregard.wordpress.com/?p=231</guid>
		<description><![CDATA[You&#8217;ve all heard about S&#38;P downgrading US debt from AAA to AA+, and how this has affected the markets. This made me wonder: how reliable have S&#38;P&#8217;s ratings on long-term debt really been historically?  They&#8217;ve been doing it for a &#8230; <a href="http://gerrybeauregard.wordpress.com/2011/08/10/do-credit-ratings-accurately-predict-risk-of-default/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gerrybeauregard.wordpress.com&amp;blog=14994846&amp;post=231&amp;subd=gerrybeauregard&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>You&#8217;ve all heard about <a href="http://en.wikipedia.org/wiki/Standard_%26_Poor%27s">S&amp;P</a> downgrading US debt from AAA to AA+, and how this has affected the markets.</p>
<p>This made me wonder: how reliable have S&amp;P&#8217;s ratings on long-term debt really been historically?  They&#8217;ve been doing it for a long time, so presumably if they were really good at it, they would boast about their track record. Indeed, S&amp;P would compete the other ratings agencies (Fitch, Moody&#8217;s) based on the accuracy of their predictions.</p>
<p>It&#8217;s well-known that S&amp;P and the other ratings agencies were spectacularly wrong in the case of collateralized debt obligations, much of which rated as AAA. I wouldn&#8217;t be at all surprised if they&#8217;ve been wrong in less spectacular ways much of the time.</p>
<p>Presumably each credit rating corresponds to a probability of payout, and given historical data, one could check how well the ratings matched reality.   For example, if I were to look at all US-dollar denominated 10-year bonds issued in the year 2000, for which S&amp;P had a rating at the time of issue, by 2010 one could tally up how they performed.</p>
<p>For simplicity, the performance of a particular bond could be expressed as a binary (yes/no) answer to a simple question: did the borrower pay the bond-holders the stipulated interest and principal?</p>
<p>If the ratings were worth anything, the probability of &#8220;yes&#8221; should be highest for the AAA bonds, and progressively lower for lower ratings (AA+, AA, etc. all the way to C).</p>
<p>Furthermore, the probabilities should be fairly stable over time; for example the probability of default for a 10-year bond rated BBB at issue in 1970 should be the same as that for a 10-year bond rated BBB in 2000. Certainly economic conditions in the 1970s were different from those in the 2000s, and some things are difficult to predict (e.g. the oil crisis in the 1970s, 9-11 in 2001), but the ratings guys are supposedly experts, so their ratings should be robust &#8211; or at least better than those of astrologists.</p>
<p>Does anyone know whether such back checking of ratings has been done? If it has been done, the information certainly sure isn&#8217;t easy to find.</p>
<p>Back to that recent downgrade: if I bought a 10-yr AAA US bond issued five years ago, according to S&amp;P it&#8217;s now AA+… which sort of means the AAA rating wasn&#8217;t really correct to begin with. A prediction for long-term creditworthiness isn&#8217;t worth much if you&#8217;re allowed to change that prediction part-way!</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/gerrybeauregard.wordpress.com/231/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/gerrybeauregard.wordpress.com/231/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/gerrybeauregard.wordpress.com/231/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/gerrybeauregard.wordpress.com/231/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/gerrybeauregard.wordpress.com/231/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/gerrybeauregard.wordpress.com/231/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/gerrybeauregard.wordpress.com/231/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/gerrybeauregard.wordpress.com/231/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/gerrybeauregard.wordpress.com/231/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/gerrybeauregard.wordpress.com/231/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/gerrybeauregard.wordpress.com/231/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/gerrybeauregard.wordpress.com/231/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/gerrybeauregard.wordpress.com/231/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/gerrybeauregard.wordpress.com/231/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gerrybeauregard.wordpress.com&amp;blog=14994846&amp;post=231&amp;subd=gerrybeauregard&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://gerrybeauregard.wordpress.com/2011/08/10/do-credit-ratings-accurately-predict-risk-of-default/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/601d233914f6e10f862be379fbe76b6e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">gerrybeauregard</media:title>
		</media:content>
	</item>
		<item>
		<title>Risks of synthetic ETFs</title>
		<link>http://gerrybeauregard.wordpress.com/2011/06/25/risks-of-synthetic-etfs/</link>
		<comments>http://gerrybeauregard.wordpress.com/2011/06/25/risks-of-synthetic-etfs/#comments</comments>
		<pubDate>Sat, 25 Jun 2011 07:55:02 +0000</pubDate>
		<dc:creator>Gerry Beauregard</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://gerrybeauregard.wordpress.com/?p=220</guid>
		<description><![CDATA[In a blog post last year, I discussed how index-tracking ETFs don&#8217;t necessarily invest in the shares of the companies that make up that index. Instead they hold a basket of shares as collateral, and use &#8220;equity-linked swaps&#8221; to make &#8230; <a href="http://gerrybeauregard.wordpress.com/2011/06/25/risks-of-synthetic-etfs/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gerrybeauregard.wordpress.com&amp;blog=14994846&amp;post=220&amp;subd=gerrybeauregard&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>In a <a title="Exchange Traded Funds: Read the Fine Print" href="http://gerrybeauregard.wordpress.com/2010/10/02/exchange-traded-funds-read-the-fine-print/">blog post last year</a>, I discussed how index-tracking ETFs don&#8217;t necessarily invest in the shares of the companies that make up that index. Instead they hold a basket of shares as collateral, and use &#8220;equity-linked swaps&#8221; to make their performance match that of some target index. It all looked a bit fishy to me, and now it appears that other folks who understand this stuff way more than I do feel the same way.</p>
<p>According to a <a title="Synthetic ETFs spell danger, says Bank of England" href="http://www.citywire.co.uk/money/synthetic-etfs-spell-danger-says-bank-of-england/a503478?re=14795&amp;ea=291189&amp;utm_source=BulkEmail_Money_Daily&amp;utm_medium=BulkEmail_Money_Daily&amp;utm_campaign=BulkEmail_Money_Daily">CityWire article</a>, The Bank of England says that synthetic ETFs spell danger, and that  ‘An investor is not just exposed to the index that he or she thought they were exposed to but also to the counter party risk of the institution.’</p>
<p>Great. It&#8217;s risky enough being exposed to the stock market. Who needs exposure to institutional risk as well? Even before 2008, it was pretty clear that banks could fail. In 1995, Barings Bank failed due to the actions of just one reckless and over-ambitious trader.</p>
<p>Meanwhile a <a title="A good idea in danger of going bad" href="http://www.economist.com/node/18867037?story_id=18867037">recent article in the Economist</a> lists a variety of risks and complications that most ETF investors are blissfully unaware of.</p>
<p>Over the past couple of years, I&#8217;ve bought and sold various ETFs with no problem, and I still have some money in ETFs. They still seem like the best way to get some diversification, especially exposure to markets that might otherwise be difficult to invest in. For example, there&#8217;s no way I could possibly by shares in all companies in the S&amp;P 500.</p>
<p>However, given the complicated structure of some ETFs and the risks now being exposed, I&#8217;m now definitely going to be more careful. You should be too&#8230;</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/gerrybeauregard.wordpress.com/220/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/gerrybeauregard.wordpress.com/220/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/gerrybeauregard.wordpress.com/220/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/gerrybeauregard.wordpress.com/220/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/gerrybeauregard.wordpress.com/220/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/gerrybeauregard.wordpress.com/220/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/gerrybeauregard.wordpress.com/220/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/gerrybeauregard.wordpress.com/220/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/gerrybeauregard.wordpress.com/220/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/gerrybeauregard.wordpress.com/220/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/gerrybeauregard.wordpress.com/220/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/gerrybeauregard.wordpress.com/220/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/gerrybeauregard.wordpress.com/220/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/gerrybeauregard.wordpress.com/220/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gerrybeauregard.wordpress.com&amp;blog=14994846&amp;post=220&amp;subd=gerrybeauregard&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://gerrybeauregard.wordpress.com/2011/06/25/risks-of-synthetic-etfs/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/601d233914f6e10f862be379fbe76b6e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">gerrybeauregard</media:title>
		</media:content>
	</item>
		<item>
		<title>Microsoft Phone Support Scam?</title>
		<link>http://gerrybeauregard.wordpress.com/2011/04/06/microsoft-phone-support-scam/</link>
		<comments>http://gerrybeauregard.wordpress.com/2011/04/06/microsoft-phone-support-scam/#comments</comments>
		<pubDate>Wed, 06 Apr 2011 06:27:27 +0000</pubDate>
		<dc:creator>Gerry Beauregard</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://gerrybeauregard.wordpress.com/?p=216</guid>
		<description><![CDATA[I think I&#8217;ve just been the target of a scam. I didn&#8217;t fall for it, but for what it&#8217;s worth, here&#8217;s what happened. Got a call on my home phone line from a woman with a strong Indian accent claiming &#8230; <a href="http://gerrybeauregard.wordpress.com/2011/04/06/microsoft-phone-support-scam/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gerrybeauregard.wordpress.com&amp;blog=14994846&amp;post=216&amp;subd=gerrybeauregard&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I think I&#8217;ve just been the target of a scam. I didn&#8217;t fall for it, but for what it&#8217;s worth, here&#8217;s what happened.</p>
<p>Got a call on my home phone line from a woman with a strong Indian accent claiming to be with Windows Live support, telling me they&#8217;ve been receiving error reports from my computer. The connection was really bad, worse than the cheapest VoIP service. She asked me to switch on my computer and follow some instructions to fix the problem.</p>
<p>I was immediately suspicious, since I only have Macs here, though I do occasionally run Windows on them  (XP under VMWare on my MacBook Pro, and Windows 7 on my iMac in BootCamp). Besides, why would they have my home phone number? If I register software at all and have to give a phone number, I always use my mobile number, not my home phone.</p>
<p>So I asked to speak to her manager. A fellow with a thick Indian accent comes online, I ask where he&#8217;s from, he says (implausibly) Australia. I ask him when these error reports came in, he says &#8220;over past three months&#8221;. I ask for the date and time of the most recent one. He says &#8220;is continuous, today, yesterday&#8221;. I tell him I haven&#8217;t run Windows today or yesterday, and that I don&#8217;t trust his organization, and hang up.</p>
<p>In retrospect, I should have just followed along (or pretended to follow along) to find out what they wanted me to do. I wouldn&#8217;t be surprised if it involved downloading some special software to &#8220;fix the error&#8221; &#8211; software that would actually just copy files, track keystrokes, etc.</p>
<p>Anyone out there had a similar experience?</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/gerrybeauregard.wordpress.com/216/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/gerrybeauregard.wordpress.com/216/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/gerrybeauregard.wordpress.com/216/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/gerrybeauregard.wordpress.com/216/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/gerrybeauregard.wordpress.com/216/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/gerrybeauregard.wordpress.com/216/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/gerrybeauregard.wordpress.com/216/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/gerrybeauregard.wordpress.com/216/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/gerrybeauregard.wordpress.com/216/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/gerrybeauregard.wordpress.com/216/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/gerrybeauregard.wordpress.com/216/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/gerrybeauregard.wordpress.com/216/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/gerrybeauregard.wordpress.com/216/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/gerrybeauregard.wordpress.com/216/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gerrybeauregard.wordpress.com&amp;blog=14994846&amp;post=216&amp;subd=gerrybeauregard&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://gerrybeauregard.wordpress.com/2011/04/06/microsoft-phone-support-scam/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/601d233914f6e10f862be379fbe76b6e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">gerrybeauregard</media:title>
		</media:content>
	</item>
		<item>
		<title>An FFT in C#</title>
		<link>http://gerrybeauregard.wordpress.com/2011/04/01/an-fft-in-c/</link>
		<comments>http://gerrybeauregard.wordpress.com/2011/04/01/an-fft-in-c/#comments</comments>
		<pubDate>Fri, 01 Apr 2011 01:06:37 +0000</pubDate>
		<dc:creator>Gerry Beauregard</dc:creator>
				<category><![CDATA[Audio]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://gerrybeauregard.wordpress.com/?p=205</guid>
		<description><![CDATA[A few weeks ago, I wrote a post comparing the performance of Flash/AS3 vs. Silverlight/C#. I used a very simple test, element-by-element addition of two vectors of numbers. The C# version was about 5 times faster. I was curious whether &#8230; <a href="http://gerrybeauregard.wordpress.com/2011/04/01/an-fft-in-c/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gerrybeauregard.wordpress.com&amp;blog=14994846&amp;post=205&amp;subd=gerrybeauregard&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>A few weeks ago, I wrote a post comparing the <a href="http://gerrybeauregard.wordpress.com/2011/03/12/performance-of-flashas3-vs-silverlightc/">performance of Flash/AS3 vs. Silverlight/C#</a>. I used a very simple test, element-by-element addition of two vectors of numbers. The C# version was about 5 times faster.</p>
<p>I was curious whether I&#8217;d get a similar performance disparity with some less trivial computation, so I ported my &#8220;even faster&#8221; <a href="http://gerrybeauregard.wordpress.com/2010/08/03/an-even-faster-as3-fft/">AS3 FFT</a> implementation to C#. The C# FFT uses only managed code (no pointers) so you can use it in Silverlight. I didn&#8217;t attempt to make any C#-specific optimizations, just did the minimal changes to get it to build.</p>
<p>As in my earlier C# vs. Flash performance test, I once again found that the C# implementation is much faster, nearly 4x faster than AS3. For 1000 forward-inverse pairs of 1024 pt FFTs, my C# FFT takes ~85 ms. The same test with the AS3 FFT takes over 300 ms.</p>
<p>I&#8217;m releasing the C# FFT code under the MIT license, which means you can pretty much do whatever you like with it, as long as you keep the comment block that says I wrote it and that you can do whatever you like with it <img src='http://s1.wp.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> . If you find it useful or have suggestions to improve it, feel free to leave a comment.</p>
<p><em>Update 3 August 2011: Graeme West reported some strange results using my FFT. I dug into it, and discovered a bug: the sign for the imaginary part of the &#8220;twiddle factors&#8221; was incorrect. I&#8217;ve fixed it now &#8211; just required changing the if (inverse) condition to if (inverse == false). I never noticed because for real input data (i.e. where the imaginary component is zero), the only impact on the output was that the sign of the imaginary component was flipped, and that had no effect on magnitude spectra (which is what I normally was computing). </em></p>
<p><pre class="brush: csharp;">
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace SpeedTest
{
	/**
	 * Performs an in-place complex FFT.
	 *
	 * Released under the MIT License
	 *
	 * Copyright (c) 2010 Gerald T. Beauregard
	 *
	 * Permission is hereby granted, free of charge, to any person obtaining a copy
	 * of this software and associated documentation files (the &quot;Software&quot;), to
	 * deal in the Software without restriction, including without limitation the
	 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
	 * sell copies of the Software, and to permit persons to whom the Software is
	 * furnished to do so, subject to the following conditions:
	 *
	 * The above copyright notice and this permission notice shall be included in
	 * all copies or substantial portions of the Software.
	 *
	 * THE SOFTWARE IS PROVIDED &quot;AS IS&quot;, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
	 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
	 * IN THE SOFTWARE.
	 */
	public class FFT2
	{
		// Element for linked list in which we store the
		// input/output data. We use a linked list because
		// for sequential access it's faster than array index.
		class FFTElement
		{
			public double re = 0.0;		// Real component
			public double im = 0.0;		// Imaginary component
			public FFTElement next;		// Next element in linked list
			public uint revTgt;			// Target position post bit-reversal
		}

		private uint m_logN = 0;		// log2 of FFT size
		private uint m_N = 0;			// FFT size
		private FFTElement[] m_X;		// Vector of linked list elements

		/**
		 *
		 */
		public FFT2()
		{
		}

		/**
		 * Initialize class to perform FFT of specified size.
		 *
		 * @param	logN	Log2 of FFT length. e.g. for 512 pt FFT, logN = 9.
		 */
		public void init(
			uint logN )
		{
			m_logN = logN;
			m_N = (uint)(1 &lt;&lt; (int)m_logN);

			// Allocate elements for linked list of complex numbers.
			m_X = new FFTElement[m_N];
			for (uint k = 0; k &lt; m_N; k++)
				m_X[k] = new FFTElement();

			// Set up &quot;next&quot; pointers.
			for (uint k = 0; k &lt; m_N-1; k++)
				m_X[k].next = m_X[k+1];

			// Specify target for bit reversal re-ordering.
			for (uint k = 0; k &lt; m_N; k++ )
				m_X[k].revTgt = BitReverse(k,logN);
		}

		/**
		 * Performs in-place complex FFT.
		 *
		 * @param	xRe		Real part of input/output
		 * @param	xIm		Imaginary part of input/output
		 * @param	inverse	If true, do an inverse FFT
		 */
		public void run(
			double[] xRe,
			double[] xIm,
			bool inverse = false )
		{
			uint numFlies = m_N &gt;&gt; 1;	// Number of butterflies per sub-FFT
			uint span = m_N &gt;&gt; 1;		// Width of the butterfly
			uint spacing = m_N;			// Distance between start of sub-FFTs
			uint wIndexStep = 1; 		// Increment for twiddle table index

			// Copy data into linked complex number objects
			// If it's an IFFT, we divide by N while we're at it
			FFTElement x = m_X[0];
			uint k = 0;
			double scale = inverse ? 1.0/m_N : 1.0;
			while (x != null)
			{
				x.re = scale*xRe[k];
				x.im = scale*xIm[k];
				x = x.next;
				k++;
			}

			// For each stage of the FFT
			for (uint stage = 0; stage &lt; m_logN; stage++)
			{
				// Compute a multiplier factor for the &quot;twiddle factors&quot;.
				// The twiddle factors are complex unit vectors spaced at
				// regular angular intervals. The angle by which the twiddle
				// factor advances depends on the FFT stage. In many FFT
				// implementations the twiddle factors are cached, but because
				// array lookup is relatively slow in C#, it's just
				// as fast to compute them on the fly.
				double wAngleInc = wIndexStep * 2.0*Math.PI/m_N;
				if (inverse == false)
					wAngleInc *= -1;
				double wMulRe = Math.Cos(wAngleInc);
				double wMulIm = Math.Sin(wAngleInc);

				for (uint start = 0; start &lt; m_N; start += spacing)
				{
					FFTElement xTop = m_X[start];
					FFTElement xBot = m_X[start+span];

					double wRe = 1.0;
					double wIm = 0.0;

					// For each butterfly in this stage
					for (uint flyCount = 0; flyCount &lt; numFlies; ++flyCount)
					{
						// Get the top &amp; bottom values
						double xTopRe = xTop.re;
						double xTopIm = xTop.im;
						double xBotRe = xBot.re;
						double xBotIm = xBot.im;

						// Top branch of butterfly has addition
						xTop.re = xTopRe + xBotRe;
						xTop.im = xTopIm + xBotIm;

						// Bottom branch of butterly has subtraction,
						// followed by multiplication by twiddle factor
						xBotRe = xTopRe - xBotRe;
						xBotIm = xTopIm - xBotIm;
						xBot.re = xBotRe*wRe - xBotIm*wIm;
						xBot.im = xBotRe*wIm + xBotIm*wRe;

						// Advance butterfly to next top &amp; bottom positions
						xTop = xTop.next;
						xBot = xBot.next;

						// Update the twiddle factor, via complex multiply
						// by unit vector with the appropriate angle
						// (wRe + j wIm) = (wRe + j wIm) x (wMulRe + j wMulIm)
						double tRe = wRe;
						wRe = wRe*wMulRe - wIm*wMulIm;
						wIm = tRe*wMulIm + wIm*wMulRe;
					}
				}

				numFlies &gt;&gt;= 1; 	// Divide by 2 by right shift
				span &gt;&gt;= 1;
				spacing &gt;&gt;= 1;
				wIndexStep &lt;&lt;= 1;  	// Multiply by 2 by left shift
			}

			// The algorithm leaves the result in a scrambled order.
			// Unscramble while copying values from the complex
			// linked list elements back to the input/output vectors.
			x = m_X[0];
			while (x != null)
			{
				uint target = x.revTgt;
				xRe[target] = x.re;
				xIm[target] = x.im;
				x = x.next;
			}
		}

		/**
		 * Do bit reversal of specified number of places of an int
		 * For example, 1101 bit-reversed is 1011
		 *
		 * @param	x		Number to be bit-reverse.
		 * @param	numBits	Number of bits in the number.
		 */
		private uint BitReverse(
			uint x,
			uint numBits)
		{
			uint y = 0;
			for (uint i = 0; i &lt; numBits; i++)
			{
				y &lt;&lt;= 1;
				y |= x &amp; 0x0001;
				x &gt;&gt;= 1;
			}
			return y;
		}
	}
}
</pre></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/gerrybeauregard.wordpress.com/205/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/gerrybeauregard.wordpress.com/205/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/gerrybeauregard.wordpress.com/205/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/gerrybeauregard.wordpress.com/205/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/gerrybeauregard.wordpress.com/205/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/gerrybeauregard.wordpress.com/205/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/gerrybeauregard.wordpress.com/205/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/gerrybeauregard.wordpress.com/205/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/gerrybeauregard.wordpress.com/205/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/gerrybeauregard.wordpress.com/205/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/gerrybeauregard.wordpress.com/205/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/gerrybeauregard.wordpress.com/205/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/gerrybeauregard.wordpress.com/205/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/gerrybeauregard.wordpress.com/205/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gerrybeauregard.wordpress.com&amp;blog=14994846&amp;post=205&amp;subd=gerrybeauregard&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://gerrybeauregard.wordpress.com/2011/04/01/an-fft-in-c/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/601d233914f6e10f862be379fbe76b6e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">gerrybeauregard</media:title>
		</media:content>
	</item>
		<item>
		<title>Reading and writing local files in Silverlight/C#</title>
		<link>http://gerrybeauregard.wordpress.com/2011/03/18/reading-and-writing-local-files-in-silverlightc/</link>
		<comments>http://gerrybeauregard.wordpress.com/2011/03/18/reading-and-writing-local-files-in-silverlightc/#comments</comments>
		<pubDate>Fri, 18 Mar 2011 01:00:43 +0000</pubDate>
		<dc:creator>Gerry Beauregard</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://gerrybeauregard.wordpress.com/?p=184</guid>
		<description><![CDATA[In a Silverlight app running in a web page, it&#8217;s possible to read and write local files with one restriction: the end-user needs to select the files. The following code is an ultra-simple example of how it&#8217;s done. LocalFileReadWrite is &#8230; <a href="http://gerrybeauregard.wordpress.com/2011/03/18/reading-and-writing-local-files-in-silverlightc/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gerrybeauregard.wordpress.com&amp;blog=14994846&amp;post=184&amp;subd=gerrybeauregard&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>In a Silverlight app running in a web page, it&#8217;s possible to read and write local files with one restriction: the end-user needs to select the files. The following code is an ultra-simple example of how it&#8217;s done.</p>
<p>LocalFileReadWrite is very simple web app that displays a TextBox and two Buttons, Save and Open. Clicking the Open button allows you to open a *.txt and display it in the TextBox. You can then edit the text, click Save to save the contents of the TextBox into another file. It&#8217;s a bit like Notepad.exe, but with even fewer features <img src='http://s1.wp.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
<p>You can try it <a href="http://audiostretch.com/localfilereadwrite">here</a>. </p>
<p>A zip of the whole project can be found <a href="http://www.audiostretch.com/localfilereadwrite/LocalFileReadWrite.zip">here</a>. It&#8217;s buildable with MS Visual Web Developer 2010 Express. Most of it&#8217;s boilerplate stuff you get when you create a new project. The only files I modified were the C# file MainPage.xaml.cs and the XAML file MainPage.xaml.</p>
<p>I was pleasantly surprised at how little code was required. It&#8217;s even less complicated than reading/writing local files in Flash (see <a href="http://www.mikechambers.com/blog/2008/08/20/reading-and-writing-local-files-in-flash-player-10/">Mike Chambers&#8217; post</a> on that).</p>
<p>Here&#8217;s the C# code:</p>
<p><pre class="brush: csharp; wrap-lines: false;">
using System;
using System.Windows;
using System.Windows.Controls;
using System.Diagnostics;		// Debug.WriteLine
using System.Text;				// UTF8Encoding
using System.IO;				// Stream, IOStream

namespace LocalFileReadWrite
{
	public partial class MainPage : UserControl
	{
		private const String FILE_FILTER = &quot;Text files (*.txt)|*.txt&quot;;
		private const String WELCOME = &quot;Type text here. Click 'Open' to open any *.txt file. Click 'Save' to save to a new *.txt file (or overwrite an existing one).&quot;;

		public MainPage()
		{
			InitializeComponent();
			textBox.Text = WELCOME;
		}

		private void OnOpenButtonClick(object sender, RoutedEventArgs e)
		{
			OpenFileDialog openFileDialog = new OpenFileDialog();
			openFileDialog.Filter = FILE_FILTER;

			if (openFileDialog.ShowDialog() == true)
			{
				System.IO.FileStream fileStream = openFileDialog.File.OpenRead();
				byte[] textBytes = new byte[fileStream.Length];
				fileStream.Read(textBytes, 0, textBytes.Length);
				textBox.Text = UTF8Encoding.UTF8.GetString(textBytes, 0, textBytes.Length);
			}
		}

		private void OnSaveButtonClick(object sender, RoutedEventArgs e)
		{
			SaveFileDialog saveFileDialog = new SaveFileDialog();
			saveFileDialog.Filter = FILE_FILTER;

			if (saveFileDialog.ShowDialog() == true)
			{
				System.IO.Stream stream = saveFileDialog.OpenFile();
				byte[] textBytes = UTF8Encoding.UTF8.GetBytes(textBox.Text);
				stream.Write(textBytes, 0, textBytes.Length);
				stream.Close();
			}
		}
	}
}
</pre></p>
<p>And here&#8217;s the XAML:</p>
<p><pre class="brush: xml; wrap-lines: false;">
&lt;UserControl x:Class=&quot;LocalFileReadWrite.MainPage&quot;
    xmlns=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
    xmlns:x=&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;
    xmlns:d=&quot;http://schemas.microsoft.com/expression/blend/2008&quot;
    xmlns:mc=&quot;http://schemas.openxmlformats.org/markup-compatibility/2006&quot;
    mc:Ignorable=&quot;d&quot;
    d:DesignHeight=&quot;380&quot; d:DesignWidth=&quot;696&quot;&gt;

    &lt;Grid x:Name=&quot;LayoutRoot&quot; Background=&quot;White&quot;&gt;
        &lt;Button Content=&quot;Open&quot; Height=&quot;23&quot; HorizontalAlignment=&quot;Left&quot; Margin=&quot;12,12,0,0&quot; Name=&quot;openButton&quot; VerticalAlignment=&quot;Top&quot; Width=&quot;75&quot; Click=&quot;OnOpenButtonClick&quot; /&gt;
        &lt;Button Content=&quot;Save&quot; Height=&quot;23&quot; HorizontalAlignment=&quot;Left&quot; Margin=&quot;93,12,0,0&quot; Name=&quot;saveButton&quot; VerticalAlignment=&quot;Top&quot; Width=&quot;75&quot; Click=&quot;OnSaveButtonClick&quot; /&gt;
        &lt;TextBox Margin=&quot;12,41,12,12&quot; Name=&quot;textBox&quot; TextWrapping=&quot;Wrap&quot; VerticalScrollBarVisibility=&quot;Auto&quot; AcceptsReturn=&quot;True&quot; /&gt;
    &lt;/Grid&gt;
&lt;/UserControl&gt;
</pre></p>
<p>Reading and writing text files is of course just one thing you can do given the ability to read/write local files.  One can imagine creating all sorts of handy web-based utilities &#8211; hex editors, waveform editors, image editors &#8211; all running in web pages, and accessible from Mac or PC.</p>
<p>Feel free to use and modify the code as you wish, though some sort of attribution (a link or comment in the code) is always appreciated. Enjoy!</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/gerrybeauregard.wordpress.com/184/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/gerrybeauregard.wordpress.com/184/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/gerrybeauregard.wordpress.com/184/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/gerrybeauregard.wordpress.com/184/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/gerrybeauregard.wordpress.com/184/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/gerrybeauregard.wordpress.com/184/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/gerrybeauregard.wordpress.com/184/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/gerrybeauregard.wordpress.com/184/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/gerrybeauregard.wordpress.com/184/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/gerrybeauregard.wordpress.com/184/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/gerrybeauregard.wordpress.com/184/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/gerrybeauregard.wordpress.com/184/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/gerrybeauregard.wordpress.com/184/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/gerrybeauregard.wordpress.com/184/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gerrybeauregard.wordpress.com&amp;blog=14994846&amp;post=184&amp;subd=gerrybeauregard&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://gerrybeauregard.wordpress.com/2011/03/18/reading-and-writing-local-files-in-silverlightc/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/601d233914f6e10f862be379fbe76b6e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">gerrybeauregard</media:title>
		</media:content>
	</item>
		<item>
		<title>Performance of Flash/AS3 vs. Silverlight/C#</title>
		<link>http://gerrybeauregard.wordpress.com/2011/03/12/performance-of-flashas3-vs-silverlightc/</link>
		<comments>http://gerrybeauregard.wordpress.com/2011/03/12/performance-of-flashas3-vs-silverlightc/#comments</comments>
		<pubDate>Sat, 12 Mar 2011 04:53:24 +0000</pubDate>
		<dc:creator>Gerry Beauregard</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://gerrybeauregard.wordpress.com/?p=174</guid>
		<description><![CDATA[A few months back, I posted some figures on the performance of Arrays, Vectors and linked lists in ActionScript 3.  Arrays were really slow, vectors much better, and linked lists the best, but all were slow compared to natively compiled C++ &#8230; <a href="http://gerrybeauregard.wordpress.com/2011/03/12/performance-of-flashas3-vs-silverlightc/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gerrybeauregard.wordpress.com&amp;blog=14994846&amp;post=174&amp;subd=gerrybeauregard&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>A few months back, I posted some figures on the performance of <a href="http://gerrybeauregard.wordpress.com/2010/08/10/vectors-vs-linked-lists-in-as3/">Arrays, Vectors and linked lists in ActionScript 3</a>.  Arrays were really slow, vectors much better, and linked lists the best, but all were slow compared to natively compiled C++ code.</p>
<p>Recently I&#8217;ve been experimenting with Silverlight and C#, and it generally seems much speedier than Flash and AS3. So I re-did the test I did a few months back, doing repeated element-by-element addition of two long arrays, this time using C#. I also redid the AS3 timing test, since I got a faster MacBook Pro since my last post on this subject.  The core of the C# timing test code looks like this:</p>
<p><pre class="brush: csharp; wrap-lines: false;">
			int startTime = System.Environment.TickCount;
			for (int j = 0; j &lt; LOOPS; j++)
			{
				for (int k = 0; k &lt; N; k++)
				{
					z[k] = x[k] + y[k];
				}
			}
			int endTime = System.Environment.TickCount;
</pre></p>
<p>So here&#8217;s how Silverlight/C# stacks up against Flash/AS3:</p>
<table>
<tbody>
<tr>
<td><strong>Approach</strong></td>
<td><strong>Time (ms)</strong></td>
</tr>
<tr>
<td>AS3, Array</td>
<td>713</td>
</tr>
<tr>
<td>AS3, Vector</td>
<td>408</td>
</tr>
<tr>
<td>AS3, Linked List</td>
<td>151</td>
</tr>
<tr>
<td>C#, Array</td>
<td>70</td>
</tr>
</tbody>
</table>
<p>So for this simple test, C# arrays are over 5x faster than Vectors in AS3, and over double the speed of linked lists. This is only one test, of course, but if this sort of difference in performance is typical, it clearly has big implications on what&#8217;s possible on the two platforms. In particular, it makes Silverlight/C# very attractive for real-time audio applications, which require lots of array accessing and number crunching.  [Update 6 April 2011: I also found that an FFT I wrote ran ~4x faster in C# than in AS3. See <a title="An FFT in C#" href="http://gerrybeauregard.wordpress.com/2011/04/01/an-fft-in-c/">An FFT in C#</a>].</p>
<p>Tests were run on my MacBook Pro (2.66 GHz Intel Core i7, Mac OS 10.6.6), with Flash Player 10.1.52.14 and Silverlight 4.0.60129.0.</p>
<p>For the full C# timing test code, click &#8220;show code&#8221; below.</p>
<p><pre class="brush: csharp; collapse: true; light: false; toolbar: true; wrap-lines: false;">
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Diagnostics;

namespace SpeedTest
{
	public partial class MainPage : UserControl
	{
        private const int N = 5000;		// No. of elements in arrays
        private const int LOOPS = 5000;	// No. of times to repeat the calculations

		public MainPage()
		{
			InitializeComponent();

			int elapsed = ArrayTest();
			status.Content = &quot;Elapsed: &quot; + elapsed.ToString(); // Note: 'status' is a 'Label' defined in the project's xaml file.
		}

		/**
		 * Add elements of one array to elements of another
		 * array, and put the results into a third array.
		 */
		private int ArrayTest()
		{
			// Create arrays
			double[] x = new double[N];
			double[] y = new double[N];
			double[] z = new double[N];

			// Initialize them with some values
			for (int k = 0; k &lt; N; k++)
			{
				x[k] = k;
				y[k] = k;
				z[k] = 0.0;
			}

			// Add the values in the two arrays, with result
			// in a third array.  Repeat many times.
			int startTime = System.Environment.TickCount;
			for (int j = 0; j &lt; LOOPS; j++)
			{
				for (int k = 0; k &lt; N; k++)
				{
					z[k] = x[k] + y[k];
				}
			}
			int endTime = System.Environment.TickCount;

			// Return the elapsed time.
			return (endTime - startTime);
		}
	}
}
</pre></pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/gerrybeauregard.wordpress.com/174/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/gerrybeauregard.wordpress.com/174/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/gerrybeauregard.wordpress.com/174/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/gerrybeauregard.wordpress.com/174/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/gerrybeauregard.wordpress.com/174/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/gerrybeauregard.wordpress.com/174/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/gerrybeauregard.wordpress.com/174/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/gerrybeauregard.wordpress.com/174/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/gerrybeauregard.wordpress.com/174/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/gerrybeauregard.wordpress.com/174/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/gerrybeauregard.wordpress.com/174/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/gerrybeauregard.wordpress.com/174/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/gerrybeauregard.wordpress.com/174/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/gerrybeauregard.wordpress.com/174/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gerrybeauregard.wordpress.com&amp;blog=14994846&amp;post=174&amp;subd=gerrybeauregard&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://gerrybeauregard.wordpress.com/2011/03/12/performance-of-flashas3-vs-silverlightc/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/601d233914f6e10f862be379fbe76b6e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">gerrybeauregard</media:title>
		</media:content>
	</item>
		<item>
		<title>PE Ratio &#8211; TTM</title>
		<link>http://gerrybeauregard.wordpress.com/2011/01/07/pe-ratio-ttm/</link>
		<comments>http://gerrybeauregard.wordpress.com/2011/01/07/pe-ratio-ttm/#comments</comments>
		<pubDate>Fri, 07 Jan 2011 09:15:39 +0000</pubDate>
		<dc:creator>Gerry Beauregard</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://gerrybeauregard.wordpress.com/?p=153</guid>
		<description><![CDATA[The P/E ratio is one of many ratios investors use to evaluate whether a company&#8217;s shares are reasonably priced. The P/E ratio (or PE) is simply the price per share divided by the annual earnings per share.  Equivalently, you can &#8230; <a href="http://gerrybeauregard.wordpress.com/2011/01/07/pe-ratio-ttm/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gerrybeauregard.wordpress.com&amp;blog=14994846&amp;post=153&amp;subd=gerrybeauregard&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>The <a title="Wiki P/E Ratio article" href="http://en.wikipedia.org/wiki/P/E_ratio">P/E ratio</a> is one of many ratios investors use to evaluate whether a company&#8217;s shares are reasonably priced. The P/E ratio (or PE) is simply the price per share divided by the annual earnings per share.  Equivalently, you can simply take the market capitalization divided by the total earnings.</p>
<p>For example, if a share is currently trading at $100 per share, and the company earnings $10 per share per year, the company&#8217;s PE is 10.  All else being equal, shares trading with a low PE are better value than those with a high PE.</p>
<p>Something to watch out for with the PE ratio is that while the numerator P is the current share price, the denominator E is a trailing value &#8211; sometimes by more than you might expect. Often the earnings for the last complete fiscal year is used.  If you&#8217;re already 3 quarters into the current fiscal year, that means the E in the PE ratio is a full 9 months out of date&#8230; and when the current full year results come out, the PE ratio can suddenly jump higher or lower, even if the share price P remains constant (which it won&#8217;t for long).</p>
<p>Consider, for example, a company with earnings of $40M in the most recent fiscal year, and a market cap of $200M.  That would give it a PE of 5 &#8211; attractive enough to attract some punters looking for bargains. Now suppose you dig a <em>tiny</em> bit deeper and find that in earnings in the first three quarters of the current fiscal year are only $7.5M.  Assuming the trend continues for the final quarter, the full year earnings would be ~$10 M&#8230; and the PE would suddenly jump to 200/10 = 20.  Not so attractive.</p>
<p>If you&#8217;re lucky, a quoted PE will use the earnings over the most recent 4 quarters, in which case it&#8217;s sometimes written as &#8220;PE (ttm)&#8221; where &#8220;ttm&#8221; means &#8220;<a title="Investopedia entry for Trailing Twelve Months (TTM)" href="http://www.investopedia.com/terms/t/ttm.asp">Trailing Twelve Months</a>&#8220;.  Even so it means the end date of that sliding 12-month window can be up to one quarter out of date, and its mid-point up to 3 quarters out of date.</p>
<p>For shares traded on the SGX, I refer a lot to <a title="Shares Investment website" href="http://www.sharesinv.com">Shares Investment</a> website, which uses the most recent reporting year in its PE values.  For info on US companies, I refer to <a href="http://finance.yahoo.com/">Yahoo Finance</a> uses TTM.</p>
<p>In some ways, P/E is a bit of a silly ratio, as it goes to positive infinity as earnings approach zero&#8230; and flips to a huge negative number if a company reports a tiny loss. In fact, when earnings go negative, the PE is generally given as NA (&#8220;not applicable&#8221;).</p>
<p>Personally I prefer to think of the reciprocal of the PE ratio, namely the EP ratio &#8211; the earnings per share divided by the share price - as it&#8217;s well-behaved for earnings near zero, and meaningful even for losses.  The denominator of the EP ratio never hits zero (though it can come frighteningly close, e.g. if you were unfortunate enough to hold shares in Nortel). EP is also easier to compare with interest rates (e.g. on bonds or fixed deposits).  Consider a company with a very high PE ratio, say 50.  That&#8217;s the same as an EP ratio of 0.02 or 2%.  If shares in a company have a PE ratio of 50, you&#8217;re probably be better off owning a safe bond yielding 2%, unless you&#8217;re really confident the a company&#8217;s earnings will grow a lot in upcoming years.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/gerrybeauregard.wordpress.com/153/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/gerrybeauregard.wordpress.com/153/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/gerrybeauregard.wordpress.com/153/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/gerrybeauregard.wordpress.com/153/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/gerrybeauregard.wordpress.com/153/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/gerrybeauregard.wordpress.com/153/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/gerrybeauregard.wordpress.com/153/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/gerrybeauregard.wordpress.com/153/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/gerrybeauregard.wordpress.com/153/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/gerrybeauregard.wordpress.com/153/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/gerrybeauregard.wordpress.com/153/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/gerrybeauregard.wordpress.com/153/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/gerrybeauregard.wordpress.com/153/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/gerrybeauregard.wordpress.com/153/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gerrybeauregard.wordpress.com&amp;blog=14994846&amp;post=153&amp;subd=gerrybeauregard&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://gerrybeauregard.wordpress.com/2011/01/07/pe-ratio-ttm/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/601d233914f6e10f862be379fbe76b6e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">gerrybeauregard</media:title>
		</media:content>
	</item>
	</channel>
</rss>
