spline

Catmull-Rom splines in python

大憨熊 提交于 2019-12-20 14:47:25
问题 Is there a library or function in python to compute Catmull-Rom spline from three points ? What I need in the end are the x,y coordinates of points along the spline, provided that they are always equidistant of a given amount t along the spline (say, the spline curve is 3 units long and I want the x,y coordinates at spline length 0, 1, 2 and 3) Nothing really exciting. I am writing it by myself, but if you find something nice, It would be great for testing (or to save time) 回答1: 3 points ?

Intersection between bezier curve and a line segment

 ̄綄美尐妖づ 提交于 2019-12-20 12:34:25
问题 I am writing a game in Python (with pygame) that requires me to generate random but nice-looking "sea" for each new game. After a long search I settled on an algorithm that involves Bezier curves as defined in padlib.py. I now need to figure out when the curves generated by padlib intersect a line segment. The brute force method would be to just use the set of approximating line segments produced by padlib to find the answer. However, I suspect that a better answer can be found analytically.

How to perform cubic spline interpolation in python?

折月煮酒 提交于 2019-12-20 09:18:31
问题 I have two lists to describe the function y(x): x = [0,1,2,3,4,5] y = [12,14,22,39,58,77] I would like to perform cubic spline interpolation so that given some value u in the domain of x, e.g. u = 1.25 I can find y(u). I found this in SciPy but I am not sure how to use it. 回答1: Short answer: from scipy import interpolate def f(x): x_points = [ 0, 1, 2, 3, 4, 5] y_points = [12,14,22,39,58,77] tck = interpolate.splrep(x_points, y_points) return interpolate.splev(x, tck) print(f(1.25)) Long

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

How to save and load spline interpolation functions in R?

ぐ巨炮叔叔 提交于 2019-12-20 05:23:40
问题 I need to create thousands and thousands of interpolation splines, each based on 5 pairs of (x, y) values. I would like to save them in a database (or csv file). How can I export / import them, say in a text format or as an array of real parameters to rebuild each function when I need them? 回答1: If you are using the splinefun function from R base package stats , it is very easy to export its construction information. set.seed(0) xk <- c(0, 1, 2) yk <- round(runif(3), 2) f <- splinefun(xk, yk,

How to add legend to geom_smooth in ggplot in R

大城市里の小女人 提交于 2019-12-19 16:56:02
问题 Have a problem of adding legend to different smooth in ggplot. library(splines) library(ggplot2) temp <- data.frame(x = rnorm(200, 20, 15), y = rnorm(200, 30, 8)) ggplot(data = temp, aes(x, y)) + geom_point() + geom_smooth(method = 'lm', formula = y ~ bs(x, df=5, intercept = T), col='blue') + geom_smooth(method = 'lm', formula = y ~ ns(x, df=2, intercept = T), col='red') I have two splines: red and blue. How I can add a legend for them? 回答1: Put the colour in aes() and add scale_colour_manual

R smooth.spline(): smoothing spline is not smooth but overfitting my data

丶灬走出姿态 提交于 2019-12-18 16:54:29
问题 I have several data points which seem suitable for fitting a spline through them. When I do this, I get a rather bumpy fit, like overfitting, which is not what I understand as smoothing. Is there a special option / parameter for getting back the function of a really smooth spline like here. The usage of the penalty parameter for smooth.spline didn't have any visible effect. Maybe I did it wrong? Here are data and code: results <- structure( list( beta = c( 0.983790622281964, 0.645152464354322

How to draw a continuous curved line from 3 given points at a time

 ̄綄美尐妖づ 提交于 2019-12-18 13:23:13
问题 I am trying to draw a continuous curved line in flash. There are many methods but none of the ones I have found so far quite fit my requirements. First of all, I want to use the flash graphic api's curveTo() method. I DO NOT want to simulate a curve with hundreds of calls to lineTo() per curved line segment. It is my experience and understanding that line segments are processor heavy. Flash's quadratic bezier curve should take less CPU power. Please challenge this assumption if you think I am

Is there easy way in python to extrapolate data points to the future?

别说谁变了你拦得住时间么 提交于 2019-12-18 02:48:22
问题 I have a simple numpy array, for every date there is a data point. Something like this: >>> import numpy as np >>> from datetime import date >>> from datetime import date >>> x = np.array( [(date(2008,3,5), 4800 ), (date(2008,3,15), 4000 ), (date(2008,3, 20), 3500 ), (date(2008,4,5), 3000 ) ] ) Is there easy way to extrapolate data points to the future: date(2008,5,1), date(2008, 5, 20) etc? I understand it can be done with mathematical algorithms. But here I am seeking for some low hanging

The intersection point between a spline and a line

自作多情 提交于 2019-12-17 22:55:25
问题 I'm trying to find a way to calculate the intersection between a b-spline and a straight line. So far Google hasn't been much help. 回答1: The most efficient algorithm that I've heard of is called Bezier clipping. Here's a book chapter on curve and spline intersection (pdf). 回答2: A pure mathematical approach: Transform the spline and the line so that the line lies on the X axis. Calculate the points on the spline where Y = 0 (depends on the order of the spline). Transform these points back to