Reputation: 11
I am trying to use the ransac
function in matlab to fit a harmonic function to relate between two variables: phi spanning -pi -> pi and theta spanning 0 -> pi. However, while my function has 9 parameters, ransac returns a list of 20 parameters. In addition, when I test the distances of the model's predicition from the actual data using the distance function I defined, I see that it filters out too many data points as outliers, while taking as inliers points which are above the defined threshold.
Here is my code. Note that my data in PointsTP is a nx2 variable with phi (my independent variable) in the second column and theta in the first. Here is a link to the data in the example including the variables PointsTP and Weight.
Weight = (0.7.*RdgPtsCrv./sum(RdgPtsCrv)+0.3.*PointsXYZ(:,3)./sum(PointsXYZ(:,3)));
TPmodel = fit(PointsTP(:,2),PointsTP(:,1),...
fittype('a1+a2*sin(x)+a3*cos(x)+a4*sin(2*x)+a5*cos(2*x)+a6*sin(3*x)+a7*cos(3*x)+a8*sin(4*x)+a9*cos(4*x)'),...
'StartPoint',[1,1,1,1,1,1,1,1,1],'weights',Weight);
TPfun = @(t) TPmodel(t);
DistFun = @(model, PointsTP) abs(PointsTP(:,1) - TPfun(PointsTP(:,2)));
[ModelPars,Inls] = ransac([PointsTP(:,2),PointsTP(:,1)],TPfun,DistFun,10,0.5,'MaxNumTrials',10000);
Running the code I receive the following warning: Warning: Maximum number of trials reached. Consider increasing the maximum distance or decreasing the desired confidence.
The real problem is the while the function I fit has only nine parameters, ModelPars is a 20X1 variable (with values which make no sense). In addition, when I check the data points selected as inliers, there are many inconsistencies with the maxDistance value of 0.5 given to ransac. If I plot the data points with colors depicting their distance from the fitted model it is clear that there much more points which are below that 0.5 thresholds than are actually selected (filled) as well as selceted points which are much higher than the threshold:
dist = DistFun(TPmodel,PointsTP);
fig1 = figure();
ax1=axes();
scatter(PointsTP(:,2),PointsTP(:,1),25,dist,'.');
hold on
scatter(PointsTP(Inls,2),PointsTP(Inls,1),25,dist(Inls),'o','filled');
plot(-pi:2*pi/99:pi,TPfun(-pi:2*pi/99:pi));
Any help will be highly appreciated.
Upvotes: 0
Views: 64