|
21 | 21 | import numpy as np |
22 | 22 | import torch |
23 | 23 |
|
24 | | -from examples.utils.plotting import ( |
25 | | - setup_plotting_style, |
| 24 | +from examples.example_utils.plotting import ( |
26 | 25 | plot_binary_channel_comparison, |
| 26 | + plot_channel_capacity_analysis, |
27 | 27 | plot_channel_error_rates, |
28 | 28 | plot_transition_matrices, |
29 | | - plot_channel_capacity_analysis |
| 29 | + setup_plotting_style, |
30 | 30 | ) |
31 | | - |
32 | 31 | from kaira.channels import BinaryErasureChannel, BinarySymmetricChannel, BinaryZChannel |
33 | 32 |
|
34 | 33 | # Set random seed for reproducibility |
|
75 | 74 |
|
76 | 75 | bsc_outputs.append((p, output, error_rate)) |
77 | 76 | # BSC Channel Analysis Results |
78 | | - # =========================== |
| 77 | + # =========================== |
79 | 78 | # BSC (p={p}): Errors: {errors}/{num_bits}, Error rate: {error_rate:.4f} |
80 | 79 |
|
81 | 80 | # %% |
|
152 | 151 | bsc_output = bsc(binary_data).numpy()[0] |
153 | 152 | channel_outputs.append(("BSC", bsc_output, bsc_p)) |
154 | 153 |
|
155 | | -# BEC output (high erasure probability for visibility) |
| 154 | +# BEC output (high erasure probability for visibility) |
156 | 155 | bec_p = 0.2 |
157 | 156 | bec = BinaryErasureChannel(erasure_prob=bec_p) |
158 | 157 | with torch.no_grad(): |
|
168 | 167 |
|
169 | 168 | # Visualize channel effects |
170 | 169 | original_data = binary_data[0].numpy() |
171 | | -fig = plot_binary_channel_comparison(original_data, channel_outputs, |
172 | | - segment_start, segment_length, |
173 | | - "Binary Channel Effects Comparison") |
| 170 | +fig = plot_binary_channel_comparison(original_data, channel_outputs, segment_start, segment_length, "Binary Channel Effects Comparison") |
174 | 171 | fig.show() |
175 | 172 |
|
176 | 173 | # %% |
|
180 | 177 |
|
181 | 178 | # Channel Error Rate Analysis |
182 | 179 | # =========================== |
183 | | -# Compare theoretical vs observed error rates across different |
| 180 | +# Compare theoretical vs observed error rates across different |
184 | 181 | # channel types to validate the implementation accuracy. |
185 | 182 |
|
186 | 183 | # Prepare BSC error rate data |
187 | 184 | theoretical_bsc = error_probs # Theoretical error rate equals p |
188 | 185 | observed_bsc = [err_rate for _, _, err_rate in bsc_outputs] |
189 | 186 |
|
190 | 187 | # Plot BSC error rates |
191 | | -fig1 = plot_channel_error_rates(error_probs, theoretical_bsc, observed_bsc, |
192 | | - ["BSC"], "Binary Symmetric Channel Error Rates") |
| 188 | +fig1 = plot_channel_error_rates(error_probs, theoretical_bsc, observed_bsc, ["BSC"], "Binary Symmetric Channel Error Rates") |
193 | 189 | fig1.show() |
194 | 190 |
|
195 | | -# Prepare BEC erasure rate data |
| 191 | +# Prepare BEC erasure rate data |
196 | 192 | theoretical_bec = erasure_probs # Theoretical erasure rate equals p |
197 | 193 | observed_bec = [erasure_rate for _, _, erasure_rate in bec_outputs] |
198 | 194 |
|
199 | 195 | # Plot BEC erasure rates |
200 | | -fig2 = plot_channel_error_rates(erasure_probs, theoretical_bec, observed_bec, |
201 | | - ["BEC"], "Binary Erasure Channel Erasure Rates") |
| 196 | +fig2 = plot_channel_error_rates(erasure_probs, theoretical_bec, observed_bec, ["BEC"], "Binary Erasure Channel Erasure Rates") |
202 | 197 | fig2.show() |
203 | 198 |
|
204 | 199 | # Prepare Z-Channel error rate data |
|
208 | 203 | observed_z = [err_rate * p_one for _, _, err_rate in z_outputs] |
209 | 204 |
|
210 | 205 | # Plot Z-Channel error rates |
211 | | -fig3 = plot_channel_error_rates(z_error_probs, theoretical_z, observed_z, |
212 | | - ["Z-Channel"], "Z-Channel Error Rates") |
| 206 | +fig3 = plot_channel_error_rates(z_error_probs, theoretical_z, observed_z, ["Z-Channel"], "Z-Channel Error Rates") |
213 | 207 | fig3.show() |
214 | 208 |
|
215 | 209 | # %% |
|
234 | 228 | z_matrix = np.array([[1, 0], [p_z, 1 - p_z]]) |
235 | 229 |
|
236 | 230 | # Plot transition matrices |
237 | | -matrices = [ |
238 | | - ("Binary Symmetric Channel", bsc_matrix, p_bsc), |
239 | | - ("Binary Erasure Channel", bec_matrix, p_bec), |
240 | | - ("Z-Channel", z_matrix, p_z) |
241 | | -] |
| 231 | +matrices = [("Binary Symmetric Channel", bsc_matrix, p_bsc), ("Binary Erasure Channel", bec_matrix, p_bec), ("Z-Channel", z_matrix, p_z)] |
242 | 232 |
|
243 | 233 | fig4 = plot_transition_matrices(matrices, "Binary Channel Transition Matrices") |
244 | 234 | fig4.show() |
|
256 | 246 | # Parameter ranges for capacity analysis |
257 | 247 | p_range = np.linspace(0, 1, 51) |
258 | 248 |
|
| 249 | + |
259 | 250 | # Calculate capacities for each channel type |
260 | 251 | def calculate_bsc_capacity(p): |
261 | 252 | """Calculate BSC capacity: C = 1 - H(p)""" |
262 | 253 | if p == 0 or p == 1: |
263 | 254 | return 1.0 if p == 0 else 0.0 |
264 | 255 | return 1 + p * np.log2(p) + (1 - p) * np.log2(1 - p) |
265 | 256 |
|
| 257 | + |
266 | 258 | def calculate_bec_capacity(p): |
267 | 259 | """Calculate BEC capacity: C = 1 - p""" |
268 | 260 | return 1 - p |
269 | 261 |
|
| 262 | + |
270 | 263 | def calculate_z_capacity(p): |
271 | | - """Calculate Z-channel capacity""" |
| 264 | + """Calculate Z-channel capacity.""" |
272 | 265 | if p == 0: |
273 | 266 | return 1.0 |
274 | 267 | if p == 1: |
275 | 268 | return 0.0 |
276 | 269 | # Z-channel capacity formula |
277 | 270 | return 1 + (1 - p) * np.log2(1 - p) + p * np.log2(p) |
278 | 271 |
|
| 272 | + |
279 | 273 | # Calculate capacities |
280 | 274 | bsc_capacities = np.array([calculate_bsc_capacity(p) for p in p_range]) |
281 | 275 | bec_capacities = np.array([calculate_bec_capacity(p) for p in p_range]) |
282 | 276 | z_capacities = np.array([calculate_z_capacity(p) for p in p_range]) |
283 | 277 |
|
284 | 278 | # Plot capacity analysis |
285 | | -capacities = { |
286 | | - "BSC": bsc_capacities, |
287 | | - "BEC": bec_capacities, |
288 | | - "Z-Channel": z_capacities |
289 | | -} |
290 | | - |
291 | | -fig5 = plot_channel_capacity_analysis(p_range, capacities, |
292 | | - "Binary Channel Capacity Analysis") |
| 279 | +capacities = {"BSC": bsc_capacities, "BEC": bec_capacities, "Z-Channel": z_capacities} |
| 280 | + |
| 281 | +fig5 = plot_channel_capacity_analysis(p_range, capacities, "Binary Channel Capacity Analysis") |
293 | 282 | fig5.show() |
294 | 283 |
|
295 | 284 | # %% |
|
0 commit comments