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
 B-spline interpolation not smooth?
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic

gschmied

USA
5 Posts

Posted - 07/05/2005 :  01:21:15 AM  Show Profile  Edit Topic  Reply with Quote  View user's IP address  Delete Topic
Origin Version (Select Help-->About Origin): OriginPro 7 SR4
Operating System: Windows 2000

I've tried both spline interpolation procedures from the "After I plot a spline curve, how can I access the spline data?" item in the knowledge base and ended up with a curve that was not smooth if you zoom in enough. Here's how I have reproducibly caused the problem (if that's what it is):

Open a new worksheet and fill column A with row numbers (30 rows or so is fine).

Fill column B with something to give the resultant graph some curvature such as col(a)^(5/3).

Follow either procedure described in the knowledge base posting above to generate a B-spline curve and interpolate it over about 3000 points covering the minimum to maximum values of col(a).

Zoom way in on the interpolated curve and you will see that it is made up of very small plateaus 10 to 20 points wide (if you set "Connect" for this curve to one of the spline modes it looks just fine). Access the interpolated data in a worksheet and you will see that the numbers creating the plateaus are identical to 10 or 12 significant figures!

For my particular application I need to take derivatives of the interpolated data so the plateaus are killing me. I also need the resolution of 3000 interpolated points (or even more). I hope I'm doing something wrong because this feature could be *very* useful for me.

Thanks for your help with this. Cheers.


gschmied

Mike Buess

USA
3037 Posts

Posted - 07/05/2005 :  06:37:21 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Spline curves can have no more than 900 points. If you really need 3000 point resolution you need to split the curve into sections.

http://www.originlab.com/forum/topic.asp?TOPIC_ID=3097

Mike Buess
Origin WebRing Member
Go to Top of Page

easwar

USA
1965 Posts

Posted - 07/05/2005 :  10:59:59 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi gschmied,

Try the Origin C function pasted below which calls NAG library to perform the spline interpolation.

Once you add this function to your workspace and compile, you can then go to the script window and type command such as:
nag_spline data1_b 3000
and a new wks will be generated with the spline values for data_b.

The NAG function also returns error code for very large numbers, but with dataset such as the one you mentioned - data1_a filled with row numbers 1 thru 30 and data1_b = col(a)^(5/3) - I could get the spline curve with 5000 points.

Note that this function assumes the X values of the data are sorted and are in ascending/increasing order.

Easwar
OriginLab.

P.S. I tested this with 7SR4


#include <OC_nag.h> // this contains all the NAG headers,
void nag_spline(string strYDataset, int nPts)
{
// Check nPts
if( nPts < 1 )
{
out_str("Number of points invalid!");
return;
}

// Declare curve using Y dataset name
Curve crvData(strYDataset);
// Create a copy of the curve
Curve crvCopy(crvData);
if( !crvCopy )
{
out_str("Failed to create curve from specified dataset name!");
return;
}

// Copy Y, X values of curve to vectors
vector vecX, vecY;
Dataset dsX;
crvCopy.AttachX(dsX);
vecY = crvCopy;
vecX = dsX;

// Set up vectors for return values from NAG function
vector vecXSpline(nPts), vecYSpline(nPts);
// Fill X vector with nPts that cover the X range of the curve
// Assume X is sorted ascending
double dMin, dMax, dStep;
int nDataSize = vecX.GetSize();
dMin = vecX[0];
dMax = vecX[nDataSize - 1];
dStep = (dMax - dMin) / (nPts - 1);
vecXSpline.Data(dMin, dMax, dStep);

// First compute spline coefficients
Nag_Spline spline;
int iRet;
if( (iRet = nag_1d_spline_interpolant(nDataSize, vecX, vecY, &spline)) != 0 )
{
printf("NAG computation of spline coefficients returned error: %d\n", iRet);
return;
}


// Now compute spline curve at each x point
double fit;
int iS = vecXSpline.GetSize();
for(int ii = 0; ii < iS; ii++)
{
if( (iRet = nag_1d_spline_evaluate(vecXSpline[ii], &fit, &spline)) != 0 )
{
printf("NAG spline computation failed at x = %f with error code: %d\n", vecXSpline[ii], iRet);
return;
}
vecYSpline[ii] = fit;
}

// Succeeded in computing spline - write results to new wks
WorksheetPage wpg;
wpg.Create("Origin");
Worksheet wks = wpg.Layers(0);
while( wks.DeleteCol(0) );
wks.AddCol("SplineX");
wks.Columns(0).SetType(OKDATAOBJ_DESIGNATION_X);
wks.AddCol("SplineY");
string strLabel;
strLabel.Format("NAG Spline Interpolation for %s", strYDataset);
wpg.Label = strLabel;
Dataset dsXSpline(wks, 0);
Dataset dsYSpline(wks, 1);
dsXSpline = vecXSpline;
dsYSpline = vecYSpline;

// Clean up
nag_free(spline.lamda);
nag_free(spline.c);
}





Edited by - easwar on 07/05/2005 11:04:13 AM
Go to Top of Page

Leo_Li

China
Posts

Posted - 07/06/2005 :  12:02:06 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
To get rid of 5000-points limitation, an extra line of code is needed just below "vecXSpline.Data(dMin, dMax, dStep);":

vecXSpline[nPts-1] = dMax;

Now, an incredible large number is allowed; the only limitation is CPU/RAM.
Go to Top of Page

gschmied

USA
5 Posts

Posted - 07/06/2005 :  01:16:21 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Dear Mike, Easwar, and Leo,

Many thanks for your quick responses!

Mike: I'm sorry to report that I can still see the problem even after interpolating onto a 200 point data set. The interpolated curve is indeed very smooth (no plateaus), but differentiating the curve shows that it is composed of straight-line segments (plateaus in the 1st derivative). In my case the point of doing the B-spline interpolation is to take a cleaner numerical derivative, though I think your solution will work for most applications.

Easwar: I'm fluent in ancient versions of BASIC and FORTRAN but I'm only just learning Origin C. Your code will jump-start my education and I have already begun working to implement it. (As an aside, I've been trying the curve_derivative and curve_bspline routines in very simple Origin C programs. This particular problem doesn't manifest, but the results are hyper-sensitive to the "smoothness parameter": they are either over smoothed or virtually un-smoothed, never in-between. If you are interested let me know and I will give you the details, but I think your proposed code is my best bet at this point.)

Leo: nPts > 5000? Very Cool!!!

Thank you all!

gschmied
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