function [data_pre,data_stim,welch_diffmean,thetadom_PRE,thetadom_STIM,thetadom_POST,thetaSHIFT,data_thshift,fWelch,speedav,speedav_stim,speedav_post,... sump_th_pre,sump_th_stim,sump_th_post,... sump_stimF_pre,sump_stimF_stim,sump_stimF_post,... sump_bgLOW_pre,sump_bgLOW_stim,sump_bgLOW_post,... sump_bgHIGH_pre,sump_bgHIGH_stim,sump_bgHIGH_post,... sumpBG_pre,sumpBG_stim,sumpBG_post,... TDRmean_pre,TDRmean_stim,TDR_pre,TDR_stim,TDR_preIndex,TDR_stimIndex,TDRshift,TDRSHIFT,TDRshiftIndex,index] = andor_INCfreemov_5_1_1(STIM,ARENA,FREQ,SELECT,welchtime,INDEX,fstim) [animalIndex, rowdata, stimIndex2, csvIndex2, speedmatrix, eegPyrmatrix, eegmatrix, power, fr, ...powerALL,... pWelch_pre, pWelch_stim, pWelch_post, fWelch, pWelchALL_pre, pWelchALL_stim, pWelchALL_post, lagDiff_pre, lagDiff_stimpost, rcor_pre, rcor_stimpost] = andor_INCfreemov_STEP5_eeg(STIM,ARENA,FREQ,SELECT,welchtime); % runindex, stillindex stillindex = false(size(speedmatrix,1),1); runindex = false(size(speedmatrix,1),1); speedav = NaN(size(speedmatrix,1),1); for k = 1:size(speedmatrix,1) for x = 1:4 meanprespeed(1,x) = mean(speedmatrix(k,(csvIndex2(k,3)-x*60):(csvIndex2(k,3)-(x-1)*60-1))); meanpostspeed(1,x) = mean(speedmatrix(k,(csvIndex2(k,5)+(x-1)*60):(csvIndex2(k,5)+x*60-1))); end if meanprespeed(1) < 4 stillindex(k,1) = true; end if all(meanprespeed > 4) runindex(k,1) = true; end speedav(k,1) = mean(meanprespeed); speedav_stim(k,1) = mean(speedmatrix(k,csvIndex2(k,3):csvIndex2(k,4))); speedav_post(k,1) = mean(meanpostspeed); end % thetaindex if strcmp(ARENA,'urethane') th1 = knnsearch(fWelch(1,:)',2.5); th2 = knnsearch(fWelch(1,:)',6); else th1 = knnsearch(fWelch(1,:)',5); th2 = knnsearch(fWelch(1,:)',12); end L = knnsearch(fWelch(1,:)',1); H = knnsearch(fWelch(1,:)',16); thetaindex = false(size(pWelch_pre,1),1); nonthetaindex = false(size(pWelch_pre,1),1); for k = 1:size(pWelch_pre,1) Lt = mean((pWelch_pre(k,L:th1-1))); th = mean((pWelch_pre(k,th1:th2))); Ht = mean((pWelch_pre(k,th2+1:H))); if and(th>Lt,th>Ht) thetaindex(k,1) = true; end if and(Lt>th,Lt>Ht) nonthetaindex(k,1) = true; end end % remindex, nonremindex if ~strcmp(ARENA,'urethane') xcorindex = false(size(rcor_pre,1),1); for k = 1:size(rcor_pre,1) temp = mean(rcor_pre{k,1},2); for x = 1:4 temp4(1,x) = mean(temp(((x-1)*20+1):(x*20))); end if all(temp4 < 0.5) xcorindex(k,1) = true; end end remindex = and(and(thetaindex,stillindex),xcorindex); nonremindex = and(nonthetaindex,stillindex); end %% INDEX selection (thetaindex,runindex,stillindex,remindex,nonremindex) index = eval(INDEX); if ~strcmp(ARENA,'urethane') speedav = speedav(index); speedav_stim = speedav_stim(index); speedav_post = speedav_post(index); else speedav = [];speedav_stim = []; speedav_post = []; end a = 1; data_pre = []; for k = 1:size(animalIndex,1) if ~isempty(animalIndex{k,1}) temp = false(size(pWelch_pre,1),1); temp(animalIndex{k,1}) = true; temp = and(temp,index); if ~isequal(sum(temp),0) data_pre(a,:) = mean(10*log10(pWelch_pre(temp,:)),1); a = a+1; end end end a = 1; data_stim = []; for k = 1:size(animalIndex,1) if ~isempty(animalIndex{k,1}) temp = false(size(pWelch_stim,1),1); temp(animalIndex{k,1}) = true; temp = and(temp,index); if ~isequal(sum(temp),0) data_stim(a,:) = mean(10*log10(pWelch_stim(temp,:)),1); a = a+1; end end end a = 1; d_pre = []; d_stim = []; welch_diff = []; welch_diffmean = []; for k = 1:size(animalIndex,1) if ~isempty(animalIndex{k,1}) temp = false(size(pWelch_stim,1),1); temp(animalIndex{k,1}) = true; temp = and(temp,index); if ~isequal(sum(temp),0) d_diff = []; b = 1; for d = 1:length(temp) if temp(d) d_pre = (pWelch_pre(d,:)); d_stim = (pWelch_stim(d,:)); welch_diff{a,1}(b,:) = 10*log10(d_stim./d_pre); b = b+1; end end welch_diffmean(a,:) = mean(welch_diff{a,1},1); a = a+1; end end end thetadom_pre = NaN(size(pWelch_pre,1),1); thetadom_stim = NaN(size(pWelch_pre,1),1); thetadom_post = NaN(size(pWelch_pre,1),1); thetashift = NaN(size(pWelch_pre,1),1); SP_stimF_pre = NaN(size(pWelch_pre,1),1); SP_stimF_stim = NaN(size(pWelch_pre,1),1); SP_stimF_post = NaN(size(pWelch_pre,1),1); SP_bgLOW_pre = NaN(size(pWelch_pre,1),1); SP_bgLOW_stim = NaN(size(pWelch_pre,1),1); SP_bgLOW_post = NaN(size(pWelch_pre,1),1); SP_bgHIGH_pre = NaN(size(pWelch_pre,1),1); SP_bgHIGH_stim = NaN(size(pWelch_pre,1),1); SP_bgHIGH_post = NaN(size(pWelch_pre,1),1); bg1 = knnsearch(fWelch(1,:)',22); bg2 = knnsearch(fWelch(1,:)',35); bgX = knnsearch(fWelch(1,:)',28); fstim1 = knnsearch(fWelch(1,:)',fstim-2); fstim2 = knnsearch(fWelch(1,:)',fstim+2); if ~strcmp(ARENA,'urethane') th1 = knnsearch(fWelch(1,:)',5); th2 = knnsearch(fWelch(1,:)',12); d1 = knnsearch(fWelch(1,:)',1); d2 = knnsearch(fWelch(1,:)',4); else th1 = knnsearch(fWelch(1,:)',2.5); th2 = knnsearch(fWelch(1,:)',6); d1 = knnsearch(fWelch(1,:)',1); d2 = knnsearch(fWelch(1,:)',2); end for k = 1:size(pWelch_pre,1) if ~all(isnan(pWelch_pre(k,:))) [pks, locs] = findpeaks(pWelch_pre(k,th1:th2)); if ~isempty(pks) [~,pksi] = max(pks); thetadom_pre(k,1) = fWelch(1,locs(pksi)+th1-1); end [pks, locs] = findpeaks(pWelch_stim(k,th1:th2)); if ~isempty(pks) [~,pksi] = max(pks); thetadom_stim(k,1) = fWelch(1,locs(pksi)+th1-1); end thetashift(k,1) = thetadom_stim(k,1)-thetadom_pre(k,1); [pks, locs] = findpeaks(pWelch_post(k,th1:th2)); if ~isempty(pks) [~,pksi] = max(pks); thetadom_post(k,1) = fWelch(1,locs(pksi)+th1-1); end SP_th_pre(k,1) = sum(pWelch_pre(k,th1:th2)); SP_th_stim(k,1) = sum(pWelch_stim(k,th1:th2)); SP_th_post(k,1) = sum(pWelch_post(k,th1:th2)); SP_d_pre(k,1) = sum(pWelch_pre(k,d1:d2)); SP_d_stim(k,1) = sum(pWelch_stim(k,d1:d2)); SP_d_post(k,1) = sum(pWelch_post(k,d1:d2)); TDR_pre(k,1) = SP_th_pre(k,1)/SP_d_pre(k,1); TDR_stim(k,1) = SP_th_stim(k,1)/SP_d_stim(k,1); TDR_post(k,1) = SP_th_post(k,1)/SP_d_post(k,1); TDRshift(k,1) = TDR_stim(k,1)-TDR_pre(k,1); SP_stimF_pre(k,1) = sum(pWelch_pre(k,fstim1:fstim2)); SP_stimF_stim(k,1) = sum(pWelch_stim(k,fstim1:fstim2)); SP_stimF_post(k,1) = sum(pWelch_post(k,fstim1:fstim2)); SP_bgLOW_pre(k,1) = sum(pWelch_pre(k,bg1:bgX-1)); SP_bgLOW_stim(k,1) = sum(pWelch_stim(k,bg1:bgX-1)); SP_bgLOW_post(k,1) = sum(pWelch_post(k,bg1:bgX-1)); SP_bgHIGH_pre(k,1) = sum(pWelch_pre(k,bgX:bg2)); SP_bgHIGH_stim(k,1) = sum(pWelch_stim(k,bgX:bg2)); SP_bgHIGH_post(k,1) = sum(pWelch_post(k,bgX:bg2)); end end a = 1; data_thshift = []; for k = 1:size(animalIndex,1) if ~isempty(animalIndex{k,1}) temp = false(size(pWelch_pre,1),1); temp(animalIndex{k,1}) = true; temp = and(temp,index); if ~isequal(sum(temp),0) data_thshift(a,:) = mean(thetashift(temp,:),1); sump_th_pre(a,1) = mean(SP_th_pre(temp,:),1); sump_th_stim(a,1) = mean(SP_th_stim(temp,:),1); sump_th_post(a,1) = mean(SP_th_post(temp,:),1); sump_d_pre(a,1) = mean(SP_d_pre(temp,:),1); sump_d_stim(a,1) = mean(SP_d_stim(temp,:),1); sump_d_post(a,1) = mean(SP_d_post(temp,:),1); TDRmean_pre(a,1) = mean(TDR_pre(temp,:),1); TDRmean_stim(a,1) = mean(TDR_stim(temp,:),1); TDRmean_post(a,1) = mean(TDR_post(temp,:),1); TDRSHIFT(a,1) = mean(TDRshift(temp,:),1); sump_stimF_pre(a,1) = mean(SP_stimF_pre(temp,:),1); sump_stimF_stim(a,1) = mean(SP_stimF_stim(temp,:),1); sump_stimF_post(a,1) = mean(SP_stimF_post(temp,:),1); sump_bgLOW_pre(a,1) = mean(SP_bgLOW_pre(temp,:),1); sump_bgLOW_stim(a,1) = mean(SP_bgLOW_stim(temp,:),1); sump_bgLOW_post(a,1) = mean(SP_bgLOW_post(temp,:),1); sump_bgHIGH_pre(a,1) = mean(SP_bgHIGH_pre(temp,:),1); sump_bgHIGH_stim(a,1) = mean(SP_bgHIGH_stim(temp,:),1); sump_bgHIGH_post(a,1) = mean(SP_bgHIGH_post(temp,:),1); sumpBG_pre(a,1) = mean(SP_bgLOW_pre(temp,:)+SP_bgHIGH_pre(temp,:),1); sumpBG_stim(a,1) = mean(SP_bgLOW_stim(temp,:)+SP_bgHIGH_stim(temp,:),1); sumpBG_post(a,1) = mean(SP_bgLOW_post(temp,:)+SP_bgHIGH_post(temp,:),1); a = a+1; end end end thetadom_PRE = thetadom_pre(index); thetadom_STIM = thetadom_stim(index); thetadom_POST = thetadom_post(index); thetaSHIFT = thetashift(index); TDR_preIndex = TDR_pre(index); TDR_stimIndex = TDR_stim(index); TDRshiftIndex = TDRshift(index);