Part VII: Health, Biosignals, and Wearable AI
Chapter 32: EMG and Neuromuscular AI

Artifact and crosstalk handling

"A clean muscle signal is a story about one muscle. Half of what reaches the electrode is a rumor from the neighbors and a complaint from the cable."

A Skeptical AI Agent

Prerequisites

This section builds directly on the volume-conductor and crosstalk picture introduced in Section 32.1 and the envelope and spectral features of Section 32.2. It leans on the filter-design vocabulary (high-pass, notch, adaptive filtering) from Chapter 6, the spectral reasoning of Chapter 7, and the change-detection idea from Chapter 12. No new physiology is needed; every corruption below is treated as an engineering signal path from an unwanted source into your channel.

The Big Picture

An EMG electrode is a microphone in a crowded room. The voice you want, the target muscle, arrives mixed with four kinds of interference: powerline hum from the mains, motion and cable artifact from the electrode-skin interface flexing, physiological contamination such as the heartbeat leaking into trunk muscles, and crosstalk from adjacent muscles sharing the detection volume. Each corruption lives in a different part of the signal, so each needs a different tool, and the deepest one, crosstalk, cannot be removed by any frequency filter because it occupies the exact same band as the signal. This section is a field guide to knowing which corruption you have, removing what is removable, spatially suppressing what filtering cannot, and, above all, preventing your model from quietly learning the artifact as if it were physiology. That last failure is the one that passes every offline test and collapses in the field.

The artifact zoo: name it before you fight it

Mislabeling a corruption wastes the wrong tool on it, so start by locating each interferer in frequency and space. Powerline interference is a narrow, near-deterministic tone at 50 or 60 Hz plus harmonics, injected capacitively from nearby wiring; it sits squarely inside the 20 to 450 Hz EMG band, which is why you cannot simply band-pass it away. Motion and baseline-wander artifact is the opposite: mostly low-frequency energy (below roughly 20 Hz) from the electrode half-cell potential shifting as the skin stretches, plus abrupt steps when a cable tugs or an electrode partially lifts. Electrocardiographic (ECG) contamination is a quasi-periodic sharp complex around 1 Hz repetition that overlaps EMG spectrally and dominates recordings from trunk and proximal muscles. Saturation and contact loss are not additive noise at all but clipping and dropout, where the amplifier rails or the signal flatlines. And crosstalk, the corruption this chapter names its own section for, is genuine muscle voltage from the wrong muscle, identical in spectrum and statistics to the signal you want.

The practical consequence is a decision tree. Narrowband and stationary? Notch it. Low-frequency and slow? High-pass it. Periodic and cardiac? Template-subtract or gate it. Clipped or flat? Detect and mask the segment, never trust it. Spectrally identical but spatially separable? Only geometry, not filtering, will help. Getting this mapping right is most of the battle.

Frequency-domain defenses and their honest limits

Two filters handle the easy majority. A high-pass (or band-pass) filter with a corner near 20 Hz removes baseline wander and most motion artifact, at the cost of discarding the low-frequency EMG content that carries some force information; the 20 Hz corner is a compromise the surface-EMG community settled on precisely because it sacrifices little muscle energy while cutting most motion energy. A notch or narrow band-stop at the mains frequency and its harmonics suppresses powerline hum. But a naive fixed notch is blunt: it also deletes real EMG energy at that frequency and rings on transients. A spectral-interpolation or adaptive notch approach, which estimates the hum's amplitude and phase and subtracts exactly that sinusoid, preserves far more signal, and is the reason we lean on the adaptive-filtering ideas from Chapter 6 rather than a hard band-stop.

ECG contamination is the interesting middle case. Its spectrum overlaps EMG, so filtering alone smears it, but its timing is exploitable: detect each R-peak, then subtract an averaged QRS template time-locked to those peaks, or gate the affected windows. This is a recurring pattern in biosignal cleaning, borrowing directly from the cardiac methods of Chapter 29: when a corruption is periodic, its period is a lever no additive-noise filter can match.

Key Insight

Crosstalk is spectrally invisible: the interfering muscle emits the same 20 to 450 Hz broadband signal as your target, so no band-pass, notch, or wavelet threshold can separate them. What differs is where the sources sit, not their frequency content. This is why crosstalk is defeated in the spatial domain, by combining multiple closely spaced electrodes so that a shared, distant source cancels while a local source survives. If you find yourself reaching for a sharper temporal filter to fix crosstalk, you are solving the wrong equation. Reach for another electrode instead.

Spatial filtering: the real crosstalk remedy

Recall from Section 32.1 that a distant source contributes to many electrodes almost equally, because its potential has spread and flattened by the time it reaches the skin, while a nearby source produces a sharp spatial peak under one electrode. That difference is a filter waiting to be built. A single-differential montage subtracts two adjacent electrodes, cancelling any voltage common to both (much of the far-field crosstalk and a good deal of powerline pickup) while keeping the local gradient of the target. A double-differential or Laplacian montage goes further, subtracting the second spatial derivative so that only sources tightly localized under the center electrode survive. Each step up sharpens selectivity, trading a wider, blurrier detection volume for a narrow one, which is exactly the surface-side lever toward the intramuscular selectivity of Section 32.1. The listing below builds a three-electrode array over a target muscle with a neighbor bleeding in, and measures how single- and double-differential montages collapse the crosstalk.

import numpy as np

rng = np.random.default_rng(1)
fs, T = 2000, 2.0
t = np.arange(int(fs * T)) / fs

# Two independent broadband "muscle" sources (band-limited noise bursts).
def muscle(seed):
    x = rng.standard_normal(t.size) if seed is None else np.random.default_rng(seed).standard_normal(t.size)
    b = np.exp(-((np.arange(-40, 41)) ** 2) / (2 * 12 ** 2)); b /= b.sum()
    return np.convolve(x, b, mode="same")

target, neighbor = muscle(10), muscle(20)

# Electrode line: target is local (steep falloff), neighbor is far (nearly common-mode).
w_t = np.array([0.55, 1.00, 0.55])     # target peaks under the center electrode
w_n = np.array([0.90, 0.95, 0.90])     # neighbor arrives almost equally on all three
E = np.outer(target, w_t) + np.outer(neighbor, w_n)   # shape (samples, 3 electrodes)

def crosstalk_ratio(sig, tgt, ngh):
    # Least-squares energy share attributable to the neighbor source.
    A = np.vstack([tgt, ngh]).T
    a_t, a_n = np.linalg.lstsq(A, sig, rcond=None)[0]
    return (a_n ** 2 * ngh.var()) / (a_t ** 2 * tgt.var() + a_n ** 2 * ngh.var())

mono = E[:, 1]                         # single electrode (monopolar)
sd   = E[:, 1] - E[:, 0]               # single differential
dd   = E[:, 0] - 2 * E[:, 1] + E[:, 2] # double differential (spatial Laplacian)

for name, s in [("monopolar", mono), ("single-diff", sd), ("double-diff", dd)]:
    print(f"{name:12s} crosstalk share: {crosstalk_ratio(s, target, neighbor):.3f}")
Listing 32.6. A spatial-filter crosstalk demo. Three electrodes see a local target source and a near-common-mode neighbor. The monopolar channel carries a large crosstalk share; the single-differential montage cancels most of the common neighbor; the double-differential (Laplacian) montage nearly eliminates it, because differencing rejects the spatially flat far-field. The cost, unshown here, is a smaller detection volume and lower signal amplitude. Sweep w_n toward w_t to see selectivity fail as the neighbor stops behaving like a common-mode source.

As Listing 32.6 shows numerically, the crosstalk share drops sharply from the monopolar channel to the double-differential montage, with no temporal filter touched. This is the whole argument of the key-insight callout made quantitative: geometry, not frequency, is the crosstalk lever.

In Practice: a prosthetic wrist that fought the road

A myoelectric hand for a transradial user worked flawlessly in the fitting clinic and misfired constantly on the drive home. Logging the raw channels revealed the culprit: every time the residual limb pressed against the socket over a bump, the electrode-skin interface flexed and threw a low-frequency step that the envelope detector read as a contraction, triggering an unwanted grasp. The vibration also modulated the baseline in the motion band. The fix layered three defenses: a 20 Hz high-pass to kill the baseline steps, a single-differential montage to reject the common-mode powerline and much of the socket-wide motion, and a small accelerometer on the socket used as a reference channel for an adaptive filter, subtracting the correlated motion component (the inertial-sensing tools of Chapter 23 earning their keep here). False grasps dropped by an order of magnitude. The lesson: the clinic bench is a clean room, and artifact robustness is a road-test property, not a bench property.

Reference-based and data-driven removal

When an artifact has a measurable cause, measure it. An adaptive filter takes a reference correlated with the interferer (an accelerometer for motion, a dedicated electrode for the mains, an ECG lead for cardiac) and subtracts the part of the EMG that the reference predicts, leaving the muscle signal that the reference cannot explain. This is the single most powerful non-spatial tool, because it removes exactly the corrupted component and nothing else. When you have many electrodes but no clean reference, blind source separation such as independent component analysis can unmix the recording into statistically independent sources, letting you discard the components that look like powerline, motion, or a wandering baseline; the same machinery that decomposes high-density surface EMG in Section 32.1 doubles as an artifact separator. Both approaches share a caution: an over-eager filter that removes any component correlated with movement will also remove the very muscle activity you are trying to measure, because in EMG the signal and the motion artifact are both caused by the same movement.

The Right Tool

Hand-rolling a full clean-and-artifact pipeline (band-pass, adaptive mains removal, rectification, envelope, plus onset-based quality checks) is roughly 60 to 90 lines that are easy to misorder. neurokit2 collapses the standard path to two lines and returns a labeled envelope you can gate on:

import neurokit2 as nk
clean = nk.emg_clean(emg, sampling_rate=2000)             # band-pass + mains handling
signals, info = nk.emg_process(clean, sampling_rate=2000) # envelope + activation onsets
Listing 32.7. neurokit2.emg_clean plus emg_process replace about 70 lines of temporal preprocessing with one call each. What no library does for you is spatial crosstalk suppression (that needs your multi-electrode montage from Listing 32.6) or deciding which segments to discard. Match the tool to the corruption: libraries handle the temporal filters; geometry and gating are yours.

Do not let the model learn the artifact

The failure that survives every offline benchmark is artifact leakage: a model that scores well by keying on a corruption that happens to correlate with the label in your dataset. If every "grasp" trial in training was recorded with the arm moving, a network can classify grasps by reading motion artifact instead of muscle activity, then fail the instant the artifact statistics shift, exactly the distribution-shift trap of Chapter 66. The defenses are threefold. First, quality gating: compute a signal-quality index per window (saturation flags, powerline-to-signal ratio, baseline-wander energy) using the change-detection tooling of Chapter 12, and mask or down-weight bad windows rather than feeding them in. Second, artifact-aware augmentation: inject synthetic powerline, motion steps, and electrode-shift into training so the model learns invariance rather than dependence. Third, leakage-safe splits: as Chapter 5 and Section 32.5 insist, split by subject and session so that no recording condition is shared across train and test, which is the only way to catch a model that has quietly learned the room instead of the muscle.

Research Frontier

The current direction is to fold artifact handling into the model rather than bolting it on upstream. Self-supervised pretraining on large unlabeled EMG corpora (the wearable-foundation-model program of Chapter 20) learns representations that are naturally robust to electrode shift and motion, and domain-adversarial training explicitly penalizes any feature that predicts the recording session, forcing the network off artifact shortcuts. Meta's surface-EMG wristband work has pushed generalized, out-of-the-box decoding that tolerates day-to-day electrode variation without per-user recalibration, a robustness bar that classical per-session cleaning never reached. The open problem is evaluation: robustness claims are only credible under the leakage-safe, cross-subject, cross-session protocols of Chapter 65, and a model that has never seen a truly novel artifact distribution has not yet been tested.

Exercise

Extend Listing 32.6 by adding a 60 Hz powerline sinusoid, identical on all three electrodes (pure common mode), plus a low-frequency baseline drift unique to the center electrode. Confirm numerically that the single-differential montage removes the powerline (because it is common mode) but does not remove the drift (because it is not shared), then add a 20 Hz high-pass and show the drift falls away. In three sentences, explain why the two corruptions needed two different tools and which domain (spatial or temporal) each attack lived in.

Self-Check

1. Why can a notch filter remove 50 Hz powerline hum but never remove crosstalk, even though both fall inside the EMG band?

2. An accelerometer-referenced adaptive filter is tuned aggressively and the classifier's accuracy drops. Give the most likely reason, given that EMG and motion artifact are often driven by the same movement.

3. A model reaches 98% grasp-classification accuracy offline and fails in deployment. Describe the artifact-leakage mechanism that produces exactly this pattern, and name the single evaluation change from Chapter 5 that would have exposed it.

What's Next

In Section 32.7, we close the loop: the cleaned, crosstalk-suppressed signal must now drive a device in real time, under tens of milliseconds of latency, where every filter and gate you added here becomes a budget item competing against responsiveness. We turn artifact handling into a streaming control problem.