Daily Archives: 12/2/2004

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 (;;) {
    output(sin(omega))
    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?

WordPress › Error

There has been a critical error on this website.

Learn more about troubleshooting WordPress.