how to use Eigen for B-Splines for noisy sequence data

馋奶兔 提交于 2019-12-23 20:26:04

问题


In the below picture, the spap2 function is used in Matlab to smooth noisy data. The result is very good. Eigen library supports this functionality Splines. I'm looking for an example in Eigen to obtain similar results. For the Matlab, I've used spap2(4, 4, time, noisyY);

Data is provided in this format time noisyData

1.766 6.61202
1.767 11.4159
1.768 8.29416
1.769 8.29416
1.77 8.29416
1.771 6.02606
1.772 4.37819
1.773 4.37819
1.774 4.37819
1.775 3.18094
1.776 2.31109
1.777 1.67911
1.778 1.21994
1.779 0.886339
1.78 0.643963
1.781 0.467867
1.782 0.339925
1.783 0.24697
1.784 0.179434
1.785 7.03822
1.786 12.0214
1.787 8.73406
1.788 6.34567
1.789 4.6104
1.79 4.6104
1.791 4.6104
1.792 10.2071
1.793 14.2732
1.794 10.3701
1.795 7.53429
1.796 5.47398
1.797 3.97708
1.798 2.88952
1.799 2.09936
1.8 8.18047
1.801 12.5985
1.802 9.15338
1.803 6.65032
1.804 11.4743
1.805 14.9787
1.806 14.9787
1.807 14.9787
1.808 14.9787
1.809 10.8827
1.81 7.90674
1.811 5.74458
1.812 5.74458
1.813 4.17368
1.814 3.03236
1.815 2.20314
1.816 2.20314
1.817 8.27015
1.818 12.678
1.819 9.2111
1.82 9.2111
1.821 6.69225
1.822 4.86221
1.823 3.5326
1.824 2.56658
1.825 1.86473
1.826 1.35481
1.827 0.984325
1.828 0.715154
1.829 0.51959
1.83 0.377504
1.831 0.274273
1.832 0.199271
1.833 0.144779
1.834 0.105188
1.835 0.0764235
1.836 0.0555249
1.837 0.0403412
1.838 0.0293096
1.839 0.0212947
1.84 0.0212947
1.841 0.0212947
1.842 0.0154715
1.843 0.0112407
1.844 0.00816684
1.845 0.00593356
1.846 0.00431098
1.847 0.00313211
1.848 0.00227561
1.849 0.00165333
1.85 0.00120121
1.851 0.000872733
1.852 0.000634078
1.853 0.000460684
1.854 0.000334707
1.855 0.000334707
1.856 0.000334707
1.857 0.000334707
1.858 0.000334707
1.859 0.000243179
1.86 0.000243179
1.861 0.000243179
1.862 0.00017668
1.863 0.000128365
1.864 9.32629e-005
1.865 9.32629e-005
1.866 9.32629e-005
1.867 9.32629e-005
1.868 6.77594e-005
1.869 4.92301e-005
1.87 3.57678e-005
1.871 2.59868e-005
1.872 1.88805e-005
1.873 1.37175e-005
1.874 9.96635e-006
1.875 7.24098e-006
1.876 5.26088e-006
1.877 3.82225e-006
1.878 3.82225e-006
1.879 3.82225e-006
1.88 2.77703e-006
1.881 2.01763e-006
1.882 1.46589e-006
1.883 1.06503e-006
1.884 6.60526
1.885 11.4043
1.886 8.28568
1.887 6.0199
1.888 4.37371
1.889 3.17769
1.89 3.17769
1.891 3.17769
1.892 2.30873
1.893 1.67739
1.894 1.21869
1.895 0.885433
1.896 7.27548
1.897 11.9181
1.898 8.65899
1.899 6.29112
1.9 11.2017
1.901 14.7692
1.902 10.7305
1.903 7.79614
1.904 6.77613
1.905 6.03755
1.906 6.03289
1.907 6.03289
1.908 4.38315
1.909 3.18455
1.91 3.18455
1.911 9.51471
1.912 14.1156
1.913 10.2556
1.914 7.45113
1.915 7.45113
1.916 7.45113
1.917 5.41357
1.918 3.93319
1.919 2.85763
1.92 2.07619
1.921 8.9437
1.922 13.9338
1.923 13.9338
1.924 13.9338
1.925 10.1235
1.926 7.35515
1.927 11.9508
1.928 15.2911
1.929 11.1096
1.93 8.07161
1.931 5.86437
1.932 4.26071
1.933 4.22711
1.934 4.20402
1.935 3.0544
1.936 2.21915
1.937 1.61231
1.938 1.17141
1.939 0.851079
1.94 0.618345
1.941 0.449254
1.942 0.326402
1.943 0.237145
1.944 0.172296
1.945 0.12518
1.946 0.0909488
1.947 0.0660782
1.948 1.10487
1.949 1.85961
1.95 1.35108
1.951 0.981619
1.952 0.713188
1.953 0.518162
1.954 0.518162
1.955 0.518162
1.956 0.376466
1.957 0.376466
1.958 0.376466
1.959 0.273519
1.96 0.273519
1.961 0.198723
1.962 0.198723
1.963 0.144381
1.964 0.144381
1.965 0.104899
1.966 0.0762134
1.967 0.0553723
1.968 0.0402303
1.969 0.029229
1.97 0.029229
1.971 0.0212361
1.972 0.015429
1.973 0.015429
1.974 0.015429
1.975 1.16617
1.976 2.00223
1.977 1.4547
1.978 1.0569
1.979 0.767886
1.98 0.557902
1.981 0.405339
1.982 6.92392
1.983 11.6607
1.984 8.472
1.985 6.15527
1.986 4.47207
1.987 3.24915
1.988 2.36064
1.989 1.71511
1.99 1.2461
1.991 0.905344
1.992 0.657771
1.993 0.477898
1.994 0.347213
1.995 0.252265
1.996 0.183281
1.997 0.133162
1.998 0.0967477
1.999 0.0702913
2 0.0510696
2.001 6.71936
2.002 11.5642
2.003 8.40185
2.004 6.1043
2.005 4.43504
2.006 3.22224
2.007 3.22224
2.008 3.22224
2.009 2.3411
2.01 1.70091
2.011 1.23578
2.012 1.23578
2.013 0.897847
2.014 0.652324
2.015 0.473941
2.016 0.344338
2.017 6.28219
2.018 9.6646
2.019 6.08633
2.02 4.42198
2.021 3.21276
2.022 2.3342
2.023 2.33601
2.024 2.33471
2.025 1.69626
2.026 1.23241
2.027 0.895397
2.028 0.650544
2.029 0.472648
2.03 0.343399
2.031 0.249494
2.032 0.181268
2.033 0.131699
2.034 6.63352
2.035 11.3573
2.036 8.25156
2.037 7.12148
2.038 6.30308
2.039 4.57946
2.04 4.57946
2.041 10.0969
2.042 10.0969
2.043 14.1081
2.044 10.2501
2.045 7.44714
2.046 4.52751
2.047 2.40446
2.048 1.74694
2.049 7.73084
2.05 13.1854
2.051 10.6942
2.052 7.76983
2.053 12.0593
2.054 12.0593
2.055 15.1777
2.056 11.0272
2.057 9.03106
2.058 7.58411
2.059 12.1218
2.06 12.1218
2.061 15.4222
2.062 15.4273
2.063 11.2086
2.064 14.753
2.065 17.3291
2.066 12.5903
2.067 10.2519
2.068 8.55693
2.069 6.21698
2.07 11.1335
2.071 14.7082
2.072 10.6861
2.073 10.6852
2.074 10.6852
2.075 7.76327
2.076 12.2467
2.077 15.5046
2.078 17.8479
2.079 19.5501
2.08 14.204
2.081 16.9469
2.082 25.5109
2.083 25.1054
2.084 24.8705
2.085 24.6988
2.086 24.8916
2.087 25.0307
2.088 25.1688
2.089 25.1703
2.09 25.1703
2.091 32.1727
2.092 31.3272
2.093 30.4857
2.094 28.8139
2.095 27.5487
2.096 33.2503
2.097 33.2504
2.098 30.778
2.099 29.6056

回答1:


One of your axioms is incorrect. Eigen (un)supports spline interpolation, which is different than approximating a function using splines. In the former, the spline must pass through the data points, whereas in the approximation they don't, as in spap2. Using the following example, you can check the output to verify that both spline(times(i)) and sins(i) give the same result.

int main()
{
    int size = 300;
    Eigen::RowVectorXd times(size), sins(size);
    times << 0, 0.0200668896321070, 0.0401337792642141, 0.0602006688963211, 0.0802675585284281, 0.100334448160535, 0.120401337792642, 0.140468227424749, 0.160535117056856, 0.180602006688963, 0.200668896321070, 0.220735785953177, 0.240802675585284, 0.260869565217391, 0.280936454849498, 0.301003344481605, 0.321070234113712, 0.341137123745819, 0.361204013377926, 0.381270903010033, 0.401337792642141, 0.421404682274248, 0.441471571906355, 0.461538461538462, 0.481605351170569, 0.501672240802676, 0.521739130434783, 0.541806020066890, 0.561872909698997, 0.581939799331104, 0.602006688963211, 0.622073578595318, 0.642140468227425, 0.662207357859532, 0.682274247491639, 0.702341137123746, 0.722408026755853, 0.742474916387960, 0.762541806020067, 0.782608695652174, 0.802675585284281, 0.822742474916388, 0.842809364548495, 0.862876254180602, 0.882943143812709, 0.903010033444816, 0.923076923076923, 0.943143812709030, 0.963210702341137, 0.983277591973244, 1.00334448160535, 1.02341137123746, 1.04347826086957, 1.06354515050167, 1.08361204013378, 1.10367892976589, 1.12374581939799, 1.14381270903010, 1.16387959866221, 1.18394648829431, 1.20401337792642, 1.22408026755853, 1.24414715719064, 1.26421404682274, 1.28428093645485, 1.30434782608696, 1.32441471571906, 1.34448160535117, 1.36454849498328, 1.38461538461538, 1.40468227424749, 1.42474916387960, 1.44481605351171, 1.46488294314381, 1.48494983277592, 1.50501672240803, 1.52508361204013, 1.54515050167224, 1.56521739130435, 1.58528428093646, 1.60535117056856, 1.62541806020067, 1.64548494983278, 1.66555183946488, 1.68561872909699, 1.70568561872910, 1.72575250836120, 1.74581939799331, 1.76588628762542, 1.78595317725753, 1.80602006688963, 1.82608695652174, 1.84615384615385, 1.86622073578595, 1.88628762541806, 1.90635451505017, 1.92642140468227, 1.94648829431438, 1.96655518394649, 1.98662207357860, 2.00668896321070, 2.02675585284281, 2.04682274247492, 2.06688963210702, 2.08695652173913, 2.10702341137124, 2.12709030100334, 2.14715719063545, 2.16722408026756, 2.18729096989967, 2.20735785953177, 2.22742474916388, 2.24749163879599, 2.26755852842809, 2.28762541806020, 2.30769230769231, 2.32775919732441, 2.34782608695652, 2.36789297658863, 2.38795986622074, 2.40802675585284, 2.42809364548495, 2.44816053511706, 2.46822742474916, 2.48829431438127, 2.50836120401338, 2.52842809364549, 2.54849498327759, 2.56856187290970, 2.58862876254181, 2.60869565217391, 2.62876254180602, 2.64882943143813, 2.66889632107023, 2.68896321070234, 2.70903010033445, 2.72909698996656, 2.74916387959866, 2.76923076923077, 2.78929765886288, 2.80936454849498, 2.82943143812709, 2.84949832775920, 2.86956521739130, 2.88963210702341, 2.90969899665552, 2.92976588628763, 2.94983277591973, 2.96989966555184, 2.98996655518395, 3.01003344481605, 3.03010033444816, 3.05016722408027, 3.07023411371237, 3.09030100334448, 3.11036789297659, 3.13043478260870, 3.15050167224080, 3.17056856187291, 3.19063545150502, 3.21070234113712, 3.23076923076923, 3.25083612040134, 3.27090301003345, 3.29096989966555, 3.31103678929766, 3.33110367892977, 3.35117056856187, 3.37123745819398, 3.39130434782609, 3.41137123745819, 3.43143812709030, 3.45150501672241, 3.47157190635452, 3.49163879598662, 3.51170568561873, 3.53177257525084, 3.55183946488294, 3.57190635451505, 3.59197324414716, 3.61204013377926, 3.63210702341137, 3.65217391304348, 3.67224080267559, 3.69230769230769, 3.71237458193980, 3.73244147157191, 3.75250836120401, 3.77257525083612, 3.79264214046823, 3.81270903010033, 3.83277591973244, 3.85284280936455, 3.87290969899666, 3.89297658862876, 3.91304347826087, 3.93311036789298, 3.95317725752508, 3.97324414715719, 3.99331103678930, 4.01337792642141, 4.03344481605351, 4.05351170568562, 4.07357859531773, 4.09364548494983, 4.11371237458194, 4.13377926421405, 4.15384615384615, 4.17391304347826, 4.19397993311037, 4.21404682274248, 4.23411371237458, 4.25418060200669, 4.27424749163880, 4.29431438127090, 4.31438127090301, 4.33444816053512, 4.35451505016722, 4.37458193979933, 4.39464882943144, 4.41471571906355, 4.43478260869565, 4.45484949832776, 4.47491638795987, 4.49498327759197, 4.51505016722408, 4.53511705685619, 4.55518394648829, 4.57525083612040, 4.59531772575251, 4.61538461538462, 4.63545150501672, 4.65551839464883, 4.67558528428094, 4.69565217391304, 4.71571906354515, 4.73578595317726, 4.75585284280936, 4.77591973244147, 4.79598662207358, 4.81605351170569, 4.83612040133779, 4.85618729096990, 4.87625418060201, 4.89632107023411, 4.91638795986622, 4.93645484949833, 4.95652173913044, 4.97658862876254, 4.99665551839465, 5.01672240802676, 5.03678929765886, 5.05685618729097, 5.07692307692308, 5.09698996655518, 5.11705685618729, 5.13712374581940, 5.15719063545151, 5.17725752508361, 5.19732441471572, 5.21739130434783, 5.23745819397993, 5.25752508361204, 5.27759197324415, 5.29765886287625, 5.31772575250836, 5.33779264214047, 5.35785953177258, 5.37792642140468, 5.39799331103679, 5.41806020066890, 5.43812709030100, 5.45819397993311, 5.47826086956522, 5.49832775919732, 5.51839464882943, 5.53846153846154, 5.55852842809365, 5.57859531772575, 5.59866220735786, 5.61872909698997, 5.63879598662207, 5.65886287625418, 5.67892976588629, 5.69899665551839, 5.71906354515050, 5.73913043478261, 5.75919732441472, 5.77926421404682, 5.79933110367893, 5.81939799331104, 5.83946488294314, 5.85953177257525, 5.87959866220736, 5.89966555183946, 5.91973244147157, 5.93979933110368, 5.95986622073579, 5.97993311036789, 6.;
    sins << 0., 0.0205663230027132, 0.0390160433647213, 0.0590891330065624, 0.0875596988696830, 0.0959822118249474, 0.121373632487893, 0.144185779407900, 0.168823702943980, 0.195714802779603, 0.182086468335945, 0.208556236727609, 0.225094826864952, 0.239997837196716, 0.261856373004331, 0.285616850959075, 0.307675255212350, 0.368369766783931, 0.323959067443884, 0.384881639934677, 0.363815465411856, 0.447469201284049, 0.453774250363027, 0.430583512112867, 0.443099359428671, 0.509029695458685, 0.475835951758427, 0.535054453990577, 0.517313698480048, 0.540962965727346, 0.545515801814871, 0.549079158057189, 0.608171721462356, 0.633254366558289, 0.661681941754734, 0.621837761755506, 0.716087740139810, 0.626367576826244, 0.655836894152803, 0.686777150859095, 0.783641511917343, 0.740251957156445, 0.687992462634859, 0.784761727801862, 0.812689567708724, 0.723454299040675, 0.867775567606339, 0.834921353384008, 0.886473449480867, 0.829994027552906, 0.936539227083496, 0.878125359050542, 0.857422602795660, 0.797532129862921, 0.943697646147127, 0.931398718818189, 0.831954959636186, 0.990429183436099, 0.937267902704322, 1.03366330075745, 0.934498869672776, 1.03339624571952, 0.997468793041113, 1.06835135036732, 0.935065588934669, 1.05824035228952, 0.900356286901882, 1.10344335276178, 1.08109260151034, 1.00924107228951, 0.894536706478818, 1.05842262698500, 0.869469062376110, 1.10339186007555, 1.12123314842733, 0.898684459599225, 1.07266484712897, 1.08181878008800, 0.859772954875467, 1.05813887444340, 0.927536817088291, 1.05875873937114, 1.05513797051990, 0.881028526179104, 0.940789571986141, 0.939650398904516, 0.895333225658922, 1.15791888887766, 0.819811378153096, 0.872483401229913, 0.913929786309735, 1.07742711882579, 0.948836532262710, 1.01695652247989, 1.07892353697484, 1.07951455459521, 0.926555376069836, 0.796683097955963, 0.971180186596668, 1.08404809595985, 0.938788138021134, 0.723978021690113, 0.845028175059507, 0.827345363692465, 0.963611479800734, 0.784913093396594, 0.902260395754080, 0.754290064829049, 0.640991769001049, 0.840961782568416, 0.884688925819984, 0.945578335759086, 0.734722787877624, 0.730891994457772, 0.560468510656587, 0.888009033487127, 0.588779454127353, 0.507860660213622, 0.723119729832821, 0.706654621382516, 0.852209479056328, 0.441125081583452, 0.703532575135291, 0.649072175609160, 0.768270742631807, 0.821436286437389, 0.812472042863249, 0.310112243913094, 0.290088866876053, 0.784062286903034, 0.724847602437022, 0.382946261736111, 0.333365214123254, 0.255326531995527, 0.510563540394420, 0.546504484804774, 0.561758001362831, 0.193398150326653, 0.211822141863330, 0.337944053003746, 0.0491697943148871, 0.506909715390791, 0.394250155597078, 0.344556489656333, 0.220319484594126, 0.466870975203072, 0.352811824041645, -0.0203204635966194, 0.000187284360740975, 0.390929784556070, -0.0636185847381180, 0.215749967807034, 0.391783875224768, 0.0131507101596210, 0.214743651863588, 0.313117360143471, -0.213347879945078, -0.152700491575586, 0.284183282760694, 0.230258633151635, -0.170326153940235, -0.138941908793679, -0.361493597493055, 0.0225982276008491, 0.136956358471678, -0.0134364651061402, -0.354908737138856, -0.338680251031658, -0.324487377118159, -0.250495042710380, -0.207682989113764, -0.201777395597538, -0.603650467101997, -0.0570224603490521, -0.494298935449787, -0.701565642803869, -0.199839524611218, -0.548524440885001, -0.336774711215616, -0.663082614278816, -0.665924584060438, -0.558180481080407, -0.709519401338939, -0.550362969544220, -0.317669574094212, -0.215362655162646, -0.557314553299032, -0.476358803662329, -0.618538714302365, -0.857936707662719, -0.650422788916676, -0.450608748642427, -1.03675737413944, -0.458466487973903, -0.435238631393051, -0.612515366468904, -1.02282034118212, -1.00591774855133, -0.754300354448578, -0.811106583146996, -0.977243563786221, -1.15873641827749, -0.967899906565075, -0.603952304631890, -0.614897411289103, -0.506405816692878, -1.08323303849733, -0.878993651876167, -1.21339204205580, -0.926163267157209, -1.14708966655702, -0.702523099754653, -0.997607886250364, -1.05378691425895, -0.520207754254763, -1.22657313553732, -0.972570592572648, -1.16907062433655, -0.906452454632314, -1.03946558253614, -0.550998692611392, -1.08167304585878, -0.897951496093406, -0.780087992998209, -1.05605506303820, -0.726866824733939, -1.09042965985860, -1.38018514904770, -0.950070929813976, -1.05770071443760, -1.07268971722784, -0.543996855503508, -0.606425043418392, -0.945835692115167, -0.669056893514066, -1.16728466807845, -0.938678055549470, -1.01236119569040, -0.725637220253413, -0.878964985274675, -0.938002625674076, -1.29352679841118, -1.12194428508496, -1.36126581275738, -0.535406897512086, -1.11461313522307, -1.26469342269406, -1.35376389844295, -0.853393422301156, -1.23411956192447, -0.889103354650984, -0.453241720722455, -0.674642312393962, -0.485956820045976, -1.34342137994975, -0.522858292122314, -1.28347238567162, -0.991285795882105, -0.837456034504287, -1.02889729768886, -0.547777267737865, -0.385836799097512, -0.618013718429130, -1.03722116318478, -0.872749491150546, -1.10717663074987, -0.974662603291053, -1.11073176801917, -0.356460205615645, -0.954131536713498, -0.953578387057411, -0.408819629096933, -1.20503187990326, -0.823728061276806, -0.639337429057833, -0.865717404705197, -0.431884787539002, -1.01026779136354, -0.756675282804455, -1.10586000122177, -0.662224314038648, -0.111456991937247, -0.769480559944682, -0.0502235743017712, -0.584109125124463, -0.902892919532128, -0.466296466268305, -0.357583159307505, -0.472303230829483, -0.482252751675765, -0.497157767695055, 0.0271725486596654, -0.370289805051389, -0.403480529070726, 0.205775238849162, -0.736735702304588, -0.668465354498537, -0.0640652586715190, -0.284538180097004, -0.156691852132801;

    Eigen::Spline<double, 1,4> spline(Eigen::SplineFitting<Eigen::Spline<double, 1,4>>::Interpolate(sins, 4, times));

    for (int i = 0+4; i < size-4; i++)
    {
        std::cout << times(i) << "\t" << spline(times(i)) << "\t" << sins(i) << "\n";
    }


    return 0;
}



回答2:


I am aware that the question is few years old, but for the next person stumbling on this problem: There indeed is a way to use eigen library to generate B-splines smoothing out the dataset.

Just do not use the Interpolate function of eigen, simply construct the spline out of control points and knot vector.

Rather nice explanation of the underlying math and, more practically, what knot vector to feed the algorithm, can be found here.



来源:https://stackoverflow.com/questions/34058076/how-to-use-eigen-for-b-splines-for-noisy-sequence-data

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