Custom Fit Function using Predefined waves


So to preface my question, I have many spectra that are made up of mixtures of two different species with overlapping features. I have the spectra of each individual specie that I can sum together to recreate the mixture spectra if I apply the right mixture coefficients. Instead of doing this manually to find the mixture ratios, I wanted to write a fit function in Igor to fit each spectra and use the coefficients to find the ratio. 

I tried to use the custom fit function to write my own fit, but I find I can't seem to use the spectra waves of the two species in the fit function without error.

Example: f() = a*Sp1+b*Sp2
a=coeff of Sp1
b=coeff of Sp2
Sp1 = reference wave of species 1
Sp2 = reference wave of species 2

How could I go about remedying this?


This should do it:

Function FitMixture(w,x) : FitFunc
    Wave w
    Variable x

    //CurveFitDialog/ These comments were created by the Curve Fitting dialog. Altering them will
    //CurveFitDialog/ make the function less convenient to work with in the Curve Fitting dialog.
    //CurveFitDialog/ Equation:
    //CurveFitDialog/ WAVE sp1, sp2
    //CurveFitDialog/ f(x) = A*sp1(x) + B*sp2(x)
    //CurveFitDialog/ End of Equation
    //CurveFitDialog/ Independent Variables 1
    //CurveFitDialog/ x
    //CurveFitDialog/ Coefficients 2
    //CurveFitDialog/ w[0] = A
    //CurveFitDialog/ w[1] = B

    WAVE sp1, sp2
    Variable xx = x
    if (x < leftx(sp1))
        xx = sp1(leftx(sp1))
    elseif (x > pnt2x(sp1,numpnts(sp1)-1))
        xx = pnt2x(sp1,numpnts(sp1)-1)
    return w[0]*sp1(xx) + w[1]*sp2(xx)

Note that this function assumes that sp1 and sp2 have the X scaling set (if this is not familiar, execute this command in Igor: DisplayHelpTopic "The Waveform Model of Data").

It handles the case where the fit X range is outside the range of sp1 and sp2 by returning the value at one end or the other as appropriate. It *does* assume that sp1 and sp2 have the SAME X range.

It looks up interpolated values from sp1 and sp2 using X indexing.

Oh, and it assumes that the waves with your standard spectra are named literally "sp1" and "sp2".