// Gather windows & vectorize, so we can call math functions in one go
n_win = int(np.floor(len(sig) / win_len))
sig_rect = np.reshape(sig[:n_win * win_len], (n_win, win_len)).T
// Calculate local trend, as the line of best fit within the time window
_, fluc, _, _, _ = np.polyfit(np.arange(win_len), sig_rect, deg=deg, full=True)
After Change
// Calculate cumulative sum of the signal & split the signal into segments
segments = split_signal(sp.cumsum(sig - np.mean(sig)), win_len).T
// Calculate local trend, as the line of best fit within the time window
_, fluc, _, _, _ = np.polyfit(np.arange(win_len), segments, deg=deg, full=True)