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.