testscript.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. % function testscript(pname,direction,movingwin,segave,params,fscorr)
  2. %
  3. % This script runs a sequence of analysis steps using the test
  4. % data contained in data. The data consists of a single tetrode
  5. % recording from macaque area LIP during a memory saccade experiment
  6. % a la Pesaran et al (2002). The data is already separated into
  7. % spikes and LFPs. LFPs are contained in variable dlfp, spikes from two
  8. % neurons are in in a struct array dsp, and event information is in
  9. % the following set of variables:
  10. %
  11. % trialtimes - start times of trials
  12. % fixon - fixation light comes on
  13. % fixacq - fixation acquired
  14. % targon - target light on
  15. % targoff - target light off
  16. % fixoff - fixation off
  17. % saccade - saccade
  18. %
  19. % Note that spikes and event times are in seconds and the sampling
  20. % frequency for the LFP in this experiment was 1kHz.
  21. %
  22. % the script takes the following input argument -
  23. % pname - path name on your computer where the data file LIPdata is stored.
  24. % direction - target direction to be analysed (0-7)
  25. %
  26. % The remaining parameters control various computations and are discussed
  27. % in chronux.m - type Help chronux.m for more information.
  28. %
  29. % if nargin < 4;
  30. % error('Need 6 input parameters - see help');
  31. % end;
  32. % if nargin < 5 | isempty(params);
  33. % [tapers,pad,Fs,fpass,err,trialave,params]=getparams(params);
  34. % end;
  35. % if nargin < 6 | isempty(fscorr);
  36. % fscorr=1;
  37. % end;
  38. pname='data';
  39. params.Fs=1000; % sampling frequency
  40. params.fpass=[10 100]; % band of frequencies to be kept
  41. params. tapers=[3 5]; % taper parameters
  42. params.pad=2; % pad factor for fft
  43. params.err=[2 0.05];
  44. params.trialave=1;
  45. movingwin=[0.5 0.05];
  46. segave=1;
  47. direction=5;
  48. wintrig=[5*movingwin(1) 5*movingwin(1)];
  49. winseg=2*movingwin(1);
  50. %
  51. % Load data
  52. %
  53. eval(['load LIPdata.mat']);
  54. % %
  55. % % Create rearranged data blocks for further analysis: We are going to
  56. % % extract segments of data centered on the target off times from the first channel of LFP data and the from one of the two spike trains
  57. % %
  58. % %
  59. % indx1=find(targets==5);indx2=find(targets==1); % trials to preferred and antipreferred direction
  60. % E1=targon(indx1); E2=targon(indx2); % target on times trials to preferred adn anti-preferred directions
  61. % dlfp1=createdatamatc(dlfp(:,1),E1,Fs,wintrig);dlfp2=createdatamatc(dlfp(:,1),E2,Fs,wintrig); % extract event triggered segments of the first LFP channel
  62. % dsp1=createdatamatpt(dsp(1),E1,wintrig); dsp2=createdatamatpt(dsp(1),E2,wintrig); % the same for one of the spike trains
  63. % compute spectrum of the first few seconds of LFP channels 1-2
  64. NT=round(params.Fs*10*movingwin(1));
  65. data=dlfp(1:NT,:); data1=data(:,1:2);
  66. [S,f,Serr]=mtspectrumc(data1,params);
  67. figure;
  68. plot(f,10*log10(S),f,10*log10(Serr(1,:)),f,10*log10(Serr(2,:))); xlabel('Frequency Hz'); ylabel('Spectrum');
  69. %%% pause
  70. % compute derivative of the spectrum for the same data
  71. phi=[0 pi/2];
  72. [dS,f]=mtdspectrumc(data1,phi,params);
  73. figure;
  74. plot(f,dS(1,:),f,dS(2,:)); xlabel('frequency Hz'); ylabel('Derivatives'); legend('Time','Frequency');
  75. %%% pause
  76. % compute coherency between channels 1-2 and 3-4
  77. data1=data(:,1:2);data2=data(:,3:4);
  78. [C,phi,S12,S1,S2,f,confC,phierr,Cerr]=coherencyc(data1,data2,params);
  79. figure;plot(f,C,f,Cerr(1,:),f,Cerr(2,:));xlabel('frequency'); ylabel('Coherency');
  80. %%% pause
  81. % coherency matrix of data1
  82. [C,phi,S12,f,confC,phierr,Cerr]=cohmatrixc(data1,params);
  83. % compute spectrogram for 1-2
  84. [S,t,f,Serr]=mtspecgramc(data1,movingwin,params);
  85. figure;imagesc(t,f,10*log10(S)'); axis xy; colorbar
  86. %%% pause
  87. % compute time-frequency derivative of the spectrogram for 1-2
  88. phi=[0 pi/2];
  89. [dS,t,f]=mtdspecgramc(data1,movingwin,phi,params);
  90. % pause
  91. % figure;subplot(211);imagesc(t,f,squeeze(dS(1,:,:))'); axis xy; colorbar;
  92. % subplot(212);imagesc(t,f,squeeze(dS(2,:,:))'); axis xy; colorbar;
  93. % %%% pause
  94. % compute coherogram between 1-2 and 3-4
  95. NT=round(movingwin(1)*Fs);
  96. [C,phi,S12,S1,S2,t,f,confC,phierr,Cerr]=cohgramc(data1,data2,movingwin,params);
  97. figure;imagesc(t,f,C'); axis xy; colorbar;
  98. %%% pause
  99. % compute segmented spectrum of 1
  100. NT=10*round(winseg*Fs);
  101. data1=dlfp(1:NT,1);
  102. [S,f,varS,C,Serr]=mtspectrumsegc(data1,winseg,params,segave);
  103. figure; subplot(211);plot(f,10*log(S));
  104. imagesc(f,f,C); axis xy;colorbar;
  105. %%% pause
  106. % compute segmented coherency between 1 and 2
  107. NT=10*round(winseg*Fs);
  108. data1=dlfp(1:NT,1); data2=dlfp(1:NT,2);
  109. [C,phi,S12,S1,S2,f,confC,phierr,Cerr]=coherencysegc(data1,data2,winseg,params);
  110. figure; subplot(311); plot(f,C);subplot(312); plot(f,10*log10(S1)); subplot(313);plot(f,10*log10(S2))
  111. %%% pause
  112. % compute spectrum of channel 1 triggered to events E
  113. E1=targon(find(targets==direction));
  114. data1=dlfp(:,1);
  115. [S,f,Serr]=mtspectrumtrigc(data1,E1,wintrig,params);
  116. figure;plot(f,10*log10(S)'); axis xy; colorbar;
  117. %%% pause
  118. % compute spectrogram of channel 1 triggered to events E
  119. E1=targon(find(targets==direction));
  120. data1=dlfp(:,1);
  121. [S,t,f,Serr]=mtspecgramtrigc(data1,E1,wintrig,movingwin,params);
  122. figure; imagesc(t,f,10*log10(S)'); axis xy; colorbar;
  123. %%% pause
  124. %
  125. % Analysis - point process stored as times
  126. %
  127. % dsp contains 2 channels of spikes
  128. data=extractdatapt(dsp,[20 30]); % extract spikes occurring between 20 and 30 seconds and compute their spectrum
  129. [S,f,R,Serr]=mtspectrumpt(data,params);
  130. figure; plot(f,10*log10(S),f,10*log10(Serr(1,:)),f,10*log10(Serr(2,:)));line(get(gca,'xlim'),[10*log10(R) 10*log10(R)]);
  131. %%% pause
  132. %
  133. % Compute the derivative of the spectrum
  134. %
  135. phi=[0 pi/2];
  136. [dS,f]=mtdspectrumpt(data,phi,params);
  137. figure; plot(f,dS);
  138. %%% pause
  139. %
  140. % Compute the derivative of the time-frequency spectrum
  141. %
  142. data=extractdatapt(dsp,[20 30]);
  143. data1=data(1); data2=data(2);fscorr=[];t=[];
  144. [C,phi,S12,S1,S2,f,zerosp,confC,phierr,Cerr]=coherencypt(data1,data2,params);
  145. figure; plot(f,C);
  146. %%% pause
  147. %
  148. % Compute event triggered average spectrum for one of the directions
  149. %
  150. E1=targon(find(targets==direction));
  151. data=dsp(1);
  152. [S,f,R,Serr]=mtspectrumtrigpt(data,E1,wintrig,params);
  153. figure;plot(f,10*log10(S),f,10*log10(Serr(1,:)),f,10*log10(Serr(2,:))); line(get(gca,'xlim'),[10*log10(R) 10*log10(R)]);
  154. %%% pause
  155. %
  156. % Compute the matrix of coherencies
  157. %
  158. data=extractdatapt(dsp,[20 30]);
  159. [C,phi,S12,f,zerosp,confC,phierr,Cerr]=cohmatrixpt(data,params,fscorr);
  160. %
  161. % Event triggered spectrogram - first way way
  162. %
  163. data=createdatamatpt(dsp(1),E1,wintrig);
  164. [S,t,f,R,Serr]=mtspecgrampt(data,movingwin,params);
  165. figure;imagesc(t,f,10*log10(S)'); axis xy; colorbar;
  166. %%% pause
  167. %
  168. % Derivative of the time-frequency spectrum
  169. %
  170. data=createdatamatpt(dsp(1),E1,wintrig);
  171. phi=[0 pi/2];
  172. [dS,t,f]=mtdspecgrampt(data,movingwin,phi,params);
  173. figure; subplot(211); imagesc(t,f,squeeze(dS(1,:,:))'); axis xy; colorbar;
  174. subplot(212); imagesc(t,f,squeeze(dS(2,:,:))'); axis xy; colorbar;
  175. %
  176. % Coherogram between the two spike trains
  177. %
  178. data1=createdatamatpt(dsp(1),E1,wintrig);
  179. data2=createdatamatpt(dsp(2),E1,wintrig);
  180. [C,phi,S12,S1,S2,t,f,zerosp,confC,phierr,Cerr]=cohgrampt(data1,data2,movingwin,params,fscorr);
  181. figure;imagesc(t,f,C');axis xy; colorbar
  182. % %%% pause
  183. %
  184. % Event Triggered spectrogram another way
  185. %
  186. data=dsp(1);
  187. [S,t,f,R,Serr]=mtspecgramtrigpt(data,E1,wintrig,movingwin,params);
  188. imagesc(t,f,10*log10(S)'); axis xy; colorbar
  189. %
  190. % Segmented spectrum
  191. %
  192. data=extractdatapt(dsp,[20 30]);
  193. data=data(1);
  194. [S,f,R,varS]=mtspectrumsegpt(data,winseg,params);
  195. plot(f,10*log10(S)); line(get(gca,'xlim'),[10*log10(R) 10*log10(R)]);
  196. %
  197. % Segmented coherency
  198. %
  199. data=extractdatapt(dsp,[20 30]);
  200. data1=data(1);data2=data(2);
  201. [C,phi,S12,S1,S2,f,zerosp,confC,phierr,Cerr]=coherencysegpt(data1,data2,winseg,params);
  202. figure; subplot(311); plot(f,C);subplot(312); plot(f,10*log10(S1));subplot(313); plot(f,10*log10(S2))
  203. %
  204. % Analysis - hybrid: one continous and one point process stored as times
  205. %
  206. offset=1;
  207. data1=dlfp(20000:30000,1); data2=extractdatapt(dsp,[20 30],offset);data2=data2(1).times;
  208. [C,phi,S12,S1,S2,f,zerosp,confC,phierr,Cerr]=coherencycpt(data1,data2,params);
  209. figure; subplot(311); plot(f,C);subplot(312); plot(f,10*log10(S1));subplot(313); plot(f,10*log10(S2))
  210. data1=dlfp(20000:30000,1); data2=extractdatapt(dsp,[20 30],offset);data2=data2(1).times;
  211. [C,phi,S12,S1,S2,f,zerosp,confC,phierr,Cerr]=coherencysegcpt(data1,data2,winseg,params,segave,fscorr);
  212. figure; subplot(311); plot(f,C);subplot(312); plot(f,10*log10(S1));subplot(313); plot(f,10*log10(S2))
  213. data1=dlfp(20000:30000,1); data2=extractdatapt(dsp,[20 30],offset);data2=data2(1).times;
  214. [C,phi,S12,S1,S2,t,f,zerosp,confC,phierr,Cerr]=cohgramcpt(data1,data2,movingwin,params,fscorr);
  215. figure; subplot(311); imagesc(t,f,C');axis xy; colorbar; subplot(312);imagesc(t,f,10*log10(S1)');axis xy; colorbar; subplot(313); imagesc(t,f,10*log10(S2)');axis xy; colorbar
  216. % Analysis: Binned spike counts
  217. [dN,t]=binspikes(dsp,params.Fs,[20 30]); % extract spikes occurring between 20 and 30 seconds
  218. data=dN;
  219. [S,f,R,Serr]=mtspectrumpb(data,params);
  220. plot(f,10*log10(S),f,10*log10(Serr(1,:)), f,10*log10(Serr(2,:))); %line(get(gca,'xlim'),[10*log10(R) 10*log10(R)]);
  221. data=dN;
  222. phi=[0 pi/2];
  223. [dS,f]=mtdspectrumpb(data,phi,params);
  224. figure; plot(f,dS);
  225. data=dN;
  226. data1=data(:,1); data2=data(:,2);fscorr=[];t=[];
  227. [C,phi,S12,S1,S2,f,zerosp,confC,phierr,Cerr]=coherencypb(data1,data2,params);
  228. figure; subplot(311); plot(f,C);subplot(312); plot(f,10*log10(S1)); subplot(313); plot(f,10*log10(S2));
  229. E=targon(find(targets==direction)); E=E(find(E>20 & E<450)); [dN,t]=binspikes(dsp,params.Fs,[20 500]);data=dN(:,1);
  230. [S,f,R,Serr]=mtspectrumtrigpb(data,E,wintrig,params);
  231. figure;plot(f,10*log10(S),f,10*log10(Serr(1,:)),f,10*log10(Serr(2,:)));
  232. [dN,t]=binspikes(dsp,params.Fs,[20 30]); % extract spikes occurring between 20 and 30 seconds
  233. data=dN;
  234. [C,phi,S12,f,zerosp,confC,phierr,Cerr]=cohmatrixpb(data,params,fscorr);
  235. [dN,t]=binspikes(dsp,params.Fs,[20 30]); % extract spikes occurring between 20 and 30 seconds
  236. data=dN;
  237. [S,t,f,R,Serr]=mtspecgrampb(data,movingwin,params);
  238. figure;imagesc(t,f,10*log10(S)'); axis xy; colorbar
  239. clear R Serr Cerr S C phierr dN dS data1 data2
  240. [dN,t]=binspikes(dsp,params.Fs,[20 30]); % extract spikes occurring between 20 and 30 seconds
  241. data=dN;
  242. phi=[0 pi/2];
  243. [dS,t,f]=mtdspecgrampb(data,movingwin,phi,params);
  244. [dN,t]=binspikes(dsp,params.Fs,[20 30]); % extract spikes occurring between 20 and 30 seconds
  245. data=dN;
  246. data1=data(:,1); data2=data(:,2);
  247. [C,phi,S12,S1,S2,t,f,zerosp,confC,phierr,Cerr]=cohgrampb(data1,data2,movingwin,params,fscorr);
  248. [dN,t]=binspikes(dsp,params.Fs); % extract spikes
  249. dN=dN(:,1);
  250. E=E(1:6);
  251. data=dN;
  252. [S,t,f,R,Serr]=mtspecgramtrigpb(data,E,wintrig,movingwin,params);
  253. [dN,t]=binspikes(dsp,params.Fs,[20 30]); % extract spikes occurring between 20 and 30 seconds
  254. data=dN;
  255. data=data(:,1);
  256. [S,f,R,varS]=mtspectrumsegpb(data,winseg,params,segave,fscorr);
  257. figure; plot(f,10*log10(S)); line(get(gca,'xlim'),10*log10([R R]));
  258. [dN,t]=binspikes(dsp,params.Fs,[20 30]); % extract spikes occurring between 20 and 30 seconds
  259. data=dN;
  260. data1=data(:,1);data2=data(:,2);
  261. [C,phi,S12,S1,S2,f,zerosp,confC,phierr,Cerr]=coherencysegpb(data1,data2,winseg,params);
  262. %
  263. % Analysis - hybrid: one continous and one point process stored as counts
  264. %
  265. data1=dlfp(20000:30000,:);data1=data1(:,1:2); [dN,t]=binspikes(dsp,params.Fs,[20 30]); % extract spikes occurring between 20 and 30 seconds
  266. data2=dN; data2=data2(1:end,:);
  267. [C,phi,S12,S1,S2,f,zerosp,confC,phierr,Cerr]=coherencycpb(data1,data2,params);
  268. data1=data1(:,1); data2=data2(:,1);
  269. [C,phi,S12,S1,S2,f,zerosp,confC,phierr,Cerr]=coherencysegcpb(data1,data2,winseg,params,segave,fscorr);
  270. data1=dlfp(20000:30000,:); data1=data1(:,1:2);
  271. [dN,t]=binspikes(dsp,params.Fs,[20 30]); % extract spikes occurring between 20 and 30 seconds
  272. data2=dN; data2=data2(1:end,:);
  273. [C,phi,S12,S1,S2,t,f,zerosp,confC,phierr,Cerr]=cohgramcpb(data1,data2,movingwin,params,fscorr);