circ_r.m 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. function r = circ_r(alpha, w, d, dim)
  2. % r = circ_r(alpha, w, d)
  3. % Computes mean resultant vector length for circular data.
  4. %
  5. % Input:
  6. % alpha sample of angles in radians
  7. % [w number of incidences in case of binned angle data]
  8. % [d spacing of bin centers for binned data, if supplied
  9. % correction factor is used to correct for bias in
  10. % estimation of r, in radians (!)]
  11. % [dim compute along this dimension, default is 1]
  12. %
  13. % If dim argument is specified, all other optional arguments can be
  14. % left empty: circ_r(alpha, [], [], dim)
  15. %
  16. % Output:
  17. % r mean resultant length
  18. %
  19. % PHB 7/6/2008
  20. %
  21. % References:
  22. % Statistical analysis of circular data, N.I. Fisher
  23. % Topics in circular statistics, S.R. Jammalamadaka et al.
  24. % Biostatistical Analysis, J. H. Zar
  25. %
  26. % Circular Statistics Toolbox for Matlab
  27. % By Philipp Berens, 2009
  28. % berens@tuebingen.mpg.de - www.kyb.mpg.de/~berens/circStat.html
  29. if nargin < 4
  30. dim = 1;
  31. end
  32. if nargin < 2 || isempty(w)
  33. % if no specific weighting has been specified
  34. % assume no binning has taken place
  35. w = ones(size(alpha));
  36. else
  37. if size(w,2) ~= size(alpha,2) || size(w,1) ~= size(alpha,1)
  38. error('Input dimensions do not match');
  39. end
  40. end
  41. if nargin < 3 || isempty(d)
  42. % per default do not apply correct for binned data
  43. d = 0;
  44. end
  45. % compute weighted sum of cos and sin of angles
  46. r = sum(w.*exp(1i*alpha),dim);
  47. % obtain length
  48. r = abs(r)./sum(w,dim);
  49. % for data with known spacing, apply correction factor to correct for bias
  50. % in the estimation of r (see Zar, p. 601, equ. 26.16)
  51. if d ~= 0
  52. c = d/2/sin(d/2);
  53. r = c*r;
  54. end