Cubic spline - return values for every row in source data?

I have some data to which I am trying to fit a background. (y_data: 1D wave, no scaling, ~2000 rows)

I have an algorithm which I want to use to minimise, and I want to fit a spline function. The nodes will be specified by me in another wave, but can be taken to be evenly spaced in the first instance (~20 nodes).

I've tried fitting a cubic spline, and I can only make it return values at the nodes.
I've tried fitting a smoothing spline. It returns values at every value, but I don't want to smooth the data, and the fit is too far off.

How can I fit a spline through the nodes and return the value of that spline for every point in the original source data?
I can then do a sum[(source-bkg)^2], and start minimising with my algorithm to refine the node positions.

Quote:
How can I fit a spline through the nodes and return the value of that spline for every point in the original source data?


Execute this:
DisplayHelpTopic "Destination X Coordinates from Destination Wave"


The cubic spline algorithm returns a curve that goes through all of the input points. The smoothing spline is not constrained to go through all of the input points.

The cubic spline has no analytical value - it is useful only to produce a reasonably smooth curve through specific points. The smoothing spline also has no analytic value but it is better at producing a smoothed representation of the input data.
hrodstein wrote:
Execute this:
DisplayHelpTopic "Destination X Coordinates from Destination Wave"


The cubic spline algorithm returns a curve that goes through all of the input points. The smoothing spline is not constrained to go through all of the input points.

The cubic spline has no analytical value - it is useful only to produce a reasonably smooth curve through specific points. The smoothing spline also has no analytic value but it is better at producing a smoothed representation of the input data.


No.

"In this mode the number of output points is determined by the destination wave and the /N flag is ignored."

Now I've just made a cubic spline go through every point of my data, not creating a smooth curve through a small number of nodes, for which I can then get the values of that smooth curve at every original x value in my original data.


I want to input my source and node waves and get back a spline wave with the same dimension as my source, where the spline was calculated through my nodes.

I can't see how to do that.




I think you will have to do a cubic spline through your nodes and then a linear interpolation using X from Dest mode with the cubic spline output as your new input and a copy of your original wave as your new output.
You're pretty much correct.

The workflow that I've come up with (and only briefly tested) is:

source_x & source_y - 1D waves containing x and y information. I want to fit the y data. ~2000 rows
node_x - 1D wave containing x positions at which I want to place my nodes. ~20 rows
node_y - Loop through node_x to lookup source_x to get the row to find the value of source_y **code below

interpolate2 /I=3 /X=source_x /Y=spline_y node_x, node_y

function makeNodeY(sx,sy,nx)
    wave sx, sy, nx
    make/O/N=(numpnts(nx)) node_y
   
    variable i
    for(i=0; i < numpnts(nx); i+=1)
        node_y[i] = sy[sx[nx[i]]]
    endfor
end
You can use the optimize operation with your node position wave (W_nodesY) and a function (chisquarefunction) that performs the interpolate2 and returns the sum-of-squares, something like this:

optimize x=W_nodesY chisquarefunction, w_data

In my experience this works, but for my application a smoothing spline achieves the same thing - see the spline option in my “Baseline Fitting” project for an example of how I implement it.