/matlab/fieldtrip-20101107/forward/private/sphfit.m

http://github.com/yuval-harpaz/ft_BIU · MATLAB · 115 lines · 49 code · 22 blank · 44 comment · 5 complexity · 0023dcff38a436eb335fb01fdf0afa33 MD5 · raw file

  1. function [center,radius]=sphfit(vc,Ni,stopth)
  2. % fits a sphere to a set of surface points
  3. % usage: [center,radius]=sphfit(vc,Ni,stopth)
  4. %
  5. % input:
  6. % vc nx3 matrix, where each row represents the location
  7. % of one surface point. vc can have more than 3 columns
  8. % (e.g. orientations) - then only the first 3 columns are used
  9. % Ni number of iteration requested, 20 by default
  10. % stopth stopping threshold used for the relative difference between 2
  11. % succeessive estimates of the radius. Fixed 10^-6 by default
  12. % If stopth<0, then no stopping till the end of the Ni iterations
  13. %
  14. % center 1x3 vector denoting the center
  15. % radius scalar denoting the radius
  16. %
  17. % written by Guido Nolte
  18. % updated by Christophe Phillips, 2009/1/19
  19. % - add the number of iterations as input, use 5 as default
  20. % - add a stopping criteria based on the relative difference between 2
  21. % successive estimates of the radius.
  22. % If rel_diff<1e-6 (default of use fixed), then break out.
  23. % Copyright (C) 2003, Guido Nolte
  24. %
  25. % This file is part of FieldTrip, see http://www.ru.nl/neuroimaging/fieldtrip
  26. % for the documentation and details.
  27. %
  28. % FieldTrip is free software: you can redistribute it and/or modify
  29. % it under the terms of the GNU General Public License as published by
  30. % the Free Software Foundation, either version 3 of the License, or
  31. % (at your option) any later version.
  32. %
  33. % FieldTrip is distributed in the hope that it will be useful,
  34. % but WITHOUT ANY WARRANTY; without even the implied warranty of
  35. % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  36. % GNU General Public License for more details.
  37. %
  38. % You should have received a copy of the GNU General Public License
  39. % along with FieldTrip. If not, see <http://www.gnu.org/licenses/>.
  40. %
  41. % $Id: sphfit.m 946 2010-04-21 17:51:16Z roboos $
  42. if nargin<2 || isempty(Ni)
  43. Ni = 20;
  44. end
  45. if nargin<3 || isempty(stopth)
  46. stopth = 1e-6;
  47. end
  48. vc=vc(:,1:3);
  49. [nvc,ndum]=size(vc);
  50. center_0=mean(vc);
  51. vcx=vc-repmat(center_0,nvc,1);
  52. radius_0=mean(sqrt(vcx(:,1).^2+vcx(:,2).^2+vcx(:,3).^2));
  53. alpha=1;
  54. rd = 1;
  55. err_0=costfun(vc,center_0,radius_0);
  56. for k=1:Ni;
  57. [center_new,radius_new]=lm1step(vc,center_0,radius_0,alpha);
  58. err_new=costfun(vc,center_new,radius_new);
  59. % disp([k,err_0,err_new,center_new,radius_new]);
  60. if err_new<err_0;
  61. rd = abs(radius_0-radius_new)/radius_0;
  62. % disp(rd)
  63. center_0=center_new;
  64. radius_0=radius_new;
  65. err_0=err_new;
  66. alpha=alpha/5;
  67. else
  68. alpha=alpha*5;
  69. end
  70. radius=radius_0;
  71. center=center_0;
  72. if rd<stopth, break, end % stop if
  73. end
  74. return;
  75. %%
  76. function err=costfun(vc,center,radius)
  77. [nvc,ndum]=size(vc);
  78. vcx=vc-repmat(center,nvc,1);
  79. err=sqrt(sqrt(mean( (vcx(:,1).^2+vcx(:,2).^2+vcx(:,3).^2-radius^2).^2)));
  80. return
  81. %%
  82. function [center_new,radius_new]=lm1step(vc,center,radius,alpha)
  83. [nvc,ndum]=size(vc);
  84. vcx=vc-repmat(center,nvc,1);
  85. f=vcx(:,1).^2+vcx(:,2).^2+vcx(:,3).^2-radius^2;
  86. L=2*[vcx,repmat(radius,nvc,1)];
  87. par_new=inv(L'*L+alpha*eye(4))*L'*f;
  88. center_new=center+par_new(1:3)';
  89. radius_new=radius+par_new(4);
  90. return