i=1;
HF_Xcorr_combined_maxfreq_NT=[];
HF_Xcorr_combined_maxfreq_T=[];
HF_TremorFreqMax_NT=[];
HF_TremorFreqMax_T=[];
HF_Xcorr_ind_maxfreq_NT=[];
HF_Xcorr_ind_maxfreq_T=[];
HF_ind_TremorFreq_NT=[];
HF_ind_TremorFreq_T=[];
HF_ind_FiringFreq_NT=[];
HF_ind_FiringFreq_T=[];

while true
[file,path]=uigetfile('*.mat');
cd(path)
load(file)
% HF_Xcorr_combined_maxfreq_NT=vertcat(HF_Xcorr_combined_maxfreq_NT,Report.Xcorr.label1_maxfreq(:,end));
% HF_Xcorr_combined_maxfreq_T=vertcat(HF_Xcorr_combined_maxfreq_T,Report.Xcorr.label2_maxfreq(:,end));
% HF_TremorFreqMax_NT=vertcat(HF_TremorFreqMax_NT,Report.TremorFreq.label1_freqMax(:,end));
% HF_TremorFreqMax_T=vertcat(HF_TremorFreqMax_T,Report.TremorFreq.label2_freqMax(:,end));
% [r1,c1]=size(Report.Xcorr.label1_maxfreq);
% [r2,c2]=size(Report.Xcorr.label2_maxfreq);
% HF_Xcorr_ind_maxfreq_NT=vertcat(HF_Xcorr_ind_maxfreq_NT,reshape(Report.Xcorr.label1_maxfreq(:,1:end-1),[r1*(c1-1),1]));
% HF_ind_TremorFreq_NT=vertcat(HF_ind_TremorFreq_NT,repmat(cell2mat(Report.TremorFreq.label1_freqMax(:,end)),c1-1,1));
% HF_Xcorr_ind_maxfreq_T=vertcat(HF_Xcorr_ind_maxfreq_T,reshape(Report.Xcorr.label2_maxfreq(:,1:end-1),[r2*(c2-1),1]));
% HF_ind_TremorFreq_T=vertcat(HF_ind_TremorFreq_T,repmat(cell2mat(Report.TremorFreq.label2_freqMax(:,end)),c2-1,1));

[r1,c1]=size(Report.FR.label1);
[r2,c2]=size(Report.FR.label2);
HF_ind_FiringFreq_NT=vertcat(HF_ind_FiringFreq_NT,reshape(Report.FR.label1(2:end,2:end),[(r1-1)*(c1-1),1]));
HF_ind_FiringFreq_T=vertcat(HF_ind_FiringFreq_T,reshape(Report.FR.label2(2:end,2:end),[(r2-1)*(c2-1),1]));

i=i+1
cd ..
end

HF_Xcorr_ind_vs_TremorFreq_Compilation.Xcorr_ind_maxfreq_NT=HF_Xcorr_ind_maxfreq_NT;
HF_Xcorr_ind_vs_TremorFreq_Compilation.Xcorr_ind_maxfreq_T=HF_Xcorr_ind_maxfreq_T;
HF_Xcorr_ind_vs_TremorFreq_Compilation.ind_TremorFreq_NT=HF_ind_TremorFreq_NT;
HF_Xcorr_ind_vs_TremorFreq_Compilation.ind_TremorFreq_T=HF_ind_TremorFreq_T;
HF_Xcorr_ind_vs_TremorFreq_Compilation.ind_FR_NT=cell2mat(HF_ind_FiringFreq_NT);
HF_Xcorr_ind_vs_TremorFreq_Compilation.ind_FR_T=cell2mat(HF_ind_FiringFreq_T);
save('HF_Xcorr_ind_vs_TremorFreq_Compilation(+FR).mat','HF_Xcorr_ind_vs_TremorFreq_Compilation')

lim=40;
figure()
tempx=cell2mat(HF_XcorrCombined_vs_TremorFreq_Compilation.TremorFreqMax_T);
tempy=HF_XcorrCombined_vs_TremorFreq_Compilation.Xcorr_combined_maxfreq_T;
scatter(tempx,tempy)

X = [ones(length(tempx),1) tempx];
b = X\tempy;
line([0 lim],[b(1) b(1)+lim*b(2)],'Color','blue')
text(lim*0.55,lim*0.4,sprintf('y=%.4fx+%.4f',b(2),b(1)))
line([0 lim],[0 lim],'Color','red','LineStyle','--')
xlim([0 lim])
ylim([0 100])

FSIc=(tempy-tempx).^2;
FSIc(1,2)=mean(FSIc(:,1));

xlabel('Tremor Frequency (hz)')
ylabel('Modulation Peak Frequency(Combined) (hz)')
title(sprintf('FSI:%.2f Modulation Frequency(Combined) vs. Tremor Frequency',FSIc(1,2)))
saveas(gcf,"XcorrModCombined_vs_TremorFreq.fig")
saveas(gcf,"XcorrModCombined_vs_TremorFreq.jpg")

lim=40;
figure()
tempx=HF_ind_TremorFreq_T;
tempy=HF_Xcorr_ind_maxfreq_T;
scatter(tempx,tempy)

X = [ones(length(tempx),1) tempx];
b = X\tempy;
line([0 lim],[b(1) b(1)+lim*b(2)],'Color','blue')
text(lim*0.55,lim*0.4,sprintf('y=%.4fx+%.4f',b(2),b(1)))
line([0 lim],[0 lim],'Color','red','LineStyle','--')
xlim([0 lim])
ylim([0 100])

FSI=(tempy-tempx).^2;
FSI(1,2)=mean(FSI(:,1));

xlabel('Tremor Frequency (hz)')
ylabel('Modulation Peak Frequency(individual) (hz)')
title(sprintf('FSI:%.2f Modulation Frequency(individual) vs. Tremor Frequency',FSI(1,2)))
saveas(gcf,"XcorrModInd_vs_TremorFreq.fig")
saveas(gcf,"XcorrModInd_vs_TremorFreq.jpg")

%% FR all
tempx=HF_Xcorr_ind_vs_TremorFreq_Compilation.ind_TremorFreq_T;
tempy=HF_Xcorr_ind_vs_TremorFreq_Compilation.ind_FR_T;
lim=50;

figure()
scatter(tempx,tempy)
line([0 lim],[0 lim],'Color','black','LineStyle','--')
xlim([0 lim])
ylim([0 100])
xlabel('Tremor Frequency (hz)')
ylabel('Firing Frequency (hz)')
saveas(gcf,'FR_vs_TremorFreq.fig')

% %% remove false tremor points
% HF_Xcorr_ind_vs_TremorFreq_Compilation.Xcorr_ind_maxfreq_T(find(tempx<10))=[];  
% HF_Xcorr_ind_vs_TremorFreq_Compilation.ind_FR_T(find(tempx<10))=[];
% HF_Xcorr_ind_vs_TremorFreq_Compilation.ind_TremorFreq_T(find(tempx<10))=[];
