3
\$\begingroup\$

I'm trying to code an exponential function for use on a PWM signal.

My question is, does anyone know how to do a power function on an ARM cortex-M (STM32F4 in this case, which has an FPU) microcontroller. Or would a taylor series approximation do the trick better.

I know this is question is very much programming related but as its related to microcontrollers I thought it would be ok to post here(?), plus it seems questions on stackoverflow get lost very quickly.

\$\endgroup\$
3
  • \$\begingroup\$ A cursory google search of arm_power_f32 reveals that it performs a sum-of-squares over a floating point array and is therefore not a power function. \$\endgroup\$
    – MikeJ-UK
    Jun 7, 2013 at 11:21
  • \$\begingroup\$ thanks, well that rules out that, I'll remove it from the question then. I though having power in the name and being in the maths header, it would be logically be a power function but names can be deceiving \$\endgroup\$
    – richendes
    Jun 7, 2013 at 11:27
  • \$\begingroup\$ Plus in my head I for some reason translated sums of squares to product of squares which would be similar to a power function ie, 2x2 is one square so 2x(2x2) is a product of the 2x2 square. \$\endgroup\$
    – richendes
    Jun 7, 2013 at 11:33

2 Answers 2

2
\$\begingroup\$

Generating a PWM signal in an embedded application generally involves a limited number of bits in the counters used to generate the signal timing. This could be as few as 8 bits or 10 bits depending upon your PWM hardware. Keep in mind too that the binary representation of the PWM values establishes a fixed universe of values in the embedded application. You can use this to your advantage in finding a solution to this problem.

The idea is to use an off-line calculation tool such as a spread sheet to compute whatever range of exponential function that you desire. This can be used to produce a look-up table of data that you then place onto the microcontroller code as a constants table. Limited bit width PWM's make the table size be practical. If you happen to have a 16-bit PWM then the table can still contain 16-bit target values but the table size can be limited to a practical size without needing 64K entries. In this latter case the target software uses linear interpolation between the table entries to find target PWM values. This technique is known as piecewise linear interpolation.

You will find this table look-up approach much less compute intensive than trying to calculate complicated floating point formulas in real time on your microcontroller. Sometimes the performance improvement gained is essential for applications that require the PWM value to update and track an input at with minimal latency.

\$\endgroup\$
3
\$\begingroup\$

The standard lib function for power on float data type is float powf(float, float). It appears to be well supported on my gcc-based STM32F4 setup. Given the performance of this MCU and its hardware FPU support for float, this function probably has excellent performance (it is likely using some taylor series or similar optimised numerical approximation). A look-up table approach might be faster if you can narrow your application to a specific set of power, but since such an implementation would be more complicated and less flexible, I would keep it as a possible optimisation if/when you determine that powf is too slow for your needs.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ For my application I also needed to do a power operation for each audio sample. My processor is a NXP K66 processor, which is also an ARM Cortex M4F with FPU, like the STM32F4 discussed above. With the CPU running at 180MHz and with a sample rate of 44.1 kHz, the powf() command consumes about 18% of CPU. In my opinion, that's not too bad. \$\endgroup\$ Dec 4, 2016 at 22:59
  • \$\begingroup\$ Now that I'm trying to do more with my K66, I think that 18% of CPU for one math operation is way too much. Does anyone know of an example of one of these tricks for doing faster pow() that I can learn from? \$\endgroup\$ Feb 4, 2017 at 12:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.