Optimizing pow() function

B

Bart van Ingen Schenau

I can't use the powerful software on the field, machine by machine.

Sure you can. Nobody is saying those methods should be integrated into
the machine.
You could enter the measurements into a curve-fitting program on a laptop
and just program the resulting coefficients into the machine.
The theory says that the exponent should be 2 (P=V^2/R). In practice I
have
a shift from the theory, I don't know why (I'm not an hw expert).

If you don't know why you see the deviations, go talk with a HW engineer.
Preferably one that works on the same machine.
As for the deviations from the theory, are you sure that you are not
trying to do the calculations to a higher accuracy than your measurements
and hardware components allow?
For example, certain commonly used resistors can deviate from their
nominal value by as much as 10%. Those tolerances will make up a large
portion of the uncertainty in the R value of the formula above.
Also, the value from the ADC corresponds to a range of voltages, not just
a single voltage. This introduces a second factor of uncertainty into the
calculations.

Other possibilities for the apparent deviations from the theoretical
ideal are
- the ADC has a non-linear response curve (if you plot the voltage
against the ADC output, the steps are not all of the same size).
- The overall resistance in the system is not constant but varies with,
for example, the temperature. As components that consume power also heat
up as a side effect, this can have a significant effect if the system
consumes a lot of power.
So I need to adjust a law that depends on a small number of parameters
(in my
case, Pr and the exponent) on the field, machine by machine, with a
couple
of measures.

My best advise would be to talk to some hardware engineers that know the
system to get a better understanding of the factors involved and how you
could compensate for them.
As it is now, you can't guarantee any better results for arbitrary
measurements than you would get with just blindly following the theory.

Bart v Ingen Schenau
 
J

James Kuyper

Yes, I can use a high-degree polynomial function to best fit the curve
I
measure, but it would be very difficult to calibrate on the field,
machine
by machine. I'd like to have a good fit curve with a few parameters
to
calibrate in a simple way.

The quadratic law (a second degree polynomial) comes from the theory.
My idea to use exponential function is to best-fit the real curve
starting
from the theory, so changing a little the exponent, without adding too
much
parameters difficult to calibrate.

Well, as you've found, modeling the curve by using a variable exponent
results in equations that you cannot evaluate quickly enough within your
memory constraints, so I'd recommend reconsidering that decision.

If you insist on "calibrating" your model with only a small number of
data points, it's really quite trivial to figure our the coefficients
for a polynomial that does so. Let v0, v1, v2 be three different
voltages, and P0, P1, P2 be the corresponding power levels. Then it's
quite straightforward to see that the following polynomial
necessarily goes through all three data points:

P(x) = P0*(x-x1)*(x-x2)/((x0-x1)*(x0-x2)) +
P1*(x-x0)*(x-x2)/((x1-x0)*(x1-x2)) +
P2*(x-x0)*(x-x1)/((x2-x0)*(x2-x1))

I can't take credit for this coming up with that formulation; but I also
can't give proper credit, because I can't remember the name of the
famous mathematician this approach is named for. It may have been Lagrange.

After filling in values for x0, x1, x2, P0, P1, P2, it's simple enough
to expand and simplify down to a simple quadratic expression that your
program can evaluate with reasonable efficiency and negligible storage
requirements.

Once you understand why it's "obvious" that this gives you a polynomial
that goes precisely through those three data points, it should be
trivial to figure out how to extend this to an arbitrarily large number
of points, but I wouldn't recommend going beyond 4.
 
T

Tim Rentsch

pozz said:
Il 23/04/2013 15:19, James Kuyper ha scritto:
On 04/23/2013 04:27 AM, (e-mail address removed) wrote:
...

Without seeing your data, I can't be sure, but I think that it's
unlikely that the correct formula is of that form. If you don't
have a theory that tells you what the shape of a curve should be,
and you're trying to fit it empirically, choose the form of the
curve to make your job easier. A power law with a non-integer
power makes things very difficult.

Ok, I could try adding a linear term to the original equation:

P = k * x ^ 2 + h * x

But now it's very difficult to calibrate and find k and h constant
in such a way the curve passes through the reference point (xr, Pr)
and another reference low-power point (xs, Ps). [snip elaboration]

It has gradually become apparent that the problem you're having
is better thought of as a problem in numerical approximation
rather than just one of performance.

Expanding on a suggestion of Eric Sosman, let's make a guess
that we can tweak a V**2 relationship a little bit and that
will give us a good approximation. The very simplest tweak
is a linear function in v, so:

Power = (a * v + b) * v * v

(In this equation you can think of the resistance as being
unity, or as being "hidden" in the parameters 'a' and 'b'.
I don't think this part is hard and so I won't explain it
further.)

Given that we have two reference values, which I will call
r and s (corresponding to your xr and xs above), we have

Pr = (a * r + b) * r * r
Ps = (a * s + b) * s * s

or writing them somewhat more "mathematically"

Pr = a rrr + b rr
Ps = a sss + b ss

These equations can easily be solved for parameters 'a'
and 'b', giving

a = (Pr ss - Ps rr) / (rrss (r-s))
b = (Ps rrr - Pr sss) / (rrss (r-s))

where a and b are determined as part of the calibration
process, knowing the four references values r, s, Pr, and Ps.

Once the calibration is done, calculating power based on voltage
is very easy -- just three multiplications and one addition:

P = (a * v + b) * v * v

This formula is easy to test, and should be tested. If even
this simple formula isn't fast enough, then you have a bigger
problem, and you need to think about, eg, using rational fixed
point arithmetic. If it is fast enough, then the question is
does it give you enough accuracy? It might. But if it doesn't,
we can take the same idea and use a quadratic tweak rather
than a linear tweak:

Power = (a*v*v + b*v + c) * v * v

In this case we would need three reference values, call them x, y,
and z, and the corresponding powers, Px, Py, Pz. The parameters a,
b, and c again can be found using linear algebra, using the six
reference values as inputs -- the formulas are more complicated but
probably still simple enough so they could be used in calibration
code. The resulting formula above can be computed using only four
multiplications and two additions - very probably plenty fast
enough. And it seems pretty likely to give results that are
accurate enough over the desired range.

A more sophisticated analysis would try to minimize the error over
the range of interest, eg, using a least squares fit, but to start
with it's worth trying the simple formulas to see if they do the
job.
 
N

Noob

pozzugno said:
Ok, it's better to explain what I'm trying to do.

I have a 10-bit ADC that acquires a voltage signal and convert it to a decimal number 0..1023.

This signal measures a power (Watt). The relation between voltage and power is theorically quadratic:

P = k * x ^ 2

where k is a constant that depends on the system and x is the ADC result.

Waaaaay off-topic.
You'd get better answers to these questions in comp.arch.embedded
or even an electronics newsgroup (in sci.electronics.* maybe)
 
P

pozz

Il 24/04/2013 14:31, James Kuyper ha scritto:
Once you understand why it's "obvious" that this gives you a polynomial
that goes precisely through those three data points, it should be
trivial to figure out how to extend this to an arbitrarily large number
of points, but I wouldn't recommend going beyond 4.

Thank you for your suggestion, I'll try to explore on this way.
 
S

Seebs

It could be, but not at very low voltages. When I have zero power, I
have zero voltage (0 ADC points)... this is for sure.

Myabe the offset starts increasing with the power level or voltage, like
in the following equation:

P = k * (x + x0(x)) ^ 2

but it's difficult to understand what is x0(x). The only thing I can
say is that x0(0)=0.

It is not unheard of for things to behave almost as though some input were
max(x - C, 0) for some constant C, rather than an unmodified x. If nothing
else, you might find it rewarding to:

1. Assemble a bunch of measurements.
2. Put them in a spreadsheet.
3. Ask a computer to find a good matching function.

-s
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top