Fixed point trigonometry?

Status
Not open for further replies.

mattomatto

Well-known member
Soooo what does one do when they need to use tanh() a lot with fixed point numbers?

Is there any standard way of dealing with this, or would you just have to convert to float and back?

Cheers!
 
I don't know of any standard way, but what's your fixed point class in the first place? Is it an arbitrary format or do you use something readily available like libfixmath?

I'm pretty sure you can find numerical recipes on the net, especially on stackexchange (overflow, math, scicomp, ...) - that's where I'd start.

What are your speed and accuracy requirements?
 
With the Lynxmotion Phoenix code base for Hexapods and Quads, most of it is done with fixed point math.

The original code was written in Basic for the Basic Micro Atom Pros by the Lynxmotion member Xan, with help and updates by several others of us. I since have converted it to Arduino, later also Linux, now Teensy... The code uses table lookups to do most of the math functions...
Note: I did a pretty straight foreword conversion of the code base and still use many of the same functions. I know in some cases like the sqrt function, the native Arduino floating point version runs faster, than our converted fixed point one. I should someday go back and update to use it. Likewise should probably check to see the performance of others as well.

Also on Linux with processors with floating point support, I have a WIP to convert back to floating point as to compare speed differences. If you are interested at looking at these functions, they are up on github in several projects including: https://github.com/KurtE/Arduino_Phoenix_Parts
In the Phoenix directory in the Phoenx_Code.h file. Note: in this project most of the source files are header files. Only way I figured out where I could get different options (which pins do what, which parts of the code to include....) for each robot.

Kurt
 
Thanks for the replies.

@Christoph - I'm using Q15 to fit in with the the core arm instructions and not using a class, just shifting things down when I need to. It's for a filter object for the audio library so needs to be pretty quick.. running at 44.1KHz with all the other stuff going on too. Will check out stack overflow - good idea.

@KurtE - I will have a good at your github and see what I can find. Perhaps a lookup table is a good idea!
 
Are you sure that you need this..?
I made a balancing robot with 8 Mhz on AVR...with floats. ok, there are less calculation, but it is in the same range.
Teensy has 96Mhz.
At which freq.are you updating your motor-pwm? 50Hz ? How fast do you get the IMU-Data ? You don't need to do your calculations faster than that.


EDIT: uh, sorry, i was confused with an other thread...
 
Last edited:
If it's for audio data, you might get away with a self-made lookup table, some angle-wrapping code, and possibly linear interpolation. Give it a try, this should be doable in a couple of hours.
 
Cheers christoph. It's to try and simulate non-linarities in filter saturation, sofrom what I gather doesn't need to be perfectly exact. Will give this a try!
 
There's a delightful series of books called Graphics Gems. The idea is to present clever practical bits of code for computer graphics. For example: Turkowski, Ken, Fixed-Point Trigonometry with CORDIC Iterations, Graphics Gems, p. 494-497, code: p. 773-774, FixedTrig.c. Not sure if you can find the associated article that explains the linked code online, but maybe the book or the article author would be of interest. Wikipedia has a good article on CORDIC algorithms.
 
Status
Not open for further replies.
Back
Top