%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Chip data processing and plots %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear, close all

data = csvread('data_chan1.csv');
data2 = csvread('LOG_DATA.txt');

data2s = reshape(data2,256,[]);
data2sc = data2s(8:256,:);
m = median(data2sc,1);
data2c = data2sc-ones(249,1)*m;
data2ss = reshape(data2c,1,[]);
data2sss = reshape(data2sc,1,[]);
figure(1)
hold on
plot(data2ss,'+r');
grid on
figure(2)
plot(m,'b');

x = norm(data2ss)/sqrt(length(data2ss));
x = x*ones(1,length(data2ss)/50000);
figure(3)
hold on
hist(data2ss,5000);
h = findobj(gca,'Type','patch');
set(h,'FaceColor','r','EdgeColor','k');
grid on
hold off
% data = reshape(data,64,[]);
% aver = mean(data,2);
% vari = var(data,0,2);
% cell = linspace(1,64,64);
% figure(1)
% for i=1:4001
% hold on
% plot(cell,data(:,i),'+r');
% end
% plot(cell,aver,'ob');
% hold off
% 
% figure(2)
% plot(cell,vari, '+b');