Discussion:
how can you perform curve fitting to a 2d array and find the maximum?
(too old to reply)
jaysmall
2008-06-26 21:10:05 UTC
Permalink
Hello all, I am working with a 2D near field range positioner and need to calibrate its position to find the maximum S21 point(s). If you don't know what that means, it doesn't matter.What does matter is I have a 2D array in labview of floating point data which I would like to fit a continuous curve to and find the maximum point(s). Is there a VI out there that someone has developed for such a problem? Or if someone had an idea of a way to go about getting what I need I would greaty appreciate the help.Cheers
smercurio_fc
2008-06-26 21:10:06 UTC
Permalink
The only S21 I'm familiar with is the S21 for network analyzer measurements. Be that as it may, I'm suspecting your 2D array is either 2 rows or 2 columns, meaning that one row/column is the "X" data (independent variable) and the other row/column is the "Y" data (dependent variable). In that case, you just need to index out the individual rows/columns to feed to the curve fit functions:<img src="Loading Image..."> To learn more about LabVIEW it is recommended that you go through the tutorial(s) and look over the material in the NI Developer Zone's <a href="http://zone.ni.com/devzone/cda/tut/p/id/5054" target="_blank">Learning Center</a> which provides links to other materials and other tutorials. You can also take the <a href="http://www.ni.com/academic/lv_training/how_learn_lv.htm" target="_blank">online courses</a> for free. Message Edited by smercurio_fc on 06-26-2008 03:47 PM


Example_VI_BD6.png:
http://forums.ni.com/attachments/ni/170/335433/1/Example_VI_BD6.png
jaysmall
2008-06-26 21:10:08 UTC
Permalink
What I have is an 11x11 array (going to grow to some X by Y) which I'm essentially trying to turn into a 3D graph using the data in the array as the dependent variable. Once I have a continuous multivariable graph I would like to find the maximums in it. I alpologize, but I am unsure what you placed in the above example.
smercurio_fc
2008-06-26 21:10:08 UTC
Permalink
What I placed above was the Index Array and the polynomial curve fit VI. As I indicated, it would probably be good for you to spend some time with the LabVIEW tutorials.It seems that you have 3D data. That's different. A curve fit is for 2D graphs, not 3D graphs (unless you're looking at slices). Can you provide a sample of your data, and what should be found?
jaysmall
2008-06-27 13:10:10 UTC
Permalink
A file of some data is attached. Looking at the data is similar to looking at the 2d positioner we have. Columns represent changes in the X axis, rows represent changes in the Y axis. What I'm doing is moving the positioner a discrete distance and taking a measurement. After I would like to fit a continuous curve to it and find the maximum so I can move the positioner to its (X,Y) coordinate.


Amplitude.txt:
http://forums.ni.com/attachments/ni/170/335640/1/Amplitude.txt
smercurio_fc
2008-06-27 15:10:12 UTC
Permalink
I plotted your data, and this is what I got:<img src="Loading Image..."> What am I looking at, and what are you looking for?Message Edited by smercurio_fc on 06-27-2008 10:06 AM


3D fit_FP.png:
http://forums.ni.com/attachments/ni/170/335691/1/3D fit_FP.png
jaysmall
2008-06-27 16:10:06 UTC
Permalink
This is just a file of some S11 parameters out of a network analyzer (measuring nothing useful) for test purposes. What I will be doing in the end is measuring S21 parameters out of a network analyzer looking for the local maxima which will designate where an antenna patch is placed (or at least that's the plan). So basically I am writing this as a scan and locate antenna patches function. I start off with a descretized scan of the 5 ft by 5 ft area taking measurements at each point, write the data into a spreadsheet type file, and I'm hoping i can extrapolate local maxima of what would be a continuous function.
smercurio_fc
2008-06-27 17:10:10 UTC
Permalink
I haven't played around with it too much, but you may find the example given <a href="http://zone.ni.com/devzone/cda/epd/p/id/470" target="_blank">here</a> useful as a starting point. It is an example to do a polynomial fit for 3D data. The example will provide you with the coefficients for the polynomial fit.
altenbach
2008-06-27 17:10:12 UTC
Permalink
First you need a mathematical model to describe your data.
&nbsp;
If you just want to fit to a 2D polynomial (e.g. Z=&nbsp;&nbsp;A x +&nbsp;&nbsp;By + Cx^2 + Dxy + Ey^2 ...) You could do something like here:
&nbsp;
<a href="http://forums.ni.com/ni/board/message?board.id=170&amp;view=by_date_ascending&amp;message.id=270976#M270976" target="_blank">http://forums.ni.com/ni/board/message?board.id=170&amp;view=by_date_ascending&amp;message.id=270976#M270976</a>
&nbsp;
(Since your xy data forms a regular grid, it is even easier)
&nbsp;
&nbsp;
If your model is more complex, (e.g. a 2D gaussian over a 2D polynomial background), You can use Levenberg Marquardt. I made a simple demo for NI week, see picture:
&nbsp;
<img src="Loading Image..."> What is your LabVIEW version?Message Edited by altenbach on 06-27-2008 09:52 AM


2Dfit-NI-Week.png:
http://forums.ni.com/attachments/ni/170/335730/1/2Dfit-NI-Week.png
jaysmall
2008-06-27 17:10:14 UTC
Permalink
Wow, that looks precisely like what I was looking for. Thank you. I'm using LabVIEW 8.5, just started using it last week, but it's very intuitive, and am picking it up fairly easily.
smercurio_fc
2008-06-27 17:10:14 UTC
Permalink
Where did you have that example tucked away, altenbach?&nbsp; :smileyvery-happy: While trying to provide a response I had come across <a href="http://forums.ni.com/ni/board/message?board.id=170&amp;message.id=82900#M82900" target="_blank">this other example</a> you had, which looks like an early version of this one. However, it, like this one, requires a mathematical model, and my assumption (right or wrong) was that the user didn't have one so I didn't mention it. Instead, I opted to suggest just the raw polynomial curve fit. Still, I should have suggested that example regardless in case the user did have a mathematical model. Thanks for pointing it out.
altenbach
2008-06-27 17:40:14 UTC
Permalink
smercurio_fc wrote: Where did you have that example tucked away, altenbach?&nbsp;:smileyvery-happy:


I submitted it to NI, so it should be available somewhere as part of the NI-week extras. I was just looking at it and found that there is a problem, possibly with the 3D graph activex component and I get a crash under some conditions. I want to research&nbsp;this first&nbsp;before posting here again.
&nbsp;
&nbsp;
If you look at the other link I posted (<a href="http://forums.ni.com/attachments/ni/170/270976/2/3DPoly_Newdata.llb" target="_blank">3DPoly_Newdata.llb</a>), it does a plain 2D polynomial of arbitrary order (See picture).
&nbsp;
<img src="Loading Image...">
jaysmall
2008-06-27 18:10:04 UTC
Permalink
Just to make sure I understand... Take a string of my X positions separated by spaces, and put that into the phi input. Same with Y pos into alpha, and associated measurements into the yaw, and I should be able to get the mathematical model displayed for me?
altenbach
2008-06-27 18:40:04 UTC
Permalink
Well, all you need is three 1D DBL arrays of equal lenght (x, y, z) and substitute for the three 1D arrays that come out of the index array connected to the "select x, y, z" controls. The rest should fall into place. (e.g. delete the two "index array", turn CurrentX, CurrentY, CurrentZ into controls. and wire in your array data in some way.
&nbsp;
There is currently al lof of extra code to&nbsp;be able to select any of 4 choices for any of the 3&nbsp;axes. (bug alert: there&nbsp;are even a few wires missing!, e.g. going to the idexing of the axis names).
&nbsp;
Anyway,&nbsp;&nbsp;you don't need all that!
&nbsp;
As I said, since your x and y form a regular grid, things could be simplified further. All you need is X0, dx, Y0, dy, and a 2D Y array. This would require some reworking though. ;)
jaysmall
2008-06-27 18:40:05 UTC
Permalink
Yeah I think what I'll end up doing is reworking my own code to make 3 files. One to write the current X position to, one to write the current y position to, and one for the measured value. Then it should be easy enough to wire those directly to the three inputs. Thank you very much for your help.
jaysmall
2008-06-27 21:10:06 UTC
Permalink
I also need a great amount of accuracy with the measurement inputs, though it only looks like it can handle to two decimal points. I've looked through your code, but cannot find where it makes this cutoff. Any idea where this might be happening?
altenbach
2008-06-27 21:10:08 UTC
Permalink
The calculations is done in full DBL&nbsp;precision. If you want to see more, right-click the indicators and change the dislpay format to show more digits.
&nbsp;
jaysmall
2008-06-27 21:40:06 UTC
Permalink
Ah, thank you.The algorithm also seems to be calculating beyond the number of points that I want it to. I attached a picture so you can see what I mean.Beyond the domain of the information I'm inputting, it is still calculating best fit. So I'm guessing that it thinks I'm also inputting a value of (x y z) = (0 0 0), which kind of stinks...


3D_Curve_Fitter.png:
Loading Image...
altenbach
2008-06-27 22:10:06 UTC
Permalink
jaysmall wrote:
Beyond the domain of the information I'm inputting, it is still calculating best fit. So I'm guessing that it thinks I'm also inputting a value of (x y z) = (0 0 0), which kind of stinks...

You actually are, look at the arrays. You need to remove the elements that are zeroes and truncate the arrays. Where are the zeroes coming from? Do you have extra empty lines in your datafile perhaps?Message Edited by altenbach on 06-27-2008 02:45 PM
jaysmall
2008-06-27 22:10:08 UTC
Permalink
I'm not putting those in, the string I pass it ends. But if you scoll down on the front page to the end of your inputs, and then continue scrolling, you can see that there is an endless string of inputs of (x y z) = (0 0 0) Hopefully if I can get this one, it will be the end of my issues since everything else seems to be working. Then it's on to find the local maximas...
altenbach
2008-06-27 22:40:07 UTC
Permalink
Please attach yor VI and some data.
&nbsp;
&nbsp;The array ends when the elements turn grey, so you seem to have real zeroes in there. I am sure there is an easy explanation where they are coming from....
jaysmall
2008-06-30 13:10:05 UTC
Permalink
I found the problem. When I was writing the measurements to the file, I would enter the measurement then add a space to prepare for the lat measurement. When I was done writing to the file, I was left with a space after my last entry, which was seen by the VI as an entry of (0 0 0). I simply cleaned up the file and was done with it. Now... I have to figure out how I'm going to search for local maxima on this graph.
Loading...