Skip to main content
abarnett
Associate II
March 2, 2017
Question

Looking for a trig function library?

  • March 2, 2017
  • 7 replies
  • 3111 views
Posted on March 02, 2017 at 21:27

I am using an STM32F407 (new to ST and this processor) and I need to be able to calculate sines, cosines, square roots, various other trig functions for a power conversion application.  

Is there a math library I can download that contains these functions? 

Thanks in advance for any assistance.

    This topic has been closed for replies.

    7 replies

    Danish1
    Lead III
    March 2, 2017
    Posted on March 02, 2017 at 21:35

    If you're using a C compiler (or pretty much any high-level language) then it should have trig functions built-in.

    With C or C++ you'd have to #include <math.h>.

    With 8-bit processors such as PIC16, I would often drop to assembly code. But for the size of project that would justify stm32, I find the benefits of C / C++ greatly outweigh the minor loss in ultimate achievable performance.

    Hope this helps,

    Danish

    Tesla DeLorean
    Guru
    March 2, 2017
    Posted on March 02, 2017 at 21:47

    #include <math.h> ??

    32-bit float variant, though FPU doesn't support transcendental functions natively

    cosf(), sinf(), sqrtf(), powf()

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    abarnett
    abarnettAuthor
    Associate II
    March 2, 2017
    Posted on March 02, 2017 at 21:48

    Danish Ali, thanks for the prompt response.  I've tried the math.h funciton and it takes too long (30us) to execute.  Not sure if I'm doing something wrong here.  

    I guess I'm looking for something from a motor drive library, maybe something used for Park / Clark transformations.  

    Tesla DeLorean
    Guru
    March 2, 2017
    Posted on March 02, 2017 at 21:50

    Make sure to a) enable the FPU, and b) use the 32-bit float variants, the FPU doesn't do 64-bit doubles, or transcendentals like an 80x87 would.

    If it still isn't fast enough, use tables.

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    abarnett
    abarnettAuthor
    Associate II
    March 2, 2017
    Posted on March 02, 2017 at 21:56

    the sinf funtion reduced execution down to about 1us, very helpful and thanks.

    Since I'm new to the chip (coming from a TI fixed point world) how do I know if the FPU is enabled?

    Assuming it is, lookup tables will be next if the speed isn't enough.  Regardless, very helpful and thanks!!