EmbeddedRelated.com
Forums
The 2024 Embedded Online Conference

Optimize pow() function

Started by pozz September 22, 2016
Am 23.09.2016 um 08:15 schrieb Jack:

> start using integer math instead of float.
Good advice in general, but hardly practical while he's still dead-set on computing, of all things, a fractional power of his input value.
Am 23.09.2016 um 09:23 schrieb pozz:
> I thought about exponentiation, because it is simple to calibrate.
Simple to calibrate it may seem -- but then, so is almost every curve you can compute at all, with only 3 measurements and 3 parameters to determine from them. But if all you're really after is a "small correction of the square law", why lock yourself so thoroughly on the idea that tweaking the exponent should be that small correction, rather than, say, a small shift of the zero point, or a bit of a cubic correction term? Let's face it: of all the functions in the usual collection of standard math library functions (as in: the Standard C <math.h>), pow() is _by_far_ the least simple. You really don't want to use that beast unless your machine has (for a task as hinted here) huge large amounts of FPU cycles you really cannot find more practical use for.
On 9/23/2016 12:23 AM, pozz wrote:
>> What is your *load*? If purely resistive, does it have a temperature >> coefficient? (e.g., if you left it operating at Pmax, would you see >> changes) >> >> In operation, do you expect the load to be "static"? Or, time varying? >> Does the load's impedance change based on how *it* is driven? etc. > > I don't care the load. I'm only working on the power transducer. If the load > changes (because of temperature, degradation, ...), the output signal of > transducer will change accordingly and the power reading will change too.
(sigh) You really don't understand the problem you're trying to solve. I have a voltmeter, here, good to 5-1/2 digits. It was calibrated 4 minutes ago to an NBS traceable reference. It's sitting in a temperature controlled housing. yada-yada-yada-yada. It is presently displaying 1.92341 volts. To what "power" does that correspond? [pull black curtain to hide my actions...] Now, it's displaying 1.84421 volts. What power level does THIS represent? Is it MORE or LESS than the power above?? (think carefully before answering). How does your "professional instrument" (acting as your authoritative reference) measure the power in the load? Why do you trust *it* -- yet KNOW that your measurements are incorrect? What does *it* know that you don't? What is it *doing* that you aren't?
On 23.9.16 10:03, pozz wrote:
> Il 22/09/2016 23:31, rickman ha scritto: >> On 9/22/2016 5:10 PM, pozz wrote: >>> Il 22/09/2016 16:17, Don Y ha scritto: >>>> On 9/22/2016 6:36 AM, pozz wrote: > >> How >> are you getting the power measurement to compare to your calculations? > > Power meter. > > >> What is the device which is the load for this calculation? > > The load is an RF amplifier.
A RF power amplifier usually reacts non-linearly to changes in main supply voltage (plate/clooector/drain feed). In AM transmitters special measures are taken to create a linear realtionship between modulating voltage and output amplitude, which would be the square law you're after. For measuring the RF output, nothing beats a RF directional wattmeter. Please note that if the load behaves non-linearly or changes its impedance, the relation between supply voltage and output power will also change, often in a non-predicatable way. -- -TV
On 9/23/2016 3:08 AM, pozz wrote:
> Il 22/09/2016 23:44, rickman ha scritto: >> On 9/22/2016 5:10 PM, pozz wrote: >>> Il 22/09/2016 16:17, Don Y ha scritto: >>>> On 9/22/2016 6: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. >>>>> >>>>> 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. >>>> >>>> No, the "problem" lies in your transducer/measurement error. >>>> The definition of a "watt" (power) doesn't change just because your >>>> observations of that APPARENT relationship (V ~ W) "has issues". >>>> >>>> Perhaps your load's characteristics are changing as a function >>>> of operating point. Or, nonlinearities in your measurement system. >>>> Or, ... >>>> >>>> Ask yourself WHY the exponent needs to be changed. I suggest you >>>> don't have an accurate model of your load and its excitation. >>>> Until you do, how will you know that this is the CORRECT means of >>>> compensating your model to agree with your observations? >>> >>> >>> I know the problem is in the poor quality of the transducer, but I'm >>> only in charge of the software and the hardware engineer can't fix it >>> (often the software solves hardware issues). >>> >>> So I need a more complex law. The quadratic law (one parameter) can be >>> calibrated with a single measure (nominal value). The generic power law >>> (exponent not exactly equals to 2, two parameters) needs two calibration >>> measures and has a nice feature: it's very simple to calibrate in two >>> steps. >>> >>> The input section has an hardware calibration, a simple digital trimmer. >>> So the first step is to calculate the power from the measured voltage >>> with the following formula: >>> P = Pn / (Vn ^ 2) * (V ^ 2) >>> where Pn is the nominal power and Vn is the voltage needed at nominal >>> power. The user increase/decrease the trimmer pot until the correct >>> power is displayed. >>> >>> Second step. Change the power to another value (half of nominal power, >>> for example). If the transducer works as in theory, the measured power >>> is correct. Otherwise the user increase/decrease the exponent until the >>> power displayed is correct. The power is calculated as: >>> P = Pn / (Vn ^ exp) * (V ^ exp) >>> >>> After the second step, we are sure the first calibration point is yet >>> valid, because that formula is correct when V=Vn, even if exp isn't 2. >> >> I think your equation is not written in a clear way. I think what you >> mean is this... >> >> P = Pn * (V ^ exp) / (Vn ^ exp) > > IMHO, they are identical mathematical expressions.
It is equivalent, but the way you wrote it the order of operations has to be implied by the standard rules. I typically don't write a division that way because often the equation really intended is A = B / (C * D) rather than A = (B / C) * D which are *not* equivalent. So A = B * D / C is more clear, to me anyway. Or supply the parentheses.
>> Now the equation makes sense to me. But that still doesn't mean it will >> give close to correct values anywhere other than the calibration points. > > Yes, I have tried and this mathematical law could be ok for my application. > > >> Have you considered... >> >> P = Pn * (V / Vn) ^ exp > > Again IMHO, this is identical to the above expressions.
Yeah, I got wrapped around the axle on this thinking of logs. A division before taking the log is equivalent to adding the logs of the two values. Still, again, this is a more clear way to write what you are doing. The value Vn is just a scale factor. Why complicated it by taking the exponent and dividing after the exponent is taken of the value measured? At least this is more clear to me.
>> Then there are an infinite set of other corrections you could use. The >> question is which ones are more accurate? > > I don't need to increase accuracy. I only need a little compensation of > the square law. The calibration process of the mathematical model > should be simple.
Isn't the "compensation" to get better accuracy?
>> Have you looked at a range of data to see what the correction curve >> needs to be? > > Yes, and it is somewhat a square law. In some cases, I need to change > the shape of the square law a little.
"A little" is a poor way to describe this. There are many ways to correct your data. You have chosen one that you find hard to calculate quickly enough. So why not consider others which can be evaluated more quickly? Others have given you other approaches. I suggest you consider them more carefully and not wed yourself to using a variable exponent. You say it is easy for the user to calibrate, but really you are saying it is easy for *you* to write the calibration algorithm. If you can do the calibration with two measurements with your variable exponent, you can do a calibration with an x^3 term with two measurements. The user doesn't need to know what the software is doing with those measurements. -- Rick C
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.
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. There are simply too many moving parts in the equation, it will not work with some mathematical trickery. -- -TV
On 09/25/16 13:36, Tauno Voipio wrote:

> > 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. > > There are simply too many moving parts in the equation, it will not > work with some mathematical trickery. >
This has been a long thread, but fundamentally, you can't determine output power into the load without measuring the load vswr. Depending on the output power, a simple directional coupler could be a short length of coax, measuring forward and reverse power simultaaneously, which could be calibrated via a lookup table and interpolation to the required accuracy. No need for complex math in the software... Why does it have to be such hard work ?... Regards, Chris
On 26.9.16 20:30, Chris wrote:
> On 09/25/16 13:36, Tauno Voipio wrote: > >> >> 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. >> >> There are simply too many moving parts in the equation, it will not >> work with some mathematical trickery. >> > > This has been a long thread, but fundamentally, you can't determine > output power into the load without measuring the load vswr. Depending > on the output power, a simple directional coupler could be a short > length of coax, measuring forward and reverse power simultaaneously, > which could be calibrated via a lookup table and interpolation to the > required accuracy. No need for complex math in the software... > > Why does it have to be such hard work ?... > > Regards, > > Chris
From a directional coupler, you get the component amplitude, which has to be squared for power. (It is kosher here, due to telegraph equations). The math happens to go so that the net output is available as forward power - reflected power. -- -TV
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 2024 Embedded Online Conference