PSD Parameter Exploration

PSD Parameter Exploration#

Explore how the overlap parameter in psd_welch affects the power spectral density estimate. The default overlap is 0.25 (25%); the Wolpert et al. (2020) convention uses 0.75 (75%).

import matplotlib.pyplot as plt

import gastropy as gp

plt.rcParams["figure.dpi"] = 100
plt.rcParams["figure.facecolor"] = "white"
egg = gp.load_egg()
best_idx, _, _, _ = gp.select_best_channel(egg["signal"], egg["sfreq"])
best_signal = egg["signal"][best_idx]
sfreq = egg["sfreq"]

Effect of Overlap#

Higher overlap gives smoother PSD estimates (more averaging) at the cost of some spectral leakage. For short recordings, higher overlap is recommended.

overlaps = [0.0, 0.25, 0.50, 0.75]

fig, ax = plt.subplots(figsize=(10, 5))
for ov in overlaps:
    freqs, psd = gp.psd_welch(best_signal, sfreq, fmin=0.0, fmax=0.1, overlap=ov)
    ax.plot(freqs, psd, label=f"overlap={ov:.0%}", linewidth=1.2)

ax.axvspan(gp.NORMOGASTRIA.f_lo, gp.NORMOGASTRIA.f_hi, alpha=0.08, color="grey", label="normogastric")
ax.set_xlabel("Frequency (Hz)")
ax.set_ylabel("Power")
ax.set_title("PSD Overlap Comparison")
ax.legend(fontsize=9)
ax.spines["top"].set_visible(False)
ax.spines["right"].set_visible(False)
plt.show()
../_images/cb0df714632c37294563ea41dc66fdc720391bc86f2089734907e712c4d515c7.png

Effect of Frequency Range#

The fmin and fmax parameters control which portion of the PSD is returned. The underlying computation always uses the full range and then trims.

fig, axes = plt.subplots(1, 3, figsize=(15, 4))

ranges = [(0.0, 0.1, "Full (0–0.1 Hz)"), (0.02, 0.08, "Gastric (0.02–0.08 Hz)"), (0.033, 0.067, "Normogastric only")]

for ax, (fmin, fmax, title) in zip(axes, ranges, strict=True):
    f, p = gp.psd_welch(best_signal, sfreq, fmin=fmin, fmax=fmax, overlap=0.75)
    ax.plot(f, p, color="forestgreen", linewidth=1.2)
    ax.set_xlabel("Frequency (Hz)")
    ax.set_ylabel("Power")
    ax.set_title(title)
    ax.spines["top"].set_visible(False)
    ax.spines["right"].set_visible(False)

fig.tight_layout()
plt.show()
../_images/48ddd22c65c4944da9001b9fb35d963197e765ba6725c6e7ca26da3ea2362efc.png

See also: PSD Visualization, Channel Selection