gastropy.fit_sine#
- gastropy.fit_sine(signal, sfreq, freq=None, freq_init=0.05)[source]#
Fit a sine wave to an EGG signal using least-squares optimisation.
Minimises the sum of squared residuals between
signaland a modelA·sin(2πft + φ)using L-BFGS-B. Iffreqis provided, only phase and amplitude are fitted (faster); otherwise frequency, phase, and amplitude are all free parameters.- Parameters:
signal (array_like) – Single-channel EGG signal (1D array).
sfreq (float) – Sampling frequency in Hz.
freq (float or None, optional) – If provided, the frequency is fixed to this value (Hz) and only phase and amplitude are optimised. If None, all three parameters are fitted jointly. Default is None.
freq_init (float, optional) – Initial frequency guess (Hz) used when
freq=None. Defaults to 0.05 Hz (normogastric centre, ~3 cpm). Set this to a value near the expected dominant frequency when analysing signals outside the normogastric band (e.g.freq_init=0.1for tachygastric recordings).
- Returns:
result – Fitted parameters:
freq_hz: float — fitted (or fixed) frequency in Hz.phase_rad: float — fitted phase in radians.amplitude: float — fitted amplitude (may be negative; a negative amplitude is equivalent to a positive amplitude with a phase shift of π).residual: float — sum of squared residuals at solution.
- Return type:
References
Dalmaijer, E. S. (2025). electrography v1.1.1. esdalmaijer/electrography
Examples
>>> import numpy as np >>> from gastropy.signal import fit_sine >>> t = np.arange(0, 300, 0.1) >>> sig = 2.5 * np.sin(2 * np.pi * 0.05 * t + 0.3) >>> result = fit_sine(sig, sfreq=10.0, freq=0.05) >>> abs(result["amplitude"] - 2.5) < 0.1 True