Electronics > Projects, Designs, and Technical Stuff

4th order polynomial coefficients for pressure at temperature readings, Help!!!

<< < (6/12) > >>

itsbiodiversity:
This may be very dense and my apologies as I am new in this area of science (obviously), but in your calculations, what would the a0, a1, a2, a3, and a4 be?  I am trying to learn how to identify what those are.

Nominal Animal:
The only real difficulty here is getting the picture right.  Hopefully, this one will get closer!


--- Quote from: itsbiodiversity on August 19, 2020, 01:38:10 pm ---Did you happen to look at the spreadsheet I attached on the last post?
--- End quote ---
Yes, it opens in LibreOffice Calc just fine (don't have Windows), but it is difficult to understand what the fields mean.  Remember, we don't have the context you do.

If I've understood you correctly, you have a very high precision pressure sensor.  It has internal quartic polynomial compensation (via five coefficients) for the pressure sensor at a specific temperature, and you are looking for how to compute these coefficients for different temperatures.


Let's assume the following:

Column A, "Standard", is the known standard pressure being measured.
Column B, "Temp", is the temperature at which the measurements are made.
Column C, "Temp Counts", is the raw temperature ADC value obtained from the device.
Column D, "Reading", is the pressure reported by the device.

We can see that the temperature compensation at temperature 21 is correct, i.e. the reported pressure matches the pressure exactly.
At lower temperatures, the magnitude of the reading is too large.
At higher temperatures, the magnitude of the reading is too small.

To me, this indicates that the pressure sensor needs to be calibrated (using five coefficients) to work correctly at a given specific temperature, i.e. that the reported pressure is
$$P = P_0 + P_1 x + P_2 x^2 + P_3 x^3 + P_4 x^4$$
with the user able to supply \$P_0\$, \$P_1\$, \$P_2\$, \$P_3\$, and \$P_4\$ to the device.  It is these coefficients that depend on temperature; each set of five coefficients work at a given temperature.  For temperature 21, the coefficients are 0 1 0 0 0, i.e. no compensation: \$P = x\$.

Most importantly: We are supplying parameters to compensate the sensor behaviour at a specific temperature, not specifying the sensor behaviour at different temperatures.

Let's save the first four columns of each temperature to a text file that Gnuplot can eat. temperature-15.txt:

--- Code: ---#Standard Temp Temp Counts Reading
-15 15 5485 -15.017
-13 15 5485 -13.014
-11 15 5485 -11.011
-9 15 5485 -9.01
-7 15 5485 -7.008
-5 15 5485 -5.006
-3 15 5485 -3.004
-2 15 5485 -2.002
0 15 5485 0
2 15 5485 2.001
3 15 5485 3.002
5 15 5485 5.004
7 15 5485 7.006
9 15 5485 9.007
11 15 5485 11.008
13 15 5485 13.008
15 15 5485 15.008

--- End code ---
The first and fourth columns are the same for the temperature 21 table, so we can skip that. (It'll just give us a linear function anyway, 0 1 0 0 0).
Note that Gnuplot ignores lines beginning with a # and those lines are just comments for us humans.

temperature-28.txt:

--- Code: ---#Standard Temp Temp Counts Reading
-15 28 9471 -14.99
-13 28 9471 -12.991
-11 28 9471 -10.993
-9 28 9471 -8.994
-7 28 9471 -6.996
-5 28 9471 -4.997
-3 28 9471 -2.998
-2 28 9471 -1.999
0 28 9471 0
2 28 9471 1.999
3 28 9471 2.998
5 28 9471 4.997
7 28 9471 6.996
9 28 9471 8.996
11 28 9471 10.996
13 28 9471 12.996
15 28 9471 14.996

--- End code ---

What we want to do, is fit a fourth-degree polynomial on the fourth column, so that it yields the first column.  However, we can see that there is no zero drift: when the first column is zero, fourth column is zero also.  We can "enforce" that by using a polynomial without a constant coefficient.  In Gnuplot:

--- Code: ---P(p) = P1*p + P2*p*p + P3*p**3 + P4*p**4
fit P(x) 'temperature-15.txt' using 4:1 via P1, P2, P3, P4

--- End code ---
where we use fourth column as x , and result in first column.  This outputs

--- Code: ---iter      chisq       delta/lim  lambda   P1            P2            P3            P4           
   0 7.4442087182e+09   0.00e+00  1.04e+04    1.000000e+00   1.000000e+00   1.000000e+00   1.000000e+00
   1 2.2294220617e+07  -3.33e+07  1.04e+03    9.985765e-01   9.947791e-01   7.430715e-01   9.651151e-03
   2 3.1706096563e+04  -7.02e+07  1.04e+02    9.945782e-01   9.802179e-01   2.086389e-02  -5.007133e-03
   3 2.5079458204e+03  -1.16e+06  1.04e+01    9.946186e-01   3.960710e-01   1.535787e-05  -2.025147e-03
   4 1.1420062378e-01  -2.20e+09  1.04e+00    9.975663e-01   2.682431e-03   8.654369e-06  -1.362051e-05
   5 4.7602533798e-06  -2.40e+09  1.04e-01    9.990046e-01   1.557076e-05   7.926488e-07   1.567490e-08
   6 4.7485651793e-06  -2.46e+02  1.04e-02    9.990118e-01   1.539077e-05   7.529785e-07   1.659336e-08
   7 4.7485651793e-06  -4.80e-07  1.04e-03    9.990118e-01   1.539077e-05   7.529765e-07   1.659336e-08
iter      chisq       delta/lim  lambda   P1            P2            P3            P4           

After 7 iterations the fit converged.
final sum of squares of residuals : 4.74857e-06
rel. change during last iteration : -4.80261e-12

degrees of freedom    (FIT_NDF)                        : 13
rms of residuals      (FIT_STDFIT) = sqrt(WSSR/ndf)    : 0.000604379
variance of residuals (reduced chisquare) = WSSR/ndf   : 3.65274e-07

Final set of parameters            Asymptotic Standard Error
=======================            ==========================
P1              = 0.999012         +/- 4.097e-05    (0.004101%)
P2              = 1.53908e-05      +/- 4.78e-06     (31.06%)
P3              = 7.52977e-07      +/- 2.475e-07    (32.87%)
P4              = 1.65934e-08      +/- 2.544e-08    (153.3%)

correlation matrix of the fit parameters:
                P1     P2     P3     P4     
P1              1.000
P2              0.001  1.000
P3             -0.917 -0.002  1.000
P4             -0.001 -0.961  0.002  1.000

--- End code ---
which means that at temperature 15, the coefficients we should supply to the device are 0 0.999012 0.0000153908 0.000000752977 0.0000000165934

Running the same for temperature-28.txt outputs (shortened to include only the important bit)

--- Code: ---Final set of parameters            Asymptotic Standard Error
=======================            ==========================
P1              = 1.0006           +/- 2.148e-05    (0.002147%)
P2              = -8.84714e-06     +/- 2.51e-06     (28.37%)
P3              = -5.92832e-07     +/- 1.301e-07    (21.95%)
P4              = -2.39269e-08     +/- 1.339e-08    (55.97%)

--- End code ---
i.e. the device coefficients needed to get correct pressure readings at temperature 28 are 0 1.0006 -0.00000884714 -0.000000592832 -0.0000000239269.


The next question is, obviously, how to obtain the five coefficients for any specific temperature.  We have three sets of parameters, so we can interpolate between them (and extrapolate outside them) using quadratic interpolation:
$$\begin{aligned}
p_{15}(x) &=  0.999012 x + 0.0000153908 x^2 + 0.000000752977 x^3 + 0.0000000165934 x^4 \\
p_{21}(x) &= x \\
p_{28}(x) &= 1.0006 x - 0.00000884714 x^2 - 0.000000592832 x^3 - 0.0000000239269 x^4 \\
w_{15}(t) &= 98/13 - 49/78 t + 1/78 t^2 \\
w_{21}(t) &= -10 + 43/42 t - 1/42 t^2 \\
w_{28}(t) &= 45/13 - 36/91 t + 1/91 t^2 \\
p(x, t) &= \frac{ p_{15}(x) w_{15}(t) + p_{21}(x) w_{21}(t) + p_{28}(x) w_{28} t }{ w_{15}(t) + w_{21}(t) + w_{28}(t) }
\end{aligned}$$
where \$w_{15}(15) = 1\$, \$w_{15}(21) = 0\$, \$w_{15}(28) = 0\$; \$w_{21}(15) = 0\$, \$w_{21}(21) = 1\$, \$w_{21}(28) = 0\$; \$w_{28}(15) = 0\$, \$w_{28}(21) = 0\$, and \$w_{28}(28) = 1\$.  These fully define the nine coefficients.  It turns out that \$w_{15}(t) + w_{21}(t) + w_{28}(t) = 1\$ for all \$t\$, so we can omit the divisor.

When combining polynomials, evaluating the polynomials first and then scaling and summing the results is equivalent to scaling and summing the coefficients first, then evaluating the resulting polynomial.  So, with the above, we can immediately describe the quadratic interpolation/extrapolation of the five configuration values to the device as a function of ambient temperature \$t\$:

* First coefficient is always \$0\$ (because the above fit observed there is no zero drift)
* Second coefficient is \$0.9946289230769221 + 0.0003833040293040879 t - 0.000006073260073258604 t^2\$
* Third coefficient is \$0.00008539823846153845 - 0.000006168612014652013 t + 0.00000010009663003663 t^2\$
* Fourth coefficient is \$0.000003624177384615384 - 0.0000002384970677655678 t + 0.000000003138913919413919 t^2\$
* Fifth coefficient is \$ 0.00000004226482307692308 - 0.0000000009584721611721613 t - 0.00000000005019706959706964 t^2\$These I obtained by telling Maxima to expand the expression \$p(x, t)\$, and collecting the coefficients for each exponent of \$x\$ and \$t\$.

Note that if you had more samples, i.e. more temperatures (and perhaps at more than the 17 pressures), you could model the temperature dependence even better: this is the "best" approximation we can do, that should match the configuration data at the configuration temperatures and pressures, but makes no other assumptions.  (Even the quadratic weight polynomials \$w_{15}(t)\$, \$w_{21}(t)\$, and \$w_{28}(t)\$ are completely defined by having them be 1 at that specific temperature, and zero elsewhere.)

Nominal Animal:

--- Quote from: itsbiodiversity on August 19, 2020, 03:49:33 pm ---in your calculations, what would the a0, a1, a2, a3, and a4 be?
--- End quote ---
I assumed that you are in full control of the data conversion, obtaining raw pressure and temperature values.

In other words, I showed the model I believe the device should already have incorporated, with say 5×3 = 15 coefficients.

Instead, it looks like the device needs to be configured for a specific ambient temperature.  The pressure sensor is factory calibrated to give correct, linear results at 21°C, but the compensation needs to be adjusted for other temperatures.

My latter response shows how to calculate the five coefficients for a given temperature (in Celsius centigrade).  If you want to change that to the temperature reading (5485 at 15°C, 7386 at 25°C, and 9471 at 28°C), you need to change the weight functions.  In Maxima:

--- Code: ---w15(t) := W00 + W01*t + W02*t^2 $
w21(t) := W10 + W11*t + W12*t^2 $
w28(t) := W20 + W21*t + W22*t^2 $
solve([ w15(5485)=1, w15(7386)=0, w15(9471)=0,
        w21(5485)=0, w21(7386)=1, w21(9471)=0,
        w28(5485)=0, w28(7386)=0, w28(9471)=1 ], [ W00,W01,W02, W10,W11,W12, W20,W21,W22 ]);

--- End code ---
which yields

--- Code: ---[[W00 = 34976403/3788693, W01 = -16857/7577386, W02 = 1/7577386,
  W10 = -3463229/264239, W11 = 14956/3963585, W12 = -1/3963585,
  W20 = 1350407/277027, W21 = -12871/8310810, W22 = 1/8310810]]

--- End code ---
So, we construct those polynomials, and see what \$p(x, t)\$ looks like:

--- Code: ---w15(t) := 34976403/3788693 - 16857/7577386*t + 1/7577386*t^2 $
w21(t) := -3463229/264239 + 14956/3963585*t - 1/3963585*t^2 $
w28(t) := 1350407/277027 -12871/8310810*t + 1/8310810*t^2 $

--- End code ---
If we check expand(w15(t) + w21(t) + w28(t)); Maxima tells us the result is always 1, so we can omit the divisor.  Next, we expand \$p(x, t)\$:

--- Code: ---p15(x) := 0.999012*x + 0.0000153908*x^2 + 0.000000752977*x^3 + 0.0000000165934*x^4 $
p21(x) := x $
p28(x) := 1.0006*x - 0.00000884714*x^2 - 0.000000592832*x^3 - 0.0000000239269*x^4 $
expand( w15(t)*p15(x) + w21(t)*p21(x) + w28(t)*p28(x) );

--- End code ---
which yields

--- Code: ----6.891513009452426E-16*t^2*x^4
+1.412920651658525E-13*t*x^4
+3.655168497054565E-8*x^4
+2.803896602141317E-14*t^2*x^3
-7.569847447073744E-10*t*x^3
+4.061479723198378E-6*x^3
+9.666149779038884E-13*t^2*x^2
-2.053746129643471E-8*t*x^2
+9.895794611233592E-5*x^2
-5.8192843849845E-11*t^2*x
+1.268726552948896E-6*t*x
+0.9938037796576857*x

--- End code ---
The first coefficient given to the device is always 0, \$C_0 = 0\$, because there is no constant term (there was no zero drift in the data).  Terms for \$x\$ give the first coefficient,
$$C_1 = 0.9938037796576857 +1.268726552948896 \cdot 10^{-6} t - 5.8192843849845\cdot 10^{-11} t^2$$
Terms for \$x^2\$ give the third coefficient, and so on, with terms for \$x^4\$ giving the fifth coefficient,
$$C_4 = 3.655168497054565\cdot 10^{-8} + 1.412920651658525 \cdot 10^{-13} t - 6.891513009452426 \cdot 10^{-16} t^2$$
These use the standard scientific notation, \$\text{1.2e-3} = 1.2 \cdot 10^{-3} = 0.0012\$.

Because the device does this compensation, we cannot do the bivariate polynomial.  (Why? Odd device.  It's not like 15 multiplications and 14 additions is that much more expensive, per pressure reading, than 5 multiplications and 4 additions.)

Instead, we must find the quartic polynomial the device can use to do the compensation itself, for each temperature separately.  Pretty annoying, but scientifically quite valid, and I kinda know the reasoning why (it keeps policy in the userland, so to speak).  Here, the cross terms appear in the interpolated form above (the last one we expanded - the one with terms including powers of \$x\$ and \$t\$), and are "hidden" in the changes of the five coefficients supplied to the device.  So, as long as the numerical computation is done with more digits than are correct in the result plus sufficient digits to cover the rounding errors (1 ULP per multiplication or addition – so 4 bits or 1.2 digits per 16 multiplications or additions), this sequential fitting model is just as precise as the single bivariate one would be.  So no worries!

itsbiodiversity:
We are so so so so close.  I am going to use Ambient temperature and the calculations below.  On return to the office I will apply the coefficients and let you know the result.  Thank you!

itsbiodiversity:
For all you've done to get me this close I truly appreciate it.  I am trying to calculate these formulas provided using t=21 (is that correct).  If so I get the following:

Coefficient #   Calculated (a)                                                  Temp         
0   0             
1   0.993830397   0.99380378   1.27E-06          5.82E-11   21    FORMULA : =0.99380378 + (1.27E-06*21)-(5.82E-11*21^2) = 0.993830397
2   9.85E-05           9.90E-05           -2.05E-08          9.67E-13    21    FORMULA : =9.90E-05 + (-2.05E-08*21)+(9.67E-13*21^2) = 9.85E-05
3   4.05E-06           4.06E-06           -7.57E-10          2.80E-14      21    FORMULA : =4.06E-06 + (-7.57E-10*21)+(2.8E-14*21^2) = 4.05E-06
4   3.6555E-08   3.66E-08            1.41E-13         -6.89E-16     21    FORMULA : =3.66E-08 + (1.41E-13*21)-(6.89E-16*21^2) = 3.6555E-08

I tried entering the seqence a0 = 0, a1 = 0.993830397, a2 = 9.85E-05, a3 = 4.05E-06, and a4 = 3.6555E-08 and the pressure went offscale.  I also tried using Counts rather than degrees C and it also went offscale.  Am I misunderstanding how to calculate those formulas above?  If you're sick of helping I don't blame you!!!  I will have another unit in house soon that was programmed by them prior to leaving the organization and am interested to see what is stored.  Default is 0 0 1 0 0, and the module meets specifications across temperatures but hoping to "dial it in".  I am kind of "pigeon holed" because the inventor/programmer did the design but all information regarding their application of the statistics is gone to the wind. 

Navigation

[0] Message Index

[#] Next page

[*] Previous page

There was an error while thanking
Thanking...
Go to full version
Powered by SMFPacks Advanced Attachments Uploader Mod