EmbeddedRelated.com
Forums
The 2024 Embedded Online Conference

Optimize pow() function

Started by pozz September 22, 2016
On Thu, 22 Sep 2016 15:36:11 +0200, pozz <pozzugno@gmail.com> wrote:

>I have to convert voltage measure (Volt) into power (Watt). As you >know, the physics says I need to square the voltage measure. > >I have to calibrate the mathematical law and I have two points: >0W -> 0V ><nominal power> -> <voltage at nominal power> > >That could be enough, but we noticed a different relation between volt >and watt at intermediate points. I, as the software engineer, have to >try to compensate differences from the theory, without increasing >complexity during calibration. > >My idea is to calibrate the exponent: it is normally 2.00, but I can >calibrate from 1.00 to 3.00. >In this way, the two old calibration points are the same as before. So >the exponent (the third calibration measure) can be changed at last. > >The problem is pow() function with double arguments takes a lot of time >to finish. Do you suggest a mathematical method to reduce its complexity? > > >uint16_t power_nominal = 1500; // In Watt >uint16_t volt_nominal; // Volt measured at nominal power >signed uint8_t exp_corr = -100 to +100; >double exp = 2.00 + exp_corr; > >uint16_t volt = adc_convert(...); // Actual volt measure >double alpha = (double)power_nominal / pow((double)volt_nominal, exp); >uint32_t watt = alpha * pow((double)volt, exp); > > >alpha can be pre-calculated, because power_nominal, volt_nominal and exp >are constant (after calibration). The last instruction is the problem.
While others have raised issues with why you're doing this, if exp is relatively constant (as you appear to indicate), just precompute all the values for the 2**16 possible (or fewer, if the range is restricted), values of voltage, raised to the desired exponent, and do a table lookup when you want the value.
On 27.9.16 00:30, pozz wrote:
> Il 25/09/2016 15:36, Tauno Voipio ha scritto: >> On 25.9.16 13:50, Richard Damon wrote: >>> On 9/22/16 9:36 AM, pozz wrote: >>>> I have to convert voltage measure (Volt) into power (Watt). As you >>>> know, the physics says I need to square the voltage measure. >>>> >>> Whoever told you that didn't give you the full message, as it applies in >>> only very limited cases. >>> >>> Electrical Power is Voltage * Current, not Voltage squared. >>> >>> IF you have a resistive load, then we have that >>> >>> Voltage = Current * Resistance. >>> >>> From this we can get to Power = Voltage * Voltage / Resistance. >>> >>> This means that for a system with constant resistance we can say that >>> Power goes as Voltage squared, with a scale factor of 1/Resistance. >>> >>> If the resistance of the system isn't constant, this relationship >>> absolutely doesn't hold. I have built systems where (within an operating >>> band) the Power consumed goes up with dropping voltage. >>> >>> If all you can measure in the field is the Voltage, and the system if >>> fairly stable, then what you can do is in the lab go to a NUMBER of >>> points and measure voltage and power to determine how your system >>> responds and come up with some sort of curve to fit the results. >>> >>> You need MORE points than unknowns to be able to sanity check your fit. >>> You also should perform it under varying conditions (like ambient >>> temperature) to make sure the results are stable. >> >> >> The situation of the OP is more complex: He's attempting to measure >> the output power of a RF amplifier by measuring the DC feed voltage >> and relating the output power to it. > > I think many misunderstandings derive from this assumption. I never > wrote (or I think I never wrote) that I need to calculate the output > power of an amplifier knowing only the DC feed voltage. > > I'm trying to calibrate a measurement device that, in my case, is a > directional coupler. For me (the firmware engineer), it is a voltage > signal that is related to the output power by a square mathematical law > (at least, this said the RF engineer). > > By comparing our results with a professional instrument, we noticed that > the square law (P=kV^2) is somewhat good for our necessity. > Unfortunately, if it is calibrated at the nominal power (k), at low > power levels the results deviate a little from the theorical square law. > > This is why I'm trying to introduce a second parameter in a mathematical > law P=f_k,h(W) that is *the* square law when the second calibration > parameter (h) isn't touched. > That formula is: > P = k * (V / Vn) ^ (2 + h) > When h=0 (second calibration parameter not touched) the law *is the* > theoric square law. > > If the operator notices a big error at, i.e., Pn/2, he tries to change > h. In this way, he is sure the measure at Pn/Vn will not change. > >> >> There are simply too many moving parts in the equation, it will not >> work with some mathematical trickery.
The power functions are difficult to compute for non-integral powers, and they may be ill-behaving. Of the basic transcendental functions, logarithm is the most difficult to compute. Get enough points and do a polynomial fit to the data. You can decide on the suitable degree by looking at the residuals. If you're looking at a directional coupler, you should measure both forward and reflected power to get the net output. -- -TV

The 2024 Embedded Online Conference