Velocity Reviews > Optimizing pow() function

# Optimizing pow() function

glen herrmannsfeldt
Guest
Posts: n/a

 04-23-2013
pozz <(E-Mail Removed)> wrote:
> Il 23/04/2013 15:08, Eric Sosman ha scritto:

(snip)
>> always an integer or half-integer" or "I only need six bits'
>> accuracy in the result"), I see no approach that's likely to
>> make a further dramatic difference. (Giant precomputed tables
>> seem inappropriate for "a simple embedded platform.")

>
> explanation about what I'm trying to do.

Well, what we still don't know is exactly, or even approximately,
how accurate the resutls need to be.

For a reasonable range of values, log and exp can be done in fixed
point, and likely good enough for you, but we don't know that.

The algoritms in "Metafont: The Program" do computation on fixed
point values with 15 bits before and 16 bits after the binary point.
They should run much faster than software emulation of floating
point, and maybe fast enough for you.

(Metafont was originally in Pascal. It is more often now run as
a machine translation to C of the Pascal source.)

-- glen

James Kuyper
Guest
Posts: n/a

 04-23-2013
On 04/23/2013 05:20 PM, pozz wrote:
> 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:

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

Depending upon what your data looks like, I'd recommend a cubic term,
and possibly a constant one as well. If you use a package to fit your
data to a polynomial, and one of the terms has a coefficient that is not
statistically significant, you can always drop it at that point, you
don't have to make the decision before determining the parameters of the
fitted model.

> 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).
>
> With my exponential equation, the calibration is very simple. I move to
> the reference point (xr=800) and measure Pr. The curve passes always
> throug the reference point (xr, Pr) even changing the second constant a
> (the exponent).
> So I can move to a second reference point correPPsponding to a low power
> level and calibrate the constant a, without worrying about the first
> reference point.

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.

>>> The best would be to have a simple formula (polynomial function?) that approximate well enough the above equation at run-time, without too big tables.

>>
>> I agree.

>
> I'll try to find two good series to calculate exp() and log().

Taking that approach builds into your model the assumption that you've
got a power law: In the absence of a good reason to believe that the
real curve is a power law (and you haven't given any), it's simpler just
to fit your actual data to a polynomial. You've got tight constraints on
the execution time and the available space for evaluating your model;
you don't want to make inefficient use of your limited time and memory
by biasing your fit in favor of a power law for which you can give no
theoretical justification.

pozz
Guest
Posts: n/a

 04-23-2013
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.

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.

Eric Sosman
Guest
Posts: n/a

 04-23-2013
On 4/23/2013 4:27 AM, (E-Mail Removed) wrote:
> On Monday, April 22, 2013 5:23:51 PM UTC+2, (E-Mail Removed) wrote:
>> Any suggestion to avoid pow() function call?

>
> 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
>
> where a is in the range 1-3.

The first thing that occurs to me is to use a simpler
parameterization, something along the lines of

P = k(x) * x^2

.... where k(x) is (most likely) an interpolation in a smallish
table. The variability of k seems a better match for your
suspicion "maybe some non-linear behavior" than fiddling around
with the exponent.

--
Eric Sosman
(E-Mail Removed)d

James Kuyper
Guest
Posts: n/a

 04-23-2013
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.

pozz
Guest
Posts: n/a

 04-24-2013
On Apr 23, 11:35*pm, glen herrmannsfeldt <(E-Mail Removed)>
wrote:
>
> Well, what we still don't know is exactly, or even approximately,
> how accurate the resutls need to be.

The result shouldn't be very accurate. Consider that the result is
always
an integer, so I loose many significant digit.

For example, if Pr=1000W I'd like to have an integer power even with
5W steps.

> For a reasonable range of values, log and exp can be done in fixed
> point, and likely good enough for you, but we don't know that.
>
> The algoritms in "Metafont: The Program" do computation on fixed
> point values with 15 bits before and 16 bits after the binary point.
> They should run much faster than software emulation of floating
> point, and maybe fast enough for you.
>
> (Metafont was originally in Pascal. It is more often now run as
> a machine translation to C of the Pascal source.)
>
> -- glen

pozz
Guest
Posts: n/a

 04-24-2013
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.

pozz
Guest
Posts: n/a

 04-24-2013
On Apr 23, 11:54*pm, James Kuyper <(E-Mail Removed)> wrote:
> On 04/23/2013 05:20 PM, pozz wrote:
>
>
>
>
>
>
>
>
>
> > Il 23/04/2013 15:19, James Kuyper ha scritto:
> >> On 04/23/2013 04:27 AM, (E-Mail Removed) wrote:

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

>
> Depending upon what your data looks like, I'd recommend a cubic term,
> and possibly a constant one as well. If you use a package to fit your
> data to a polynomial, and one of the terms has a coefficient that is not
> statistically significant, you can always drop it at that point, you
> don't have to make the decision before determining the parameters of the
> fitted model.
>
> > 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).

>
> > With my exponential equation, the calibration is very simple. *I moveto
> > the reference point (xr=800) and measure Pr. *The curve passes always
> > throug the reference point (xr, Pr) even changing the second constant a
> > (the exponent).
> > So I can move to a second reference point correPPsponding to a low power
> > level and calibrate the constant a, without worrying about the first
> > reference point.

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

> >>> The best would be to have a simple formula (polynomial function?) that approximate well enough the above equation at run-time, without too big tables.

>
> >> I agree.

>
> > I'll try to find two good series to calculate exp() and log().

>
> Taking that approach builds into your model the assumption that you've
> got a power law: In the absence of a good reason to believe that the
> real curve is a power law (and you haven't given any), it's simpler just
> to fit your actual data to a polynomial. You've got tight constraints on
> the execution time and the available space for evaluating your model;
> you don't want to make inefficient use of your limited time and memory
> by biasing your fit in favor of a power law for which you can give no
> theoretical justification.

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

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.

pozz
Guest
Posts: n/a

 04-24-2013
On Apr 24, 12:31*am, Eric Sosman <(E-Mail Removed)>
wrote:
>
> * * *The first thing that occurs to me is to use a simpler
> parameterization, something along the lines of
>
> * * * * P = k(x) * x^2
>
> ... where k(x) is (most likely) an interpolation in a smallish
> table. *The variability of k seems a better match for your
> suspicion "maybe some non-linear behavior" than fiddling around
> with the exponent.

How can I calibrate this "smallish table"? I would need to make many
measurements for each machine on the field.

If I write your function as

P = Pr * k(x) * x^2

where x is the ADC points normalized to 800 (reference value).

It should be nice if k(x) would be a function that values 1 when x=0
and x=1, and in between change smoothly, with the maximum at the
center
(x=0.5). This maximum correction at the center (about +/-30%) should
be the second parameter to calibrate in a simple way with a second
measurement, without changing the calibration of Pr (made during the
first measurement).

BartC
Guest
Posts: n/a

 04-24-2013
<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On Monday, April 22, 2013 5:23:51 PM UTC+2, (E-Mail Removed) wrote:

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

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

Low input impedance of the ADC? It would be interesting to find out why
that's happening, although fixing it in hardware would involve extra costs,
and it sounds like many of these units already exist!

However if I had to solve that in software, on a processor where 2KB of
memory is a big deal, then I would stay well away from double-precision
floating point arithmetic, especially involving powers, logarithms and
anti-logarithms. You've also said elsewhere that you only need accuracy of
0.5% (5W in 1000W), so integer/fixed-point methods could be viable.

--
Bartc