Matlab interp1图显示偏离数据

时间:2014-09-19 22:33:03

标签: matlab interpolation smoothing

基本上,我试图使用Matlab的interp1方法从图像分割中平滑轮廓线。不幸的是,interp1的行为并不像我预期的那样,可能是因为我使用不正确。我的插值代码如下:

y2 = interp1(x, y, 'nearest');

然后,我尝试将原始x值与原始函数一起绘制为y2(参见附图)。

plot(x, y2, 'x');

我认为插值会使原始函数平滑 - 在某种程度上,它实际上 - 但是,interp1方法似乎偏离原点并旋转。我希望这个结果与原始数据更加一致。任何指导都将不胜感激。

figure

+------------------+------------------+
|       y          |        x         |
+------------------+------------------+
| 37.9838709677419 | 1                |
| 38               | 1.02500000000000 |
| 38.9750000000000 | 2                |
| 38.9935064935065 | 3.00000000000000 |
| 39               | 3.08333333333333 |
| 39.9166666666667 | 4                |
| 39.9935064935065 | 5.00000000000000 |
| 40               | 5.01562500000000 |
| 40.9843750000000 | 6                |
| 40.9928571428571 | 7                |
| 41               | 7.01388888888889 |
| 41.9861111111111 | 8                |
| 42               | 8.01388888888889 |
| 42.9861111111111 | 9                |
| 42.9966216216216 | 10               |
| 43               | 10.0055555555556 |
| 43.9944444444445 | 11.0000000000000 |
| 44               | 11.0384615384615 |
| 44.9615384615385 | 12               |
| 44.9948979591837 | 13               |
| 45               | 13.0555555555556 |
| 45.9444444444444 | 14               |
| 45.9848484848485 | 15               |
| 46               | 15.0333333333333 |
| 46.9666666666667 | 16               |
| 46.9916666666667 | 17               |
| 46.9939759036145 | 18               |
| 47               | 18.0357142857143 |
| 47.9642857142857 | 19               |
| 47.9861111111111 | 20               |
| 48               | 20.0098039215686 |
| 48.9901960784314 | 21.0000000000000 |
| 49               | 21.0074626865672 |
| 49.9925373134328 | 22               |
| 50               | 22.0076923076923 |
| 51               | 22.1000000000000 |
| 51.9000000000000 | 23               |
| 52               | 23.0500000000000 |
| 52.9500000000000 | 24.0000000000000 |
| 53               | 24.0161290322581 |
| 53.9838709677419 | 25               |
| 54               | 25.0217391304348 |
| 54.9782608695652 | 26               |
| 55               | 26.1666666666667 |
| 55.8333333333333 | 27               |
| 55.9886363636364 | 28               |
| 56               | 28.0087719298246 |
| 56.9912280701754 | 29               |
| 56.9935897435897 | 30               |
| 57               | 30.1000000000000 |
| 57.9000000000000 | 31.0000000000000 |
| 57.9722222222222 | 32               |
| 58               | 32.1250000000000 |
| 58.8750000000000 | 33               |
| 58.9782608695652 | 34               |
| 58.9687500000000 | 35               |
| 58.9883720930233 | 36.0000000000000 |
| 59               | 36.0833333333333 |
| 59.9166666666667 | 37               |
| 59.9736842105263 | 38               |
| 60               | 38.1000000000000 |
| 60.9000000000000 | 39               |
| 60.9750000000000 | 40               |
| 61               | 40.0113636363636 |
| 61.9886363636364 | 41               |
| 61.9883720930233 | 42               |
| 62               | 42.0714285714286 |
| 62.9285714285714 | 43               |
| 63               | 43.0357142857143 |
| 64               | 43.0714285714286 |
| 65               | 43.1000000000000 |
| 66               | 43.0833333333333 |
| 67               | 43.0833333333333 |
| 68               | 43.0625000000000 |
| 69               | 43.0384615384615 |
| 70               | 43.0166666666667 |
| 71               | 43.0106382978723 |
| 71.1666666666667 | 43               |
| 72               | 42.1666666666667 |
| 73               | 42.0833333333333 |
| 74               | 42.0625000000000 |
| 75               | 42.0625000000000 |
| 76               | 42.1666666666667 |
| 76.8333333333333 | 43               |
| 77               | 43.0142857142857 |
| 78               | 43.0277777777778 |
| 78.9722222222222 | 44               |
| 79               | 44.0064102564103 |
| 79.9935897435898 | 45               |
| 80               | 45.0277777777778 |
| 81               | 45.2500000000000 |
| 81.7500000000000 | 46               |
| 82               | 46.0833333333333 |
| 82.9166666666667 | 47.0000000000000 |
| 83               | 47.0166666666667 |
| 84               | 47.1250000000000 |
| 84.8750000000000 | 48               |
| 85               | 48.0131578947368 |
| 86               | 48.0714285714286 |
| 86.9285714285714 | 49               |
| 87               | 49.5000000000000 |
| 87.5000000000000 | 50               |
| 88               | 50.0714285714286 |
| 88.9285714285714 | 51.0000000000000 |
| 89               | 51.0178571428571 |
| 89.9821428571429 | 52               |
| 89.9583333333333 | 99               |
| 89.9923076923077 | 100              |
| 89.9938271604938 | 101              |
| 89.9666666666667 | 102              |
| 89.9375000000000 | 103              |
| 90               | 52.0106382978723 |
| 90.9893617021277 | 53               |
| 90.9750000000000 | 85               |
| 90.9915254237288 | 86               |
| 90.9871794871795 | 87               |
| 90.9687500000000 | 88               |
| 90.9583333333333 | 89               |
| 90.9935064935065 | 90               |
| 90.9937500000000 | 91               |
| 90.9807692307692 | 92               |
| 90.9687500000000 | 93               |
| 90.9827586206897 | 94               |
| 90.9814814814815 | 95               |
| 90.9500000000000 | 96.0000000000000 |
| 90.9687500000000 | 97               |
| 90               | 98.9444444444444 |
| 90.9444444444444 | 98               |
| 90               | 103.062500000000 |
| 90.9375000000000 | 104              |
| 90.8333333333333 | 105              |
| 91               | 53.0166666666667 |
| 91.9833333333333 | 54.0000000000000 |
| 91.9687500000000 | 75               |
| 91.9545454545455 | 77               |
| 91.9642857142857 | 78.0000000000000 |
| 91.9444444444444 | 79               |
| 91.8333333333333 | 80               |
| 91.9166666666667 | 81               |
| 91.9000000000000 | 82               |
| 91.5000000000000 | 83               |
| 91               | 84.7500000000000 |
| 91.7500000000000 | 84               |
| 91               | 105.100000000000 |
| 91.9000000000000 | 106              |
| 91.9615384615385 | 107              |
| 91.9500000000000 | 124              |
| 91.9000000000000 | 125              |
| 91.7500000000000 | 126              |
| 91.8750000000000 | 127              |
| 91.9000000000000 | 128              |
| 91.8750000000000 | 129              |
| 92               | 54.0294117647059 |
| 92.9705882352941 | 55               |
| 92.9910714285714 | 56               |
| 92               | 74.9687500000000 |
| 92.9687500000000 | 74               |
| 92               | 75.0416666666667 |
| 92               | 76.9583333333333 |
| 92.9583333333333 | 76.0000000000000 |
| 92               | 107.125000000000 |
| 92.8750000000000 | 108              |
| 92.9687500000000 | 109              |
| 92.9500000000000 | 122.000000000000 |
| 92               | 123.875000000000 |
| 92.8750000000000 | 123              |
| 92               | 129.125000000000 |
| 92.8750000000000 | 130              |
| 93               | 56.0125000000000 |
| 93.9750000000000 | 69.0000000000000 |
| 93.9838709677419 | 70               |
| 93.9761904761905 | 71               |
| 93.9761904761905 | 72.0000000000000 |
| 93               | 73.9500000000000 |
| 93.9500000000000 | 73               |
| 93               | 109.071428571429 |
| 93.9285714285714 | 110.000000000000 |
| 93               | 121.968750000000 |
| 93.9687500000000 | 121              |
| 93               | 130.083333333333 |
| 93.9166666666667 | 131              |
| 94               | 56.0625000000000 |
| 94.9375000000000 | 57               |
| 94.9838709677419 | 58               |
| 94.9864864864865 | 59               |
| 94.9893617021277 | 66               |
| 94.9722222222222 | 67               |
| 94.0000000000000 | 68.9833333333333 |
| 94.9833333333333 | 68               |
| 94               | 110.062500000000 |
| 94.9375000000000 | 111              |
| 94.9861111111111 | 112.000000000000 |
| 94.9861111111111 | 115.000000000000 |
| 94.9933333333333 | 119              |
| 94               | 120.980769230769 |
| 94.9807692307692 | 120              |
| 94               | 131.012820512821 |
| 94.9871794871795 | 132              |
| 95               | 59.0384615384615 |
| 95.9615384615385 | 60               |
| 95.9807692307692 | 61.0000000000000 |
| 95.9722222222222 | 62               |
| 95.9838709677419 | 63               |
| 95.9852941176471 | 64               |
| 95               | 65.9583333333333 |
| 95.9583333333333 | 65               |
| 95               | 112.026315789474 |
| 95.9736842105263 | 113              |
| 95               | 114.994252873563 |
| 95.9942528735632 | 114              |
| 95               | 115.015151515152 |
| 95.9848484848485 | 116              |
| 95.9893617021277 | 117.000000000000 |
| 95               | 118.982142857143 |
| 95.9821428571429 | 118              |
| 95               | 132.014705882353 |
| 95.9852941176471 | 133.000000000000 |
| 95.9954545454546 | 134              |
| 96               | 134.014285714286 |
| 96.9857142857143 | 135              |
| 97               | 135.022727272727 |
| 97.9772727272727 | 136              |
| 98               | 136.021739130435 |
| 98.9782608695652 | 137              |
| 99               | 137.038461538462 |
| 99.9615384615385 | 138.000000000000 |
| 100              | 138.022727272727 |
| 100.977272727273 | 139              |
| 101              | 139.041666666667 |
+------------------+------------------+

1 个答案:

答案 0 :(得分:1)

您的代码几乎没有问题 首先,我认为您错误地使用了interp1函数。在您使用它时,该函数会在x指定的点处插入y,假设{1,2}在1,2,3,......上采样{
1 查看interp1文档 我认为你试图做的是在1,2,3,...
插入y 如果我是正确的,那么语法应该是(但它不会像我稍后解释的那样工作)

x

这让我想到了另外两个问题 您的网格点不是单调增加的,也不是唯一的 第一个问题可以通过使用x2 = 1:max(round(x)); y2 = interp1(x, y,x2 , 'nearest'); plot(x2,y2,'x');

来解决
sort

第二个可以用[x,ind] = sort(x); y = y(ind); 来解决(但这不一定是你想要的那样):

unique

最后,您可以按照我上面的建议进行插值。

顺便说一句:还有其他方法可以去除数据中的大驼峰"或者"将其平滑",例如过滤(线性和非线性)。