circ_corrcl.m 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. function [rho pval] = circ_corrcl(alpha, x)
  2. %
  3. % [rho pval ts] = circ_corrcc(alpha, x)
  4. % Correlation coefficient between one circular and one or more linear random
  5. % variables (1 column per variable).
  6. %
  7. % Input:
  8. % alpha sample of angles in radians
  9. % x sample of linear random variable
  10. %
  11. % Output:
  12. % rho correlation coefficient
  13. % pval p-value
  14. %
  15. % References:
  16. % Biostatistical Analysis, J. H. Zar, p. 651
  17. %
  18. % PHB 6/7/2008
  19. %
  20. % Circular Statistics Toolbox for Matlab
  21. % By Philipp Berens, 2009
  22. % berens@tuebingen.mpg.de - www.kyb.mpg.de/~berens/circStat.html
  23. if size(alpha,2) > size(alpha,1)
  24. alpha = alpha';
  25. end
  26. if size(alpha,2) > size(alpha,1)
  27. x = x';
  28. end
  29. if size(alpha,1)~=size(x,1)
  30. error('Input dimensions do not match.')
  31. end
  32. n = size(alpha,1);
  33. % compute correlation coefficent for sin and cos independently
  34. usefast = true;
  35. if usefast
  36. %disp('Fast!')
  37. rxs = corrcoef([x,sin(alpha)]); rxs = rxs(1:(end-1),end);
  38. rxc = corrcoef([x,cos(alpha)]); rxc = rxc(1:(end-1),end);
  39. rcs = corrcoef([sin(alpha),cos(alpha)]); rcs = rcs(1,2);
  40. else
  41. rxs = corr(x,sin(alpha));
  42. rxc = corr(x,cos(alpha));
  43. rcs = corr(sin(alpha),cos(alpha));
  44. end
  45. % compute angular-linear correlation (equ. 27.47)
  46. rho = sqrt((rxc.^2 + rxs.^2 - 2*rxc.*rxs*rcs)/(1-rcs^2));
  47. % compute pvalue
  48. pval = 1 - chi2cdf(n*rho.^2,2);