# Optimize pow() function

• posted

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 ->

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.

• posted

Only if you're talking about voltage applied to a resistive load. There's a lot of loads that aren't purely resistive.

As already suggested, the first thing to do is determine what's wrong with your measurement and fix it.

Assuming you can't do that, the first thing that I'd suggest is to use interpolation or a curve fit to a number of measured points. Interpolation is easy -- just store a number of calibration points and then use linear, quadratic, or cubic interpolation between them. Even cubic interpolation should be quicker than using pow. Alternately, fit a function y = A + B * x + C * x^2 + D * x^3 to your calibration points, and store A, B, C & D in memory.

```--
Tim Wescott
Control systems, embedded software and circuit design ```
• posted

itto:

and the use the alternate form:

y = A + x *(B + x *(C + x*D))

to calculate the thing, so you need less operations.

Bye Jack

• posted

Definitely. Unless you're doing the computations with an ADSP 21xx chip, in which case you can code up my original function into a hardware loop, with one set of registers doing a MAC and another register doing z = z * x at each iteration. Very handy, if you happen to need to do it.

(The ADSP 21xx core is a frustrating chip to write optimized code for. It has a buttload of registers, each of which does two or three things, and no two of which do the _same_ set of things. So you'll be coding along, and have to go back a dozen instructions just to (ferinstance) use x2 instead of x1, so that when you get to where you are you don't need to juggle data around.)

```--
Tim Wescott
Wescott Design Services ```
• posted

or you can let the C compiler do the job for you ;)

Bye Jack

```--
Yoda of Borg am I! Assimilated shall you be! Futile resistance is, hmm?```
• posted

ha

nts,

p,

,

z *

se

to

the adsp21xx c compiler was horrible, but assembler was very human readable

• posted

I pity the poor bastard who's called upon to do a good job making an optimizing compiler for that chip. It's the answer to the question "How can an instruction set _not_ be orthogonal?"

```--
Tim Wescott
Wescott Design Services ```
• posted

Il 22/09/2016 17:01, Tim Wescott ha scritto: > [...]

Yes, this is a lighter calculation for the MCU, but the calibration would be much more difficult to the operator.

Please, take a look at my answer to Don Y for additional details on my system: I have a digital trimmer.

• posted

Assuming that you could go offline for a moment right after calibration, you could choose to have the operator do whatever calibration action you want externally, then compute the values of your coefficients from those results.

```--
Tim Wescott
Wescott Design Services ```
• posted

start using integer math instead of float.

Bye Jack

• posted

Yes. Measure lots of points, getting data on real power versus measured signal - then you can see what the curve looks like.

Then do the same with different boards, different loads, different temperatures, different source voltages, different cooling of the loads

- whatever can vary in the system.

Then you can see if you are actually able to get an accurate value for the real power based only on the measured signal - or if you need more data (such as a temperature sensor) in your calibration. And you can find out what levels of accuracy you can expect from the system. If different load types mean a 10% variation in the signal for the same real power, then there is no point trying to make a complex curve to squeeze an extra 1% of fit.

Yes, cubics are good for this sort of thing.

Even if the underlying curve really is a power-law curve, a cubic approximation or cubic spline is likely to be the fastest way to get a reasonable fit.

• posted

Nah, it's just a few matrix inversions. When it is part of a calibration operation, it doesn't matter if it takes a second for the calculations (and that would be for the dumbest of 8051 chips - with an ARM or similar you'd be well under a millisecond).

But the maths is more difficult for the /programmer/ - that might be a challenge :-)

• posted

Still seems worth spending some effort identifying sources of error in the measurement, then seeing how they combine into a joint error term and trying to estimate that, rather than just guess at the final shape.

• posted

Certainly, if you can get a better model of the system (including its errors) then that's a good idea.

• posted

Well, yes, but sometimes that's just not in the cards, either because of the abilities of the people involved, or because management's breathing down one's neck.

Finding root problems and fixing them are almost always best in the long run. Unless they delay product introduction to such an extent that your project gets canceled, or misses its market window -- then they're bad in the long run.

```--
Tim Wescott
Control systems, embedded software and circuit design ```
• posted

Am 23.09.2016 um 08:15 schrieb Jack:

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.

• posted

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.

• posted

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```
• posted

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

• posted

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```

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.