LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Basic Spectra in Levenberg Marquardt

Dear Labview enthousiast,
 
To fit data to a non linear formula with basic spectra in it, I am trying to use the Levenberg-Marquardt fit routine. Up till now I have written out all the sub-vi's of the example exponential LM fit routine, so that i was able to easily add more input nodes. My approach was to treat the basic spectra as independent variables, like x (that happens to be the wavelength in my case). However, the fitting doesn't work, even with very good guess coefficients.
I can imagine that there are some of you out there that have already tackled this problem, or can supply me with some useful thoughts on this.
 
Kind regards,
Olav Kaspers 
 
0 Kudos
Message 1 of 14
(4,305 Views)
I think your program design is highly flawed, because you lost all modularity of the original code. The formula node of your model function occurs in at least 8 (!) different places on the block diagram, meaning you have to make identical edits just to change the formula. There is a reason for subVIs, and this is it!
 
True, the stock lev-mar is a bit flawed, because the subVIs need to be modified in order to change the function, but there are plenty of modification available that avoid this. One possibility is to call the model function via reference node. Do a search for Levenberg-Marquardt and you'll find quite a few examples posted by me, even multidemensional fits.
 
For example, have a look at the demo code posted here for some ideas. Just substitute your model. In my experience, numerical partial derivatives work just fine and don't really impose a speed penalty. It is often not worth the effort to get all the analytical derivatives.
 
What is "Hb" in your formula?
 
I've never had any problems with exponential fits, even multiexponential as long as the data is good. Could you provide me with some real raw data? I'll give it a spin. 🙂
 
 
Message 2 of 14
(4,293 Views)

Hi mr. Altenbach,

 

Thank you for your quick response to my question, although I don't think it solves my problem. I looked at the file you referred to and think that this method will not work, just as mine doesn't. Elsewhere on the forum I found messages in which you stated something about having the fitfunction operate at the whole dataset at the same time, and I think something like this should be done here. Up till now I tried to put my basic inputspectra on the same foot as the independent variable x, which is linearly increasing and so not degenerated: every datapoint corresponds to a unique x-value. I know i'm just guessing now, but thats all i can do at the moment, because I didn't run through the LM fit routine in full detail (yet). But since fitting a simple spectrum A to a dataset that is linearly derived from A does not give a good fit (like y = a0*[A]) it seems reasonable that there's something wrong here.

 

Its very kind of you that you want to look at my data. lIl provide you with a dataset [D], a basic spectrum [A] and a wavelength axis [W], as well as a fitformula y(d(w),a(w)). Hope you'll find it an easy problem, please don't spend too much time.

 

Kind regards,

Olav Kaspers
0 Kudos
Message 3 of 14
(4,284 Views)
Olav,
 
I got your e-mail. Some confusion here:
 
In the attached VI, the formula is as follows:
y= a0*x*exp((Hb-x)/(a1*x))+a2;
 
In the zip file, the formula however is:
R = (wavelength)^(-b) * exp(-C*concentration(x)/Concentration(Basis)*[BasisSpectrum])
 
Sorry, I don't see any similarity, but maybe I am missing something obvious. Do you have e.g. a reference or a link to a website that describes the mathematical model in more detail?
 
Message 4 of 14
(4,279 Views)

Again

Sorry,

I was just playing with the program I made and filled in some formula to fit very simple data that were just generated by myself. The formulas in the program are irrelevant. When you want to know more, then search for DPS and Amelink, but I cannot imagine that the information you have now is insufficient.

Kind regards,

Olav

 

0 Kudos
Message 5 of 14
(4,270 Views)

Sorry,

I was just playing with the program I made and filled in some formula to fit very simple data that were just generated by myself. The data I send you as well as the formula in the text doc are original. The formulas in the program are irrelevant. When you want to know more, then search for DPS and Amelink, but I cannot imagine that the information you have now is insufficient.

Kind regards,

Olav

Message 6 of 14
(4,270 Views)

Hi mr. Altenbach,

It has been a while since I have been working on the problem with the Levenberg-Marquardt fitting. After my last post I have tried several ideas that didn't work out right. After some laboratory work it still seems significant to use a different fit-approach than we were used to do. Especially a non-linear fit of the scattering component in our backscattering signal would probably be beneficial. The levenberg-marquardt routine I finally came up with could only fit a very simple function and was very instable. I see that you have not replied to my last response. If you got stuck somewhere, can you please tell me your findings up to now? Otherwise I will look in another direction. Provided that you are still interested in the problem, I could also give you any information you want directly if you think this is helpful.

Kind regards,

Olav Kaspers 

 

0 Kudos
Message 7 of 14
(4,231 Views)

Sorry, I haven't studied this further. Let me try to find your old e-mail.

If you have newer example code or better sample data, please send it to me. (I might not have time to look at it until next week, though).

Message 8 of 14
(4,223 Views)
Hi mr. Altenbach,

As you might remember, I tried to add extra nodes to the formula boxes of the standard Levenberg-Marquardt fit routine – using the exponential LM fit example as a basis. When I look at your code, this certainly does not seem to be the right way to do it, but it is in full analogy to the way we did this in the old linear fit-routine, which I also added to this mail.
(linearfit.vi)

I’ve written down the core problem in a word file, which is also attached.
(coreproblem.doc)

I tidied up my old LM program that actually (mis)fits, so to some extent my approach worked.
(OlavLM.vi)

I also looked at your 2D fit code which actually looked very nice. Although the basic description of the dataset might be 2D, it seems to me that you are calling the LM routine to fit a line of points chosen from your 2D field, which basically makes it 1D. I don’t see how to implement my fit-problem into it. Moreover, our data do not have a 2D nature, it’s just a simple curve - with bumps and holes that is. The point is that we want to add a Matrix of blood absorbtion values for each wavelength (x) and possibly other basisspectra as well.

I hope this states my question a bit more clearly and that you can find some way to help me out!

Kind regards,

Olav
0 Kudos
Message 9 of 14
(4,167 Views)
Olav,
 
Sorry, I cannot troubleshoot your VIs because I cannot deal with diagrams that cover many screens, backwards wires, overlapping wires, heavily stacked sequence structures, and duplicate code everywhere (e.g. you have the identical formula node in 8 different places on the same diagram). 😞
 
Instead, I adapted my LM fit to your simple problem (y= a0*Hb+a1*(x-300)+a2), which would also be equally easy to fit with the general LS liner fit routines. I show how you can transfer the basis spectrum to the subVI by overloading the X input. It converges very quickly to a stable set of parameters, independently of the initial guesses. There is something wrong with your implementation. It seems like the fit (and a1) is exaclty 2x of what it should be.
 
Simply extract the contents of the attached zip file to an empty folder and run it. Changing the initial guess recalculates the spectrum from the guess values. Pressing [fit], fits the spectrum.)
 
It would be easy to adapt it to your more complex model described in the word document, but the problem does not quite make sense to me:
  • Are a, b, c, d constants or fitting parameters? (you call them constants!) If they are constants, what are reasonable values?
  • What are the fitting paramters? What are reasonable starting values?
  • What is the correct value for tau?

Notice, that changing to the more complex formula starting with the attached code would only involve very little work:

  • Change the size of the initial estimate array to the correct number of parameters
  • Change the diagram constant with the parameter names accordingly
  • replace the model subVI.
  • Voila! 😄

Shouldn't take more than 5 minutes! 😉

Message 10 of 14
(4,161 Views)