Using floating point math in include files for objects


#1

I'd like to use floating point math (sin, cos, log, exp) in a header file that I inlude in an object but this throws an error (undefined reference to `sin'). Neither including cmath nor math.h resolved the error. It works if fixed values are given (e.g. sin(0.5)), probably because the calculation is done by the compiler.

Is it at all possible to use these floating point math functions with axoloti?


#2

It's possible to use the standard math library, but generally not desirable. The standard math library favors accuracy over speed. ie - they'll run the algo until they get enough significant digits to fill out the fractional portion of the float type being returned. That's typically not needed to audio DSP. The axoloti firmware has table based math functions that should be called instead. They are less accurate but much faster. Take a look at the implementation of the factory sine wave oscillator to see how to do this.


#3

Thanks for the reply. I was able to get the functions up and running by using sinf/cosf and pow(e, x) instead of sin/cos/exp.

The math is only used during initialisation but I need the higher accuracy there. This is for a speaker crossover where I have given filters that should run on the axoloti. As I'm much more familiar with floating point dsp I implemented the filters in floating point which seems to work fine (the whole crossover uses about 20% CPU).


#4

Certainly you want to stick to single precision float on the axoloti because the cortex-m4 doesn't have instructions for 64 bit floats. Also keep in mind that a constant written like "2.0" will be treated as a double (64 bits) while the same written as "2.0f" or "2.f" will be treated as float (32 bits). That is: make sure you put an "f" on the end of your constants so the compiler doesn't try to do 64 bit float operations.


#5

Johannes told me to stay away from pow function, as it takes a lot of resources. But not sure how to test it.

May I ask which filter you are making?

Anything special? :slight_smile:


#6

The pow function is only used during initialisation as the filters don't change.

I use rather typical EQ filters (lowcut, hicut, peak and shelfs). The crossover filters are two butterworh cuts for a linkwitz-riley filter and the shelfs & peak filters are for equalisation. Unfortunately it's not easy to set gain and Q factor in the axoloti objects I found. At least not for me. I also had problems understanding the parameters mappings while trying to hard code a Q factor of sqrt(0.5) for a 2nd order butterworth filter.

The processing is done in floating point using this implementation.


#7

Sounds great.

A bit more complicated than I understand, but it sounds nice.

I hope you get the Q working :wink: