Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Optimizing pow() function

Reply
Thread Tools

Optimizing pow() function

 
 
Bart van Ingen Schenau
Guest
Posts: n/a
 
      04-24-2013
On Wed, 24 Apr 2013 00:32:20 -0700, pozz wrote:

> On Apr 23, 11:54*pm, James Kuyper <(E-Mail Removed)> wrote:
>>
>> Packages that allow you to fit a data set to a polynomial are widely
>> available, and you should use a lot more than just two points to
>> calibrate the parameters of your fitted model.

>
> 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
 
Reply With Quote
 
 
 
 
James Kuyper
Guest
Posts: n/a
 
      04-24-2013
On 04/24/2013 03:28 AM, pozz wrote:
> On Apr 24, 12:58�am, James Kuyper <(E-Mail Removed)> wrote:
>> On 04/23/2013 06:12 PM, pozz wrote:
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>> Il 23/04/2013 23:24, James Kuyper ha scritto:
>>>> On 04/23/2013 05:10 PM, pozz wrote:
>>>>> Il 23/04/2013 14:35, Richard Damon ha scritto:

>>
>>>>>> Hearing your problem, my guess would be that the issue isn't that the
>>>>>> exponent is a little bit off, but that there is an offset to the voltage
>>>>>> measured. Thus rather than changing to

>>
>>>>>> P = Pr * (x / xr ) ^ a

>>
>>>>>> you might want to look at

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

>>
>>>>> It is nonsense when x=0, because P must be 0 in that case.

>>
>>>> You're essentially claiming certainty that there is no voltage offset,
>>>> that x0 must be exactly 0. You've already indicated that the data
>>>> doesn't exactly match theory. How certain are you (and on what basis?),
>>>> that the failure of reality to match theory doesn't involve a voltage
>>>> offset?

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

>>
>> That would make sense only if your method for measuring the voltage was
>> perfectly accurate, which is simply impossible. Counting of discrete
>> entities (such as people or houses or cars) can be done, in some
>> circumstances, with perfect accuracy, but any measurement that doesn't
>> boil down to counting discrete entities always has an error. Whenever
>> someone tells you that the error in their measurements is zero, it just
>> means that they don't properly understand how their measurements were
>> performed.
>>
>> Any finite error in the calibration of the instrument used to measure
>> the voltage will cause x0 to have a non-zero value.
>>
>>> 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.

>>
>> That's the wrong way to think about it. If you're justified in your
>> certainty that P(0) = 0 (which still seems unlikely to me), then I agree
>> that x0(x) must also be 0. It then follows, if P(x) is sufficiently
>> smooth, that x0(x) will have a power series expansion with a constant
>> term that is zero. (x-x0(x))^2 will therefore also have a power series
>> expansion, also with a constant term of 0. Therefore, you're better off
>> just directly fitting P(x) to a power series, and if you insist, you can
>> tell the fitting program to force the constant to be 0. However, don't
>> be surprised if letting the constant term be non-zero improves the fit.

>
> 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.
--
James Kuyper
 
Reply With Quote
 
 
 
 
Tim Rentsch
Guest
Posts: n/a
 
      04-24-2013
pozz <(E-Mail Removed)> writes:

> Il 23/04/2013 15:19, James Kuyper ha scritto:
>> On 04/23/2013 04:27 AM, http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
>> ...
>>> 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.
>>>
>>> In order to calibrate k, I measure with an instrument the power
>>> at a nominal/reference value, for example I have Pr power
>>> measured ad Vr voltage that xr ADC points. So I can write:
>>>
>>> P = Pr * (x / xr) ^ 2
>>>
>>> Usually I make the reference measurement at xr=800pt.
>>>
>>> The quadratic law above is simple to implement, but in practice
>>> I have to fine-tune this law, because at low power levels the
>>> voltage/power relation seems to deviate from the quadratic law
>>> (I don't know exactly why, maybe some non-linear behaviour of
>>> some components). So I changed the formula with:
>>>
>>> P = Pr * (x / xr) ^ a

>>
>> 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.
 
Reply With Quote
 
Noob
Guest
Posts: n/a
 
      04-25-2013
pozzugno wrote:

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

 
Reply With Quote
 
pozz
Guest
Posts: n/a
 
      04-25-2013
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.

 
Reply With Quote
 
Seebs
Guest
Posts: n/a
 
      04-25-2013
On 2013-04-23, pozz <(E-Mail Removed)> wrote:
> 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
--
Copyright 2013, all wrongs reversed. Peter Seebach / (E-Mail Removed)
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
I am not speaking for my employer, although they do rent some of my opinions.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Optimizing a function? carl C++ 10 12-13-2009 07:35 PM
optimizing function puneet vyas C++ 0 02-26-2009 09:21 AM
optimizing function puneet vyas C++ 0 02-26-2009 08:57 AM
Optimizing function pointer usage Jack C++ 2 03-08-2006 05:59 AM
Optimizing a text statistics function Nickolay Kolev Python 13 04-22-2004 03:16 PM



Advertisments