<!-- TE10 Studio documentation: /docs/api/postprocessing -->
# Post-processing

```python
from te10_studio.postprocess import networks, channel
ntwk = result.network()                 # or networks.load_touchstone("board.s4p")
```

## Networks: `te10_studio.postprocess.networks`

The functions behind the app's S-parameter window, so a script gets the same numbers as the app. Each
takes a scikit-rf `Network` (`result.network()`, or `networks.load_touchstone(path)`), with ports
numbered from **1**:

| Function | Result |
|---|---|
| `trace(ntwk, quantity, i, j, param="S")` | `(f_GHz, values)`; quantity `db`, `mag`, `phase`, `phase_unwrapped`, `group_delay` (ns), `vswr`, `return_loss`, `real`, `imag`; param `S`, `Z`, `Y`, `ABCD`, `T` |
| `renormalize(ntwk, z0)`, `port_extension(ntwk, {port: length_m}, eps_eff=1)` | new Networks (reference impedance / reference planes moved) |
| `mixed_mode(ntwk, pairs=None)` | differential / common mode; `pairs=[(1, 3), (2, 4)]` (positive, negative), default (1,2), (3,4), …; ports D1..DP, C1..CP, then unpaired ports |
| `interpolate(ntwk, f1_GHz, f2_GHz, points)`, `cascade(a, b)`, `deembed_fixtures(ntwk, left, right)` | new Networks (fixtures: 2-ports removed from either side) |
| `enforce_passivity(ntwk)` | singular values of S above 1 clipped, frequency by frequency |
| `causality(ntwk)` | {(i, j): impulse-response energy fraction at negative time}; > ~1 % is suspect |
| `time_response(ntwk, i, j, "impulse", "step" or "tdr", window="hamming", gate=(center_ns, span_ns), rise_time_ps=None, rise_definition="10-90")` | `(t_ns, response)`; `rise_definition` `10-90`, `20-80` or `0-100` (linear ramp) |
| `tdr(s_reflection, f_hz, z_ref, Edge("rise", rise_ps=None, definition="10-90"))` | `(t_ns, z_ohm)` of any reflection term (e.g. SDD11 from `channel`); `rise_ps=None` = the fastest the sweep supports, a faster one is clamped (`edge_rise_ps`) |
| `min_rise_time_ps(f_max_hz, definition="10-90")`, `tdr_view_range(t, z, rise_ns, round_trip_ns=None)` | fastest edge of a sweep; `(t0, t1, z_lo, z_hi)` worth showing |
| `checks(ntwk)` | passivity, reciprocity and, for 2-ports, Rollett K, μ and maximum gain |
| `write_touchstone(ntwk, path)`, `write_csv(ntwk, path)` | files (`.sNp` follows the port count) |

`time_response` raises `ValueError` when a `rise_time_ps` is faster than the sweep's top frequency
supports (about 0.875 / f_max for 10–90 %); leave it `None` for the fastest edge.

## Channel measurements: `te10_studio.postprocess.channel`

The S-parameter window's port map and measurements. A **port map** says which channel each port belongs
to, which end (side 1 or 2) it is at, and its polarity (single-ended, or the + / − leg of a pair).

| Function | Result |
|---|---|
| `auto_port_map(ntwk, pair_hint=None)` | `PortMap`: each port's channel, side (1 / 2) and polarity (`single`, `+`, `-`); `pair_hint=[(p, n), …]` |
| `PortMap.signals()`, `.ports(channel, side)`, `.pair(channel, side)`, `.is_pair(channel)`, `.to_dict()` / `from_dict` | the map |
| `mode_s(ntwk, pmap, mode_out, lane_out, side_out, mode_in, lane_in, side_in)` | one mixed-mode term (modes `S`, `D`, `C`), e.g. SDD21 = `mode_s(n, pm, "D", 1, 2, "D", 1, 1)` |
| `curves(ntwk, pmap, measurement, channels, sides, mode=None, aggressors="each")` | list of `Curve(label, x, y, unit, meta)`; measurement `il`, `rl`, `vswr`, `next`, `fext`, `modeconv`, `gd`, `skew`, `phase`, `zin`, `smith`, `polar_refl`, `polar_thru`; mode `SE`, `DD`, `CC`, `CD`, `DC`, … |
| `available(ntwk, pmap)`, `modes_available(pmap, measurement)` | what the map supports |
| `power_sum(terms)`, `skew_ps(ntwk, pmap, channel)`, `input_impedance(s, zref)`, `through_delay_ns(ntwk, pmap, channel)` | helpers |

```python
from te10_studio.postprocess import channel
n = result.network()
pm = channel.auto_port_map(n)
for c in channel.curves(n, pm, "il", pm.signals(), [1], "DD"):
    print(c.label, c.y.min())                       # e.g. "SDD21 · Channel 1"
```

## Examples

```python
from te10_studio.postprocess import networks as nw

ntwk = result.network()
f, il = nw.trace(ntwk, "db", 2, 1)                       # insertion loss, dB
f, gd = nw.trace(ntwk, "group_delay", 2, 1)              # ns
mm = nw.mixed_mode(ntwk, pairs=[(1, 3), (2, 4)])         # differential + common mode network
t, z = nw.time_response(ntwk, 1, 1, "tdr")       # fastest edge the sweep supports
print(nw.checks(ntwk))                                   # passivity, reciprocity, stability
nw.write_touchstone(nw.renormalize(ntwk, 100), "board_100ohm.s4p")
```
