LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Fit Sinc Function to Data

Solved!
Go to solution

Dear community,

 

I recently wrote a program that fits a Gaussian function to some intensity data I pulled from an image of a fluorescent nanoparticle. Here's a snapshot:

 

cropXZ.jpg

 

title picture.png

 

My program would be a lot more useful if I could fit the data to a Sinc function instead of a Gaussian. Could someone please give me some points in the right direction about how to do this? I am very new to LabVIEW, so I may need a little more help than just which VI to use. I'd appreciate your help very much.

 

Please note: I need to fit all the data, not just half.

 

I am using version 9.0.1 with the Windows 7 64-bit OS.

My program is attached below.

 

Thanks!

-Patrick

0 Kudos
Message 1 of 18
(9,131 Views)

http://zone.ni.com/reference/en-XX/help/371361H-01/gmath/nonlinear_curve_fit/

 

There is a Nonlinear Curve Fit VI. I haven't used it a lot, but "sinc(x)" is a built in function. Just make sure you pay attention to X and Y inputs (Y is on top, X is beneath Y).

 

I added noise to a sinc line and then put it in as a constant. (I know there are more efficient methods for simulating and such) But this should show you essentially what you want. For instance, I'm not sure what parameters you want on the sinc fit (there could be many), but this should be able to get you started.

 

EDIT: Original is 12.0.1, so added a 9.0 copy.

Download All
Message 2 of 18
(9,110 Views)

Since you have 2D data, you might want to do a 2D fit on the while image instead.

0 Kudos
Message 3 of 18
(9,099 Views)

@Quevvy

 

Thanks so much for your help. Your program was extremely easy to follow even for a noob like me. I've got my program up and running now. There are just a couple problems that I will adress below.

 

@altenbach

 

At this point I just need a sinc fit for a cross section of the data. It would be interesting to learn how to graph in 3D at some point though.

 

A couple more questions:

  • Is there any way to make the fit plot look smoother on the graph? It is as jagged and crude looking as my data. I know the parameters are right, but I'd like to have it draw smoothly for the sake of visuals.
  • Is there anywhere to raise a trigonometric function to a power using formula strings? I would like to use sinc^2(x) but right now I am stuck using sinc(x)*sinc(x).

Thanks for the quick responses.

-Patrick

0 Kudos
Message 4 of 18
(9,077 Views)

@帕特里克 wrote:

A couple more questions:

  • Is there any way to make the fit plot look smoother on the graph? It is as jagged and crude looking as my data. I know the parameters are right, but I'd like to have it draw smoothly for the sake of visuals.
  • Is there anywhere to raise a trigonometric function to a power using formula strings? I would like to use sinc^2(x) but right now I am stuck using sinc(x)*sinc(x).

 

  • You just need to have a smaller dx in the points. For example, in my VI, I used -10 to 10 in steps of one. You could then change the x to go from -10 to 10 in steps of 0.1. The way to do this is to use the parameters that you acquired from the nonlinear fitting VI and make a formula for the sinc values. OR you could just interpolate, but why do that when you have the formula at your fingertips?
  • You can just use sinc(x)^2. No programming language that I know accepts trigonometric powers likesin^2(x).

 

[The VI is probably not the most elegant solution, but enough to get you started!]

Message 5 of 18
(9,072 Views)

@Quevvy

 

Thanks, but I can't open your VI. It is giving me a later version of LabVIEW error. I was able to open the other one.

0 Kudos
Message 6 of 18
(9,061 Views)
Solution
Accepted by 帕特里克

Here's a quick attempt to fit the entire 2D data. Seems to work just fine. (LabVIEW 9.0)

 

  1. Run VI. It will start fitting immediately.
  2. Change simulation paramters at any time to see the simulated result using the model (lower right)
  3. Once fitting completes, you can drag the yellow cursor to inspect data and fit along a vertical line.

 

The attached zip file contains the toplevel VI, the VI model VI and the picture. Extract all to a new folder and load and run the toplevel VI.

 

Since you have limited data, it probably helps to fit all data instead of only a single line. Let me know if anything is not clear. To be honest, I don't think a squared sinc is the correct model. There are no real wings in your data!

 

Download All
Message 7 of 18
(9,049 Views)

Also remember that formula models are relatively inefficient. The VI models is recommended if significant processing is involved. Here a picture of the model. Simple enough? 😄

 

(Note that the base grid is sent as a complex 1D array in the data variant to simplfy coding. The "width" units are arbitrary, scale according to your requirements)

 

0 Kudos
Message 8 of 18
(9,045 Views)

@altenbach,

 

First, thanks for taking the time to write this awesome program. It is very helpful and very cool indeed. 

 

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. The areas I do not understand are highlighted below.

 

altenbach.jpg

 

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.)

 

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."

 

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.

 

Thanks a lot, Mr. Altenbach.

-Patrick

 

 

 

 

0 Kudos
Message 9 of 18
(8,978 Views)

@帕特里克 wrote:

@Quevvy

 

Thanks, but I can't open your VI. It is giving me a later version of LabVIEW error. I was able to open the other one.


 I thought I had saved it for 9.0, but I tried it again. Let me know if this one doesn't work.

0 Kudos
Message 10 of 18
(8,973 Views)