Matlab's spline equivalent in Python, three inputs.

谁说胖子不能爱 提交于 2019-12-20 05:36:09

问题


I'm converting a matlab script to python and I have it a roadblock. In order to use cubic spline interpolation on a signal. The script uses the command spline with three inputs. f_o, c_signal and freq. so it looks like the following.

cav_sig_freq = spline(f_o, c_signal, freq)
f_o = 1x264, c_signal = 1x264 and freq = 1x264

From the documentation in matlab it reads that "s = spline(x,y,xq) returns a vector of interpolated values s corresponding to the query points in xq. The values of s are determined by cubic spline interpolation of x and y."

In python i'm struggling to find the correct python equivalent. Non of different interpolation functions I have found in the numpy and Scipy documentation let's use the third input like in Matlab.

Thanks for taking the time to read this. If there are any suggestion to how I can make it more clear, I'll be happy to do so.


回答1:


Basically you will first need to generate something like an interpolant function, then give it your points. Using your variable names like this:

from scipy import interpolate
tck = interpolate.splrep(f_o, c_signal, s=0)

and then apply this tck to your points:

c_interp = interpolate.splev(freq, tck, der=0)

For more on this your can read this post.




回答2:


Have you tried the InterpolatedUnivariateSpline within scipy.interpolate? If I understand the MatLab part correctly, then I think this will work.

import numpy as np
from scipy.interpolate import InterpolatedUnivariateSpline as ius

a = [1,2,3,4,5,6]
b = [r * 2 for r in a]
c = ius(a, b, k=1)

# what values do you want to query?
targets = [3.4, 2.789]

interpolated_values = c(targets)

It seems this may add one more step to your code than what MatLab provides, but I think it is what you want.



来源:https://stackoverflow.com/questions/44680739/matlabs-spline-equivalent-in-python-three-inputs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!