Search code examples
pandasscipyfilteringsignal-processingsmoothing

Pandas.rolling().median vs Scipy.signal.medfilt()


I applied Pandas.rolling().median(), and it has a delay or phase shift (green line).

If I use Scipy.signal.medfilt() the results are not shifted (yellow line).

enter image description here

Why, the results are not the same?

P.S. I've tried to look into medfilt implementation, which uses sigtools._order_filterND, which I assume is not in python.

CODE:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from scipy.signal import medfilt

x = np.zeros(100)
x[30:70] = 2
x+= .1 * np.random.randn(100)

y1 = medfilt(x, kernel_size=5)

plt.plot(x, '.-', label='original signal');
plt.plot(y1, '.-', alpha=.5, label='medfilt'); 
plt.plot(pd.Series(x).rolling(5).median(), label='rolling window');
plt.legend();


Solution

  • Have you tried to center the window while rolling?

    Pandas.rolling(center=True).median()