# inspect one trial (loaded from the preprocessing artefacts)
EXAMPLE = 0
example_path = aligned_files[EXAMPLE]
pair, trail_number = parse_aligned_filename(example_path)
aligned = pd.read_csv(example_path) # time, envelope, sway_speed, sway_lean, condition
time = aligned['time'].to_numpy()
sig = aligned['envelope_change'].to_numpy()
sway_lean = aligned['sway_lean'].to_numpy()
# decompose the envelope and take each IMF's power (this is the method)
imf_env = emd.sift.mask_sift(sig, max_imfs=2)
amp = np.abs(hilbert(imf_env, axis=0))
pwr = amp ** 2
# gesture and voiced windows for THIS trial (onset/offset already in the tables)
spans = gest[(gest['pair'] == pair) & (gest['trial'] == trail_number)][['onset', 'offset']]
vspans = voicing[(voicing['pair'] == pair) & (voicing['trial'] == trail_number)]
print(f"{len(spans)} gestures, {len(vspans)} voiced segments (trial ends {time[-1]:.1f}s)")
def overlay_lean(a, show_label=False):
ab = a.twinx()
ab.plot(time, sway_lean, color='C3', lw=0.8, alpha=0.5, label='|lean| (AngleX dev)')
ab.set_ylabel('|AngleX dev| (°)', color='C3'); ab.tick_params(axis='y', labelcolor='C3')
if show_label:
ab.legend(loc='upper right', fontsize=8)
return ab
def mark_gestures(a, label=False):
for i, (onset, offset) in enumerate(spans.itertuples(index=False)):
a.axvspan(onset, offset, color='0.5', alpha=0.18, zorder=0,
label='gesture' if (label and i == 0) else None)
def mark_voiced(a, label=False):
for i, (on, off) in enumerate(vspans[['onset','offset']].itertuples(index=False)):
a.axvspan(on, off, color='red', alpha=0.10, zorder=0,
label='voiced' if (label and i == 0) else None)
fig, ax = plt.subplots(5, 1, figsize=(12, 10), sharex=True)
ax[0].plot(time, sig, color='0.4', lw=0.8)
ax[0].set_title('Envelope_change (overlap window)')
mark_voiced(ax[0], label=True); mark_gestures(ax[0], label=True); overlay_lean(ax[0], show_label=True)
ax[0].legend(loc='upper left', fontsize=8)
for k, name in [(0, 'IMF 1 (fast)'), (1, 'IMF 2')]:
a_imf = ax[1 + 2 * k] # axis for this IMF
a_imf.plot(time, imf_env[:, k], color='0.1', lw=0.7)
a_imf.set_title(name)
mark_voiced(a_imf); mark_gestures(a_imf); overlay_lean(a_imf)
a_pwr = ax[2 + 2 * k] # axis for this IMF's power
a_pwr.plot(time, pwr[:, k], 'C0', lw=1.0)
a_pwr.fill_between(time, pwr[:, k], color='C0', alpha=0.2)
a_pwr.set_title(f'{name} — power')
mark_voiced(a_pwr); mark_gestures(a_pwr); overlay_lean(a_pwr)
ax[-1].set_xlabel('time (s)')
plt.tight_layout()