How to define a higher-degree spline using python? - python

I am using the Scipy CubicSpline interpolation based on a certain number of points as shown in the diagram below:
My problem is, the second derivative of the Cubic Splive function looks a little bit edgy:
In order to smooth the second curve I need a higher degree of spline interpolation. Is there a Scipy build in function (similar to CubicSpline) or an easy way to do that? (A b-spline function want work)

make_interp_spline should be able to construct BSpline objects of higher degrees (FITPACK only goes up to k=5, which is hardcoded fairly deep down).

Related

Evenly distributed points on spline using numpy

I'm using numpy to generate a spline based on b-spline parameters. These parameters have been read from a STEP file exported from CAD with the end goal being to write a 2D unstructured mesh generator. I need to generate evenly spaced points on the spline to act as vertexes for the mesh elements.
Annoyingly, however, numpy is generating highly unevenly distributed points along the spline and I'm not sure why.
Here is an example of an aerofoil b-spline - the control points are relatively evenly placed around the spline and there is sufficient resolution in the spline coordinates.
But when I evaluate the same spline with 10x less points, you can see the distribution is heavily weighted to the upper line.
This is the code I'm using to generate the points (the bspline parameters have already been defined according to the STEP file):
N=int(ctrl_pts.shape[0]*100) # number of points to evaluate
spline_range=np.linspace(
knot_vector[0],
knot_vector[-1],
N
)
bspline=numpy.interpolate.BSpline(knot_vector,ctrl_pts,degree)
points=bspline(spline_range)
Is there a way to 'redistribute' the points on the spline?

Use Quadpy to numerically integrate sets of points on a Sphere

I have a set of values on a sphere in three dimensions. I want to numerically integrate them, and I heard that quadpy offers good speed and functionality. However, I do not have a function
def func(x, y, z):
do something
return f
which I could pass to quadpy. Can I just use its integration somehow to numerically integrate my set of points with one of their schemes? Otherwise, if someone knows a good, and fast numpy or scipy alternative I'd be also OK with that.
quadpy author here. All methods in quadpy are Gaussian integration. meaning that you must be able to evaluate a function at given points. (The magic in Gaussian integration is how the points are chosen.)
If you only have numerical data here and there, the best you can do is probably form Voronoi cells, i.e., for each point i compute the area V_i that is closest to this point, and then
sum(V_i f(x_i))
As an approximation, you can use meshzoo to create a spherical mesh and assign the triangles to their closest x_i.

scipy.interpolate.splrep: What do the derivatives beyond der=1 represent?

I've been using scipy.interpolate.splrep to create path animations in a 3D application. I use the first derivative (der=1) option to get the tangent on curve at a given point to calculate a forward vector which the allows me to orient the object following that path.
This is when it occurred to me that splrep can also compute the second (der=2) and third (der=3) derivatives (and presumably more?)
I know that the first derivative of a spline at a given query point corresponds to the tangent on spline at that point, what i'd like to know is what do the derivatives beyond (der > 1) represent and what can the values splrep returns for them be used for?

How interpolate 3D coordinates

I have data points in x,y,z format. They form a point cloud of a closed manifold. How can I interpolate them using R-Project or Python? (Like polynomial splines)
It depends on what the points originally represented. Just having an array of points is generally not enough to derive the original manifold from. You need to know which points go together.
The most common low-level boundary representation ("brep") is a bunch of triangles. This is e.g. what OpenGL and Directx get as input. I've written a Python software that can convert triangular meshes in STL format to e.g. a PDF image. Maybe you can adapt that to for your purpose. Interpolating a triangle is usually not necessary, but rather trivail to do. Create three new points each halfway between two original point. These three points form an inner triangle, and the rest of the surface forms three triangles. So with this you have transformed one triangle into four triangles.
If the points are control points for spline surface patches (like NURBS, or Bézier surfaces), you have to know which points together form a patch. Since these are parametric surfaces, once you know the control points, all the points on the surface can be determined. Below is the function for a Bézier surface. The parameters u and v are the the parametric coordinates of the surface. They run from 0 to 1 along two adjecent edges of the patch. The control points are k_ij.
The B functions are weight functions for each control point;
Suppose you want to approximate a Bézier surface by a grid of 10x10 points. To do that you have to evaluate the function p for u and v running from 0 to 1 in 10 steps (generating the steps is easily done with numpy.linspace).
For each (u,v) pair, p returns a 3D point.
If you want to visualise these points, you could use mplot3d from matplotlib.
By "compact manifold" do you mean a lower dimensional function like a trajectory or a surface that is embedded in 3d? You have several alternatives for the surface-problem in R depending on how "parametric" or "non-parametric" you want to be. Regression splines of various sorts could be applied within the framework of estimating mean f(x,y) and if these values were "tightly" spaced you may get a relatively accurate and simple summary estimate. There are several non-parametric methods such as found in packages 'locfit', 'akima' and 'mgcv'. (I'm not really sure how I would go about statistically estimating a 1-d manifold in 3-space.)
Edit: But if I did want to see a 3D distribution and get an idea of whether is was a parametric curve or trajectory, I would reach for package:rgl and just plot it in a rotatable 3D frame.
If you are instead trying to form the convex hull (for which the word interpolate is probably the wrong choice), then I know there are 2-d solutions and suspect that searching would find 3-d solutions as well. Constructing the right search strategy will depend on specifics whose absence the 2 comments so far reflects. I'm speculating that attempting to model lower and higher order statistics like the 1st and 99th percentile as a function of (x,y) could be attempted if you wanted to use a regression effort to create boundaries. There is a quantile regression package, 'rq' by Roger Koenker that is well supported.

How to use interpolation to calculate a force based on angle

I am trying to make a python script that will output a force based on a measured angle. The inputs are time, the curve and the angle, but I am having trouble using interpolation to fit the force to the curve. I looked at scipy.interpolate, but I'm not sure it will help me because the points aren't evenly spaced.
numpy.interp does not require your points to be evenly distributed. I'm not certain if you mean by "The inputs are time, the curve and the angle" that you have three independent variables, if so you will have to adapt it quite a bit... But for one-variable problems, interp is the way to go.

Categories