OriginPro 2017 Ver.9.4 (Academic):
Operating System: Windows 10 (64-bit)
Dear all, I saw tutorial on how to fit with convolution here:
http://www.originlab.com/doc/Tutorials/Fitting-Convolution
However, it seems like the code used here is for Single-Exponential fitting with IRF. I need double-exponential fitting with IRF, so I modified the code provided in the tutorial. I want you guys to check to see if the following code looks right.
Thank you,
HAYAO
 
- Function Name: FitConv
 
- Function Type: User-Defined
 
- Independent Variables: x
 
- Dependent Variables: y 
 
- Parameter Names: y0, A1, A2, t1, t2
 
- Function Form: Origin C
 
Function:
#pragma warning(error : 15618)
#include <origin.h>
// Header files need to be included
#include <ONLSF.H>
#include <fft_utils.h>
//
// 
void _nlsfTestConv(
// Fit Parameter(s):
double y0, double A1, double A2, double t1, double t2,
// Independent Variable(s):
double x,
// Dependent Variable(s):
double& y)
{
	// Beginning of editable part	
	NLFitContext *pCtxt = Project.GetNLFitContext();
        Worksheet wks;
        DataRange dr;
        int c1,c2;
        dr = pCtxt->GetSourceDataRange(); //Get the source data range
        dr.GetRange(wks, c1, c2);  //Get the source data worksheet
	if ( pCtxt )
	{	
		// Vector for the output signal in each iteration.
		static vector vSignal;
		// If parameters were updated, we will recalculate the convolution result.
		BOOL bIsNewParamValues = pCtxt->IsNewParamValues();
		if ( bIsNewParamValues )
		{
			// Read sampling and response data from worksheet.
			Dataset dsSampling(wks, 0);
			Dataset dsResponse(wks, 2);
			int iSize = dsSampling.GetSize();
 
			vector vResponse, vSample;
 
			vResponse = dsResponse;
			vSample = dsSampling;
 
			vSignal.SetSize(iSize);
			vResponse.SetSize(iSize);
			vSample.SetSize(iSize);
 
			// Compute the exponential decay curve
			vSignal = A1 * exp( -t1*vSample ) + A2 * exp( -t2*vSample );
			// Perform convolution
			int iRet = fft_fft_convolution(iSize, vSignal, vResponse);
                        //Correct the convolution by multiplying the sampling interval
                        vSignal = (vSample[1]-vSample[0])*vSignal;
 
 
		}
 
		NLSFCURRINFO    stCurrInfo;
		pCtxt->GetFitCurrInfo(&stCurrInfo);
		// Get the data index for the iteration
		int nCurrentIndex = stCurrInfo.nCurrDataIndex;
		// Get the evaluated y value
		y = vSignal[nCurrentIndex] + y0;
		// For compile the function, since we haven't use x here.
		x;
	}
	// End of editable part
}