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 for Programming
 Forum for Origin C
 Fitting with convolution of two functions
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic

PNPI_man

Russia
2 Posts

Posted - 01/08/2014 :  09:30:14 AM  Show Profile  Edit Topic  Reply with Quote  View user's IP address  Delete Topic
Origin Ver. and Service Release (Select Help-->About Origin): 9.0 SR2
Operating System: Windows 7
Hello!
I have a question about Fitting with Convolution of Two Functions topic from Origin Wiki.
As I understand, in its code we use data boundaries set manually (I mean string
double dx = 0.05;
vX.Data(-16.0, 16.0, dx);
nSize = vX.GetSize();
)
Can I get rid of this? I mean, it would be much more comfortable to use range from my experimental data than to make correction in code each time I have new data.
Can I actually do this and how?
Thank you.


Edited by - PNPI_man on 01/08/2014 09:35:34 AM

Penn

China
644 Posts

Posted - 01/09/2014 :  03:45:32 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi,

You can try to calculate these values from the experimental data. Here is example code, you can change it accordingly.

double dx;
Layer lay = Project.ActiveLayer();  // Get the active layer, here will be graph layer
if(lay.IsValid())  // Valid layer
{
Page pg = lay.GetPage();  // Get the page of the layer
if(EXIST_PLOT == pg.GetType())  // Test if the page is graph
{
	GraphLayer gl(lay);  // Convert to graph layer
	DataPlot dp = gl.DataPlots(0);  // Get the first data plot from the graph layer
	XYRange xy;  // XY range of the data plot
	Column colX;  // X column of the data plot
	dp.GetDataRange(xy);  // Get XY range of the data plot
	xy.GetXColumn(colX);  // Get the x column of the data plot
	vector& vv = colX.GetDataObject();  // Get X column data
	vector vDiff;
	vv.Difference(vDiff);  // Get the difference of the X column
	double dSum;
	vDiff.Sum(dSum);  // Sum of the differences
	dx = dSum/vDiff.GetSize()/10.0;  // Calculate dx
	double dMin, dMax;
	vv.GetMinMax(dMin, dMax);  // Get min and max of X
	dMax = (abs(dMin) > abs(dMax)) ? abs(dMin) : abs(dMax);  // Get the max of abs of min and max
	vX.Data(-dMax-dx, dMax+dx, dx);  // Set data for X vector
	nSize = vX.GetSize();  // Set size
}
}


Penn
Go to Top of Page

PNPI_man

Russia
2 Posts

Posted - 01/28/2014 :  02:51:54 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Thnak you, Penn!
You data input procedure works so fine.
But I've new problem to deal with. I've wrote small Origin function,which should make fitting of scattering experiment data taking into account for non-monochromacy of a beam, considered to be gaussian function. My problem is that function compiles in Origin C without, but when executed in Origin, it calculates nothing.

I think problem may be in Origin templates, I have no idea about

#pragma numlittype(push, TRUE)
#pragma numlittype(push, TRUE)
#pragma numlittype(push, TRUE)
#pragma numlittype(push, TRUE)
#pragma numlittype(push, TRUE)
#pragma numlittype(push, TRUE)
#pragma numlittype(push, TRUE)
#pragma numlittype(push, TRUE)
#pragma numlittype(push, TRUE)
#pragma numlittype(push, TRUE)
#pragma numlittype(push, TRUE)
#pragma numlittype(push, TRUE)
#pragma numlittype(push, TRUE)
#pragma numlittype(push, TRUE)
#pragma numlittype(push, TRUE)
#pragma numlittype(push, TRUE)
#pragma numlittype(push, TRUE)
#pragma numlittype(push, TRUE)
#pragma numlittype(push, TRUE)
#pragma numlittype(push, TRUE)
#pragma warning(error : 15618)
#include <origin.h>
#include <ONLSF.H>
#include <fft_utils.h>

// Add your special include files here.
// For example, if you want to fit with functions from the NAG library,
// add the header file for the NAG functions here.

// Add code here for other Origin C functions that you want to define in this file,
// and access in your fitting function.

// You can access C functions defined in other files, if those files are loaded and compiled
// in your workspace, and the functions have been prototyped in a header file that you have
// included above.

// You can access NLSF object methods and properties directly in your function code.

// You should follow C-language syntax in defining your function.
// For instance, if your parameter name is P1, you cannot use p1 in your function code.
// When using fractions, remember that integer division such as 1/2 is equal to 0, and not 0.5
// Use 0.5 or 1/2.0 to get the correct value.

// For more information and examples, please refer to the "User-Defined Fitting Function"
// section of the Origin Help file.

//----------------------------------------------------------
//
void _nlsfHelicalStr(
// Fit Parameter(s):
double A, double D, double h, double r, double y0, double L_rel,
// Independent Variable(s):
double x,
// Dependent Variable(s):
double& y)
{
// Beginning of editable part

NLFitContext *pCtxt = Project.GetNLFitContext();
if ( pCtxt )
{
// Vector for the output in each iteration.
static vector vX, vY;

static int nSize;

BOOL bIsNewParamValues = pCtxt->IsNewParamValues();

double dx;
int i;

Layer lay = Project.ActiveLayer(); // Get the active layer, here will be graph layer
if(lay.IsValid()) // Valid layer
{
Page pg = lay.GetPage(); // Get the page of the layer
if(EXIST_PLOT == pg.GetType()) // Test if the page is graph
{
GraphLayer gl(lay); // Convert to graph layer
DataPlot dp = gl.DataPlots(0); // Get the first data plot from the graph layer
XYRange xy; // XY range of the data plot
Column colX; // X column of the data plot
dp.GetDataRange(xy); // Get XY range of the data plot
xy.GetXColumn(colX); // Get the x column of the data plot
vector& vv = colX.GetDataObject(); // Get X column data
vector vDiff;
vv.Difference(vDiff); // Get the difference of the X column
double dSum;
vDiff.Sum(dSum); // Sum of the differences
dx = dSum/vDiff.GetSize()/10.0; // Calculate dx
double dMin, dMax;
vv.GetMinMax(dMin, dMax); // Get min and max of X
dMax = (abs(dMin) > abs(dMax)) ? abs(dMin) : abs(dMax); // Get the max of abs of min and max
vX.Data(-dMax-dx, dMax+dx, dx); // Set data for X vector
nSize = vX.GetSize(); // Set size


if ( bIsNewParamValues )
{
//Sampling Interval

vector vF, vG, vTerm1, vTerm2, vBase, vAddBase, vBessel1, vBessel0, vS1, vX_rel;
int dx_c;
double S0;
for (i=0; i<=nSize; i++)
{
if(vX[i]>2*3.1415927/(h))
{
vBessel1[i]=(sin(vX[i]*r)-vX[i]*r*cos(vX[i]*r))/((vX[i]*r)^3)*Jn((vX[i]*D)/2*(1-((2*3.1415927)/(h*vX[i]))^2)^0.5,1);
}
else
{
vBessel1[i]=0;
}
}
vBessel0=(sin(vX*r)-vX*r*cos(vX*r))/((vX*r)^3)*Jn((vX*D)/2,0);

//Function f(x)
vF = 1/vX*((vBessel0)^2+2*(vBessel1)^2);

//Function g(x)
// vG = 1/(vS*sqrt(pi/2))*exp(-0.5*(vX-1)^2/vS^2);

// Non-monochromasity
S0 = L_rel*1/(2*sqrt(2*ln(2)));


//Calculate dx_c
dx_c = 4*S0/nSize;
vX_rel.Data(1-2*S0+dx_c/2, 1+2*S0-dx_c, dx_c);

vG = exp(-0.5*(vX_rel-1)^2/S0^2);

//Pad zeroes at the end of f and g before convolution
vector vA(2*nSize-1), vB(2*nSize-1);
vA.SetSubVector( vF );
vB.SetSubVector( vG );

//Perform circular convolution
int iRet = fft_fft_convolution(2*nSize-1, vA, vB);

//Truncate the beginning and the end
vY.SetSize(nSize);
vA.GetSubVector( vY, floor(nSize/2), nSize + floor(nSize/2)-1 );

//Baseline
vBase = y0;
// vAddBase = b2 * A2/(w2*sqrt(pi/2))*exp( -2*(vX-xc2)^2/w2^2 );

//Fitted Y
vS1 = vX*L_rel*1/(2*sqrt(2*ln(2)));
vY = A/(vS1*sqrt(pi/2))*dx*vY + vBase;
}

//Interpolate y from x for the fitting data on the convolution result.
ocmath_interpolate( &x, &y, 1, vX, vY, nSize );
}
}
}
// End of editable part
}
Go to Top of Page

kfauth

Germany
33 Posts

Posted - 06/20/2016 :  03:27:40 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi there,

did you get this resolved?

Could it be that you don't assign anything to the dependent variable y?
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