Question about "Fitting Sums of Fit Functions" (FuncFit)

I would like to fit a spectrum with a sum of several waves. Until the actual fit, I can also read in different numbers of waves and pass them on to the actual user function that is to perform the fit.
For this I would actually pass the fit as a string to the FuncFit, but unfortunately I don't quite understand the requirements that the string must fulfil in the end.
If I were to display this fit normally, it would look like this, for example:

Threadsafe function f_fit_function_name(w,x) : FitFunc
    Wave w
    Variable x
    wave wave0=path
    wave wave1=path
    return w[0]*wave0(x)+w[1]*wave1(x)+w[2]

And be called using this function:

FuncFit/Q f_fit_function_name wave[v_start,v_end] /D /C={M_FitConstraint,W_FitConstraint}

If I have understood correctly, I can also trigger such a function via a string:

FuncFit/Q {string=fitSpecStr} wave[v_start,v_end] /D /C={M_FitConstraint,W_FitConstraint}

There is something about it in this chapter in the manual:

DisplayHelpTopic "FuncFit" //Fitting Sums of Fit Functions

The function could be represented like this:

{{func1, coef1, keyword =value },{func2, coef2, keyword =value }, ...}

Or the expression can be entered via a string:

{string = fitSpecStr}
with fitSpecStr = "{func1, coef1, keyword =value },{func2, coef2, keyword =value }, ..."

But I'm not really getting anywhere here. I can leave out the keywords, but the comma has to stay? Or does the statement apply to all other commas?

"{wave0, w[0],},{wave1, w[1],},{wave2, w[2],}"
"{wave0, w[0]},{wave1, w[1]},{wave2, w[2]}"

Can "func" be a wave? Or do I have to define the wave as a wave?

"{wave wave0, w[0]},{wave wave1, w[1]},{wave wave2, w[2]}"

Do I have to specify the x-dependency?

"{wave0(x), w[0]},{wave1(x), w[1]},{wave2(x), w[2]}"

Is coef the same as w[n] or is it something like W_coef[n]?

"{wave0(x), W_Coef[0]},{wave1(x), W_Coef[1]},{wave2(x), W_Coef[2]}"

How can I represent an offset as a function? Simply a "1"?

"{wave0(x), W_Coef[0]},{wave1(x), W_Coef[1]},{1, W_Coef[2]}"

I have tried various things, but always get the same error:

error: expected fitting function

And directly another question: Is a FuncFit based on a string Threadsafe?

I know the questions are very specific, but still hope someone can help me.

Can you save a global wave reference wave filled with references to the waves to be fit?

Then you invoke the fit function with an appropriately dimensioned coefficient wave, the fit function looks up the wave reference wave, and loops though the waves adding together values at x, each multiplied by a fit coefficient.


So you mean a multidimensional reference wave where each column contains one reference wave?

Threadsafe function f_fit_function_name(w,x) : FitFunc
    Wave w
    Variable x
    wave reference=path
    variable index
    string s_return
    for (index=0;index<(dimsize(reference,1));index+=1)
    return $s_return

That would be a good solution, but how do I assemble a return in this case? I can't set the loop around a return, isn't is? (The frist return of the loop will end the function.) Igor doesn't want to translate a string like in my example. Probably this problem is super simple, but in the DisplayHelpTopic "return" I could not find anything about the form in which the expression has to be passed. Any idea?


i mean something like this

function fitFunc(wave w_coef, variable x)
    wave /WAVE refs = pathToWaveRefsWave
    variable i
    variable total = w_coef[numpnts(w_coef)-1]
        wave w = refs[i]
        total += w_coef[i] * w(x)
    return total

see  also

DisplayHelpTopic "Wave Reference Waves"

Note: I understood from your question that you want to perform a fit like the one in the first function in your post, but with an arbitrary selection of waves rather than wave0 and wave1. Maybe that wasn't what you meant?

So there was my error of thought. Then I will implement it with this much simpler solution. Thank you!

Back from vacation...

I will note that the Sum of Fit Functions facility is for a different purpose. It was created originally with Multipeak Fit in mind; each of the summed fit functions represents a single peak or a background function. The string is used to list all the fit functions and coefficient waves required.

If you wanted to make your life very complicated, you could write a fit function that computes the multiplier for one of your reference waves, and then use the sum of fit functions feature to add them together. But that would be difficult to set up, and probably much slower. The WAVE wave is a much better solution.