format long g;

% define data processing constants
SET_SIZE = 16;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%load data file
%data = load('chsh_0_20141124.dat');
%data = load('chsh_180_20141125');
data_1 = load('chsh_h_optimise_20141129.dat');
data_2 = load('chsh_h_optimise_20141201.dat');
data_3 = load('chsh_h_optimise_20141202.dat');
data = [data_1; data_2; data_3];

% evaluate the number of complete set
NO_OF_SET = floor(size(data, 1)/SET_SIZE);

% truncating incomplete set
data(NO_OF_SET*SET_SIZE + 1 :end, :) = [];

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

data_singles = data(:, [2, 3]);
data_singles_total = data(:, 1);

%data_2_fold = reshape(data(:, 4), 4, 4*NO_OF_SET);
%data_singles = data_singles./[data(:, 1), data(:, 1)];

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

a0_1_total = data_singles_total(1:16:end);
a0_2_total = data_singles_total(3:16:end);
a0_3_total = data_singles_total(5:16:end);
a0_4_total = data_singles_total(7:16:end);
a0_total = [a0_1_total; a0_2_total; a0_3_total; a0_4_total];

a0_90_1_total = data_singles_total(2:16:end);
a0_90_2_total = data_singles_total(4:16:end);
a0_90_3_total = data_singles_total(6:16:end);
a0_90_4_total = data_singles_total(8:16:end);
a0_90_total = [a0_90_1_total; a0_90_2_total; a0_90_3_total; a0_90_4_total];

a0_1_total = data_singles_total(1:16:end);
a0_2_total = data_singles_total(3:16:end);
a0_3_total = data_singles_total(5:16:end);
a0_4_total = data_singles_total(7:16:end);
a0_total = [a0_1_total; a0_2_total; a0_3_total; a0_4_total];

a0_90_1_total = data_singles_total(2:16:end);
a0_90_2_total = data_singles_total(4:16:end);
a0_90_3_total = data_singles_total(6:16:end);
a0_90_4_total = data_singles_total(8:16:end);
a0_90_total = [a0_90_1_total; a0_90_2_total; a0_90_3_total; a0_90_4_total];

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

a1_1_total = data_singles_total(9:16:end);
a1_2_total = data_singles_total(11:16:end);
a1_3_total = data_singles_total(13:16:end);
a1_4_total = data_singles_total(15:16:end);
a1_total = [a1_1_total; a1_2_total; a1_3_total; a1_4_total];

a1_90_1_total = data_singles_total(10:16:end);
a1_90_2_total = data_singles_total(12:16:end);
a1_90_3_total= data_singles_total(14:16:end);
a1_90_4_total = data_singles_total(16:16:end);
a1_90_total = [a1_90_1_total; a1_90_2_total; a1_90_3_total; a1_90_4_total];

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

b0_1_total = data_singles_total(1:16:end);
b0_2_total = data_singles_total(2:16:end);
b0_3_total = data_singles_total(9:16:end);
b0_4_total = data_singles_total(10:16:end);
b0_total = [b0_1_total; b0_2_total; b0_3_total; b0_4_total];

b0_90_1_total = data_singles_total(3:16:end);
b0_90_2_total = data_singles_total(4:16:end);
b0_90_3_total = data_singles_total(11:16:end);
b0_90_4_total = data_singles_total(12:16:end);
b0_90_total = [b0_90_1_total; b0_90_2_total; b0_90_3_total; b0_90_4_total];

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

b1_1_total = data_singles_total(5:16:end);
b1_2_total = data_singles_total(6:16:end);
b1_3_total = data_singles_total(13:16:end);
b1_4_total = data_singles_total(14:16:end);
b1_total = [b1_1_total; b1_2_total; b1_3_total; b1_4_total];

b1_90_1_total = data_singles_total(7:16:end);
b1_90_2_total = data_singles_total(8:16:end);
b1_90_3_total = data_singles_total(15:16:end);
b1_90_4_total = data_singles_total(16:16:end);
b1_90_total = [b1_90_1_total; b1_90_2_total; b1_90_3_total; b1_90_4_total];

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

a0_1 = data_singles(1:16:end, 1);
a0_2 = data_singles(3:16:end, 1);
a0_3 = data_singles(5:16:end, 1);
a0_4 = data_singles(7:16:end, 1);
a0 = [a0_1; a0_2; a0_3; a0_4];

a0_90_1 = data_singles(2:16:end, 1);
a0_90_2 = data_singles(4:16:end, 1);
a0_90_3 = data_singles(6:16:end, 1);
a0_90_4 = data_singles(8:16:end, 1);
a0_90 = [a0_90_1; a0_90_2; a0_90_3; a0_90_4];

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

a1_1 = data_singles(9:16:end, 1);
a1_2 = data_singles(11:16:end, 1);
a1_3 = data_singles(13:16:end, 1);
a1_4 = data_singles(15:16:end, 1);
a1 = [a1_1; a1_2; a1_3; a1_4];

a1_90_1 = data_singles(10:16:end, 1);
a1_90_2 = data_singles(12:16:end, 1);
a1_90_3 = data_singles(14:16:end, 1);
a1_90_4 = data_singles(16:16:end, 1);
a1_90 = [a1_90_1; a1_90_2; a1_90_3; a1_90_4];

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

b0_1 = data_singles(1:16:end, 2);
b0_2 = data_singles(2:16:end, 2);
b0_3 = data_singles(9:16:end, 2);
b0_4 = data_singles(10:16:end, 2);
b0 = [b0_1; b0_2; b0_3; b0_4];

b0_90_1 = data_singles(3:16:end, 2);
b0_90_2 = data_singles(4:16:end, 2);
b0_90_3 = data_singles(11:16:end, 2);
b0_90_4 = data_singles(12:16:end, 2);
b0_90 = [b0_90_1; b0_90_2; b0_90_3; b0_90_4];

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

b1_1 = data_singles(5:16:end, 2);
b1_2 = data_singles(6:16:end, 2);
b1_3 = data_singles(13:16:end, 2);
b1_4 = data_singles(14:16:end, 2);
b1 = [b1_1; b1_2; b1_3; b1_4];

b1_90_1 = data_singles(7:16:end, 2);
b1_90_2 = data_singles(8:16:end, 2);
b1_90_3 = data_singles(15:16:end, 2);
b1_90_4 = data_singles(16:16:end, 2);
b1_90 = [b1_90_1; b1_90_2; b1_90_3; b1_90_4];

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
index = 1:length(a0);
plot(index, a0./a0_total, 'b', index, a0_90./a0_90_total, 'g', index, a1./a1_total, 'r', index, a1_90./a1_90_total, 'c',...
    index, b0./b0_total, 'm', index, b0_90./b0_90_total, 'y', index, b1./b1_total, 'k', index, b1_90./b1_90_total, 'b-.'),...
    xlabel('No. of Measurments'),...
    ylabel('Normalized Counts'),...
    title('Normalized Counts versus No. of Measurments'),...
    grid;

legend('a0', 'a0_9_0', 'a1', 'a1_9_0', 'b0', 'b0_9_0', 'b1', 'b1_9_0');

data_out = [a0_total, a0, a0_90_total, a0_90, a1_total, a1, a1_90_total, a1_90,...
    b0_total, b0, b0_90_total, b0_90, b1_total, b1, b1_90_total, b1_90,];

dlmwrite('singles.dat', data_out, 'delimiter', ' ', 'precision', 16)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

mean(a0)
std(a0)

mean(a0_90)
std(a0_90)

mean(b0)
std(b0)

mean(b0_90)
std(b0_90)


mean(a1)
std(a1)

mean(a1_90)
std(a1_90)

mean(b1)
std(b1)

mean(b1_90)
std(b1_90)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

std(a0)/mean(a0)
std(a0_90)/mean(a0_90)

std(b0)/mean(b0)
std(b0_90)/mean(b0_90)

std(a1)/mean(a1)
std(a1_90)/mean(a1_90)

std(b1)/mean(b1)
std(b1_90)/mean(b1_90)

test = [mean(a0), mean(a0_90), mean(a1), mean(a1_90)];
test_a = test/max(test) % max is a0

test = [mean(b0), mean(b0_90), mean(b1), mean(b1_90)];
test_b = test/max(test) % max is b0_90

delta_a0 = std(a0);
delta_a0_90 = std(a0_90);
delta_a1 = std(a1);
delta_a1_90 = std(a1_90);
delta_a = [std(a0), std(a0_90), std(a1), std(a1_90)];

delta_b0 = std(b0);
delta_b0_90 = std(b0_90);
delta_b1 = std(b1);
delta_b1_90 = std(b1_90);
delta_b = [std(b0), std(b0_90), std(b1), std(b1_90)];

a = [mean(a0), mean(a0_90), mean(a1), mean(a1_90)];
a_norm = a/max(a);
delta_a_norm = sqrt((1/mean(a0))^2*delta_a.^2 + (a/mean(a0)^2).^2*delta_a0^2);

b = [mean(b0), mean(b0_90), mean(b1), mean(b1_90)];
b_norm = b/max(b);
delta_b_norm = sqrt((1/mean(b0_90))^2*delta_b.^2 + (b/mean(b0_90)^2).^2*delta_b0_90^2);

term_1_ele = powerset(b_norm, delta_a_norm);
term_1 = term_1_ele(:, 1).*term_1_ele(:, 2);

term_2_ele = powerset(a_norm, delta_b_norm);
term_2 = term_2_ele(:, 1).*term_2_ele(:, 2);

ab_norm_ele = powerset(a_norm, b_norm);
ab_norm = ab_norm_ele(:, 1).*ab_norm_ele(:, 2);
delta_ab_norm = sqrt(term_1.^2 + term_2.^2);

correction_factor = 1./ab_norm;
delta_correction_factor = delta_ab_norm./ab_norm.^2;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


test_ab = powerset(test_a, test_b)
test_ab(:, 1).*test_ab(:, 2)
powerset(1:4, 1:4)
plot(test_ab(:, 1).*test_ab(:, 2), 'x-')

correction_factor = 1./(test_ab(:, 1).*test_ab(:, 2));

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
