Ejemplo n.º 1
0
def biolowhigh(signal, sampling_rate, low, high, mode='includehigh'):
    if mode == 'includehigh':  #think i included this for testing purposes
        b, a = st.get_filter(ftype='butter',
                             band='highpass',
                             order=8,
                             frequency=low,
                             sampling_rate=sampling_rate)

        aux, _ = st._filter_signal(b,
                                   a,
                                   signal=signal,
                                   check_phase=True,
                                   axis=0)

    if mode == 'nohigh':
        aux = signal

    # low pass filter
    b, a = st.get_filter(ftype='butter',
                         band='lowpass',
                         order=16,
                         frequency=high,
                         sampling_rate=sampling_rate)

    filtered, _ = st._filter_signal(b, a, signal=aux, check_phase=True, axis=0)
    return filtered
Ejemplo n.º 2
0
    def butter_filter(self, signal, cutoff_frequency, framerate):
        """
        :param signal: .wav data
        :return: butter filtered data
        """

        if (cutoff_frequency == "" or cutoff_frequency == "0"):
            cutoff_frequency = 1000

        cutoff_frequency = int(cutoff_frequency)

        b, a = st.get_filter(ftype="butter",
                             order=2,
                             band="lowpass",
                             frequency=cutoff_frequency,
                             sampling_rate=framerate)

        filtered, zf = st._filter_signal(b, a, signal, check_phase=True)

        max_value = max(filtered)
        filtered = [i / max_value for i in filtered]

        return np.array(filtered)
Ejemplo n.º 3
0
def plot_filter(ftype='FIR',
                band='lowpass',
                order=None,
                frequency=None,
                sampling_rate=1000.,
                path=None,
                show=True,
                **kwargs):
    """Plot the frequency response of the filter specified with the given
    parameters.

    Parameters
    ----------
    ftype : str
        Filter type:
            * Finite Impulse Response filter ('FIR');
            * Butterworth filter ('butter');
            * Chebyshev filters ('cheby1', 'cheby2');
            * Elliptic filter ('ellip');
            * Bessel filter ('bessel').
    band : str
        Band type:
            * Low-pass filter ('lowpass');
            * High-pass filter ('highpass');
            * Band-pass filter ('bandpass');
            * Band-stop filter ('bandstop').
    order : int
        Order of the filter.
    frequency : int, float, list, array
        Cutoff frequencies; format depends on type of band:
            * 'lowpass' or 'bandpass': single frequency;
            * 'bandpass' or 'bandstop': pair of frequencies.
    sampling_rate : int, float, optional
        Sampling frequency (Hz).
    path : str, optional
        If provided, the plot will be saved to the specified file.
    show : bool, optional
        If True, show the plot immediately.
    ``**kwargs`` : dict, optional
        Additional keyword arguments are passed to the underlying
        scipy.signal function.

    """

    # get filter
    b, a = st.get_filter(ftype=ftype,
                         band=band,
                         order=order,
                         frequency=frequency,
                         sampling_rate=sampling_rate,
                         **kwargs)

    # plot
    fig = _plot_filter(b, a, sampling_rate)

    # make layout tight
    fig.tight_layout()

    # save to file
    if path is not None:
        path = utils.normpath(path)
        root, ext = os.path.splitext(path)
        ext = ext.lower()
        if ext not in ['png', 'jpg']:
            path = root + '.png'

        fig.savefig(path, dpi=200, bbox_inches='tight')

    # show
    if show:
        plt.show()
    else:
        # close
        plt.close(fig)
Ejemplo n.º 4
0
def plot_filter(ftype='FIR',
                band='lowpass',
                order=None,
                frequency=None,
                sampling_rate=1000.,
                path=None,
                show=True, **kwargs):
    """Plot the frequency response of the filter specified with the given
    parameters.

    Parameters
    ----------
    ftype : str
        Filter type:
            * Finite Impulse Response filter ('FIR');
            * Butterworth filter ('butter');
            * Chebyshev filters ('cheby1', 'cheby2');
            * Elliptic filter ('ellip');
            * Bessel filter ('bessel').
    band : str
        Band type:
            * Low-pass filter ('lowpass');
            * High-pass filter ('highpass');
            * Band-pass filter ('bandpass');
            * Band-stop filter ('bandstop').
    order : int
        Order of the filter.
    frequency : int, float, list, array
        Cutoff frequencies; format depends on type of band:
            * 'lowpass' or 'bandpass': single frequency;
            * 'bandpass' or 'bandstop': pair of frequencies.
    sampling_rate : int, float, optional
        Sampling frequency (Hz).
    path : str, optional
        If provided, the plot will be saved to the specified file.
    show : bool, optional
        If True, show the plot immediately.
    ``**kwargs`` : dict, optional
        Additional keyword arguments are passed to the underlying
        scipy.signal function.

    """

    # get filter
    b, a = st.get_filter(ftype=ftype,
                         band=band,
                         order=order,
                         frequency=frequency,
                         sampling_rate=sampling_rate, **kwargs)

    # plot
    fig = _plot_filter(b, a, sampling_rate)

    # make layout tight
    fig.tight_layout()

    # save to file
    if path is not None:
        path = utils.normpath(path)
        root, ext = os.path.splitext(path)
        ext = ext.lower()
        if ext not in ['png', 'jpg']:
            path = root + '.png'

        fig.savefig(path, dpi=200, bbox_inches='tight')

    # show
    if show:
        plt.show()
    else:
        # close
        plt.close(fig)
Ejemplo n.º 5
0
data_eeg = utl.load(filename)[:, 0:14]

######################################################
# calculates logarithm of the PSDs of the five bands #
# theta, slow_alpha, alpha, beta, gamma              #
# window size = 1.0s (128 samples)                   #
######################################################
signal = data_eeg
rate = 128.
window_size = 1.0
overlap = .5

# high pass filter
b, a = st.get_filter(ftype='butter',
                     band='highpass',
                     order=8,
                     frequency=2,
                     sampling_rate=rate)

aux, _ = st._filter_signal(b, a, signal=signal, check_phase=True, axis=0)

# low pass filter
b, a = st.get_filter(ftype='butter',
                     band='lowpass',
                     order=16,
                     frequency=47,
                     sampling_rate=rate)

filtered, _ = st._filter_signal(b, a, signal=aux, check_phase=True, axis=0)

plt.plot(signal[:, 0])