Dan Newcome, blog

I'm bringing cyber back

Archive for November 2009

Fun with fractals using HTML5

with 3 comments

I was writing some fractal code and thought that it would be interesting to see if it could be done in Javascript with graphical output provided by the HTML5 <canvas> element.  As you can see, it worked just fine, albeit a little bit slow.

Generated fractal image


<html>
<head>
<script src="fractal.js" type="text/javascript" language="javascript"></script>
</head>
<body onload="drawFractal();">
<canvas style="border: 1px solid black" id="canvas" width="500" height="500"></canvas>
</body>
</html>

// file: fractal.js

// draw a point on our canvas element using the rect function
// set to draw a 1x1 square
function drawPoint( in_x, in_y ) {
 var canvas = document.getElementById( "canvas" );
 var ctx = canvas.getContext( "2d" );
 ctx.fillStyle = "rgb(0,0,0)";
 ctx.fillRect( in_x, in_y, 1, 1 );
}

// data structure that represents a complex number ( re + im )
function Complex( in_re, in_im ) {
 this.re = in_re;
 this.im = in_im;
}

// multiplication function for 2 complex numbers.. Performs FOIL
function ComplexMult( in_c1, in_c2 ) {
 var real = ( in_c1.re * in_c2.re ) - ( in_c1.im * in_c2.im );
 var imag = ( in_c1.re * in_c2.im ) + ( in_c1.im * in_c2.re );

 return new Complex( real, imag );
}

// addition of two complex numbers
function ComplexAdd( in_c1, in_c2 ) {
 return new Complex( in_c1.re + in_c2.re, in_c1.im + in_c2.im );
}

// absolute value of a complex number using sqrt( a^2 + b^2 ) form
function ComplexAbs( in_c ) {
 return Math.sqrt( in_c.re*in_c.re + in_c.im*in_c.im );
}

function drawFractal() {

 // resolution of our pixel image
 var x = 500;
 var y = 500;

 // real axis max/min
 var remin = -2;
 var remax = 2;

 // imaginary axis max/min
 var immin = -2;
 var immax = 2;

 // calculate the ranges from the bounds
 var rerange = remax - remin;
 var imrange = immax - immin;

 // number of iterations that we peform on each pixel
 var depth = 25;

 // iterate over each pixel in our output image
 for( var i=0; i < x; i++ ) {
 for( var j=0; j < y; j++ ) {

 var zn = new Complex( 0, 0 );

 // we need a mapping from pixels to complex plane
 var re = ( rerange/x ) * i - rerange/2;
 var im = ( imrange/y ) * j - imrange/2;
 var c = new Complex( re, im );

 // solve the iterative function for n = depth
 for( var n=1; n <= depth; n++ ) {
 // equation is zn = zn*zn + c
 zn = ComplexAdd( ComplexMult( zn, zn ), c );
 }

 // test to see if the point is in the set using 2 as the
 // boundary
 if( ComplexAbs( zn ) < 2 ) {
 drawPoint( i, j );
 }
 }
 }
}

Advertisements

Written by newcome

November 5, 2009 at 10:56 pm

Posted in Uncategorized

Updated thoughts on the smartphone market

with one comment

Earlier, I wrote about why I thought Palm was a winnner.  Following that post, there have been some developments in the market. Namely, the announcement of the Motorola Droid mobile device. Since then, Palm’s stock has dropped precipitously and suddenly they are really looking like a distant third player in the market. It looked like they really had a shot at growth, but they didn’t hit the magic formula evidently. Droid is shaping up with a lot of promise to revitalize the Android phone market.  I wrote earlier (before the Palm Pre) about how I thought that Andriod could achieve silent ubiquity as more mobile devices ship with the OS installed.   However, Android seemed to have been stuck in the unenviable position of being a geek-only favorite, with mainstream smartphone buyers mostly sticking with Apple.  Additionally, the HTC hardware was not particularly sexy, especially when compared with similar offerings from Apple.

With the announcement of the Palm Pre, I really thought that we had a strong contender to shake the mobile market up. We had a radical platform design, based on Linux, that consumers would never know about and geeks would love to program for. I reasoned that this would help them to quickly ramp up application development so that they could compete with Apple.  Also, they are taking the ‘total control’ strategy of hardware and software just like Apple has shown to be effective.  However, now that we see that Palm has announced the Pixi that runs the same OS but with slightly different specs (the screen resolution is different, for example) they are treading into the no-man’s land of controlling the hardware platform, but offering different variants of the platform. Apple has a slight advantage here, since although there have been three generations of the iPhone, their hardware capabilities have been mostly compatible. There haven’t been screen resolution changes or anything major like that.

What has happened though, is that no one seems to be developing for the Pre even though they have a familar web stack rather than the proprietary Objective-C stack that Apple has.  It costs a lot of money to develop an iPhone app since the programmers that know Objective-C well are relatively few and they are in demand.  So it looks like the development platform really has less to do with app numbers than I thought.  Palm is now solidly a third-runner in the `2.0′ smartphone game. Google is doing what Microsoft did in the desktop OS market against Apple in the 90s — unbunding the OS from the hardware platform.  This strategy proved incredibly successful for MS in the desktop arena, but they failed miserably at applying the same model to the smartphone OS arena with Windows CE and Windows Mobile.  The biggest question I have with Palm now, is where do they fit? Is there room for more than one provider of bundled hardware and software in the smartphone market?

I think that Palm has done an outstanding job product and platform wise. From a marketing and business development standpoint, they didn’t get things off to a good start.  They missed their window of opportunity to break into the market in a big way, and now they are going to be playing a very long game of catch-up if they are going to stay in the market. However, I could see them being a juicy acquisition target for a big tech player wanting to dive into the mobile market.

Written by newcome

November 4, 2009 at 2:19 pm

Posted in Uncategorized

Simple Fourier transform in Javascript

with 16 comments

I’m gearing up to do some signal processing stuff, and I thought it would be good to brush up a little on some of the mathematical concepts. I’m very familiar with the uses of the various transforms including the Fourier. However, although I’ve used others’ implementations of the FFT algorithm, I’ve never attempted to write my own.  The code shown here is a naive implementation (ie non-FFT – we don’t do the ‘butterfly’ method of successive reduction of the input) of the Fourier transform.  This code is for illustrative purposes — you probably won’t want to use this in any real code, since it will be very slow compared to something that uses the FFT method.  Note that this is probably the simplest code that I’ve seen that does a Fourier transform. If you see anything even simpler than this around on the net, let me know.


function fourier( in_array ) {
 var len = in_array.length;
 var output = new Array();

 for( var k=0; k < len; k++ ) {
   var real = 0;
   var imag = 0;
   for( var n=0; n < len; n++ ) {
     real += in_array[n]*Math.cos(-2*Math.PI*k*n/len);
     imag += in_array[n]*Math.sin(-2*Math.PI*k*n/len);
   }
   output.push( [ real, imag ] )
 }
 return output;
}

Written by newcome

November 4, 2009 at 11:33 am

Posted in Uncategorized