Filtering noisy signals with variable and drifting baselines

Solution for Filtering noisy signals with variable and drifting baselines
is Given Below:

I have an optical rotary encoder input to an Arduino Due and the voltage trace is being recorded by National Instruments hardware. I am having trouble analyzing these data because there appears to be variable drifting in the voltage baseline – sometimes none, sometimes it’s oscillatory, sometimes there appears to be a sudden shift in the middle of the session.

Does anyone have suggestions for a one-size fits all filter to correct the baseline? I have used a butterworth filter with some success, but it’s not enabling me completely to do the analysis I need to know. The filter would have to be zero-lag.

Here’s the code for the filter I’ve used:

d1 = designfilt('lowpassiir','FilterOrder',12,'HalfPowerFrequency',0.15,'DesignMethod','butter');
rpmFiltered = filtfilt(d1,signalData);