Interpolating Specific Point Using Centripedal Catmull-Rom Spline - python

I have been investigating Catmull-Rom (CR) splines for interpolating curves and like my results so far, though I am having a fatal issue: Essentially, I am unable to map the "t" (say 0<=t<=1 where it measures the... for lack of a better way to explain it... 'parameterized' horizontal distance from P1 to P2 (using the P0,P1,P2,P3) notation here) to a tangible "x" value that would actually be charted in an X,Y coordinate space. Of course this is mostly a problem when using non-uniform CR splines (because xi=(ti-t0)+x0 in that case). I specifically like the features of the centripedal (alpha=0.5) CR splines, but the best I can do right now is ask my function to give me back a load of points and draw the curve. I am unable to say for example "Give me the point on the curve when x=some point i want. I have been able to recreate CR splines in excel and python, but still having no luck with the t->x mapping and my ultimate goal, asking to interp a specific x point and returning the corresponding y-value.
https://en.wikipedia.org/wiki/Centripetal_Catmull%E2%80%93Rom_spline is a good summary of what I have (the python code there is close to mine as well)

Related

How to plot a cube or parallelepid with function-related color map

I am unfortunately quite inexperienced with python, and programming in general. I am devoting a lot of time to get better but this one really got me.
I need to evaluate a time evolving funtion happening within the boundaries of a solid, a cube if you like.
My idea was to plot a 3D surface with x, y and z being the dimensions of my solid, and the colormap being the values of the function i mentioned at a given point in time. The final result would be a video with the sequence of plots for a given time interval.
I smashed my head with matplotlib recently but I don't seem to understand the idea behind the need for numpy 2D arrays for surface plotting. The examples given in the docs are somewhat not revelant as my function values come from a numerical solution, hence there is no explicit relation between x,y and z and F(x,y,z).
Does anyone have any suggestion? I hope I haven't been negligent on the doc reading on the topic.

Plotly: What kind of splines do we plot when using the option line_shape='spline'?

I am preparing some code to interpolate a serie of points with splines.
There are many kinds of splines: quadratic, cubic, many boundary conditions...
So far I have tried the most popular ones: cubic splines, with boundary conditions:
Natural: second derivative is zero at first and last points.
Clamped: first derivative is zero at first and last points.
Not-a-knot: third derivative is continuous at second and second-to-last points.
I have also tried quadratic splines with "clamped" initial condition.
I have discovered that Plotly also has a built-in interpolation function when we define the trace like this:
fig.add_trace(go.Scatter(
x=df['timestamp'],
y=df['values'],
mode='lines',
line_shape='spline',
))
This plotly's spline looks very good for my taste. It is soft and has less oscillation than, for example, the natural cubic spline:
Red line is natural cubic spline.
Gray line is plotly's spline.
So my question is: What exact kind of spline is this?
I have tried to compare it with the curves I have mentioned above. None of them is like the Plotly's spline.
I have checked Plotly's documentation and it does not tell you what kind of curves are they using. But it says that you can add the parameter "smoothing" in order to control the curvature.
Does anyone know how the Plotly guys do it?
I haven't been able to find a complete description in the docs either. But by the looks of your figure, I would assume that it's some sort of Monotone cubic interpolation.
If you compare your figure to a similar figure from the source above, you'll see that the illustrated splines have quite a bit in common:
Judging by the areas highlighted by the grey, red and green circles, the splines applied by plotly seem to have the same smoother traits than other comparable options.

Interpolation of irregularly spaced data into 3d grid

I'm working with some instrument data that has records the temperature at a specific latitude, longitude, and pressure (height) coordinate. I need to create a 3d grid from this instrument data that I can then use to take a vertical cross sections of the interpolated gridded data. I've looked at pretty much every interpolation function/library I can find and I'm still having trouble just wrapping my head around how to do this.
I'd prefer not to use Mayavi, since it seems to bug out on my school's server and I'd rather not try to deal with fixing it right now.
The data is currently in 4 separate 1d arrays and I used those to mock up some scatter plots of what I'm trying to get.
Here is the structure of my instrument data points:
And here is what I'm trying to create:
Ultimately, I'd like to create some kind of 3d contour from these points that I can take slices of. Each of the plotted points has a corresponding temperature attached to it, which is really what I think is throwing me off in terms of dimensions and whatnot.
There are a few options to go from the unstructured data which you have to a structured dataset.
The simplest option might be to use the scipy interpolate.griddata method which can interpolate unstructured points using, linear or cubic interpolation.
Another option is to define your grid and then average all of the unstructured points which fall into each grid cell, giving you some gridded representation of the data. You could use a tool such as CIS to do this easily (full disclosure, I wrote this package to do exactly this kind of thing).
Or, there are more complicated methods of interpolating the data by trying to determine the most likely value of the grid points based on the unstructured data, for example using kriging with the pyKriging package, though I've never used this.

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