# Tuning hampel_threshold for Noisy Environments in Micro-ESPectre

> Tune hampel_threshold for noisy environments in Micro-ESPectre. Increase threshold to 6.0-7.0 and optionally window to 9 to remove interference while preserving motion signatures.

- Repository: [Francesco Pace/espectre](https://github.com/francescopace/espectre)
- Tags: how-to-guide
- Published: 2026-06-10

---

**Increase `HAMPEL_THRESHOLD` from its default 5.0 to 6.0–7.0 in noisy RF environments while optionally enlarging `HAMPEL_WINDOW` to 9, ensuring the filter removes interference spikes without suppressing genuine motion signatures.**

Micro-ESPectre processes Wi-Fi Channel State Information (CSI) to detect motion through spatial turbulence analysis. The `HampelFilter` class removes isolated outliers from the turbulence signal that would otherwise trigger false motion reports in the `francescopace/espectre` repository. Tuning the `hampel_threshold` parameter is critical when deploying in environments with strong RF interference from multiple Wi-Fi devices or industrial equipment.

## How the Hampel Filter Processes CSI Turbulence

The filter maintains a circular buffer of the last *window* samples. For each new turbulence sample, it computes the **median** of the buffer and the **Median Absolute Deviation (MAD)**. If the sample deviates from the median by more than `threshold × MAD`, the filter replaces it with the median; otherwise, it passes through unchanged. This robust statistical approach isolates spikes without affecting the underlying variance that the MVS (Motion Vector Segmentation) algorithm requires.

## Configuration Parameters and Default Values

Default values are defined in [`micro-espectre/src/config.py`](https://github.com/francescopace/espectre/blob/main/micro-espectre/src/config.py) at lines 76-80:

```python

# config.py

HAMPEL_WINDOW = 7
HAMPEL_THRESHOLD = 5.0

```

These constants flow into the `MVSDetector` class defined in [`micro-espectre/tools/csi_utils.py`](https://github.com/francescopace/espectre/blob/main/micro-espectre/tools/csi_utils.py) (lines 14-16). The constructor accepts `hampel_window` and `hampel_threshold` as arguments:

```python

# csi_utils.py - MVSDetector initialization

def __init__(self, ..., enable_hampel=True, hampel_window=7, hampel_threshold=5.0):

```

The detector forwards these values to a `SegmentationContext`, which instantiates the `HampelFilter` internally (see the call at line 44 in [`csi_utils.py`](https://github.com/francescopace/espectre/blob/main/csi_utils.py)). During processing, the `add_turbulence` method in [`micro-espectre/src/segmentation.py`](https://github.com/francescopace/espectre/blob/main/micro-espectre/src/segmentation.py) invokes `self.hampel.filter(value)` to clean each incoming sample.

## Tuning Strategies for Noisy Environments

### Understanding the Threshold Trade-off

A lower `HAMPEL_THRESHOLD` makes the filter more aggressive, risking the replacement of genuine motion-related spikes and reducing detection sensitivity. A higher threshold becomes more permissive, allowing noise spikes to pass through and increasing false positives.

### Recommended Configurations by Noise Level

- **Mild noise, occasional spikes**: Retain the defaults (`HAMPEL_WINDOW=7`, `HAMPEL_THRESHOLD=5.0`).
- **Moderate interference**: Increase `HAMPEL_THRESHOLD` to `6.0` and test `HAMPEL_WINDOW=9` for smoother median calculation.
- **Heavy RF noise**: Raise both parameters to `HAMPEL_WINDOW=9` and `HAMPEL_THRESHOLD=7.0`, then monitor the false-positive rate.
- **Low-latency requirements**: Keep `HAMPEL_WINDOW` minimal (≥5) and compensate by raising `HAMPEL_THRESHOLD` if false positives rise.

## Practical Implementation and Optimization

### Instantiate a Detector with Custom Hampel Settings

```python
from micro_espectre.tools.csi_utils import MVSDetector
from micro_espectre.src.config import DEFAULT_SUBCARRIERS

detector = MVSDetector(
    window_size=100,
    threshold=2.5,
    selected_subcarriers=DEFAULT_SUBCARRIERS,
    enable_hampel=True,
    hampel_window=9,      # Smoother median for noisy environments

    hampel_threshold=7.0,  # More tolerant outlier detection

    enable_lowpass=False
)

detector.process_packet(csi_packet)

```

### Automated Parameter Optimization

Use the optimization script at [`micro-espectre/tools/6_optimize_filter_params.py`](https://github.com/francescopace/espectre/blob/main/micro-espectre/tools/6_optimize_filter_params.py) (lines 78-84) to search for the best configuration:

```bash
python micro-espectre/tools/6_optimize_filter_params.py --hampel

```

The script evaluates window sizes from 3-11 and thresholds from 2.0-6.0, outputting the configuration with the highest F-score while maintaining low false-positive rates.

### Validation Metrics

After tuning, verify performance using the `test_mvs_configuration` helper in [`csi_utils.py`](https://github.com/francescopace/espectre/blob/main/csi_utils.py). Ensure:

- **Recall** remains ≥ 95% (motion packets detected)
- **False-positive rate** stays ≤ 10%

If metrics deviate, adjust the threshold in steps of 0.5 until achieving the desired trade-off.

## Summary

- The Hampel filter in Micro-ESPectre uses a sliding median and MAD calculation to remove outlier spikes from Wi-Fi CSI turbulence data.
- Default parameters (`HAMPEL_WINDOW=7`, `HAMPEL_THRESHOLD=5.0`) suit typical indoor conditions but require adjustment in noisy RF environments.
- Increase `hampel_threshold` to 6.0–7.0 for heavy interference, optionally enlarging the window to 9 for median stability.
- Validate changes using the [`6_optimize_filter_params.py`](https://github.com/francescopace/espectre/blob/main/6_optimize_filter_params.py) script or manual testing to ensure recall ≥ 95% and false positives ≤ 10%.
- Configuration originates in [`config.py`](https://github.com/francescopace/espectre/blob/main/config.py) (lines 76-80) and propagates through `MVSDetector` in [`csi_utils.py`](https://github.com/francescopace/espectre/blob/main/csi_utils.py) to the `SegmentationContext` class.

## Frequently Asked Questions

### What is the default hampel_threshold in Micro-ESPectre?

The default value is `5.0`, defined in [`micro-espectre/src/config.py`](https://github.com/francescopace/espectre/blob/main/micro-espectre/src/config.py) at line 80. This represents a multiplier of the Median Absolute Deviation (MAD) that determines the outlier detection limit.

### How does increasing the Hampel window size affect detection latency?

Larger window sizes (e.g., 9 instead of 7) increase the median calculation's stability but introduce slight latency because the filter must buffer more samples. For low-latency requirements, keep `HAMPEL_WINDOW` at 5 or 7 and compensate by raising `HAMPEL_THRESHOLD` instead.

### Can I disable the Hampel filter entirely?

Yes. Set `enable_hampel=False` when constructing the `MVSDetector` class in [`micro-espectre/tools/csi_utils.py`](https://github.com/francescopace/espectre/blob/main/micro-espectre/tools/csi_utils.py). However, disabling the filter is not recommended for noisy environments as it will significantly increase false motion detections from RF interference spikes.

### Where does the actual Hampel filtering occur in the codebase?

The filtering happens in [`micro-espectre/src/segmentation.py`](https://github.com/francescopace/espectre/blob/main/micro-espectre/src/segmentation.py) within the `SegmentationContext` class. The `add_turbulence` method calls `self.hampel.filter(value)` each time a new turbulence sample is processed, replacing outliers with the window median according to the configured threshold.