A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
three-gpp-v2v-channel-example.py
Go to the documentation of this file.
1#!/bin/python3
2
3# Copyright (c) 2020, University of Padova, Dep. of Information Engineering, SIGNET lab
4# Copyright (c) 2025, Centre Tecnologic de Telecomunicacions de Catalunya (CTTC)
5#
6# SPDX-License-Identifier: GPL-2.0-only
7
8import matplotlib.animation as animation
9import matplotlib.patches as patches
10import matplotlib.pyplot as plt
11
12#
13# Plot the traces generated by three-gpp-v2v-channel-example
14#
15import pandas as pd
16
17# Load the data
18df = pd.read_csv("example-output.txt", sep=r"\s+", comment="#")
19df = df.iloc[::10, :]
20
21# Column indices (adjust if needed)
22TIME = 0
23TX_X = 1
24TX_Y = 2
25RX_X = 3
26RX_Y = 4
27SNR = 6
28
29# Create the figure and subplots
30fig, (ax_map, ax_snr) = plt.subplots(1, 2, figsize=(12, 6))
31
32# Map plot config
33ax_map.set_xlim(-25, 600)
34ax_map.set_ylim(-25, 1000)
35ax_map.set_xlabel("X [m]")
36ax_map.set_ylabel("Y [m]")
37ax_map.set_aspect("equal")
38tx_circle = patches.Circle((0, 0), 5, color="blue", alpha=0.35)
39rx_circle = patches.Circle((0, 0), 5, color="red", alpha=0.35)
40ax_map.add_patch(tx_circle)
41ax_map.add_patch(rx_circle)
42
43# SNR plot config
44ax_snr.set_xlim(0, 40)
45ax_snr.set_ylim(-20, 100)
46ax_snr.set_xlabel("Time [s]")
47ax_snr.set_ylabel("SNR [dB]")
48ax_snr.grid(True)
49(snr_line,) = ax_snr.plot([], [], "k-")
50
51buildings = pd.read_csv("buildings.txt", sep=r"\s+", comment="#", header=None)
52building_patches = []
53for idx, row in buildings.iterrows():
54 x0, y0, x1, y1 = row
55 width = x1 - x0
56 height = y1 - y0
57 rect = patches.Rectangle((x0, y0), width, height, color="gray", alpha=0.5)
58 ax_map.add_patch(rect)
59 building_patches.append(rect)
60
61
62# Animation update function
63def update(frame):
64 row = df.iloc[frame]
65 tx_circle.set_center((row.iloc[TX_X], row.iloc[TX_Y]))
66 rx_circle.set_center((row.iloc[RX_X], row.iloc[RX_Y]))
67 snr_line.set_data(df.iloc[: frame + 1, TIME], df.iloc[: frame + 1, SNR])
68 ax_map.set_title(f"Time = {row.iloc[TIME]:.1f} s")
69 return tx_circle, rx_circle, snr_line
70
71
72# Run animation
73ani = animation.FuncAnimation(fig, update, frames=len(df), interval=0.001, blit=False)
74
75# Save animation
76ani.save("map.gif", writer="pillow")
77
78# Save final SNR plot separately
79plt.figure()
80plt.plot(df.iloc[:, TIME], df.iloc[:, SNR], "k-")
81plt.xlabel("Time [s]")
82plt.ylabel("SNR [dB]")
83plt.grid(True)
84plt.xlim(0, 40)
85plt.ylim(-20, 100)
86plt.savefig("snr.png")