R中的样条插值

时间:2015-11-27 14:40:50

标签: r spline

我有一些数据

h = c[157 144  80 106 124  46 207 188 190 208 143 170 162 178 155 163 162 149 135 160 149 147 133 146 126 120 151  74 122 145 160 155 173 126 172  93]

然后我用diff函数获得局部最大值

max = [1  5  7 10 12 14 16 20 24 27 31 33 35 36]

我有简单的matlab代码来查找样条插值

maxenv = spline(max,h(max),1:N);

此代码将显示结果

maxenv = 157    86.5643152828762    67.5352696350679    84.9885891697257    124 169.645228239041    207 224.396380746179    223.191793341491    208 185.421032390413    170 172.173624690130    178 172.759468849065    163 158.147870987344    157.874968589889    159.414581897490    160 157.622863516083    153.308219179638    148.839465253375    146 146.051320982064    148.167322961480    151 153.474200222188    155.606188003845    157.685081783579    160 163.653263154551    173 186.027639098700    172 93

现在我正在尝试使用R但是遇到了一些错误

maxenv <- spline(max, h(max), seq(N))

然后错误

  

xy.coords(x,y)出错:'x'和'y'长度不同

1 个答案:

答案 0 :(得分:0)

你的代码和问题真的不清楚,所以我不能100%确定这就是你要找的东西:

# vector of values

h <- c(157, 144, 80, 106, 124, 46, 207, 188, 190, 208, 143, 170, 162, 178, 155, 
       163, 162, 149, 135, 160, 149, 147, 133, 146, 126, 120, 151, 74, 122, 145,
       160, 155, 173, 126, 172, 93)

# local maxima
lmax <- h[c(1, which(diff(sign(diff(h)))==-2)+1, length(h))]

# spline calculation
spl <- spline(1:length(lmax), lmax)

# visual inspection
plot(spl)
lines(spl)

enter image description here