pyhctsa.robust.bf_fit_sinusoids

pyhctsa.robust.bf_fit_sinusoids(y, k, constant=False)

Least-squares fit of a sum of k sinusoids to a time series (hctsa BF_FitSinusoids).

Fits y(t) = sum_i a_i sin(2 pi f_i t) + b_i cos(2 pi f_i t), t = 0..N-1 (plus a constant c if constant). The amplitudes and phases (and c) are linear, found by least squares for given frequencies, so only the k frequencies are searched (variable projection), deterministically and stagewise: each frequency is added at the grid frequency (2N points on [1/N, 1/2 - 1/(2N)]) that most reduces the residual sum of squares given those already chosen (with the constant, if there is one, fitted jointly throughout), and then all of the chosen frequencies are refined, twice in turn by 8 zooming local grids and then together by Gauss-Newton iterations (_sin_refine()), to the least-squares minimum at machine precision, before the next is added (so that the residual of an exact sum of sinusoids is rounding error, and each frequency is added to the least-squares fit of those before it rather than to the leakage left by their grid resolution).

The frequencies are bounded to [1/N, 1/2 - 1/(2N)] cycles per sample. Each sinusoid is an oscillation that completes at least one full cycle within the series: a component with less than one cycle in the window is a trend, not an oscillation (its period is not observed, and below 1/(2N) its sine and cosine terms are not distinguishable from a constant and a linear trend). A frequency at 1/2 has a zero sine term.

Parameters:
y : array-like

The time series.

k : int

The number of sinusoids.

constant : bool, optional

Whether the model includes a constant term (default False).

Returns:

  • yfit (numpy.ndarray) – The fitted values.

  • freqs (numpy.ndarray) – The k fitted frequencies in cycles per sample, in increasing order.

Return type:

Tuple[ndarray, ndarray]