LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

least squares

Hello,
 
With LabVIEW 8, I acquire 4096 points (analog inputs). I would like to fit the curve obtained (which is NON LINEAR) by the least square method. How could I do that please ?
 
Thank you very much in advance !
0 Kudos
Message 1 of 11
(4,669 Views)
Would you like to fit a straight line to non linear data or some nth degree polynomial?  If you know the general trend of your data and are able to decide which degree polynomial to fit to, you would reduce the computational time greatly.  Otherwise you can fit a linear, quadratic, cubic, etc line and compare the erorrs and select the one with the smallest error.  Doing it this way for a lot of data points is going to take a very long time.  Also, I assume you have your data in two arrays(X and Y values)?
0 Kudos
Message 2 of 11
(4,653 Views)
Thank you very much for trying to help me.
 
Indeed I have my data in two arrays (or I can easily do that : X for time and Y for voltage).
 
I would like to fit a Nth degree polynomial (least square method) to non linear data. There is an example which will give you an idea of what I would like to do : General LS Fitting.vi (LabVIEW 8.2 example), but the problem is that I don't know how I could extract the H(X,i) parameter (from my acquired data, I assume it is not possible ?) which is needed by General LS Linear Fit.vi.
 
Maybe I should rather use NonLinear Curve Fit.vi to perform the fitting but I don't understand how to use it.
 
Or maybe I should use General Polynomial Fit.vi ? Should it work with it ?
0 Kudos
Message 3 of 11
(4,647 Views)
I don't have 8.2 so i cannot look at the example you mentioned.  What does your data represent physically?  Is there a general trend in your data?  If you know you are dealing with exponential decay or growth I would recomend the Exponential Fit.  If the data is parabolic I would recomend either a quadtratic or quartic fit.  If you have no idea what the data trends are going to be, you can just use a General Polynomial Fit.  You will have to pass it your X & Y arrays of data, along with a polynomial order.  You can run a loop for the order, starting from one and going up to a preset number like 10 and store the MSE value (Error) into an array then run another loop finding the smallest value of the MSE and use those coefficients.  Or you can use a shift register to compare the previous MSE value to the current one and once the difference becomes small (data is converging) end the loop and grab your coefficients.  If you include a VI with your data I can make a simple VI to demonstrate this.  Please save the VI as v8.0 or earlier.
0 Kudos
Message 4 of 11
(4,636 Views)
I understand, so I'll use either an Exponential fit or a General Polynomial Fit (for the work I do one of those two solutions should be suitable). Physically my data represent Pressure = function (Time). But will it be OK if I use my voltage values (representing pressure) for Y array and my time values (0, 20E-6, 40E-6,...etc) for X array ?
 
I started developing my application without the DAQ board, and I'm still waiting this board. So I can't include a VI with MY data. But can you make a simple VI to demonstrate this. Even if the data you use for your example are not the ones I will have to process, it will make me understand the principle : you can show me with 10 values in X and Y arrays for instance (for time : 0, 20E-6, 40E-6,...180E-6 in X array, and for voltage : 0, 0.1, 0.5, 1, 2, 5, 8,12, 20, 21 in Y array).
 
Thank you for your help, I appreciate it.
0 Kudos
Message 5 of 11
(4,631 Views)
If exponential and polynomial fit are both suitable, you are probably not looking to extract parameters, but just want to generate a smooth curve through the data points. Is this right?


@krsone wrote:
But will it be OK if I use my voltage values (representing pressure) for Y array and my time values (0, 20E-6, 40E-6,...etc) for X array ?

Why would that not be OK? Have you tried? 😄
 
Please attach a sample VI containing typical data arrays in diagram constants.
0 Kudos
Message 6 of 11
(4,623 Views)
Attached is a VI with a very simple case using your numbers.  You can see the array of X, Y, Fitted Data and the coefficients for the 4th order polynomial.  Hope this helps.
0 Kudos
Message 7 of 11
(4,609 Views)

Exactly ! I just need to generate a curve through data points ! And now it's done, I should have tried with a simple example earlier (for those who could have the same problems than me, see my stupid attached VI) instead of fighting against my "complicated" code. But thank you for your help !

I have just one more question. Is there any way to use exponential fit with positive AND negative values in Y array ?

0 Kudos
Message 8 of 11
(4,600 Views)
Thank you acolunga, I had not seen your VI before my previous post ! (sh.. !   🙂   )
0 Kudos
Message 9 of 11
(4,599 Views)
I noticed your solution goes up to a 10th degree polynomial which is the same amt of data sets you have and so... you result with 0 error.  This is not practical for the amount of data you mentioned earlier.  Your final solution will have some error.  You need to decide how big/small of an error is appropriate for your solution and stop the loop then.  You can do that a number of different ways.  You can compare the previous error to the current error using shift registers and check for convergence (probably the best way) or you can just specify an error value and stop it when the returned error is less than that.  Attached is a modified version of your VI with the 2nd solution.
0 Kudos
Message 10 of 11
(4,588 Views)