align_hv.m 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. function [offset,inc_t,x,y]=align_hv(ev,ts,val,varargin)
  2. % [offset,inc_t,x,y]=align_hv(ev, ts,val, varargin)
  3. %
  4. % pairs={'pre' 3;...
  5. % 'post' 3;...
  6. % 'binsz' 0.001;...
  7. % 'meanflg' 0;...
  8. % 'krn' 0.25;...
  9. % 'max_offset' 1;...
  10. % 'pre_mask', -inf;...
  11. % 'post_mask',+inf;...
  12. % 'do_plot' false;...
  13. % 'max_iter' 100;...
  14. % 'max_peak' 1000;...
  15. % 'var_thres' 0.05;...
  16. % 'save_plot' '';...
  17. % 'col_axis' [-50 500];...
  18. % 'col_map' jet;...
  19. % 'mark_this',[];...
  20. % }; parseargs(varargin,pairs,{},1);
  21. % %
  22. pre = 3;
  23. post = 3;
  24. binsz = 0.001;
  25. % pre_mask = -inf;
  26. % post_mask = +inf;
  27. max_offset = 1;
  28. do_plot = false;
  29. max_iter = 100;
  30. max_peak = 1000;
  31. var_thres = 0.05;
  32. save_plot = '';
  33. col_axis = [-50 500];
  34. col_map = jet;
  35. mark_this = [];
  36. utils.overridedefaults(who, varargin);
  37. old_var=10e10;
  38. done=0;
  39. thres=1000;
  40. offset=zeros(size(ev));
  41. if do_plot
  42. clf;ax(1)=axes('Position',[0.1 0.1 0.2 0.2]);
  43. ax(2)=axes('Position',[0.1 0.1 0.2 0.2]);
  44. hold on;
  45. end
  46. cnt=1;
  47. inc_t=ones(size(ev))==1;
  48. %% Calculate the mean and ci of the
  49. while ~done
  50. [y,x]=stats.cdraster(ev+offset,ts(:),val(:),pre,post,binsz);
  51. y(isnan(y))=0;
  52. [rowi,coli]=find(abs(y)>max_peak);
  53. inc_t(unique(rowi))=false;
  54. % [y x]=maskraster(x,y,pre_mask(ref),post_mask(ref));
  55. ymn = nanmean(y(inc_t,:));
  56. yst = stats.nanstderr(y(inc_t,:));
  57. if do_plot
  58. plot_this(ax,x,y,inc_t,offset,save_plot,pre,post,cnt,0,col_axis,col_map,mark_this);
  59. end
  60. for tx=1:numel(ev);
  61. if inc_t(tx)
  62. [xcy,xcx]=xcorr(y(tx,:)-mean(y(tx,:)),ymn-mean(ymn));
  63. [v,peakx]=max(xcy);
  64. offset(tx)=offset(tx)+xcx(peakx)*binsz;
  65. if abs(offset(tx))>max_offset
  66. inc_t(tx)=false;
  67. end
  68. end
  69. end
  70. new_var=sum(nanvar(y));
  71. var_diff=(old_var-new_var)/old_var;
  72. if do_plot
  73. fprintf('Variance improved by %2.3g %% of total variance\n',100*var_diff);
  74. end
  75. old_var=new_var;
  76. cnt=cnt+1;
  77. if abs(var_diff)<var_thres || cnt>max_iter
  78. done=true;
  79. if do_plot
  80. plot_this(ax,x,y,inc_t,offset,save_plot,pre,post,cnt+1,0,col_axis,col_map,mark_this);
  81. end
  82. end
  83. end
  84. function plot_this(ax,x,y,inc_t,offset,save_plot,pre,post,cnt,do_sort,col_axis,col_map,mark_this)
  85. cla(ax(1));
  86. cla(ax(2));
  87. ymn = nanmean(y(inc_t,:));
  88. yst = stats.nanstderr(y(inc_t,:));
  89. if mean(mean(y))<0
  90. iy=-y;
  91. else
  92. iy=y;
  93. end
  94. if do_sort
  95. [so,si]=sort(-offset);
  96. offset=offset(si);
  97. inc_t=inc_t(si);
  98. iy=iy(si,:);
  99. end
  100. imagesc(x,[],iy(inc_t,:),'Parent',ax(1));
  101. set(ax(1), 'Ydir','normal')
  102. hold(ax(1),'on');
  103. caxis(ax(1),col_axis);
  104. colormap(ax(1),col_map);
  105. if cnt==1
  106. cbh=colorbar('peer',ax(1),'East');
  107. set(cbh,'Position',get(cbh,'Position')-[0.2 0 0 0])
  108. end
  109. % plot(ax,x,ymn-yst,x,ymn+yst,'Color',[1-0.2*cnt, 0 ,0]);
  110. % plot(ax(2),x,ymn-yst,x,ymn+yst,'Color',[0.2 0.2 0.9],'LineWidth',2);
  111. % set(ax(1),'YTick',[]);
  112. set(ax,'YTick',[]);
  113. set(ax(2),'Color','none');
  114. set(ax,'XLim', [-pre post],'YLim',[1 sum(inc_t)])
  115. %ylim([0 maxy])
  116. xlabel(ax(2),'Time (s)')
  117. % ylabel(ax(2),'degrees/sec')
  118. yss=1:sum(inc_t==1);
  119. plot(ax(1),-offset(inc_t),yss(:) ,'w.');
  120. if ~isempty(mark_this)
  121. plot(ax(1),-offset(inc_t)+mark_this(inc_t), yss(:), 'gx');
  122. end
  123. drawnow
  124. if ~isempty(save_plot)
  125. saveas(gcf,sprintf('ahv_%s_%d.eps',save_plot,cnt),'epsc2');
  126. end
  127. if cnt==1
  128. set(cbh, 'Visible','off')
  129. end