# EMP Detection and Simulation

## Overview

The SDR Drone Pursuit System now includes EMP (Electromagnetic Pulse) detection and simulation capabilities. This feature enables the system to:

1. **Simulate EMP events** with configurable yield settings
2. **Detect potential EMP signatures** in real-time SDR data
3. **Visualize EMP effects** on RF spectrum
4. **Model signal disruption** based on scientific models

## Technical Background

An electromagnetic pulse (EMP) is a short burst of electromagnetic energy that can disrupt electronic equipment and communications systems. EMPs can be generated by:

- Nuclear detonations (NEMP)
- Non-nuclear EMP devices (NNEMP)
- Natural phenomena like solar flares (SEMP)

The EMP simulation module models these effects with focus on:

- E1 phase (initial fast pulse)
- E2 phase (intermediate pulse)
- E3 phase (slower, longer-lasting component)

## Configuration

EMP detection and simulation can be configured in the `CONFIG` dictionary:

```python
'emp': {
    'enabled': False,        # EMP simulation enabled by default
    'yield_kt': 50,          # Default yield in kilotons
    'distance_km': 10,       # Default distance in km
    'duration_sec': 5,       # Duration of EMP effects in seconds
    'auto_detect': True      # Auto-detect EMP events
}
```

## Using the EMP Simulator

### WebSocket Commands

The EMP simulator can be controlled via WebSocket messages:

#### Trigger EMP Simulation

```javascript
{
    "type": "emp_simulate",
    "yield_kt": 100,         // Yield in kilotons
    "distance_km": 5         // Distance from detector in km
}
```

#### Configure EMP Simulator

```javascript
{
    "type": "emp_configure",
    "enabled": true,         // Enable/disable EMP simulation
    "yield_kt": 75,          // Yield in kilotons
    "distance_km": 8,        // Distance from detector in km
    "duration_sec": 10,      // Duration of effects in seconds
    "auto_detect": true      // Enable auto-detection
}
```

#### Stop EMP Simulation

```javascript
{
    "type": "emp_stop"
}
```

### WebSocket Responses

The server will respond with messages describing EMP events:

#### EMP Simulation Started

```javascript
{
    "type": "emp_simulation_started",
    "event": {
        "event": "emp_triggered",
        "yield_kt": 100,
        "radius_km": 9.5,
        "distance_km": 5,
        "field_strength": 24563.2,
        "timestamp": 1621234567.89
    },
    "plot_file": "emp_simulation_20230415_123045.png"
}
```

#### EMP Configuration Updated

```javascript
{
    "type": "emp_config_updated",
    "config": {
        "enabled": true,
        "yield_kt": 75,
        "distance_km": 8,
        "duration_sec": 10,
        "auto_detect": true
    }
}
```

#### EMP Effects in Signal Data

When an EMP event is active or detected, the signal data JSON will include an `emp` object:

```javascript
{
    "freqs": [...],
    "amplitudes": [...],
    "signals": [...],
    "timestamp": 1621234567.89,
    "emp": {
        "active": true,
        "yield_kt": 100,
        "distance_km": 5,
        "radius_km": 9.5,
        "elapsed_sec": 2.3,
        "auto_detected": false,
        "confidence": null
    }
}
```

## Auto-Detection

The system can automatically detect potential EMP events in the RF spectrum by analyzing:

1. **Sudden broadband noise increase** across the spectrum
2. **Characteristic spectral patterns** associated with EMPs
3. **Rapid changes in overall power** levels

When `auto_detect` is enabled, the system will:

1. Monitor the RF spectrum continuously
2. Compare current FFT data with previous samples
3. Compute a confidence score based on EMP signatures
4. Trigger an EMP event if confidence exceeds threshold (default: 0.75)

## Signal Effects

When an EMP event is active, it affects the RF spectrum and signal analysis in several ways:

1. **Increased noise floor** proportional to EMP intensity and proximity
2. **Compressed dynamic range** making signals less distinct
3. **Random impulse noise** characteristic of EMPs
4. **Signal classification disruption** due to spectral distortion
5. **Time-varying effects** reflecting the E1, E2, and E3 phases

## Visualization

Each EMP event generates a plot showing field strength vs. distance, which is saved as a PNG file. This visualization helps understand:

- Maximum effect radius based on yield
- Field strength at the detector location
- Exponential decay of EMP effects with distance

## EMP Simulation Model

The EMP simulator uses a scientific model to calculate:

- **EMP radius**: `4.4 * (yield_kt ^ (1/3))` in kilometers
- **Field strength**: Based on exponential decay from the source
- **Time phases**: Modeling the E1, E2, and E3 components

## Implementation Details

For developers, the EMP simulation is implemented in the `EMP_Simulator` class with these key methods:

- `calculate_emp_radius()`: Estimates effect radius based on yield
- `emp_field_strength(distance_km)`: Calculates field strength at distance
- `apply_emp_effect_to_signal(fft_data, duration_sec)`: Applies EMP effects to FFT data
- `detect_emp_signature(fft_data, prev_fft_data)`: Detects potential EMP events
- `plot_emp_effect()`: Generates visualization of EMP effects