clear all;close all;
all_SC=[];
all_PC=[];
all_HPC=[];
all_tuning_strength=[];

% --- Step 1: Load and Preprocess Data ---
vel_threshold=4;

cd 'D:\ACC Data\ACC\AllDays\635\day9'; 



% Step 1.2: Load spike data and extract relevant neurons
data=table2array(readtable('XYtotalspike2.csv'));
SCid=table2array(readtable('SC.csv'));
SCid=find(SCid(:,2));
spike_angle_ms1=cell(length(SCid),1);
bad_data=find(data(:,2)==1);
x_posn=data(:,2);
x_posn=smooth(x_posn,15);
y_posn=data(:,3);
y_posn=smooth(y_posn,15);
x_posn(bad_data)=[];
y_posn(bad_data)=[];
data(bad_data,:)=[];
vel_cmsec=abs(diff(x_posn))+abs(diff(y_posn))*(1/0.05);
vel_cmsec=smooth(vel_cmsec,30);
still_periods=find(vel_cmsec<vel_threshold);
data=data(1:end,6:end);
data=data(:,SCid);
notactive=find(sum(data)<5);
data(:,notactive)=[];
data(still_periods,:)=[];


spike_data=data;
% Assumed original matrix: `spike_data`
num_rows = size(spike_data, 1);
num_neurons = size(spike_data, 2);

% Calculate the number of rows to pad
rows_to_pad = mod(-num_rows, 3);

% Pad the matrix with zeros at the end
padded_data = [spike_data; zeros(rows_to_pad, num_neurons)];


% Reshape the padded data into a 3D array
num_new_rows = size(padded_data, 1) / 3;
binned_3d = reshape(padded_data, 3, num_new_rows, num_neurons);

% Sum the spikes within each bin
summed_binned = squeeze(sum(binned_3d, 1));

% Convert the summed data back to binary (0 or 1)
final_binned_data = double(summed_binned > 0);

sync_val=(sum(final_binned_data,2));
sync_val=sync_val/size(data,2);
all_SC=[all_SC;mean(nonzeros(sync_val))];


% --- Step 1: Load and Preprocess Data ---
vel_threshold=4;

cd 'D:\ACC Data\ACC\AllDays\681\day9'; 




% Step 1.2: Load spike data and extract relevant neurons
data=table2array(readtable('XYtotalspike2.csv'));
SCid=table2array(readtable('SC.csv'));
SCid=find(SCid(:,2));
spike_angle_ms1=cell(length(SCid),1);
bad_data=find(data(:,2)==1);
x_posn=data(:,2);
x_posn=smooth(x_posn,15);
y_posn=data(:,3);
y_posn=smooth(y_posn,15);
x_posn(bad_data)=[];
y_posn(bad_data)=[];
data(bad_data,:)=[];
vel_cmsec=abs(diff(x_posn))+abs(diff(y_posn))*(1/0.05);
vel_cmsec=smooth(vel_cmsec,30);
still_periods=find(vel_cmsec<vel_threshold);
data=data(1:end,6:end);
data=data(:,SCid);
notactive=find(sum(data)<5);
data(:,notactive)=[];
data(still_periods,:)=[];


spike_data=data;
% Assumed original matrix: `spike_data`
num_rows = size(spike_data, 1);
num_neurons = size(spike_data, 2);

% Calculate the number of rows to pad
rows_to_pad = mod(-num_rows, 3);

% Pad the matrix with zeros at the end
padded_data = [spike_data; zeros(rows_to_pad, num_neurons)];


% Reshape the padded data into a 3D array
num_new_rows = size(padded_data, 1) / 3;
binned_3d = reshape(padded_data, 3, num_new_rows, num_neurons);

% Sum the spikes within each bin
summed_binned = squeeze(sum(binned_3d, 1));

% Convert the summed data back to binary (0 or 1)
final_binned_data = double(summed_binned > 0);

sync_val=(sum(final_binned_data,2));
sync_val=sync_val/size(data,2);
all_SC=[all_SC;mean(nonzeros(sync_val))];


% --- Step 1: Load and Preprocess Data ---
vel_threshold=4;

cd 'D:\ACC Data\ACC\AllDays\691\day9'; 



% Step 1.2: Load spike data and extract relevant neurons
data=table2array(readtable('XYtotalspike2.csv'));
SCid=table2array(readtable('SC.csv'));
SCid=find(SCid(:,2));
spike_angle_ms1=cell(length(SCid),1);
bad_data=find(data(:,2)==1);
x_posn=data(:,2);
x_posn=smooth(x_posn,15);
y_posn=data(:,3);
y_posn=smooth(y_posn,15);
x_posn(bad_data)=[];
y_posn(bad_data)=[];
data(bad_data,:)=[];
vel_cmsec=abs(diff(x_posn))+abs(diff(y_posn))*(1/0.05);
vel_cmsec=smooth(vel_cmsec,30);
still_periods=find(vel_cmsec<vel_threshold);
data=data(1:end,6:end);
data=data(:,SCid);
notactive=find(sum(data)<5);
data(:,notactive)=[];
data(still_periods,:)=[];


spike_data=data;
% Assumed original matrix: `spike_data`
num_rows = size(spike_data, 1);
num_neurons = size(spike_data, 2);

% Calculate the number of rows to pad
rows_to_pad = mod(-num_rows, 3);

% Pad the matrix with zeros at the end
padded_data = [spike_data; zeros(rows_to_pad, num_neurons)];


% Reshape the padded data into a 3D array
num_new_rows = size(padded_data, 1) / 3;
binned_3d = reshape(padded_data, 3, num_new_rows, num_neurons);

% Sum the spikes within each bin
summed_binned = squeeze(sum(binned_3d, 1));

% Convert the summed data back to binary (0 or 1)
final_binned_data = double(summed_binned > 0);

sync_val=(sum(final_binned_data,2));
sync_val=sync_val/size(data,2);
all_SC=[all_SC;mean(nonzeros(sync_val))];


% --- Step 1: Load and Preprocess Data ---
vel_threshold=4;

cd 'D:\ACC Data\ACC\AllDays\747\day9'; 


% Step 1.2: Load spike data and extract relevant neurons
data=table2array(readtable('XYtotalspike2.csv'));
SCid=table2array(readtable('SC.csv'));
SCid=find(SCid(:,2));
spike_angle_ms1=cell(length(SCid),1);
bad_data=find(data(:,2)==1);
x_posn=data(:,2);
x_posn=smooth(x_posn,15);
y_posn=data(:,3);
y_posn=smooth(y_posn,15);
x_posn(bad_data)=[];
y_posn(bad_data)=[];
data(bad_data,:)=[];
vel_cmsec=abs(diff(x_posn))+abs(diff(y_posn))*(1/0.05);
vel_cmsec=smooth(vel_cmsec,30);
still_periods=find(vel_cmsec<vel_threshold);
data=data(1:end,6:end);
data=data(:,SCid);
notactive=find(sum(data)<5);
data(:,notactive)=[];
data(still_periods,:)=[];


spike_data=data;
% Assumed original matrix: `spike_data`
num_rows = size(spike_data, 1);
num_neurons = size(spike_data, 2);

% Calculate the number of rows to pad
rows_to_pad = mod(-num_rows, 3);

% Pad the matrix with zeros at the end
padded_data = [spike_data; zeros(rows_to_pad, num_neurons)];


% Reshape the padded data into a 3D array
num_new_rows = size(padded_data, 1) / 3;
binned_3d = reshape(padded_data, 3, num_new_rows, num_neurons);

% Sum the spikes within each bin
summed_binned = squeeze(sum(binned_3d, 1));

% Convert the summed data back to binary (0 or 1)
final_binned_data = double(summed_binned > 0);

sync_val=(sum(final_binned_data,2));
sync_val=sync_val/size(data,2);
all_SC=[all_SC;mean(nonzeros(sync_val))];



% --- Step 1: Load and Preprocess Data ---
vel_threshold=4;

cd 'D:\ACC Data\ACC\AllDays\635\day9'; 
heading=table2array(readtable('heading.csv'));
head_pos=heading(:,5:6);
body_pos=heading(:,8:9);

% Step 1.1: Calculate the heading angle for the entire session
delta_x = head_pos(:, 1) - body_pos(:, 1);
delta_y = head_pos(:, 2) - body_pos(:, 2);
heading_angle_rad = atan2(delta_x, delta_y);
heading_angle_deg = heading_angle_rad * (180 / pi);
heading_angle = heading_angle_deg;
heading_angle(heading_angle < 0) = heading_angle(heading_angle < 0) + 360;

% Step 1.2: Load spike data and extract relevant neurons
data=table2array(readtable('XYtotalspike2.csv'));
SCid=table2array(readtable('pPC.csv'));
SCid=find(SCid(:,2));
spike_angle_ms1=cell(length(SCid),1);
bad_data=find(data(:,2)==1);
x_posn=data(:,2);
x_posn=smooth(x_posn,15);
y_posn=data(:,3);
y_posn=smooth(y_posn,15);
x_posn(bad_data)=[];
y_posn(bad_data)=[];
data(bad_data,:)=[];
vel_cmsec=abs(diff(x_posn))+abs(diff(y_posn))*(1/0.05);
vel_cmsec=smooth(vel_cmsec,30);
still_periods=find(vel_cmsec<vel_threshold);
data=data(1:end,6:end);
data=data(:,SCid);
notactive=find(sum(data)<5);
data(:,notactive)=[];
data(still_periods,:)=[];


spike_data=data;
% Assumed original matrix: `spike_data`
num_rows = size(spike_data, 1);
num_neurons = size(spike_data, 2);

% Calculate the number of rows to pad
rows_to_pad = mod(-num_rows, 3);

% Pad the matrix with zeros at the end
padded_data = [spike_data; zeros(rows_to_pad, num_neurons)];


% Reshape the padded data into a 3D array
num_new_rows = size(padded_data, 1) / 3;
binned_3d = reshape(padded_data, 3, num_new_rows, num_neurons);

% Sum the spikes within each bin
summed_binned = squeeze(sum(binned_3d, 1));

% Convert the summed data back to binary (0 or 1)
final_binned_data = double(summed_binned > 0);

sync_val=(sum(final_binned_data,2));
sync_val=sync_val/size(data,2);
all_PC=[all_PC;mean(nonzeros(sync_val))];


% --- Step 1: Load and Preprocess Data ---
vel_threshold=4;

cd 'D:\ACC Data\ACC\AllDays\681\day9'; 
heading=table2array(readtable('heading.csv'));
head_pos=heading(:,5:6);
body_pos=heading(:,8:9);

% Step 1.1: Calculate the heading angle for the entire session
delta_x = head_pos(:, 1) - body_pos(:, 1);
delta_y = head_pos(:, 2) - body_pos(:, 2);
heading_angle_rad = atan2(delta_x, delta_y);
heading_angle_deg = heading_angle_rad * (180 / pi);
heading_angle = heading_angle_deg;
heading_angle(heading_angle < 0) = heading_angle(heading_angle < 0) + 360;

% Step 1.2: Load spike data and extract relevant neurons
data=table2array(readtable('XYtotalspike2.csv'));
SCid=table2array(readtable('pPC.csv'));
SCid=find(SCid(:,2));
spike_angle_ms1=cell(length(SCid),1);
bad_data=find(data(:,2)==1);
x_posn=data(:,2);
x_posn=smooth(x_posn,15);
y_posn=data(:,3);
y_posn=smooth(y_posn,15);
x_posn(bad_data)=[];
y_posn(bad_data)=[];
data(bad_data,:)=[];
vel_cmsec=abs(diff(x_posn))+abs(diff(y_posn))*(1/0.05);
vel_cmsec=smooth(vel_cmsec,30);
still_periods=find(vel_cmsec<vel_threshold);
data=data(1:end,6:end);
data=data(:,SCid);
notactive=find(sum(data)<5);
data(:,notactive)=[];
data(still_periods,:)=[];


spike_data=data;
% Assumed original matrix: `spike_data`
num_rows = size(spike_data, 1);
num_neurons = size(spike_data, 2);

% Calculate the number of rows to pad
rows_to_pad = mod(-num_rows, 3);

% Pad the matrix with zeros at the end
padded_data = [spike_data; zeros(rows_to_pad, num_neurons)];


% Reshape the padded data into a 3D array
num_new_rows = size(padded_data, 1) / 3;
binned_3d = reshape(padded_data, 3, num_new_rows, num_neurons);

% Sum the spikes within each bin
summed_binned = squeeze(sum(binned_3d, 1));

% Convert the summed data back to binary (0 or 1)
final_binned_data = double(summed_binned > 0);

sync_val=(sum(final_binned_data,2));
sync_val=sync_val/size(data,2);
all_PC=[all_PC;mean(nonzeros(sync_val))];


% --- Step 1: Load and Preprocess Data ---
vel_threshold=4;

cd 'D:\ACC Data\ACC\AllDays\691\day9'; 
heading=table2array(readtable('heading.csv'));
head_pos=heading(:,5:6);
body_pos=heading(:,8:9);

% Step 1.1: Calculate the heading angle for the entire session
delta_x = head_pos(:, 1) - body_pos(:, 1);
delta_y = head_pos(:, 2) - body_pos(:, 2);
heading_angle_rad = atan2(delta_x, delta_y);
heading_angle_deg = heading_angle_rad * (180 / pi);
heading_angle = heading_angle_deg;
heading_angle(heading_angle < 0) = heading_angle(heading_angle < 0) + 360;

% Step 1.2: Load spike data and extract relevant neurons
data=table2array(readtable('XYtotalspike2.csv'));
SCid=table2array(readtable('pPC.csv'));
SCid=find(SCid(:,2));
spike_angle_ms1=cell(length(SCid),1);
bad_data=find(data(:,2)==1);
x_posn=data(:,2);
x_posn=smooth(x_posn,15);
y_posn=data(:,3);
y_posn=smooth(y_posn,15);
x_posn(bad_data)=[];
y_posn(bad_data)=[];
data(bad_data,:)=[];
vel_cmsec=abs(diff(x_posn))+abs(diff(y_posn))*(1/0.05);
vel_cmsec=smooth(vel_cmsec,30);
still_periods=find(vel_cmsec<vel_threshold);
data=data(1:end,6:end);
data=data(:,SCid);
notactive=find(sum(data)<5);
data(:,notactive)=[];
data(still_periods,:)=[];


spike_data=data;
% Assumed original matrix: `spike_data`
num_rows = size(spike_data, 1);
num_neurons = size(spike_data, 2);

% Calculate the number of rows to pad
rows_to_pad = mod(-num_rows, 3);

% Pad the matrix with zeros at the end
padded_data = [spike_data; zeros(rows_to_pad, num_neurons)];


% Reshape the padded data into a 3D array
num_new_rows = size(padded_data, 1) / 3;
binned_3d = reshape(padded_data, 3, num_new_rows, num_neurons);

% Sum the spikes within each bin
summed_binned = squeeze(sum(binned_3d, 1));

% Convert the summed data back to binary (0 or 1)
final_binned_data = double(summed_binned > 0);

sync_val=(sum(final_binned_data,2));
sync_val=sync_val/size(data,2);
all_PC=[all_PC;mean(nonzeros(sync_val))];


% --- Step 1: Load and Preprocess Data ---
vel_threshold=4;

cd 'D:\ACC Data\ACC\AllDays\747\day9'; 
heading=table2array(readtable('heading.csv'));
head_pos=heading(:,5:6);
body_pos=heading(:,8:9);

% Step 1.1: Calculate the heading angle for the entire session
delta_x = head_pos(:, 1) - body_pos(:, 1);
delta_y = head_pos(:, 2) - body_pos(:, 2);
heading_angle_rad = atan2(delta_x, delta_y);
heading_angle_deg = heading_angle_rad * (180 / pi);
heading_angle = heading_angle_deg;
heading_angle(heading_angle < 0) = heading_angle(heading_angle < 0) + 360;

% Step 1.2: Load spike data and extract relevant neurons
data=table2array(readtable('XYtotalspike2.csv'));
SCid=table2array(readtable('pPC.csv'));
SCid=find(SCid(:,2));
spike_angle_ms1=cell(length(SCid),1);
bad_data=find(data(:,2)==1);
x_posn=data(:,2);
x_posn=smooth(x_posn,15);
y_posn=data(:,3);
y_posn=smooth(y_posn,15);
x_posn(bad_data)=[];
y_posn(bad_data)=[];
data(bad_data,:)=[];
vel_cmsec=abs(diff(x_posn))+abs(diff(y_posn))*(1/0.05);
vel_cmsec=smooth(vel_cmsec,30);
still_periods=find(vel_cmsec<vel_threshold);
data=data(1:end,6:end);
data=data(:,SCid);
notactive=find(sum(data)<5);
data(:,notactive)=[];
data(still_periods,:)=[];


spike_data=data;
% Assumed original matrix: `spike_data`
num_rows = size(spike_data, 1);
num_neurons = size(spike_data, 2);

% Calculate the number of rows to pad
rows_to_pad = mod(-num_rows, 3);

% Pad the matrix with zeros at the end
padded_data = [spike_data; zeros(rows_to_pad, num_neurons)];


% Reshape the padded data into a 3D array
num_new_rows = size(padded_data, 1) / 3;
binned_3d = reshape(padded_data, 3, num_new_rows, num_neurons);

% Sum the spikes within each bin
summed_binned = squeeze(sum(binned_3d, 1));

% Convert the summed data back to binary (0 or 1)
final_binned_data = double(summed_binned > 0);

sync_val=(sum(final_binned_data,2));
sync_val=sync_val/size(data,2);
all_PC=[all_PC;mean(nonzeros(sync_val))];


histogram(all_PC,0.01:0.05:1)
figure;histogram(all_SC,0.01:0.05:1)


% --- Step 1: Load and Preprocess Data ---
vel_threshold=4;

cd 'D:\ACC Data\HPC\57\day9'; 

% Step 1.2: Load spike data and extract relevant neurons
data=table2array(readtable('XYtotalspike2.csv'));
SCid=table2array(readtable('pPC.csv'));
SCid=find(SCid(:,2));
spike_angle_ms1=cell(length(SCid),1);
bad_data=find(data(:,2)==1);
x_posn=data(:,2);
x_posn=smooth(x_posn,15);
y_posn=data(:,3);
y_posn=smooth(y_posn,15);
x_posn(bad_data)=[];
y_posn(bad_data)=[];
data(bad_data,:)=[];
vel_cmsec=abs(diff(x_posn))+abs(diff(y_posn))*(1/0.05);
vel_cmsec=smooth(vel_cmsec,30);
still_periods=find(vel_cmsec<vel_threshold);
data=data(1:end,6:end);
data=data(:,SCid);
notactive=find(sum(data)<5);
data(:,notactive)=[];
data(still_periods,:)=[];


spike_data=data;
% Assumed original matrix: `spike_data`
num_rows = size(spike_data, 1);
num_neurons = size(spike_data, 2);

% Calculate the number of rows to pad
rows_to_pad = mod(-num_rows, 3);

% Pad the matrix with zeros at the end
padded_data = [spike_data; zeros(rows_to_pad, num_neurons)];


% Reshape the padded data into a 3D array
num_new_rows = size(padded_data, 1) / 3;
binned_3d = reshape(padded_data, 3, num_new_rows, num_neurons);

% Sum the spikes within each bin
summed_binned = squeeze(sum(binned_3d, 1));

% Convert the summed data back to binary (0 or 1)
final_binned_data = double(summed_binned > 0);

sync_val=(sum(final_binned_data,2));
sync_val=sync_val/size(data,2);
all_HPC=[all_HPC;mean(nonzeros(sync_val))];



cd 'D:\ACC Data\HPC\221\day9'; 

% Step 1.2: Load spike data and extract relevant neurons
data=table2array(readtable('XYtotalspike2.csv'));
SCid=table2array(readtable('pPC.csv'));
SCid=find(SCid(:,2));
spike_angle_ms1=cell(length(SCid),1);
bad_data=find(data(:,2)==1);
x_posn=data(:,2);
x_posn=smooth(x_posn,15);
y_posn=data(:,3);
y_posn=smooth(y_posn,15);
x_posn(bad_data)=[];
y_posn(bad_data)=[];
data(bad_data,:)=[];
vel_cmsec=abs(diff(x_posn))+abs(diff(y_posn))*(1/0.05);
vel_cmsec=smooth(vel_cmsec,30);
still_periods=find(vel_cmsec<vel_threshold);
data=data(1:end,6:end);
data=data(:,SCid);
notactive=find(sum(data)<5);
data(:,notactive)=[];
data(still_periods,:)=[];


spike_data=data;
% Assumed original matrix: `spike_data`
num_rows = size(spike_data, 1);
num_neurons = size(spike_data, 2);

% Calculate the number of rows to pad
rows_to_pad = mod(-num_rows, 3);

% Pad the matrix with zeros at the end
padded_data = [spike_data; zeros(rows_to_pad, num_neurons)];


% Reshape the padded data into a 3D array
num_new_rows = size(padded_data, 1) / 3;
binned_3d = reshape(padded_data, 3, num_new_rows, num_neurons);

% Sum the spikes within each bin
summed_binned = squeeze(sum(binned_3d, 1));

% Convert the summed data back to binary (0 or 1)
final_binned_data = double(summed_binned > 0);

sync_val=(sum(final_binned_data,2));
sync_val=sync_val/size(data,2);
all_HPC=[all_HPC;mean(nonzeros(sync_val))];



cd 'D:\ACC Data\HPC\851\day9'; 

% Step 1.2: Load spike data and extract relevant neurons
data=table2array(readtable('XYtotalspike2.csv'));
SCid=table2array(readtable('pPC.csv'));
SCid=find(SCid(:,2));
spike_angle_ms1=cell(length(SCid),1);
bad_data=find(data(:,2)==1);
x_posn=data(:,2);
x_posn=smooth(x_posn,15);
y_posn=data(:,3);
y_posn=smooth(y_posn,15);
x_posn(bad_data)=[];
y_posn(bad_data)=[];
data(bad_data,:)=[];
vel_cmsec=abs(diff(x_posn))+abs(diff(y_posn))*(1/0.05);
vel_cmsec=smooth(vel_cmsec,30);
still_periods=find(vel_cmsec<vel_threshold);
data=data(1:end,6:end);
data=data(:,SCid);
notactive=find(sum(data)<5);
data(:,notactive)=[];
data(still_periods,:)=[];


spike_data=data;
% Assumed original matrix: `spike_data`
num_rows = size(spike_data, 1);
num_neurons = size(spike_data, 2);

% Calculate the number of rows to pad
rows_to_pad = mod(-num_rows, 3);

% Pad the matrix with zeros at the end
padded_data = [spike_data; zeros(rows_to_pad, num_neurons)];


% Reshape the padded data into a 3D array
num_new_rows = size(padded_data, 1) / 3;
binned_3d = reshape(padded_data, 3, num_new_rows, num_neurons);

% Sum the spikes within each bin
summed_binned = squeeze(sum(binned_3d, 1));

% Convert the summed data back to binary (0 or 1)
final_binned_data = double(summed_binned > 0);

sync_val=(sum(final_binned_data,2));
sync_val=sync_val/size(data,2);
all_HPC=[all_HPC;mean(nonzeros(sync_val))];



cd 'D:\ACC Data\HPC\887\day9'; 

% Step 1.2: Load spike data and extract relevant neurons
data=table2array(readtable('XYtotalspike2.csv'));
SCid=table2array(readtable('pPC.csv'));
SCid=find(SCid(:,2));
spike_angle_ms1=cell(length(SCid),1);
bad_data=find(data(:,2)==1);
x_posn=data(:,2);
x_posn=smooth(x_posn,15);
y_posn=data(:,3);
y_posn=smooth(y_posn,15);
x_posn(bad_data)=[];
y_posn(bad_data)=[];
data(bad_data,:)=[];
vel_cmsec=abs(diff(x_posn))+abs(diff(y_posn))*(1/0.05);
vel_cmsec=smooth(vel_cmsec,30);
still_periods=find(vel_cmsec<vel_threshold);
data=data(1:end,6:end);
data=data(:,SCid);
notactive=find(sum(data)<5);
data(:,notactive)=[];
data(still_periods,:)=[];


spike_data=data;
% Assumed original matrix: `spike_data`
num_rows = size(spike_data, 1);
num_neurons = size(spike_data, 2);

% Calculate the number of rows to pad
rows_to_pad = mod(-num_rows, 3);

% Pad the matrix with zeros at the end
padded_data = [spike_data; zeros(rows_to_pad, num_neurons)];


% Reshape the padded data into a 3D array
num_new_rows = size(padded_data, 1) / 3;
binned_3d = reshape(padded_data, 3, num_new_rows, num_neurons);

% Sum the spikes within each bin
summed_binned = squeeze(sum(binned_3d, 1));

% Convert the summed data back to binary (0 or 1)
final_binned_data = double(summed_binned > 0);

sync_val=(sum(final_binned_data,2));
sync_val=sync_val/size(data,2);
all_HPC=[all_HPC;mean(nonzeros(sync_val))];



cd 'D:\ACC Data\HPC\987\day9'; 

% Step 1.2: Load spike data and extract relevant neurons
data=table2array(readtable('XYtotalspike2.csv'));
SCid=table2array(readtable('pPC.csv'));
SCid=find(SCid(:,2));
spike_angle_ms1=cell(length(SCid),1);
bad_data=find(data(:,2)==1);
x_posn=data(:,2);
x_posn=smooth(x_posn,15);
y_posn=data(:,3);
y_posn=smooth(y_posn,15);
x_posn(bad_data)=[];
y_posn(bad_data)=[];
data(bad_data,:)=[];
vel_cmsec=abs(diff(x_posn))+abs(diff(y_posn))*(1/0.05);
vel_cmsec=smooth(vel_cmsec,30);
still_periods=find(vel_cmsec<vel_threshold);
data=data(1:end,6:end);
data=data(:,SCid);
notactive=find(sum(data)<5);
data(:,notactive)=[];
data(still_periods,:)=[];


spike_data=data;
% Assumed original matrix: `spike_data`
num_rows = size(spike_data, 1);
num_neurons = size(spike_data, 2);

% Calculate the number of rows to pad
rows_to_pad = mod(-num_rows, 3);

% Pad the matrix with zeros at the end
padded_data = [spike_data; zeros(rows_to_pad, num_neurons)];


% Reshape the padded data into a 3D array
num_new_rows = size(padded_data, 1) / 3;
binned_3d = reshape(padded_data, 3, num_new_rows, num_neurons);

% Sum the spikes within each bin
summed_binned = squeeze(sum(binned_3d, 1));

% Convert the summed data back to binary (0 or 1)
final_binned_data = double(summed_binned > 0);

sync_val=(sum(final_binned_data,2));
sync_val=sync_val/size(data,2);
all_HPC=[all_HPC;mean(nonzeros(sync_val))];

all_HPC=nonzeros(all_HPC);
all_PC=nonzeros(all_PC);
all_SC=nonzeros(all_SC);

 mean(all_HPC)
 std(all_HPC) / sqrt(length(all_HPC))

 mean(all_PC)
 std(all_PC) / sqrt(length(all_PC))

 mean(all_SC)
 std(all_SC) / sqrt(length(all_SC))