Electronics > Projects, Designs, and Technical Stuff
Curve fitting question
T3sl4co1l:
You'll need a quite high polynomial to fit that lumpy mess.
Also, is it actually lumpy, or is it truly noisy? Is this an ADC input transfer function, by any chance? Typically many-bits code crossings exhibit spikes, for which you could use a curve to minimize peak error, but not really get the bulk of it without just using a lookup table on the whole damn thing.
A procedural function could produce blips around specific breakpoints, without costing too much execution time or data space, but may cost a fair amount of code itself.
Most embedded platforms don't have div or float (many don't have mul either!), which makes anything but plain polynomial functions awkward.
Rational functions can fit spiky data very well indeed for a given order, but require at least p+q multiplies and one division (for rationals of the form A / B, for polynomials A and B, of order p and q respectively), assuming you don't run into problems with numerical range and stability, or other optimization options, that may suggest another factorization (sums of terms p/q + r/s + ..).
Tim
AlcidePiR2:
As with Lagrange interpolation, large degree polynomial is a very bad idea.
The problem of this plot is that most probably a large component is noise. You dont want to interpolate precisely noise. On the contrary, you want to use the least degree possible, to make the correction robust.
Without knowing better the setting, it is difficult to give precise advice, but I would dump the two extreme part of the curve, and conserve only the [10:28] data points, where there is an obvious trend.
The just fit a line by quadratic regression.
One get cor = -0.663145 + 0.001981 * V
The two attached plots are (V, error) and (V, error-cor).
Then only you wonder if you want to add additionnal corrections for the extreme parts
AlcidePiR2:
Looking more closely to the data, I see the problem.
The plotted error is the relative error. This is bad, as for very small values it is meaningless.
One should plot the absolute errors.
Then it appears that for large values, there was probably a reading problem on the scale. I would then discard these values.
A quadratic fit is then very adequate (see the plot) . The plot is with the absolute error.
The fit is F = - 0.005786 * V + 1.199683E-5 * V^2
The last curve is ERR-F vs V.
I believe that the 5 last points are bad measures.
Berni:
I say don't bother with curve fitting and just use a lookup table with interpolation.
Curve fitting works great when your graph is a smooth curve all the way (for example NTK temperature sensor corrections) as such a curve can be made into a polynomial with only 2 to 4 elements. While storing a precise lookup table would require something like 100 points. Speed of lookup is about the same on both, but the polynomial uses less memory. However when your curve is so erratic it would take a awfully long polynomial to aproximate it to a point where the two basically look the same. This ruins the advantage of polynomials as now you need to store a long list of elements in memory anyway while calculating the polynomials becomes significantly slower than a lookup table (And as you found curve fitting becomes hard at this point).
The trick to having a lookup table work just as well is in interpolation. For a smooth and dense graph you can use simple linear interpolation between points that's fast to compute too. If the points are too far apart then a bit of extra math can be added to do cubic interpolation. This looks at the data points around the point of interest to make a smooth spline trough them. This is computationally a little more expensive (About 6 multiplies and 10 sums) but still very fast.
Here is a nice page on interpolation: http://paulbourke.net/miscellaneous/interpolation/
AlcidePiR2:
--- Quote from: Berni on March 08, 2019, 07:06:25 am ---I say don't bother with curve fitting and just use a lookup table with interpolation.
--- End quote ---
Berni, I dont agree with you. You need first to distinguish what part is due to the moise. You dont want to interpolate or use look out table for noise. It is pointless.
In the given example. I bet the level of noise is about 0.1 %.
Navigation
[0] Message Index
[#] Next page
[*] Previous page
Go to full version