OnlyCross_data_adaptive_zero_delay.m 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. %%CROSS-CORRELATION
  2. cross=zeros(chNO,chNO);
  3. delay=zeros(chNO,chNO);
  4. CorrLength = (2*size(A,2))-1
  5. meancorr=zeros(chNO,CorrLength);
  6. % ALL THE CHANNELS
  7. for channel=1:chNO
  8. Correlations=zeros(59,CorrLength);
  9. y1=A(channel,:);
  10. j=0;
  11. valmax1=zeros(1,chNO-channel);
  12. lag1=zeros(1,chNO-channel);
  13. valmax2=zeros(1,chNO-channel);
  14. lag2=zeros(1,chNO-channel);
  15. for i=(channel+1):chNO
  16. j=j+1;
  17. y2=A(i,:);
  18. [xcf,lags,bounds]=crosscorr(y1,y2,(size(A,2)-1));
  19. Correlations(i,1:CorrLength)=xcf;%calculating cross-correlations' mean for each channel
  20. maxDelayforCorr = 0;% max delay for max correlation in seconds
  21. zeroLagindex = find(lags==0);
  22. winSizeforDelay = (maxDelayforCorr/(WS/fs));
  23. valmax1(1,j)=max(xcf(zeroLagindex-winSizeforDelay:...
  24. zeroLagindex+winSizeforDelay));
  25. index=find(xcf==valmax1(j));
  26. if isempty(index)
  27. lag1(1,j)=NaN;
  28. else
  29. lag1(1,j)=lags(index);
  30. end
  31. [xcf,lags,bounds]=crosscorr(y2,y1,(size(A,2)-1));
  32. maxDelayforCorr = 0;% max delay for max correlation in seconds
  33. zeroLagindex = find(lags==0);
  34. winSizeforDelay = (maxDelayforCorr/(WS/fs));
  35. valmax2(1,j)=max(xcf(zeroLagindex-winSizeforDelay:...
  36. zeroLagindex+winSizeforDelay));
  37. index=find(xcf==valmax2(j));
  38. if isempty(index)
  39. lag2(1,j)=NaN;
  40. else
  41. lag2(1,j)=lags(index);
  42. end
  43. end
  44. cross((channel+1):chNO,channel)=valmax1;
  45. delay((channel+1):chNO,channel)=lag1;
  46. cross(channel,(channel+1):chNO)=valmax2;
  47. delay(channel,(channel+1):chNO)=lag2;
  48. meancorr(channel,1:CorrLength)=mean(Correlations);
  49. end
  50. %%
  51. cross_selection_correlated_channels()