detrending in real time I have a certain signal that has a linear trend that should be removed:



The problem is that the signal needs to be processed in real time(sampling rate is 20Hz). 
If I only apply the detrending per window of say, 20 samples and then concatenate detrending results, the signal is disrupted.

I wonder if there is some efficient technique to detrend the data per window in real time and then concatenate it so the final signal will make sense? Or should I just detrend every new incoming data with the older data which is far from being efficient.

I would like to note that taking the entire signal and processing it offline using MATLAB's detrend works correctly. The problem occurs when breaking the signal into certain fixed size windows and then applying detrend on each.

Thanks, 
Mike
What is the nature of the trend?

If the trend is due to DC drift (perhaps due to the signal acquisition apparatus), you may be able to remove it with a simple high-pass filter around 0.1 - 0.5 Hz. This would eliminate the need for any real-time post-processing.

DC drift gives a slowly rising/falling DC component, such as in the first figure here: