Origin Version (Select Help-->About Origin): OriginPro 7.5 SR6 v7.5885 (B885)
Operating System: Win XP SP2
Whenever I run this (quadratic_loop_correction()), approximately the first half of the y-values for each data set get deleted from the worksheet. Why does this happen? As far as I can tell, I'm only reading data from the worksheet, not trying to change it anywhere.
Probably doesn't matter much what I intend the code to do, in terms of answering my question. However, in case it helps someone to understand what's going on here, I run this code on a plot of hysteresis loops. Since they are loops, the x-values of the curves don't change monotonically - they start at large x ("top" of the loop), go to small x ("bottom" of the loop), and then back up to large x. I divide the loop into segments along the x-axis, and perform a linear fit on both the top segment and the bottom segment. The slope of these fits should be the same; I use the difference between the slopes to calculate the coefficient for a quadratic factor I can use to correct the data.
int seg_midpt_index(int inSeg, int iL, int iU, int iNSeg)
{
//inSeg is the number of the segment of interest (starting from 1)
//iL is lower index for entire range
//iU is upper index for entire range
//iNSeg is the total number of segments the range is divided into
//
//this is set up so integer division only occurs once, so
//rounding errors don't compound
return (iNSeg*(2*iL -1) + (2*inSeg-1) * (iU-iL+1)) / (2*iNSeg);
}
bool quadratic_loop_correction(int iSeg)//number of segments into which
{ //to divide loop
GraphLayer gl = Project.ActiveLayer();
if(!gl)
{
out_str("Active layer is not a graph.");
return false;
}
foreach(DataPlot dp in gl.DataPlots)
{
Curve crvLoop(dp);
double arrCoeffS2a[2];
double arrCoeffS2b[2];
double arrCoeffS1a[2];
double arrCoeffS1b[2];
//linear fits for top of hysteresis loop
fitpoly(crvLoop, 1, &arrCoeffS2a, 1, 2*iSeg);
fitpoly(crvLoop, 1, &arrCoeffS2b, 2*iSeg, 2*iSeg);
//linear fits for bottom of hysteresis loop
fitpoly(crvLoop, 1, &arrCoeffS1a, iSeg, 2*iSeg);
fitpoly(crvLoop, 1, &arrCoeffS1b, iSeg+1, 2*iSeg);
//find average slopes
double dSlope1 = (arrCoeffS1a[1] + arrCoeffS1b[1]) / 2.;
double dSlope2 = (arrCoeffS2a[1] + arrCoeffS2b[1]) / 2.;
//find x values from segment midpoints
Dataset dsX;
crvLoop.AttachX(dsX);//attach dataset to X part of curve
if(!dsX) return false; //return if that didn't work
int iLowerXIndex = dsX.GetLowerIndex();
int iUpperXIndex = dsX.GetUpperIndex();
int iXIndexMidSeg2a = seg_midpt_index(1, iLowerXIndex, iUpperXIndex, 2*iSeg);
int iXIndexMidSeg2b = seg_midpt_index(1, iLowerXIndex, iUpperXIndex, 2*iSeg);
int iXIndexMidSeg1a = seg_midpt_index(iSeg, iLowerXIndex, iUpperXIndex, 2*iSeg);
int iXIndexMidSeg1b = seg_midpt_index(iSeg+1, iLowerXIndex, iUpperXIndex, 2*iSeg);
double dXAvg = (fabs(Curve_x(&crvLoop,iXIndexMidSeg2a)) + fabs(Curve_x(&crvLoop,iXIndexMidSeg2b))
+ fabs(Curve_x(&crvLoop,iXIndexMidSeg1a)) + fabs(Curve_x(&crvLoop,iXIndexMidSeg1b))) / 4.;
//calculate value of coefficient for quadratic correction
double dQuadCoeff = (dSlope2 - dSlope1) / (4. * dXAvg);
out_double("quadratic coefficient is ", dQuadCoeff);
}
return true;
}