Thursday, 12 April 2012

Introduction Into Imaginary Numbers

I'm back on track for some serious audio programming stuff in the midst of other madness.  I also got back exam results which I'm relieved about, and have assured me programming so far hasn't ruined my life.  At least academically.  Before I delve into the area of Fourier transforms and convolution, I want to clear up a little topic that I skimmed over before, and I guarantee will be useful in the next stuff.  A lot of the mathematically literate can probably give this a skip though.

Imaginary numbers aren't nearly as complex as they sounds (pun fully intended).  You do Cartesian coordinates in very early maths, yet don't look at imaginary numbers until much later.  But all a complex number really is, is a number with two parts.  So really very easy to picture as a number with an x part, and a y part, or a point on 2d space.  The "x component" is called the real part, and will be a plain old number, while the "y component" is called the imaginary part, and basically a multiple of i.  i is the square root of -1, which if you think about it, can't be represented by any tangible number.  If you don't believe me, and have time to burn, prove it to yourself by trying to find a normal number that squares to get -1.

Mathematicians aren't the type to be concerned about if something is useful, but we definitely are.  And the handy thing that we'll see is complex numbers have some cool formulae with them that will allow us to do clever things.  We've already seem this with the filter theory, that complex numbers have a part to play but more with analysis than the actual processing.  But with Fourier transforms, we'll be explicitly making use of complex numbers in the processing.

First up Pythagoras makes a cameo.  As we've mentioned, we'll have no problem picturing an imaginary number.  A point on a 2d plane.  Now if we draw a line from (0,0), or 0 + 0i up to our point we have something we could measure.  Just like on a Cartesian plane, we can use Pythagoras to calculate what's called the "modulus" / "magnitude" / "absolute" of the complex number.  For a number a + bi this means the formula:
sqrt(a^2 + b^2)
So to recap, this formula is giving the length of the line we've drawn from (0,0) to the point, or the complex number.


Trigonometry could also come in handy, say if we want to work out the angle between the line and the positive real axis, otherwise known as the argument.  If you draw it out, it becomes clearer that you have both the "opposite" and "adjacent" side of the triangle, so the argument can be calculated tan(imaginary / real).

Then Euler's identity comes in.  Some people think it's the most beautiful formula in maths, because it combines all the important constants and all the operations - adding, multiplication and exponentiation.  However stupid you think this is, it does have application in filter theory, in analysing the response of filters.  This identity is used to represent sinusoids, and as mentioned ages ago, sound is fundamentally built up of sinusoids.  Also, the formula lends itself well to algebraic manipulation, because there's nifty rules you can apply to indices, e.g. adding indices when multiplying two exponents and subtracting indices when dividing.
e^(pi*i) + 1 = 0
Urgh that's less than beautiful like that though.  *sigh* if only Blogger had some half-decent LaTeX support...  Anyway, as a side note, often engineers will use j instead of i when representing imaginary numbers.  This is because engineers are special, possibly not wanting to confuse it with current or something.

Complex numbers are an unusual concept mathematically, but to a programmer they should be very familiar.  It doesn't take a stretch of the imagination to work out how to store a complex number - a struct with members real and imaginary would be one reasonable way.  I don't think complex numbers are used much in the context of audio programming, considering audio should be a real signal (one component), but I wanted to properly introduce them as they appear again mysteriously in spectral programming (when using the Fourier transform).  This and filter theory seem to be the main areas where they come up.

Next I'm going to put up some stuff I've been doing on the Fourier transform and convolution, which has yielded some pretty exciting results (for me anyway).

No comments:

Post a Comment