The Origin Forum
File Exchange
Try Origin for Free
The Origin Forum
Home | Profile | Register | Active Topics | Members | Search | FAQ | Send File to Tech support
Username:
Password:
Save Password
Forgot your Password? | Admin Options

 All Forums
 Origin Forum
 Origin Forum
 Double-Exponential + IRF convolution fit
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic

HAYAO

Japan
10 Posts

Posted - 04/05/2017 :  6:44:04 PM  Show Profile  Edit Topic  Reply with Quote  View user's IP address  Delete Topic
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
}

yuki_wu

896 Posts

Posted - 04/06/2017 :  01:23:38 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi HAYAO,

I think you’d better try to compile the fitting function after you define the function in Code Builder. Compiling the code can help you to find out the error in your code firstly. Then, you should try to evaluate the fitting function as the following page, so that you can do a quick check to see whether the function is appropriate.
http://www.originlab.com/doc/Origin-Help/UserDef-FitFunc

Regards,
Yuki
OriginLab
Go to Top of Page

HAYAO

Japan
10 Posts

Posted - 04/20/2017 :  06:04:43 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi Yuki,

Thanks, I'll try that. I'll come back if it doesn't go well.
Go to Top of Page
  Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
The Origin Forum © 2020 Originlab Corporation Go To Top Of Page
Snitz Forums 2000