EmbeddedRelated.com
Forums
The 2024 Embedded Online Conference

Optimize pow() function

Started by pozz September 22, 2016
pozz <pozzugno@gmail.com> writes:
> I found changing exponent is good in my application, because I'm sure > it is exact at three points (0, Pn/2, Pn) and good at other points.
I'm real skeptical, and anyway the exponent should vary at most over a small range, like 1.98 to 2.02, not 1 to 3. In that small range you can approximate as e=2 and make a small correction with a term or two of the infinite series for exp.
Il 22/09/2016 23:50, Don Y ha scritto:
> On 9/22/2016 2:10 PM, pozz wrote: > >> > No, the "problem" lies in your transducer/measurement error. >> > The definition of a "watt" (power) doesn't change just because your >> > observations of that APPARENT relationship (V ~ W) "has issues". >> > >> > Perhaps your load's characteristics are changing as a function >> > of operating point. Or, nonlinearities in your measurement system. >> > Or, ... >> > >> > Ask yourself WHY the exponent needs to be changed. I suggest you >> > don't have an accurate model of your load and its excitation. >> > Until you do, how will you know that this is the CORRECT means of >> > compensating your model to agree with your observations? >> >> I know the problem is in the poor quality of the transducer, but I'm >> only in >> charge of the software and the hardware engineer can't fix it (often the >> software solves hardware issues). >> >> So I need a more complex law. The quadratic law (one parameter) can be >> calibrated with a single measure (nominal value). The generic power law >> (exponent not exactly equals to 2, two parameters) needs two calibration >> measures and has a nice feature: it's very simple to calibrate in two >> steps. > > Backup. > > You're calibrating because, presumably, you need *some* degree of accuracy. > How (formally) has that been specified? Percent of reading? Percent of > range? etc. > > This tells you how "good" your model of the load must represent the > ACTUAL load, as viewed from the standpoint of your *single* measurement > device. So far, you appear to only be considering fitting a curve > to two points: > - 0V (= 0 power!) > - Vmax (= some particular power) > > Yet, you seem to care about the values between 0 and Vmax. > > How *much* do you care about them? If you are only operating at the > 0 & Vmax points on the curve, then you don't need to fit an equation; > just store: > (0V, 0W) > and > (Vmax, Pmax) > > [Obviously, this is not the case!]
Yes, I already made those considerations and I already tested my mathemaical model (exponentiation) against the correct measures made by a professional instrument. I found exponentiation (after calibration) is good for the accuracy I need in the application.
>> The input section has an hardware calibration, a simple digital trimmer. >> So the first step is to calculate the power from the measured voltage >> with the >> following formula: >> P = Pn / (Vn ^ 2) * (V ^ 2) >> where Pn is the nominal power and Vn is the voltage needed at nominal >> power. > > But, you aren't *controlling* any of these things (?) rather, just > *sensing* them (i.e., someone tells you "this is a load of X" while > you examine a sensed parameter -- voltage -- to correlate with that > declaration).
The operator enters nominal power Pn (depending on the machine under test). The voltage needed at nominal power Vn is fixed (somewhat at 80% of the ADC input range, 0-4.096V = 3276 points). During first step calibration, the operator actually changes the trimmer position until the input voltage to the ADC is Vn (the ADC conversion value is 3276 points).
>> The user increase/decrease the trimmer pot until the correct power is >> displayed. > > What is your *load*? If purely resistive, does it have a temperature > coefficient? (e.g., if you left it operating at Pmax, would you see > changes) > > In operation, do you expect the load to be "static"? Or, time varying? > Does the load's impedance change based on how *it* is driven? etc.
I don't care the load. I'm only working on the power transducer. If the load changes (because of temperature, degradation, ...), the output signal of transducer will change accordingly and the power reading will change too.
>> Second step. Change the power to another value (half of nominal power, >> for >> example). If the transducer works as in theory, the measured power is >> correct. >> Otherwise the user increase/decrease the exponent until the power >> displayed is >> correct. The power is calculated as: >> P = Pn / (Vn ^ exp) * (V ^ exp) >> >> After the second step, we are sure the first calibration point is yet >> valid, >> because that formula is correct when V=Vn, even if exp isn't 2. >> >> If I use the linear law P=AV^2+BV, I think the two calibration steps >> aren't >> indipendent: the operator should execute two measures, the system >> should save >> the two couples (P,V) and only after that calculate A and B coefficients. >> >> It seems to me the exponentiation law is simpler during calibration. > > Your concern should be that of how *accurate* the proposed calibration > will be, in the context of the specifications governing that accuracy. > Unless you know how your load behaves, how can you begin to analyze how > well your model (equation) will fit its observed behavior? > > Someone (customer) will apply your device and complain that the accuracy > AT THEIR PARTICULAR OPERATING POINT sucks. Regardless of how accurate it > is at "0" and "Pmax" (and P/2). > > Understand the load, first. This gives you an idea of what *style* of > model you should be using. Then, design the calibration procedure to > exploit that model as appropriate.
I think I'm making a measurement device. It is independent from the load. I only have to calibrate my measurement device, that is my transducer. A voltage meter designer doesn't need to know the load, he only needs to test the transducer at different voltage levels. For my accuracy (that isn't so high), a small change in the square shape law is ok. I thought about exponentiation, because it is simple to calibrate.
> E.g., if you have a linear relationship, you know you just need to address > offset and span. Higher order models can require more tweaking ESPECIALLY > IF THE MODEL IS JUST AN APPROXIMATION to the actual behavior. You need > to know > where the errors are likely to lie -- both in the circuit's performance > AND the model's idea of how it "should" be behaving. > > Lastly, do yourself (your company) a BIG favor and let the device calibrate > itself (esp at run-time, if possible) *or* have a jig that automates the > process at Final Test -- it will pay for itself overnight! > > [E.g., connect device to harness/fixture; invoke magic incantation to > start calibration procedure; device TELLS fixture to apply LOAD1; device > measures V1; device tells fixture to apply LOAD2; device measures V2; > lather, rinse, repeat; device figures out coefficients to fit model > to observations as good as possible (using some criteria for "good") > and stores calibration parameters -- OR, sounds alarm indicating > calibration not possible (FAIL)]
I don't need a complex model with many parameters to calibrate. I think P = AX^2+BX could be ok too as P = AV^exp
On 22/09/16 17:01, Tim Wescott wrote:

> As already suggested, the first thing to do is determine what's wrong > with your measurement and fix it.
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.
> > 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. >
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.
On 9/23/2016 12:23 AM, pozz wrote:
> I don't need a complex model with many parameters to calibrate. I think > P = AX^2+BX > could be ok too as > P = AV^exp
Then just implement it and you're done!
On 22/09/16 23:15, pozz wrote:
> Il 22/09/2016 17:01, Tim Wescott ha scritto: >> [...] >> 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. > > Yes, this is a lighter calculation for the MCU, but the calibration > would be much more difficult to the operator.
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 :-)
> > Please, take a look at my answer to Don Y for additional details on my > system: I have a digital trimmer. > >
David Brown <david.brown@hesbynett.no> writes:
> 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.
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.
On 23/09/16 09:47, Paul Rubin wrote:
> David Brown <david.brown@hesbynett.no> writes: >> 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. > > 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. >
Certainly, if you can get a better model of the system (including its errors) then that's a good idea.
On 23.09.2016 10:23, pozz wrote:
> Il 22/09/2016 23:50, Don Y ha scritto: >> On 9/22/2016 2:10 PM, pozz wrote: >> >>> > No, the "problem" lies in your transducer/measurement error. >>> > The definition of a "watt" (power) doesn't change just because your >>> > observations of that APPARENT relationship (V ~ W) "has issues". >>> > >>> > Perhaps your load's characteristics are changing as a function >>> > of operating point. Or, nonlinearities in your measurement system. >>> > Or, ... >>> > >>> > Ask yourself WHY the exponent needs to be changed. I suggest you >>> > don't have an accurate model of your load and its excitation. >>> > Until you do, how will you know that this is the CORRECT means of >>> > compensating your model to agree with your observations? >>> >>> I know the problem is in the poor quality of the transducer, but I'm >>> only in >>> charge of the software and the hardware engineer can't fix it (often the >>> software solves hardware issues). >>> >>> So I need a more complex law. The quadratic law (one parameter) can be >>> calibrated with a single measure (nominal value). The generic power law >>> (exponent not exactly equals to 2, two parameters) needs two calibration >>> measures and has a nice feature: it's very simple to calibrate in two >>> steps. >> >> Backup. >> >> You're calibrating because, presumably, you need *some* degree of >> accuracy. >> How (formally) has that been specified? Percent of reading? Percent of >> range? etc. >> >> This tells you how "good" your model of the load must represent the >> ACTUAL load, as viewed from the standpoint of your *single* measurement >> device. So far, you appear to only be considering fitting a curve >> to two points: >> - 0V (= 0 power!) >> - Vmax (= some particular power) >> >> Yet, you seem to care about the values between 0 and Vmax. >> >> How *much* do you care about them? If you are only operating at the >> 0 & Vmax points on the curve, then you don't need to fit an equation; >> just store: >> (0V, 0W) >> and >> (Vmax, Pmax) >> >> [Obviously, this is not the case!] > > Yes, I already made those considerations and I already tested my > mathemaical model (exponentiation) against the correct measures made by > a professional instrument. I found exponentiation (after calibration) > is good for the accuracy I need in the application. > > >>> The input section has an hardware calibration, a simple digital trimmer. >>> So the first step is to calculate the power from the measured voltage >>> with the >>> following formula: >>> P = Pn / (Vn ^ 2) * (V ^ 2) >>> where Pn is the nominal power and Vn is the voltage needed at nominal >>> power. >> >> But, you aren't *controlling* any of these things (?) rather, just >> *sensing* them (i.e., someone tells you "this is a load of X" while >> you examine a sensed parameter -- voltage -- to correlate with that >> declaration). > > The operator enters nominal power Pn (depending on the machine under > test). The voltage needed at nominal power Vn is fixed (somewhat at 80% > of the ADC input range, 0-4.096V = 3276 points). > During first step calibration, the operator actually changes the trimmer > position until the input voltage to the ADC is Vn (the ADC conversion > value is 3276 points). > > >>> The user increase/decrease the trimmer pot until the correct power is >>> displayed. >> >> What is your *load*? If purely resistive, does it have a temperature >> coefficient? (e.g., if you left it operating at Pmax, would you see >> changes) >> >> In operation, do you expect the load to be "static"? Or, time varying? >> Does the load's impedance change based on how *it* is driven? etc. > > I don't care the load. I'm only working on the power transducer. If > the load changes (because of temperature, degradation, ...), the output > signal of transducer will change accordingly and the power reading will > change too. > > >>> Second step. Change the power to another value (half of nominal power, >>> for >>> example). If the transducer works as in theory, the measured power is >>> correct. >>> Otherwise the user increase/decrease the exponent until the power >>> displayed is >>> correct. The power is calculated as: >>> P = Pn / (Vn ^ exp) * (V ^ exp) >>> >>> After the second step, we are sure the first calibration point is yet >>> valid, >>> because that formula is correct when V=Vn, even if exp isn't 2. >>> >>> If I use the linear law P=AV^2+BV, I think the two calibration steps >>> aren't >>> indipendent: the operator should execute two measures, the system >>> should save >>> the two couples (P,V) and only after that calculate A and B >>> coefficients. >>> >>> It seems to me the exponentiation law is simpler during calibration. >> >> Your concern should be that of how *accurate* the proposed calibration >> will be, in the context of the specifications governing that accuracy. >> Unless you know how your load behaves, how can you begin to analyze how >> well your model (equation) will fit its observed behavior? >> >> Someone (customer) will apply your device and complain that the accuracy >> AT THEIR PARTICULAR OPERATING POINT sucks. Regardless of how accurate it >> is at "0" and "Pmax" (and P/2). >> >> Understand the load, first. This gives you an idea of what *style* of >> model you should be using. Then, design the calibration procedure to >> exploit that model as appropriate. > > I think I'm making a measurement device. It is independent from the > load. I only have to calibrate my measurement device, that is my > transducer. >
Nope. In general case the load will influence the measurement, unless it's specifically provided for by the hardware. What I mean is the relationship doesn't have to be linear. What I would suggest you to do -- get your hardware schematics, then use it to produce a model of how does the measurement work (involving the load). In that model you would have perhaps two unknown variables (like real and imaginary parts of complex impedance of the load), so then you would know exactly how many measurements do you need to make, and how to calculate the power. HTH, Gene
On Fri, 23 Sep 2016 00:47:02 -0700, Paul Rubin wrote:

> David Brown <david.brown@hesbynett.no> writes: >> 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. > > 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.
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 I'm looking for work! See my website if you're interested http://www.wescottdesign.com
On 9/23/2016 2:56 AM, Evgeny Filatov wrote:
> Nope. In general case the load will influence the measurement, unless it's > specifically provided for by the hardware. What I mean is the relationship > doesn't have to be linear. > > What I would suggest you to do -- get your hardware schematics, then use it to > produce a model of how does the measurement work (involving the load). In that > model you would have perhaps two unknown variables (like real and imaginary > parts of complex impedance of the load), so then you would know exactly how > many measurements do you need to make, and how to calculate the power.
You are also likely to have aspects of the measurement system that contribute to errors/nonlinearities in the measurement. I.e., assuming "0 = 0" inherently assumes you can measure "0"!

The 2024 Embedded Online Conference