examples/EM Wave Simulator/README.md
A Python script that simulates a propagating electromagnetic plane wave (photon wave packet) and streams the field data over UDP for real-time 3D visualization in Serial Studio.
This example visualizes a Gaussian-enveloped linearly polarized electromagnetic wave packet as described by Maxwell's equations:
$$ E(x,t) = E_0 , \exp!\Bigl(-\frac{(x - x_0 - ct)^2}{2\sigma^2}\Bigr) \sin\bigl(k(x - ct)\bigr) $$
$$ B(x,t) = B_0 , \exp!\Bigl(-\frac{(x - x_0 - ct)^2}{2\sigma^2}\Bigr) \sin\bigl(k(x - ct)\bigr) $$
The electric field oscillates along the Y axis, the magnetic field along the Z axis, and the wave propagates along the X axis — all three mutually perpendicular, as required by Maxwell's equations. The Gaussian envelope localizes the wave into a packet that travels at speed $c$, loops back after exiting the view, and repeats.
| Widget Type | Data Source |
|---|---|
| 3D Plot | X (propagation), E-field (Y), B-field (Z) |
| Multi-Plot | E-field and B-field vs. propagation axis |
python3 em_wave_udp.py
Optional arguments:
--host HOST — UDP destination (default: 127.0.0.1)--port PORT — UDP port (default: 9000)--wavelength WAVELENGTH — Carrier wavelength in arbitrary units (default: 12.0)--speed SPEED — Wave propagation speed, units/s (default: 20.0)--amplitude AMPLITUDE — E-field peak amplitude (default: 10.0)--sigma SIGMA — Gaussian envelope width / spatial std dev (default: 20.0)--samples SAMPLES — Spatial sample points per frame (default: 80)--interval INTERVAL — Time between frames in seconds (default: 0.025)Example:
# Tighter wave packet with higher frequency
python3 em_wave_udp.py --wavelength 6.0 --sigma 10.0 --samples 120
EMWaveSimulator.ssproj as the project fileEach frame, the simulator samples the spatial window at --samples evenly spaced points. For each point, it computes the Gaussian-modulated sinusoidal carrier for both the E-field and B-field, then sends a CSV line:
x,ey,bz\n
Serial Studio receives 80 lines per frame (one per spatial sample) and plots them using the custom X-axis feature — the propagation position (x) drives the horizontal axis while the field amplitudes are plotted vertically (and in 3D, along their respective perpendicular axes).
CSV over UDP, newline-delimited:
x_position,e_field_y,b_field_z\n