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("three-gpp-v2v-channel-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(
52 "three-gpp-v2v-channel-example-buildings.txt", sep=r"\s+", comment="#", header=None
53)
54building_patches = []
55for idx, row in buildings.iterrows():
56 x0, y0, x1, y1 = row
57 width = x1 - x0
58 height = y1 - y0
59 rect = patches.Rectangle((x0, y0), width, height, color="gray", alpha=0.5)
60 ax_map.add_patch(rect)
61 building_patches.append(rect)
62
63
64# Animation update function
65def update(frame):
66 row = df.iloc[frame]
67 tx_circle.set_center((row.iloc[TX_X], row.iloc[TX_Y]))
68 rx_circle.set_center((row.iloc[RX_X], row.iloc[RX_Y]))
69 snr_line.set_data(df.iloc[: frame + 1, TIME], df.iloc[: frame + 1, SNR])
70 ax_map.set_title(f"Time = {row.iloc[TIME]:.1f} s")
71 return tx_circle, rx_circle, snr_line
72
73
74# Run animation
75ani = animation.FuncAnimation(fig, update, frames=len(df), interval=0.001, blit=False)
76
77# Save animation
78ani.save("three-gpp-v2v-channel-example-map.gif", writer="pillow")
79
80# Save final SNR plot separately
81plt.figure()
82plt.plot(df.iloc[:, TIME], df.iloc[:, SNR], "k-")
83plt.xlabel("Time [s]")
84plt.ylabel("SNR [dB]")
85plt.grid(True)
86plt.xlim(0, 40)
87plt.ylim(-20, 100)
88plt.savefig("three-gpp-v2v-channel-example-snr.png")