filtering.py 文件源码

python
阅读 29 收藏 0 点赞 0 评论 0

项目:nelpy 作者: nelpy 项目源码 文件源码
def bandpass_filter(data, lowcut=None, highcut=None, *, numtaps=None,
                    fs=None):
    """Band filter data using a zero phase FIR filter (filtfilt).

    Parameters
    ----------
    data : AnalogSignalArray, ndarray, or list
    lowcut : float, optional (default 1 Hz)
        Lower cut-off frequency
    highcut : float, optional (default 600 Hz)
        Upper cut-off frequency
    numtaps : int, optional (default 25)
        Number of filter taps
    fs : float, optional if AnalogSignalArray is passed
        Sampling frequency (Hz)

    Returns
    -------
    filtered : same type as data
    """

    if numtaps is None:
        numtaps = 25
    if lowcut is None:
        lowcut = 1
    if highcut is None:
        highcut = 600

    if isinstance(data, (np.ndarray, list)):
        if fs is None:
            raise ValueError("sampling frequency must be specified!")
        # Generate filter for detection
        b = firwin(numtaps=numtaps,
                   cutoff=[lowcut/(fs/2), highcut/(fs/2)],
                   pass_zero=False)
        # Filter raw data to get ripple data
        ripple_data = filtfilt(b, 1, data)
        return ripple_data
    elif isinstance(data, AnalogSignalArray):
        if fs is None:
            fs = data.fs
            warnings.warn("no sampling frequency provided,"
                " using fs={} Hz from AnalogSignalArray".format(fs))
        # Generate filter for detection
        b = firwin(numtaps=numtaps,
                   cutoff=[lowcut/(fs/2), highcut/(fs/2)],
                   pass_zero=False)
        # Filter raw data to get ripple data
        ripple_data = filtfilt(b,1,data.ydata)
        # Return a copy of the AnalogSignalArray with the filtered data
        filtered_analogsignalarray = data.copy()
        filtered_analogsignalarray._ydata = ripple_data
        return filtered_analogsignalarray
    else:
        raise TypeError(
          "Unknown data type {} to filter.".format(str(type(data))))
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号