Methods for sinwave aproximation

Image

As you may have gathered from my previous posts, I like to use fixed point arithmetic where possible. There are a number of reasons for this, speed being one, but also some processors even in this day and age don’t have and kind of FPU on them. This makes fixed point the only viable solution for fractional maths on these kind of machines. I own a Dingoo A320 handled console and it is just one such machine, running a Mips processor without any FPU. I believe that the NintendoDS is another such machine, but I cant say for sure since I haven’t programmed for on yet.

I need a fast way to perform rotations in 2D, which is a process requiring fractional maths and access to the sine and cosine function.  I also need the sin functions for programming a digital low pass filter in an audio synthesizer.  The sine and cosine implementation in the standard library are slow, mostly because they choose accuracy over speed, and they also work exclusively with floating point values.  I wanted to find a accurate and fast method for getting sin-wave values, without using floating point values.  After some research I managed to put together a small library containing a few different ways of making these approximations, very fast and with various degrees of accuracy.

So what is in this library:

  • Six different tables, each encoding a sin-wave with different sizes and resolution.
  • Tables are each a power of two in size, providing optimization opportunities.
  • Tables are in 8bit and 16bit resolution.
  • A function to index one of the tables using linear interpolation.
  • A very nice and fast approximate sin-wave function that performs more accurately (and quicker I expect) then the table based methods.
  • Additional Cosine functions using the same methods.
  • Wrapping for indices that lie outside of the sin-waves period.
  • It has been tested a little and they all seem to work nicely.

Source Code:

sin_aprox.h
sin_aprox.cpp

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s