Detect and Capture RF Signals on NI USRP Radios
Reference Loading
- Code patterns (required at Step 4): for the seven copy-ready capture patterns (A–G) and the loopback table,
Read references/patterns.md before generating any capture code.
- API reference (required at Step 3):
Read references/api.md for signatures, property ranges, and threshold constraints. Every generation validates parameters against it before emitting code — see the Always rule below. This read is not optional.
- On demand:
references/overview.md — conceptual architecture and detector behavior.
- On demand, after a failure only:
references/threshold-calibration.md — the calibration sub-workflow (plotDetectionSignals / plotThreshold step-by-step). Do not load it during first-time code generation.
Sub-Workflows
This skill exposes one sub-workflow that is loaded on demand from references/:
| Sub-workflow |
What it provides |
Threshold Calibration (references/threshold-calibration.md) |
Step-by-step plot -> diagnose -> tune procedure for both energyDetector and preambleDetector, plus combined symptom -> cause -> fix table |
Trigger: when to route into Threshold Calibration
Route into references/threshold-calibration.md whenever any of these conditions hold:
- No detection — generated Pattern returned
status == 0 (capture timed out, no data captured) and the user expected a signal to fire it. This is the primary trigger.
- Wrong detection — Pattern returned
status >= 1 but the captured data is wrong: noise instead of signal, baseline instead of preamble, saturated waveform (max(abs(data)) near 1.414), or sanity ratio fails (r < 50 for energyDetector, r < 100 for preambleDetector).
- Unstable detection — the user reports, or an earlier run in this session already showed, that the Pattern fires inconsistently (sometimes
status=0, sometimes status=1); thresholds are sitting on a jittery boundary. Do not run extra captures hunting for this — the Always rule below caps a clean run at one.
- User explicitly asks to "calibrate", "tune thresholds", "tune detection", "fix detection", "why didn't it detect", "diagnose", or names
plotDetectionSignals / plotThreshold.
Sanity ratio r — where it comes from. Every Pattern's %% Verify block computes and prints r, so triggers 2 and 3 are read straight off the pattern's own output. No extra run is needed.
| Detector |
Formula the pattern prints |
Real signal |
energyDetector (A/B/E/F) |
r = max(abs(data).^2) / median(abs(data).^2) |
r >= 50 |
preambleDetector (C/D/G) |
same ratio on the matched-filter output, filter(conj(flipud(pd.Preamble)), 1, data) |
r > 100 |
| Wideband chirp / OFDM (F) |
raw r under-reports — 15-20 is normal; the pattern also prints a matched-filter ratio |
matched-filter ratio >> 100 |
Do not route into the sub-workflow for first-time code generation — generate the Pattern from this SKILL.md, run it, and only route into calibration on a failure outcome. The sub-workflow assumes a detector object is already configured and a TX or external signal is present.
If the failure mode is RF-path (no peaks at all on the figure, wrong frequency, broken cable, wrong antenna name), the sub-workflow exits early back to this skill — calibration cannot fix RF problems.
Generate working MATLAB code to detect and capture RF signals using energy detection or preamble detection triggers on NI USRP radios with Wireless Testbench.
Prerequisites
Before this skill applies, the user must have:
- MATLAB R2022a+ (preambleDetector) or R2023b+ (energyDetector)
- Wireless Testbench toolbox installed
- Wireless Testbench Support Package for NI USRP Radios installed
- Radio previously configured using the Radio Setup wizard
- Physical NI USRP radio connected and validated
When to Use
Trigger this skill when the user wants smart/triggered capture — record only when something interesting appears on the air, not continuously:
- Capture only when signal energy rises above the noise floor (
energyDetector, Patterns A/B/E/F) — wake-on-signal recording, opportunistic capture, transmit-then-detect verification
- Capture only when a known protocol preamble is cross-correlated and matched (
preambleDetector, Patterns C/D/G) — WLAN L-LTF, 5G NR PSS/SSS, LTE PSS, Zadoff-Chu, custom sync sequences
- Calibrate detection thresholds (
plotDetectionSignals for energyDetector, plotThreshold for preambleDetector)
- Scan multiple frequencies / channels for activity (e.g., WLAN channel scan across 2.4 GHz band)
- Transmit a test waveform from the same radio and capture it back upon detection (loopback verification)
- Capture multiple consecutive triggered signals with sample-clock timestamps
When NOT to Use
Route to another skill if the user's goal is:
- Untriggered capture (immediate IQ capture without detection) ->
matlab-transmit-capture-usrp
- Continuous streaming (System objects, real-time processing loops) -> use the
matlab-read-documentation skill for "Live Data I/O" (Wireless Testbench)
- Radio setup or troubleshooting ->
matlab-set-up-usrp-radio
- Clock/time synchronization -> use the
matlab-read-documentation skill for "Radio Management" (Wireless Testbench)
- FPGA targeting -> use the
matlab-read-documentation skill for "Target NI USRP Radios" (Wireless Testbench)
Decision Tree
User request
|
|-- Mentions "untriggered capture", "immediate capture", "basebandReceiver"
| -> REDIRECT to matlab-transmit-capture-usrp
|
|-- Mentions "System object", "streaming loop", "real-time processing"
| -> REDIRECT: use matlab-read-documentation for "Live Data I/O" (Wireless Testbench streaming)
|
|-- Wants triggered capture (detection-based)
| |
| |-- Knows signal structure (preamble sequence available)
| | |-- Adaptive threshold -> Pattern C
| | |-- Fixed threshold -> Pattern D
| | |-- Wants to calibrate threshold -> Pattern D + plotThreshold
| | |-- Frequency scanning loop -> Pattern G
| |
| |-- Explicit energy/power cue (energy rise, power increase), no preamble
| | |-- Adaptive threshold (energy delta + minimum) -> Pattern A
| | |-- Fixed threshold -> Pattern B
| | |-- Wants to calibrate threshold -> Pattern A/B + plotDetectionSignals
| | |-- Multiple captures with timestamps -> Pattern E
| | |-- Transmit-then-detect workflow -> Pattern F
| |
| |-- Ambiguous: no energy/power cue, no preamble, no parameters given
| -> ASK FIRST (do not assume energy): "Do you have a known preamble
| sequence to correlate against, or do you want to trigger on any
| signal energy increase? Also, what threshold values or detection
| parameters would you like to use?"
Code Generation Steps
Key Functions
| Function |
Purpose |
Toolbox |
Available From |
energyDetector |
Arm radio; trigger capture when energy rises above the noise floor |
Wireless Testbench |
R2023b |
preambleDetector |
Arm radio; trigger capture on correlation with a known preamble |
Wireless Testbench |
R2022a |
capture |
Blocking triggered IQ capture (returns on detection or timeout) |
Wireless Testbench |
with detector object |
capture(..., "NumCaptures", N) |
Capture N consecutive triggered signals (Pattern E) |
Wireless Testbench |
R2024a |
plotDetectionSignals |
Calibrate energy threshold — energyDetector only |
Wireless Testbench |
R2023b |
plotThreshold |
Calibrate preamble threshold — preambleDetector only |
Wireless Testbench |
R2022a |
transmit / stopTransmission |
Send / stop a test waveform on the detector object (Pattern F) |
Wireless Testbench |
with detector object |
zadoffChuSeq |
Generate a Zadoff-Chu preamble sequence (Patterns C/D) |
Communications Toolbox |
R2012b |
wlanLLTF / wlanNonHTConfig |
Build the WLAN L-LTF preamble (Pattern G) |
WLAN Toolbox |
R2015b |
chirp |
Generate a test chirp waveform (Pattern F) |
Signal Processing Toolbox |
before R2006a |
Step 1: Determine Detector Type
Select on an explicit cue, not on the mere absence of a preamble.
| Signal |
User says |
Detector |
| Energy-based |
explicit energy/power cue: "energy", "power increase/rise", "amplitude above the noise floor" |
energyDetector |
| Preamble-based |
"preamble", "Zadoff-Chu", "L-LTF", "PSS/SSS", "correlation", a known sequence |
preambleDetector |
| Ambiguous |
only "detect a signal" / "capture when it appears" — no energy/power cue, no preamble, no threshold parameters |
Ask first (Guardrails -> Ask First); do not default to energy |
Step 2: Gather Required Inputs
| Parameter |
Required |
Ask if missing |
| Radio name |
Yes |
Always |
| Center frequency |
Yes |
Yes |
| Sample rate |
Yes |
Yes |
| Threshold method |
Yes |
Yes (adaptive/fixed) |
| Threshold values |
Yes |
Yes |
| Capture duration |
Yes |
Yes |
| Timeout |
Yes |
Default seconds(1) if not specified |
| Preamble sequence |
Only for preambleDetector |
Yes |
Step 3: Validate Constraints
Check parameter values against valid ranges before generating code. See references/api.md for complete constraint tables (WindowLength, FixedThreshold, Preamble length, TriggerOffset, AdaptiveThresholdGain, AdaptiveThresholdOffset).
If a value violates these constraints, inform the user and suggest the valid range. Do NOT generate code with invalid values.
Step 4: Generate Code
The seven copy-ready patterns (A–G) live in references/patterns.md. Read references/patterns.md now, then use the pattern the Decision Tree selected. Each pattern ships with concrete default values — replace the values marked % <- set with the user's radio and signal parameters, and validate them against the references/api.md ranges (Step 3) before running. For loopback bring-up (transmit-then-detect), follow the Local Hardware Testing table in that file.
| Pattern |
Detector |
Threshold |
Use case |
| A |
energyDetector |
adaptive |
Wake-on-signal capture on an energy rise |
| B |
energyDetector |
fixed |
Capture above a fixed power level |
| C |
preambleDetector |
adaptive |
Capture on correlation with a known preamble |
| D |
preambleDetector |
fixed |
Preamble capture / plotThreshold calibration |
| E |
energyDetector |
adaptive |
Multiple consecutive captures with timestamps |
| F |
energyDetector |
adaptive |
Transmit-then-detect (loopback) |
| G |
preambleDetector |
adaptive |
Frequency-scanning loop (WLAN channel scan) |
If a generated pattern fires unreliably (timeout despite a real signal, false positives, or captures of noise/baseline), route into the threshold calibration sub-workflow — see the per-pattern calibration notes in references/patterns.md and the procedure in references/threshold-calibration.md.
Guardrails
Organized by when they apply: Always rules fire on every generation, Ask First rules pause for the user, Never rules trigger a refusal and redirect.
Always
- Use a
radioName variable from user input — never hardcode radio names.
- Begin every pattern with
clear ed pd — releases any prior radio lease regardless of detector class. The same physical radio can be held by either an energyDetector or a preambleDetector object; clearing only one variable name does not release a lease held by the other, which causes validateLeaseOwner errors when patterns are pasted sequentially into the same MATLAB session.
- Validate threshold ranges before generating code — see
references/api.md for exact valid ranges per parameter (key: energyDetector FixedThreshold [0, 8191], preambleDetector FixedThreshold [0, 4095]). If a value is out of range, state the valid range and do not emit the code.
- End every generated code block with a
%% Verify section that reports the outcome and prints the sanity ratio r (formulas in the table above). Do NOT hard-assert on status: a timeout (status == 0) is an expected result to handle (warn + route to calibration), not a crash — a hard assert makes a no-signal bench run throw.
- Run the generated script once — do not re-run it to "confirm." This caps whole-script runs, not the loops inside one run: let Pattern E complete all
NumCaptures captures and let Pattern G finish every channel in its scan loop. Once %% Verify reports success (status == 1, or status > 0 for multi-capture) and the printed r clears its threshold — or, for a wideband chirp/OFDM signal, the matched-filter ratio confirms it — the task is done. Do NOT execute the full deliverable again end-to-end or re-open plotDetectionSignals / plotThreshold to double-check — each extra radio round-trip costs real seconds and risks a timeout. Only exception: the user reports earlier inconsistent firing (sub-workflow trigger 3), where a short repeat is allowed — see Exit Criteria in references/threshold-calibration.md.
- Use
transmit() / stopTransmission() on the detector object for transmit-then-detect — do NOT create a separate basebandTransmitter.
- Set
ThresholdMethod before calling transmit() — you cannot switch fixed↔adaptive during a continuous transmission (it errors: "stop the ongoing transmission first"). MinimumEnergy, EnergyDeltaThreshold, and FixedThreshold can be tuned live, so sweep those without stopping.
- Match the calibration plot to the detector class —
plotDetectionSignals is energyDetector-only; plotThreshold is preambleDetector-only. Never cross them.
- Normalize any custom or random preamble into [-1, 1] before assigning
pd.Preamble. Preamble elements must lie in [-1, 1] (references/api.md). Zadoff-Chu via seq / norm(seq, 2) already satisfies this; for a random or protocol-derived sequence, scale it with preamble = preamble / max(abs(preamble)) — raw randn/sample values overflow [-1, 1] and error at assignment.
Ask First
- When the detection method is unstated, ask before generating — do not default to energy detection. The absence of a preamble in the request is not evidence for energy detection. If the request names no explicit energy/power cue, no preamble/correlation sequence, and no threshold parameters, ask one question covering both the method (energy vs. preamble) and its parameters, then proceed. If the method is clear and only parameters are missing, ask once for those.
Never
- Never generate untriggered capture — if the user wants immediate capture without detection, redirect to
matlab-transmit-capture-usrp.
- Never generate streaming code — if the user wants continuous real-time processing, redirect to the streaming workflow (use the
matlab-read-documentation skill for "Live Data I/O", Wireless Testbench).
Copyright 2026 The MathWorks, Inc.
1---2name: matlab-detect-capture-usrp3description: Smart triggered RF capture on NI USRP radios with Wireless Testbench — record only when a signal of interest appears, not continuously (triggered spectrum sensing). Use energy detection to capture when signal power rises above the noise floor, or preamble detection to cross-correlate against a known sequence (WLAN L-LTF, 5G NR PSS/SSS, LTE PSS, Zadoff-Chu, custom protocols) and capture only when that protocol's preamble is detected. Use when implementing triggered (wake-on-signal) capture or spectrum sensing, capturing protocol-based signals via cross-correlation, calibrating detection thresholds (plotDetectionSignals / plotThreshold), scanning frequency bands for activity, or building transmit-then-detect workflows.4license: https://www.mathworks.com/content/dam/mathworks/license/pmrl/license.md5metadata:6 source: https://skillmd.com/skills/matlab/matlab-detect-capture-usrp7 author: MathWorks8 version: "1.1"9---1011# Detect and Capture RF Signals on NI USRP Radios1213## Reference Loading14- **Code patterns (required at Step 4):** for the seven copy-ready capture patterns (A–G) and the loopback table, `Read references/patterns.md` before generating any capture code.15- **API reference (required at Step 3):** `Read references/api.md` for signatures, property ranges, and threshold constraints. Every generation validates parameters against it before emitting code — see the Always rule below. This read is not optional.16- **On demand:** `references/overview.md` — conceptual architecture and detector behavior.17- **On demand, after a failure only:** `references/threshold-calibration.md` — the calibration sub-workflow (`plotDetectionSignals` / `plotThreshold` step-by-step). Do not load it during first-time code generation.1819## Sub-Workflows2021This skill exposes one sub-workflow that is loaded on demand from `references/`:2223| Sub-workflow | What it provides |24|--------------|------------------|25| **Threshold Calibration** (`references/threshold-calibration.md`) | Step-by-step plot -> diagnose -> tune procedure for both `energyDetector` and `preambleDetector`, plus combined symptom -> cause -> fix table |2627### Trigger: when to route into Threshold Calibration2829Route into `references/threshold-calibration.md` whenever **any** of these conditions hold:30311. **No detection** — generated Pattern returned `status == 0` (capture timed out, no data captured) and the user expected a signal to fire it. This is the primary trigger.322. **Wrong detection** — Pattern returned `status >= 1` but the captured data is wrong: noise instead of signal, baseline instead of preamble, saturated waveform (`max(abs(data))` near `1.414`), or sanity ratio fails (`r < 50` for energyDetector, `r < 100` for preambleDetector).333. **Unstable detection** — the user reports, or an earlier run in this session already showed, that the Pattern fires inconsistently (sometimes `status=0`, sometimes `status=1`); thresholds are sitting on a jittery boundary. Do **not** run extra captures hunting for this — the Always rule below caps a clean run at one.344. **User explicitly asks** to "calibrate", "tune thresholds", "tune detection", "fix detection", "why didn't it detect", "diagnose", or names `plotDetectionSignals` / `plotThreshold`.3536**Sanity ratio `r` — where it comes from.** Every Pattern's `%% Verify` block computes and prints `r`, so triggers 2 and 3 are read straight off the pattern's own output. No extra run is needed.3738| Detector | Formula the pattern prints | Real signal |39|----------|---------------------------|-------------|40| `energyDetector` (A/B/E/F) | `r = max(abs(data).^2) / median(abs(data).^2)` | `r >= 50` |41| `preambleDetector` (C/D/G) | same ratio on the matched-filter output, `filter(conj(flipud(pd.Preamble)), 1, data)` | `r > 100` |42| Wideband chirp / OFDM (F) | raw `r` under-reports — `15-20` is normal; the pattern also prints a matched-filter ratio | matched-filter ratio `>> 100` |4344Do **not** route into the sub-workflow for first-time code generation — generate the Pattern from this SKILL.md, run it, and only route into calibration on a failure outcome. The sub-workflow assumes a detector object is already configured and a TX or external signal is present.4546If the failure mode is RF-path (no peaks at all on the figure, wrong frequency, broken cable, wrong antenna name), the sub-workflow exits early back to this skill — calibration cannot fix RF problems.4748Generate working MATLAB code to detect and capture RF signals using energy detection or preamble detection triggers on NI USRP radios with Wireless Testbench.4950## Prerequisites5152Before this skill applies, the user must have:53- MATLAB R2022a+ (preambleDetector) or R2023b+ (energyDetector)54- Wireless Testbench toolbox installed55- Wireless Testbench Support Package for NI USRP Radios installed56- Radio previously configured using the Radio Setup wizard57- Physical NI USRP radio connected and validated5859## When to Use6061Trigger this skill when the user wants smart/triggered capture — record only when something interesting appears on the air, not continuously:6263- Capture only when signal energy rises above the noise floor (`energyDetector`, Patterns A/B/E/F) — wake-on-signal recording, opportunistic capture, transmit-then-detect verification64- Capture only when a known protocol preamble is cross-correlated and matched (`preambleDetector`, Patterns C/D/G) — WLAN L-LTF, 5G NR PSS/SSS, LTE PSS, Zadoff-Chu, custom sync sequences65- Calibrate detection thresholds (`plotDetectionSignals` for energyDetector, `plotThreshold` for preambleDetector)66- Scan multiple frequencies / channels for activity (e.g., WLAN channel scan across 2.4 GHz band)67- Transmit a test waveform from the same radio and capture it back upon detection (loopback verification)68- Capture multiple consecutive triggered signals with sample-clock timestamps6970## When NOT to Use7172Route to another skill if the user's goal is:73- **Untriggered capture** (immediate IQ capture without detection) -> `matlab-transmit-capture-usrp`74- **Continuous streaming** (System objects, real-time processing loops) -> use the `matlab-read-documentation` skill for "Live Data I/O" (Wireless Testbench)75- **Radio setup or troubleshooting** -> `matlab-set-up-usrp-radio`76- **Clock/time synchronization** -> use the `matlab-read-documentation` skill for "Radio Management" (Wireless Testbench)77- **FPGA targeting** -> use the `matlab-read-documentation` skill for "Target NI USRP Radios" (Wireless Testbench)7879## Decision Tree8081```82User request83|84|-- Mentions "untriggered capture", "immediate capture", "basebandReceiver"85| -> REDIRECT to matlab-transmit-capture-usrp86|87|-- Mentions "System object", "streaming loop", "real-time processing"88| -> REDIRECT: use matlab-read-documentation for "Live Data I/O" (Wireless Testbench streaming)89|90|-- Wants triggered capture (detection-based)91| |92| |-- Knows signal structure (preamble sequence available)93| | |-- Adaptive threshold -> Pattern C94| | |-- Fixed threshold -> Pattern D95| | |-- Wants to calibrate threshold -> Pattern D + plotThreshold96| | |-- Frequency scanning loop -> Pattern G97| |98| |-- Explicit energy/power cue (energy rise, power increase), no preamble99| | |-- Adaptive threshold (energy delta + minimum) -> Pattern A100| | |-- Fixed threshold -> Pattern B101| | |-- Wants to calibrate threshold -> Pattern A/B + plotDetectionSignals102| | |-- Multiple captures with timestamps -> Pattern E103| | |-- Transmit-then-detect workflow -> Pattern F104| |105| |-- Ambiguous: no energy/power cue, no preamble, no parameters given106| -> ASK FIRST (do not assume energy): "Do you have a known preamble107| sequence to correlate against, or do you want to trigger on any108| signal energy increase? Also, what threshold values or detection109| parameters would you like to use?"110```111112## Code Generation Steps113114### Key Functions115116| Function | Purpose | Toolbox | Available From |117|----------|---------|---------|----------------|118| `energyDetector` | Arm radio; trigger capture when energy rises above the noise floor | Wireless Testbench | R2023b |119| `preambleDetector` | Arm radio; trigger capture on correlation with a known preamble | Wireless Testbench | R2022a |120| `capture` | Blocking triggered IQ capture (returns on detection or timeout) | Wireless Testbench | with detector object |121| `capture(..., "NumCaptures", N)` | Capture N consecutive triggered signals (Pattern E) | Wireless Testbench | R2024a |122| `plotDetectionSignals` | Calibrate energy threshold — energyDetector only | Wireless Testbench | R2023b |123| `plotThreshold` | Calibrate preamble threshold — preambleDetector only | Wireless Testbench | R2022a |124| `transmit` / `stopTransmission` | Send / stop a test waveform on the detector object (Pattern F) | Wireless Testbench | with detector object |125| `zadoffChuSeq` | Generate a Zadoff-Chu preamble sequence (Patterns C/D) | Communications Toolbox | R2012b |126| `wlanLLTF` / `wlanNonHTConfig` | Build the WLAN L-LTF preamble (Pattern G) | WLAN Toolbox | R2015b |127| `chirp` | Generate a test chirp waveform (Pattern F) | Signal Processing Toolbox | before R2006a |128129### Step 1: Determine Detector Type130131Select on an **explicit cue**, not on the mere absence of a preamble.132133| Signal | User says | Detector |134|--------|-----------|----------|135| Energy-based | explicit energy/power cue: "energy", "power increase/rise", "amplitude above the noise floor" | `energyDetector` |136| Preamble-based | "preamble", "Zadoff-Chu", "L-LTF", "PSS/SSS", "correlation", a known sequence | `preambleDetector` |137| Ambiguous | only "detect a signal" / "capture when it appears" — no energy/power cue, no preamble, no threshold parameters | **Ask first** (Guardrails -> Ask First); do not default to energy |138139### Step 2: Gather Required Inputs140141| Parameter | Required | Ask if missing |142|-----------|----------|----------------|143| Radio name | Yes | Always |144| Center frequency | Yes | Yes |145| Sample rate | Yes | Yes |146| Threshold method | Yes | Yes (adaptive/fixed) |147| Threshold values | Yes | Yes |148| Capture duration | Yes | Yes |149| Timeout | Yes | Default seconds(1) if not specified |150| Preamble sequence | Only for preambleDetector | Yes |151152### Step 3: Validate Constraints153154Check parameter values against valid ranges before generating code. See `references/api.md` for complete constraint tables (WindowLength, FixedThreshold, Preamble length, TriggerOffset, AdaptiveThresholdGain, AdaptiveThresholdOffset).155156If a value violates these constraints, inform the user and suggest the valid range. Do NOT generate code with invalid values.157158### Step 4: Generate Code159160The seven copy-ready patterns (A–G) live in `references/patterns.md`. **`Read references/patterns.md` now**, then use the pattern the Decision Tree selected. Each pattern ships with concrete default values — replace the values marked `% <- set` with the user's radio and signal parameters, and validate them against the `references/api.md` ranges (Step 3) before running. For loopback bring-up (transmit-then-detect), follow the Local Hardware Testing table in that file.161162| Pattern | Detector | Threshold | Use case |163|---------|----------|-----------|----------|164| A | `energyDetector` | adaptive | Wake-on-signal capture on an energy rise |165| B | `energyDetector` | fixed | Capture above a fixed power level |166| C | `preambleDetector` | adaptive | Capture on correlation with a known preamble |167| D | `preambleDetector` | fixed | Preamble capture / `plotThreshold` calibration |168| E | `energyDetector` | adaptive | Multiple consecutive captures with timestamps |169| F | `energyDetector` | adaptive | Transmit-then-detect (loopback) |170| G | `preambleDetector` | adaptive | Frequency-scanning loop (WLAN channel scan) |171172If a generated pattern fires unreliably (timeout despite a real signal, false positives, or captures of noise/baseline), route into the threshold calibration sub-workflow — see the per-pattern calibration notes in `references/patterns.md` and the procedure in `references/threshold-calibration.md`.173174## Guardrails175176Organized by when they apply: **Always** rules fire on every generation, **Ask First** rules pause for the user, **Never** rules trigger a refusal and redirect.177178### Always179- **Use a `radioName` variable from user input** — never hardcode radio names.180- **Begin every pattern with `clear ed pd`** — releases any prior radio lease regardless of detector class. The same physical radio can be held by either an `energyDetector` or a `preambleDetector` object; clearing only one variable name does not release a lease held by the other, which causes `validateLeaseOwner` errors when patterns are pasted sequentially into the same MATLAB session.181- **Validate threshold ranges before generating code** — see `references/api.md` for exact valid ranges per parameter (key: energyDetector `FixedThreshold` [0, 8191], preambleDetector `FixedThreshold` [0, 4095]). If a value is out of range, state the valid range and do not emit the code.182- **End every generated code block with a `%% Verify` section** that reports the outcome and prints the sanity ratio `r` (formulas in the table above). Do NOT hard-`assert` on `status`: a timeout (`status == 0`) is an expected result to handle (warn + route to calibration), not a crash — a hard assert makes a no-signal bench run throw.183- **Run the generated script once — do not re-run it to "confirm."** This caps whole-script runs, *not* the loops inside one run: let Pattern E complete all `NumCaptures` captures and let Pattern G finish every channel in its scan loop. Once `%% Verify` reports success (`status == 1`, or `status > 0` for multi-capture) and the printed `r` clears its threshold — or, for a wideband chirp/OFDM signal, the matched-filter ratio confirms it — the task is done. Do NOT execute the full deliverable again end-to-end or re-open `plotDetectionSignals` / `plotThreshold` to double-check — each extra radio round-trip costs real seconds and risks a timeout. *Only* exception: the user reports earlier inconsistent firing (sub-workflow trigger 3), where a short repeat is allowed — see Exit Criteria in `references/threshold-calibration.md`.184- **Use `transmit()` / `stopTransmission()` on the detector object** for transmit-then-detect — do NOT create a separate `basebandTransmitter`.185- **Set `ThresholdMethod` before calling `transmit()`** — you cannot switch fixed↔adaptive during a continuous transmission (it errors: "stop the ongoing transmission first"). `MinimumEnergy`, `EnergyDeltaThreshold`, and `FixedThreshold` *can* be tuned live, so sweep those without stopping.186- **Match the calibration plot to the detector class** — `plotDetectionSignals` is energyDetector-only; `plotThreshold` is preambleDetector-only. Never cross them.187- **Normalize any custom or random preamble into [-1, 1] before assigning `pd.Preamble`.** `Preamble` elements must lie in [-1, 1] (`references/api.md`). Zadoff-Chu via `seq / norm(seq, 2)` already satisfies this; for a random or protocol-derived sequence, scale it with `preamble = preamble / max(abs(preamble))` — raw `randn`/sample values overflow [-1, 1] and error at assignment.188189### Ask First190- **When the detection method is unstated, ask before generating — do not default to energy detection.** The absence of a preamble in the request is *not* evidence for energy detection. If the request names no explicit energy/power cue, no preamble/correlation sequence, and no threshold parameters, ask **one** question covering both the method (energy vs. preamble) and its parameters, then proceed. If the method is clear and only parameters are missing, ask once for those.191192### Never193- **Never generate untriggered capture** — if the user wants immediate capture without detection, redirect to `matlab-transmit-capture-usrp`.194- **Never generate streaming code** — if the user wants continuous real-time processing, redirect to the streaming workflow (use the `matlab-read-documentation` skill for "Live Data I/O", Wireless Testbench).195196----197198Copyright 2026 The MathWorks, Inc.199200----