Need help figuring out how to compare value against a known curve - Page 1
Forum section: electronics / beginners

author: MatCat
author_rank: Frequent Contributor
timestamp: September 09, 2018, 11:23:14 pm
content: I am working with a sensor that I have graphed and found it's curve across the points of measurement, but my problem is trying to figure this out against different calibrations. In one medium the plots will be different values then another, but it all follows the same curve. My issue is I am not sure how to programatically / mathematically take a calibration value at a known point, and use that to correlate to the known curve. Here I have a screenshot of 3 collected datasets, the orange line has data for each point, for the other 2 datasets I did not collect all points.

author: rstofer
author_rank: Super Contributor
timestamp: September 10, 2018, 01:50:14 am
content: Quadratic Regression Analysis (QRA). There are a number of calculators available via Google.Once it comes up with the equation of the line, you can plug in an 'x' and get a good fitting 'y' for anything in the domain.https://keisan.casio.com/exec/system/14059932254941You are going to have to come up with an explanation for the 3 curves or you are going to have to do 3 different QRAs and have 3 separate equations.There are higher order versions if a quadratic doesn't fit well. Search for more info with 'higher order regression analysis'http://www.xuru.org/rt/pr.asp

author: Mr. Scram
author_rank: Super Contributor
timestamp: September 10, 2018, 01:57:31 am
content: Quote from: rstofer on September 10, 2018, 01:50:14 amI just learnt a bunch. I would have chosen a sloppy interpolation approach, but this is much more elegant.Thanks!

author: Mechatrommer
author_rank: Super Contributor
timestamp: September 10, 2018, 08:40:40 am
content: Quote from: MatCat on September 09, 2018, 11:23:14 pmyour data seem to followy = (A / (x + B)) + Ccurve, try to find value for A, B and C (start with B and C = 0) that closely match your curve and use the values for interpolation purpose.

author: CatalinaWOW
author_rank: Super Contributor
timestamp: September 10, 2018, 11:41:38 am
content: Beware when fitting data to an arbitrary equation. It is easy to convince yourself that the equation, not the data, is correct.Your data seems to follow a simple curve, but may indicate different behavior for low values of the x coordinate. Either be suspicious of fitted behavior in this region, or look for reasons for changed behavior. A zener threshold crossed, or a stage saturated, or .....

author: MatCat
author_rank: Frequent Contributor
timestamp: September 10, 2018, 09:33:33 pm
content: Just to give some background on what it is, the sensor is a capacitive sensor that is detecting the level of moisture in a tank. The data points 1 to 15 are incremental measured submersion lengths, the difference between the 3 curves is blue distilled water, orange is tap water, and grey is nutriented water. Since the sensors voltage output was quite low compared to my ADC I upped the gain to get more resolution as seen in the attached curve, which is tap water as a base reference. In reality the final system will generally contain nutriented water which is closer to tap then distilled. I seem to have the resolution to do what I want it's just a matter of the variable difference in water people will use and fitting it to the curve to approximate as best as I can, at least the highest resolution is towards when the tank will be empty which is more important then when its half full or full.

author: mikerj
author_rank: Super Contributor
timestamp: September 11, 2018, 02:54:56 pm
content: A second fixed capacitive sensor at the bottom of the tank could be used to calculate the dielectric constant of the liquid. Knowing this you can correct your measurement.

author: MatCat
author_rank: Frequent Contributor
timestamp: September 25, 2018, 08:13:05 pm
content: It's been a few weeks and I still haven't figured out how to make this comparison, surely someone out there has a good code example of how this can be done.

author: rhb
author_rank: Super Contributor
timestamp: September 25, 2018, 10:51:50 pm
content: Get gnuplot. Learn to use it. Done.Seriously, fit the appropriate curves to each set of measurements and plot your heart out.The following reads two columns of input in an ASCII text file separated by spaces (the default), fits a polynomial and then plots the polynomial and the data points.a(x) = A + B*x + C*x**4fit a(x) 'data_file' using 1:2 via A,B,Cplot 'data_file' u 1:2, a(x)

author: rstofer
author_rank: Super Contributor
timestamp: September 25, 2018, 11:33:00 pm
content: Once you have the coefficients for a good fitting polynomial of some power, you will have an equation like given above. For a fourth degree polynomial it will look likey = Ax^4 + Bx^3 + Cx^2 + Dx + E - A,B,C,D & E are the coefficients you gained from the curve fit.Now, take a reading and call it 'x' and stuff it back into the equation with the appropriate coefficients and the resulting 'y' is the value you seek.You really don't want to have to form all those powers of x so you can factor and solve thusly:y = (((A*x+B)*x+C)*x+D)*x+E

author: rstofer
author_rank: Super Contributor
timestamp: September 25, 2018, 11:40:31 pm
content: Your plot looks like an exponential. About half way down this page is an example of fitting an exponential curve using Excel:http://www.engineerexcel.com/nonlinear-curve-fitting-in-excel/I haven't tried it but it looks pretty nice! It's also pretty tricky because it tries to use linear math on an exponential.Again, you get coefficients and then you use them to get a y value from a given x value.