Fast Generation of Sine Waves

Every once in a while, I want to generate some pure sine waves for audio purposes, and I have to go digging around to find this simple technique, so I thought I would write it down here. Suppose that you are trying to generate a 440Hz (middle A, if memory serves) sine wave sampled at 44.1Khz. The expensive way would look like this:

double t = 0.0 ; /* time begins at zero */
double omega = 0.0 ; /* angle is zero */
double dt = 1.0 / 44100 ; /* each sample advances time by dt */
double domega = dt * 440.0 * 2.0 * M_PI ; /* the angle advances by domega */

for (;;) {
    t += dt ;
    omega += domega ;

(Yes, yes, astute readers will note that t isn’t needed. So sue me.) This requires a single call to sin for each sample. On my little Via box, a call to sin takes about .584 microseconds, so you can generate over 1.7 million samples per second with this technique. Not bad, but we can do better. Much better.

The key is to remember this recurrence relationship:

(Formula courtesy of Don Cross)

You can compute samples of simple sine waves very fast using this technique, since it takes only a multiply and a subtract per sample. On my machine, that turns down to about 7 nanoseconds per sample. That is a speedup of over 84x.

Unnecessary? Perhaps, but it is still cool.

Why am I looking at this? Stay tuned.

Anti-Lamenessing Engine (ALE)

My perusal of freshmeat today turned up a link to the ill-named Anti-Lamenessing Engine (ALE), an image processing program that contains (among other things) an implementation of Irani and Peleg’s super resolution technique. The idea of super-resolution is to use multiple low resolution scans to generate a single improved high resolution scan. I first got interested in this as part of my interest in CCD imaging in astronomy, so it is nice to see an actual implementation.

Where does Brainwagon belong?

A direct question from Ruth Meers at Bloggercon served to percolate a question that has been kicking around subliminally for quite some time.

What is your podcast (or more generally) your weblog about?

Brainwagon isn’t really a personal diary weblog. I do occasionally mention stuff about my personal life, but that isn’t the focus. I don’t write it especially for my friends, although some of them do get mentions from time to time. I don’t write it to pitch business ideas or find new jobs.

If that’s what this site is not, then what is it?

Have you ever wandered into somebody’s office and scanned the list of books on their shelves? Books naturally grab my eye, so I do it pretty much everytime I go into people’s office. Brainwagon is basically a way of giving you a glimpse into my office bookshelves. You aren’t going to find anything deeply personal here, but you are going to find the list of topics that I am interested in, at least to the point where I took the trouble to buy a book. If you see me posting about Mayan art, the Enigma machine, photography or whatever, you can be rest assured that somewhere there are some dead trees which I’ve been thumbing through lately to help fuel my understanding.

On my desk today, amidst the empty cups which previously held Diet coke, I counted six books:

  • The Physics of Baseball, which has been there since the Red Sox defeated the Yankees. A great book, incidently. Behind me is a copy of Ross’ The Mathematics of Baseball, much less interesting.
  • Meyer and Downing’s Java Virtual Machine. For some reason this was on the top of a big pile. It is one book I’ve never really found a good reason to read.
  • Advanced Renderman, by Apodcaca and Gritz, a nominally work related book.
  • Ken Steiglitz’s A Digital Signal Processing Primer, which I consider one of the best books on DSP around. I’ve read it about six times, and it finally is beginning to sink in more or less completely.
  • Jones and Flynn’s Mobile Robots. I have lots of books on robotics, but never built one. A pity.
  • Helen Fouché Gaines’ Cryptanalysis, a book no budding code breaker should be without.

Okay, so where am I going with this? Imagine trying to assign a particular category to the variety of books that are on my desk at the moment. Just what do you think that word would be?