circ_mean.m 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. function [mu ul ll] = circ_mean(alpha, w, dim)
  2. %
  3. % mu = circ_mean(alpha, w)
  4. % Computes the mean direction for circular data.
  5. %
  6. % Input:
  7. % alpha sample of angles in radians
  8. % [w weightings in case of binned angle data]
  9. % [dim compute along this dimension, default is 1]
  10. %
  11. % If dim argument is specified, all other optional arguments can be
  12. % left empty: circ_mean(alpha, [], dim)
  13. %
  14. % Output:
  15. % mu mean direction
  16. % ul upper 95% confidence limit
  17. % ll lower 95% confidence limit
  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 < 3
  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. % compute weighted sum of cos and sin of angles
  42. r = sum(w.*exp(1i*alpha),dim);
  43. % obtain mean by
  44. mu = angle(r);
  45. % confidence limits if desired
  46. if nargout > 1
  47. t = circ_confmean(alpha,0.05,w,[],dim);
  48. ul = mu + t;
  49. ll = mu - t;
  50. end