Let me suggest you to perform the filtering in sliding blocks, not separate. Assume you want to filter the signal $\{ x_i\}$. At each index $k$, you can estimate the linear trend on the block $$X_k=[ x_{k-K_l},\ldots,x_k,\ldots, x_{k+K_r}],$$ taken at indices $$I_k=[ {k-K_l},\ldots,k,\ldots, {k+K_r}],$$ in the form $\beta_k x + \alpha_k$, and remove $\beta x_k + \alpha$ from $x_k$. If $$ denotes the average of the block $Y_k$, then $\beta_k = \frac{ - }{-^2}$, and $\alpha_k = - \beta_k $. All the averages $$, $$, $$ and $$ can be computed recursively with few computations.
LOESSまたはLOWESSの平滑化とも呼ばれるローカル線形回帰の実装になります。
たとえば、 Hodrick-Prescottフィルタのような代替案を考えることができます。