Batch curve fitting and muti-peak fitting

Dear everybody,

I have a problem to do curve fitting peak fitting for a typical peak from Raman spectrum. I want to fit the peak with 2 Gaussian functions, that is y=y0 + y1*exp(-((x-x1)/w1)^2)+y2*exp(-((x-x2)/w2)^2).

I use multi-peak fit 2.0 to deal with single data manually, and then use the batch curve fitting to fit the other datas using the result from the multi-peak fit 2.0 as initial guess. I can easily get the fitting results in terms of the parameters y0, (peak1) y1,x1,w1, (peak2)y2,x2,w2. However I want to know the parameters of the Guass peak, i.e the location, Height, Area, FWHM of peak1 and peak2, which is not shown in the results. How to fix this?

Thanks you.
It's actually quite difficult to do in a general way. Here is a modified version of the MultiPeakBatchFit() example function with computation of the "derived" parameters included. Note the added code in an else branch at the end of the function, starting with                         SetDataFolder $( saveDF+BatchDFName)
// Function designed to take as input the name of a Multi-peak Fit 2
// set folder found in root:Packages:MultiPeakFit2:, along with a
// list of input data waves. This function loops through the list using
// the coefficient waves found in the data folder as the initial guesses
// for each of the data waves found in ywavelist, xwavelist.
Function MultiPeakBatchFit(datafolderName, ywavelist, xwavelist)
    String datafolderName, ywavelist, xwavelist
   
    Variable nfits = ItemsInList(ywavelist)
   
    // the structure required by MPF2_DoMPFit()
    STRUCT MPFitInfoStruct MPStruct

    Variable i, err

    // switch to the Multi-peak fit folder and look up some stuff
    String saveDF = GetDataFolder(1)
    SetDataFolder datafolderName
        SVAR SavedFunctionTypes
        MPStruct.ListOfFunctions = SavedFunctionTypes
        MPStruct.ListOfCWaveNames = ""
        MPStruct.ListOfHoldStrings = ""
       
        Wave/Z cw = 'Baseline Coefs'
        if (WaveExists(cw))
            MPStruct.ListOfCWaveNames = NameOfWave(cw)
        endif
        MPStruct.ListOfCWaveNames += ";"
       
        Variable npeaks = 0
        npeaks = 0
        do
            Wave/Z cw = $("Peak "+num2istr(npeaks)+" Coefs")
            if (!WaveExists(cw))
                break
            endif
            MPStruct.ListOfCWaveNames += NameOfWave(cw)+";"
            npeaks += 1
        while (1)
    SetDataFolder saveDF
   
    // fill in more members of the structure
    MPStruct.NPeaks = npeaks
    MPStruct.XPointRangeBegin = 0
    MPStruct.FitCurvePoints = 1000
    MPStruct.fitOptions = 0
   
    // loop through each of the data sets, calling MPF2_DoMPFit
    // on each one. After each call, the results are checked and acted
    // on appropriately.
    for (i = 0; i < nfits; i += 1)
        // We make a copy of the Multi-peak fit folder for
        // each data set. That also duplicates all the coefficient waves
        // so that we preserve the results for each fit.
        String BatchDFName = "BatchPeakFit_"+num2str(i)
        DuplicateDataFolder $datafolderName, $BatchDFName
       
        // Most of the contents of the structure don't need to change.
        // Naturally, we have to put in wave reference to the
        // data set being fit this time through the loop
        Wave MPStruct.yWave = $StringFromList(i, ywavelist)
        Wave/Z MPStruct.xWave = $StringFromList(i, xwavelist)
        // just in case the new wave has a different number of points
        MPStruct.XPointRangeEnd = numpnts(MPStruct.yWave)-1
       
        err = MPF2_DoMPFit(MPStruct, saveDF+BatchDFName+":")
        if (err)
            // error return from MPF2_DoMPFit generally indicates
            // a programmer error
            DoAlert 0, "Error calling MPF2_DoMPFit: "+num2str(err)
            return err
        endif
        if (MPStruct.fitQuitReason == 2)
            // if the user aborts a fit, we assume that the whole process
            // should be aborted
            DoAlert 0, "User aborted batch fit"
            return -1
        endif
        if (MPStruct.fitError)
            // Something went wrong with the current fit, and it
            // failed to converge. We note this fact to the user via
            // an alert, then move on to the next one. You may wish
            // to do something more sophisticated than this.
           
            // Avoid a long line by concatenating the message
            // in small pieces
            String alertMsg = "Error doing fit to "
            alertMsg += StringFromList(i, ywavelist)+": "
            alertMsg += num2str(MPStruct.fitError)
            alertMsg += "; continuing with next fit."
            DoAlert 0, alertMsg
        else
            // fit was successful. Now compute the extra "derived" parameters.
            SetDataFolder $( saveDF+BatchDFName)        // we will work inside the data folder for the successful fit
            Variable j
            // The covariance matrix for the fit is required for computing errors for the derived parameters. MPF2_DoMPFit tells FuncFit to save the
            // covariance matrix, so it should exist here.
            Wave M_Covar
            // The covariance matrix is in one big lump. To get the particular values we need, we need to know how many overall fit coefficients
            // there are. So here we find out the name of the baseline coefficient wave (if any) and record the number of baseline coefficients.
            String BLName = StringFromList(0, MPStruct.ListOfCWaveNames)
            Wave/Z blw = $BLName
            Variable totalParams = WaveExists(blw) ? numpnts(blw) : 0
            // Now we loop through each peak computing derived parameters for each.
            for (j = 0; j < MPStruct.NPeaks; j += 1)
                // The coefficient wave from the fit
                Wave cWave = $(StringFromList(j+1, MPStruct.ListOfCWaveNames))
                // and the name of the peak function used for this peak
                String functionName = StringFromList (j+1, MPStruct.ListOfFunctions)
               
                // We need the info func for this peak type to get the information needed to call the derived parameter function.
                // MPF2_FuncInfoTemplate is defined by Multipeak Fit 2.
                FUNCREF MPF2_FuncInfoTemplate infoFunc=$(functionName+PEAK_INFO_SUFFIX)
                // Now we can all the info func to get the names of the derived parameters (and the length of the list of names
                // also tells us how many there are)
                String ParamNames = infoFunc(PeakFuncInfo_DerivedParamNames)
                // This gets the name of the derived parameter function so that we can make a FUNCREF allowing us to call it.
                // MPF2_ParamFuncTemplate is defined by Multipeak Fit 2.
                String derivedParamFunc = infoFunc(PeakFuncInfo_ParameterFunc)
                FUNCREF MPF2_ParamFuncTemplate paramFunc=$derivedParamFunc
                // Now make a wave to receive the derived parameters. It has two columns- one for the value and one for the standard deviation.
                Variable numDerivedParams = ItemsInList(ParamNames)
                Make/D/N=(numDerivedParams, 2) $(NameOfWave(cWave)+"DER")/WAVE=dw
                dw = NaN
                // As a help to the person who has to look at this, we put the names of the derived parameters as dimension labels. They can be
                // viewed in a table by selecting the Edit Dimension Labels and Data Columns option in the New Table dialog.
                Variable k
                Variable ncoefs = numpnts(cWave)
                for (k = 0; k < numDerivedParams; k += 1)
                    SetDimLabel 0, k, $(StringFromList(k, ParamNames)),dw
                endfor
                // Extract the part of the covariance matrix that applies to the particular peak fit we're working on here
                Make/D/N=(ncoefs, ncoefs)/FREE tempCovar
                tempCovar[][] = M_covar[totalParams+p][totalParams+q]
                // Finally! We can call the function to get the derived parameters
                paramFunc(cWave, tempCovar, dw)
               
                totalParams += ncoefs
            endfor
            SetDataFolder saveDF
        endif
    endfor
end


John Weeks
WaveMetrics, Inc.
support@wavemetrics.com