Using the output of scipy.interpolate.UnivariateSpline later in python or in Matlab without the need for source data

I use scipy.interpolate.UnivariateSpline to smoothly interpolate a large amount of data. It works great. I get an object that acts as a function.

Now I want to save the spline points later and use them in Matlab (as well as Python, but this is less urgent), without requiring source data. How can i do this?

In scipy, I have no idea; UnivariateSpline does not seem to offer a constructor with previously computed nodes and coefficients.

In MATLAB, I tried the Matlab spline() and pchip() functions, and although they both come close, they have errors near the endpoints that look solid like Gibbs ears .

Here is an example of a dataset that I have in Matlab format:

 splinedata = struct('coeffs',[-0.0412739180955273 -0.0236463479425733 0.42393753107602 -1.27274336116436 0.255711720888164 1.93923263846732 -2.30438927604816 1.02078680231079 0.997156858475075 -2.35321792387215 0.667027554745454 0.777918416623834],... 'knots',[0 0.125 0.1875 0.25 0.375 0.5 0.625 0.75 0.875 0.9999],... 'y',[-0.0412739180955273 -0.191354308450615 -0.869601364377744 -0.141538578624065 0.895258135865578 -1.04292294390242 0.462652465278345 0.442550440125204 -1.03967756446455 0.777918416623834]) 

Odds and nodes are the result of calling get_coeffs() and get_knots() on a scipy UnivariateSpline. The 'y' values โ€‹โ€‹are the UnivariateSpline values โ€‹โ€‹on the nodes, more specifically:

  y = f(f.get_knots()) 

where f is my UnivariateSpline.

How can I use this data to create a spline that matches the behavior of UnivariateSpline without using the Curve-Fitting Toolbox? I do not need to do any data in Matlab, I just need to know how to build a cubic spline from the values โ€‹โ€‹of nodes / coefficients / splines.

+6
source share
2 answers

You can do this using the _eval_args() and _from_tck() functions from the UnivariateSpline class. The first gives the return spline parameters that you can save, and then create a similar spline object using the second.

Here is an example:

 import numpy as np import matplotlib.pyplot as plt from scipy.interpolate import UnivariateSpline x = np.linspace(-3, 3, 50) y = np.exp(-x**2) + 0.1 * np.random.randn(50) spl1 = UnivariateSpline(x, y, s=.5) xi = np.linspace(-3, 3, 1000) tck = spl1._eval_args spl2 = UnivariateSpline._from_tck(tck) plt.plot(x, y, 'ro', ms=5, label='data') plt.plot(xi, spl1(xi), 'b', label='original spline') plt.plot(xi, spl2(xi), 'y:', lw=4, label='recovered spline') plt.legend() plt.show() 

enter image description here

+3
source

In scipy, try scipy.interpolate.splev , which accepts

tck: sequence ... containing nodes, coefficients, and spline degree.

Added: the following python class creates spline functions: init with (nodes, coefficients, degree), then use it the same way as spline functions created by UnivariateSpline( x, y, s ) :

 from scipy.interpolate import splev # http://docs.scipy.org/doc/scipy/reference/generated/scipy.interpolate.splev.html class Splinefunc: """ splinef = Splinefunc( knots, coefs, degree ) ... y = splinef( x ) # __call__ 19june untested """ def __init__( self, knots, coefs, degree ): self.knots = knots self.coefs = coefs self.degree = degree def __call__( self, x ): return splev( x, (self.knots, self.coefs, self.degree )) 
+1
source

Source: https://habr.com/ru/post/946548/


All Articles