'Filter the data using Savitzky-Golay polynomial method.
Parameters
h_freq : float
Approximate high cut-off frequency in Hz. Note that this
is not an exact cutoff, since Savitzky-Golay filtering [1]_ is
done using polynomial fits instead of FIR/IIR filtering.
This parameter is thus used to determine the length of the
window over which a 5th-order polynomial smoothing is used.
copy : bool
If True, a copy of the object, filtered, is returned.
If False (default), it operates on the object in place.
Returns
inst : instance of Epochs or Evoked
The object with the filtering applied.
See Also
mne.io.Raw.filter
Notes
For Savitzky-Golay low-pass approximation, see:
https://gist.github.com/Eric89GXL/bbac101d50176611136b
.. versionadded:: 0.9.0
Examples
>>> import mne
>>> from os import path as op
>>> evoked_fname = op.join(mne.datasets.sample.data_path(), \'MEG\', \'sample\', \'sample_audvis-ave.fif\')  # doctest:+SKIP
>>> evoked = mne.read_evokeds(evoked_fname, baseline=(None, 0))[0]  # doctest:+SKIP
>>> evoked.savgol_filter(10.)  # low-pass at around 10 Hz # doctest:+SKIP
>>> evoked.plot()  # doctest:+SKIP
References
.. [1] Savitzky, A., Golay, M.J.E. (1964). "Smoothing and
Differentiation of Data by Simplified Least Squares
Procedures". Analytical Chemistry 36 (8): 1627-39.'

def savgol_filter(self, h_freq, copy=False):

    inst = (self.copy() if copy else self)
    if (not inst.preload):
        raise RuntimeError('data    must    be    preloaded    to    filter')
    data = inst._data
    h_freq = float(h_freq)
    if (h_freq >= (inst.info['sfreq'] / 2.0)):
        raise ValueError('h_freq    must    be    less    than    half    the    sample    rate')
    if (not check_version('scipy', '0.14')):
        raise RuntimeError('scipy    >=    0.14    must    be    installed    for    savgol')
    from scipy.signal import savgol_filter
    window_length = (((int(np.round((inst.info['sfreq'] / h_freq))) // 2) * 2) + 1)
    data[...] = savgol_filter(data, axis=(-1), polyorder=5, window_length=window_length)
    return inst
