%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 
% Script to Generate Test Data for Bit Compression Experiment 
% Violation Versus Angle of Separation (theta, rad) 
% Output from this script will be processed by bit_compression_data_processing_step_3.m
% 
% April 9,2015 
% Author: Poh Hou Shun, Centre for Quantum Technologies, National University of Singapore 
% 
% a0 - 0 
% a1 - 2*theta 
% b0 - theta 
% b1 - 3*theta 
% 
% SEPARATION_ANGLE - theta 
% LIST_SIZE - length of string 
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% The binary pattern output from the coincidence unit are represented by their
% decimal equivalent, i.e. -

% 5 - 0101 (hh), 6 - 0110 (vh), 9 - 1001 (hv), and 10 - 1010 (vv)

PATTERN_INDEX = [5, 6, 9, 10];

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

% defining input parameters

SEPARATION_ANGLE_MAX = 0.15;
SEPARATION_ANGLE = 0:0.02:0.24;
SEPARATION_ANGLE = [SEPARATION_ANGLE(1:8), SEPARATION_ANGLE_MAX, SEPARATION_ANGLE(9:end)];

LIST_SIZE = 1500000;

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

% loop through for LIST_SIZE for list generation
for i = 1:length(SEPARATION_ANGLE)

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

% For a0b0 case

phh = sin(0 - SEPARATION_ANGLE(i))^2/2;
phv = cos(0 - SEPARATION_ANGLE(i))^2/2;
pvh = cos(0 - SEPARATION_ANGLE(i))^2/2;
pvv = sin(0 - SEPARATION_ANGLE(i))^2/2;

prob_list = [phh, pvh, phv, pvv];

%generating output patterns in accordance to calculated probability distribution
rand_list_a0b0 = randsample(PATTERN_INDEX, LIST_SIZE, true, prob_list);

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

% for a0b1 case

phh = sin(0 - 3*SEPARATION_ANGLE(i))^2/2;
phv = cos(0 - 3*SEPARATION_ANGLE(i))^2/2;
pvh = cos(0 - 3*SEPARATION_ANGLE(i))^2/2;
pvv = sin(0 - 3*SEPARATION_ANGLE(i))^2/2;

prob_list = [phh, pvh, phv, pvv];

rand_list_a0b1 = randsample(PATTERN_INDEX, LIST_SIZE, true, prob_list);

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

% for a1b0 case

phh = sin(2*SEPARATION_ANGLE(i) - SEPARATION_ANGLE(i))^2/2;
phv = cos(2*SEPARATION_ANGLE(i) - SEPARATION_ANGLE(i))^2/2;
pvh = cos(2*SEPARATION_ANGLE(i) - SEPARATION_ANGLE(i))^2/2;
pvv = sin(2*SEPARATION_ANGLE(i) - SEPARATION_ANGLE(i))^2/2;

prob_list = [phh, pvh, phv, pvv];

rand_list_a1b0 = randsample(PATTERN_INDEX, LIST_SIZE, true, prob_list);

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

% for a1b1 case

phh = sin(2*SEPARATION_ANGLE(i) - 3*SEPARATION_ANGLE(i))^2/2;
phv = cos(2*SEPARATION_ANGLE(i) - 3*SEPARATION_ANGLE(i))^2/2;
pvh = cos(2*SEPARATION_ANGLE(i) - 3*SEPARATION_ANGLE(i))^2/2;
pvv = sin(2*SEPARATION_ANGLE(i) - 3*SEPARATION_ANGLE(i))^2/2;

prob_list = [phh, pvh, phv, pvv];

rand_list_a1b1 = randsample(PATTERN_INDEX, LIST_SIZE, true, prob_list);

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

% concatenating the generated lists
rand_list = [rand_list_a0b0; rand_list_a0b1; rand_list_a1b0; rand_list_a1b1];

% Output to file
dlmwrite(strcat(num2str(SEPARATION_ANGLE(i)), '_output.dat'), rand_list, ' ');

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

end;

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