-
Notifications
You must be signed in to change notification settings - Fork 0
/
probability_hists.m
202 lines (153 loc) · 6.13 KB
/
probability_hists.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
function probability_hists(Trep_alignment, Tbin_start, Tbin_end, debugging_mode, Td_filter_max)
% Using picoseconds as the unit
if debugging_mode == 1
Trep_alignment = - 0.324465;
Tbin_start = 4e12;
Tbin_end = 5e12;
end
% Initialize variables
load('synchronized_data.mat');
Trep = 100e3 + Trep_alignment;
% Find the bins and Tp boundaries
bins = (Tbin_start:Trep:Tbin_end)'; %find bin times
Tp_limits = [bins, bins + Td_filter_max];
%% Find the detection counts of each pulse
s_data = synchronized_data(:, 2);
Tp_l = Tp_limits(:, 1);
Tp_h = Tp_limits(:, 2);
corresponding_data_count = zeros(size(Tp_l));
for i = 1:length(Tp_l)
lower_limit = Tp_l(i);
upper_limit = Tp_h(i);
% Binary search for the lower limit index
left = 1;
right = length(s_data);
while left < right
mid = left + floor((right - left) / 2);
if s_data(mid) < lower_limit
left = mid + 1;
else
right = mid;
end
end
lower_idx = left;
% Binary search for the upper limit index
left = 1;
right = length(s_data);
while left < right
mid = left + ceil((right - left) / 2);
if s_data(mid) > upper_limit
right = mid - 1;
else
left = mid;
end
end
upper_idx = left;
corresponding_data_count(i) = upper_idx - lower_idx + 1;
end
occurrences = histcounts(corresponding_data_count, -0.5:1:4.5);
result = struct();
result.zero_corresponding = occurrences(1);
result.one_corresponding = occurrences(2);
result.two_corresponding = occurrences(3);
result.three_corresponding = occurrences(4);
result.four_corresponding = occurrences(5);
%% Create a histogram to show the results
% Create the histogram with separate bar plot objects
figure;
bar_positions = 0:4;
bar_handles = zeros(1, 5);
for i = 1:5
bar_handles(i) = bar(bar_positions(i), occurrences(i), 'FaceColor', 'b');
hold on;
end
xlabel('Number of Corresponding Data');
ylabel('Occurrences');
title('Histogram of Corresponding Data Occurrences');
% Calculate percentages
total_occurrences = sum(occurrences);
percentages = occurrences / total_occurrences * 100;
% Create legend labels with percentages and occurrences
legend_labels = cell(1, 5);
for i = 1:5
legend_labels{i} = sprintf('%d: %.4f%% (%d)', i-1, percentages(i), occurrences(i));
end
% Add legend with percentages and occurrences
legend(bar_handles, legend_labels, 'Location', 'eastoutside');
%% Poisson Distribution
% Merged the two figures into a single figure
% Number of samples to generate
num_samples = 10000;
total_occurrences = sum(occurrences);
% Calculate average photon per bin
avg_photon_per_bin_exp = sum((0:4) .* occurrences) / total_occurrences;
% Generate random values from a Poisson distribution
random_values = poissrnd(avg_photon_per_bin_exp, [1, num_samples]);
% Calculate the Poisson PMF
max_val = max(random_values);
x = 0:max_val;
pmf = poisspdf(x, avg_photon_per_bin_exp);
% Create the merged figure
figure;
% Plot the histogram
histogram(random_values, 'Normalization', 'pdf');
hold on;
bar_positions = 0:4;
% Plot the corresponding data occurrences histogram
for i = 1:5
bar(bar_positions(i), occurrences(i) / total_occurrences, 'FaceColor', "#77AC30");
hold on;
end
hold off;
xlabel('Number of photons per bin');
ylabel('Probability');
title('Comparison of Poisson Distribution and Corresponding Data Occurrences');
legend('Poisson Distribution', 'Experimental Data', 'Location', 'eastoutside');
%% Task #4
% Calculate experimental probabilities P(0), P(1), and P(2)
P_exp = occurrences / total_occurrences;
P0_exp = P_exp(1);
P1_exp = P_exp(2);
P2_exp = P_exp(3);
% Calculate average photon per bin
avg_photon_per_bin_exp = sum((0:4) .* occurrences) / total_occurrences;
% Theoretical values from the paper
Pc1 = 0.04514;
Pc2 = 53e-5;
avg_photon_per_bin_theory = 0.04620;
% Theoretical Poisson probabilities
P_theory = zeros(1, 5);
P_theory(1) = 1 - Pc1 - Pc2;
P_theory(2) = Pc1;
P_theory(3) = Pc2;
% Plot the experimental and theoretical probability histograms
figure;
bar_handle = bar(0:4, [P_exp; P_theory]', 'grouped');
xlabel('Number of Detected Photons');
ylabel('Probability');
title('Probability Histogram');
legend('Experimental', 'Theoretical', 'Location', 'eastoutside');
% Display P(n) values on the bars
for i = 1:5
x_exp = bar_handle(1).XEndPoints(i);
y_exp = bar_handle(1).YEndPoints(i);
x_theory = bar_handle(2).XEndPoints(i);
y_theory = bar_handle(2).YEndPoints(i);
text(x_exp, y_exp, sprintf('%.6f', P_exp(i)), 'HorizontalAlignment', 'center', 'VerticalAlignment', 'bottom');
text(x_theory, y_theory, sprintf('%.6f', P_theory(i)), 'HorizontalAlignment', 'center', 'VerticalAlignment', 'bottom');
end
% Display probabilities
disp('Experimental Probabilities:');
fprintf('P(0) = %.6f\n', P_exp(1));
fprintf('P(1) = %.6f\n', P_exp(2));
fprintf('P(2) = %.6f\n', P_exp(3));
fprintf('P(3) = %.6f\n', P_exp(4));
fprintf('P(4) = %.6f\n', P_exp(5));
disp('Theoretical Probabilities:');
fprintf('P(0) = %.6f\n', P_theory(1));
fprintf('P(1) = %.6f\n', P_theory(2));
fprintf('P(2) = %.6f\n', P_theory(3));
fprintf('P(3) = %.6f\n', P_theory(4));
fprintf('P(4) = %.6f\n', P_theory(5));
fprintf('n_bar = %f\n', avg_photon_per_bin_exp);
end