Search code examples
pythonscipybspline

Is there a way to generate B spline functions in Python without the coefficients known?


I'm trying to generate a basis spline function by defining the order of b-splines, number of basis functions, knots and range of evaluation. Please refer me to a suitable function in Python that can help me.

My current implementation is using the johntfoster/bspline method. It doesn't allow me to define the number of basis functions and the results are not similar to that of MATLAB. https://github.com/johntfoster/bspline

The scipy.interpolate.BSpline.basis_element function doesn't allow me to define the order of spline, number of basis functions,knots

Matlab Implementation:

nbreaks = 20;
nbasis = nbreaks + norder - 2;
breaks = linspace(0,taufmax,nbreaks)';

%Create a smooth function that passes through the break point / knots
wtaubasis = create_bspline_basis([0,max(breaks)], nbasis, norder, breaks);

% Create a matrix of basis functions at each break points for the entire Tau
basisValueMat_f = full(eval_basis(wtaubasis, tauf));

Python Implementation (johntfoster/bspline method)

import numpy as np
import bspline
import bspline.splinelab as splinelab

norder = 4
nbreaks = 20

#This defines the number of basis function
nbasis = nbreaks + norder - 2

#For the spline, it has to pass thorough the corresponding break points
breaks = np.linspace(0,tauf_max,nbreaks)
k = splinelab.augknt(breaks, norder)

# create spline basis of order p on knots k  
B = bspline.Bspline(k, norder)  
A0 = B.collmat(np.squeeze(tau_f), deriv_order=0)

I would like to get the B Spline basis functions evaluated at specified points. Results similar to MATLAB would be highly encouraging.


Solution

  • There's evaluate_all_bspl, https://github.com/scipy/scipy/blob/v1.3.0/scipy/interpolate/_bspl.pyx#L163 which computes all non-zero b-splines given knots at a given evaluation point. It is not a public function though, so if you end up using it, you're on your own.