07-22-2013 11:18 AM - edited 07-22-2013 01:55 PM
帕特里克 wrote:Before I make use of this, may I pick your brain a little? I want to make sure I understand how it works because I don't want to plagiarize your program.
There is nothing to "plagiarize" Use whatever you need for your purpose. If you use my code, place a link to this thread as a diagram comment for reference. 😄
@帕特里克 wrote:
I am mainly having trouble with your use of complex numbers which you mentioned are there to simplify coding. This is probably because I am not a computer scientist :). (I am an undergrad. Physics major.)
Complex numbers have little to do with computers, but a lot to do with physics, so take this as an opportunity to learn. In this case instead of keeping track of x and y coordinates, we can combine the two into a single complex number. Each complex number is a pair of x and y coordinates in the picture. This simplifies the distance calculation between any two complex points, because we can simply take the difference between the two complex numbers and take the absolute value. This will give us the distance which is no longer a complex number. (doing the same without complex number would involve squares and square roots. Here is the formula). For the current purpose, that's all you need to know. So, you could do the entire thing without complex numbers if you want, it would simply be messier and involve more code. 😄 It would probably also be slower....
帕特里克 wrote:This "prepare grid" section seems to be working its way through the pixels in the image and creating complex numbers out of their indices. Then this array is then resized and sent to the data terminal of the LM.vi. Why is this needed? I have noticed that when I remove this step I get the error, "The system of equations cannot be solved because the input matrix is singular."
Double-click the static control reference, and you'll see that the model VI uses the grid to calculate the function for all positions on the unit grid. Since we can only fit 1D arrays, we take all rows of the 2D array and concatenate them into one long 1D array. That's what "reshape array" does. We do the same to the 2D data too. If you omit the grid step, the model has no grid and the result is an empty array. An empty array does not have enough information to fit the data. We don't use the x-input of the nonlinear fit for the independent data, because it does not support complex arrays. The model needs to know the values, so we can tell via the more general purpose variant data input. Same difference.
@帕特里克 wrote:
I am also confused about the use of the Ramp Pattern.vi. As I understand, you are making 128 complex numbers out of the cursor position index and the ramp data, then feeding it into the data terminal of the 2DSinc2ModelComplex.vi. Then another complex number is made out of the ramp pattern array and the f(X,a) output of the sinc. Why is this needed?
.
Something similar seems to be happening in the highlighted area above the ramp section. If you could try to clarify the use of complex numbers a little I think that would help me. My searches online have not been effective.
The ramp pattern generates grid points for a single row/column at a higher frequency so the fit looks smoother. The size is unwired so it default to 128 points. Of course you can set it to any size you want. Since we only want to calculate the model for a single line, one of the coordinates is fixed at the cursor position we select. At the same time we use "index array" to get the data along a single slice of the full 2D array of the data. This is NOT needed for the fit, just as a convenient way to visualize the fit result. You could instead show the data and fit on a 3D graph, or whatever you like.
07-24-2013 01:23 AM - edited 07-24-2013 01:24 AM
Thanks, Mr. Altenbach. My 2D fits are now up and running. I will upload the final version of my program when I finish it this week.
-Patrick
07-26-2013 03:59 AM
Dear Mr. Altenbach,
I noticed that the f'(x,a) is not wired in your 2dSinc2ModelComplex.vi.
For data analysis, I want to locate the maxima/minima of my cursor profile sinc^2 fits by finding where the derivative = 0.
The Help Context says, "f'(x,a) is the Jacobian of the model function. If this is empty then the Jacobian is approximated numerically."
When I look inside this 2D array I see nothing. I am not sure how to make use of this feature. Is there any way to use this to find the derivatives of my fits?
Thanks,
-Patrick
07-26-2013 11:19 AM
This is the part of the VI I am talking about.
Below is the attached 2dSinc2ModelComplex.vi for anyone who doesn't have it.
07-26-2013 01:17 PM
You don't need to find the maximum, because you already have it from the fit. It is the "other" position parameter from the best fit (as long as you are on the peak).
The partial derivative is the change in the function with respect to each fitting parameter, and thus not suitable for your purpose. It might be worth to add explicit derivatives to the model for performance, but it won't change the outcome.
07-26-2013 10:38 PM
Altenbach, Your quick replies are always appreciated :).
Surely I have the position of the largest peak, but the troughs seem more difficult to find. I would like to identify one of the two minima on either side of the largest peak and then find its distance from the center. This would be easy if the troughs were x-intercepts, but they are not exactly equal to zero, just close to zero.
Right now I am identifying the minima as a point with an amplitude lower than a certain tolerance value (e.g. <0.01). It should be good enough for my needs. I was just hoping for a more accurate way to do this. That is why I was thinking about derivatives.
-Patrick
07-27-2013 11:59 AM
OK, I have a solution worked out. I was able to solve the problem by using the ramp pattern to slice up my sinc fit very finely (I chose 4096 samples) and then filtering the samples' amplitudes through a very strict tolerance to locate the minima. This method is as accurate as my purposes require.
One issue I ran into was the anti-aliasing of my graph being thrown off by my extremely dense data and producing a jagged, ugly fit. I worked around this by using a second ramp pattern with 256 samples just for graphing the fit without any calculations.
I have uploaded my VI and sample data if anyone is interested.
-Patrick
07-27-2013 12:17 PM
I am happy it worked for you. 😄