Example #1
0
 def test_filtfilt_copy(self):
     """filtfilt must not modify argument."""
     cpy = self.dat.copy()
     fn = self.dat.fs / 2
     b, a = butter(4, [6 / fn, 8 / fn], btype='band')
     filtfilt(self.dat, b, a)
     self.assertEqual(cpy, self.dat)
Example #2
0
 def test_filtfilt_swapaxes(self):
     """filtfilt must work with nonstandard timeaxis."""
     fn = self.dat.fs / 2
     b, a = butter(4, [6 / fn, 8 / fn], btype='band')
     dat = filtfilt(swapaxes(self.dat, 0, 1), b, a, timeaxis=1)
     dat = swapaxes(dat, 0, 1)
     dat2 = filtfilt(self.dat, b, a)
     self.assertEqual(dat, dat2)
Example #3
0
def preprocess(data, filt=None):
    # copying data
    dat = data.copy()
    fs_n = dat.fs / 2.0

    # butter filtering low
    b, a = proc.signal.butter(5, [13 / fs_n], btype='low')
    #dat = proc.filtfilt(dat, b, a)

    # butter filtering high
    b, a = proc.signal.butter(4, [9 / fs_n], btype='high')
    dat = proc.filtfilt(dat, b, a)

    # subsampling
    #dat = proc.subsample(dat, 50)

    if filt is None:
        # calculate_csp
        filt, pattern, _ = proc.calculate_csp(dat)

        # plot_csp_pattern
        #plot_csp_pattern(pattern)

    # apply_csp
    dat = proc.apply_csp(dat, filt)

    # variance and logarithm
    dat = proc.variance(dat)
    #dat = proc.logarithm(dat)
    return dat, filt
def offline_experiment(filename_, cfy_, true_labels_):
    print("\n")
    cnt = io.load_bcicomp3_ds2(filename_)

    fs_n = cnt.fs / 2

    b, a = proc.signal.butter(5, [HIGH_CUT / fs_n], btype='low')
    cnt = proc.filtfilt(cnt, b, a)

    b, a = proc.signal.butter(5, [LOWER_CUT / fs_n], btype='high')
    cnt = proc.filtfilt(cnt, b, a)

    cnt = proc.subsample(cnt, SUBSAMPLING)

    epo = proc.segment_dat(cnt, MARKER_DEF_TEST, SEG_IVAL)

    fv = proc.jumping_means(epo, JUMPING_MEANS_INTERVALS)
    fv = proc.create_feature_vectors(fv)

    lda_out = proc.lda_apply(fv, cfy_)
    markers = [fv.class_names[cls_idx] for cls_idx in fv.axes[0]]
    result = zip(markers, lda_out)
    endresult = []
    markers_processed = 0
    letter_prob = {i: 0 for i in 'abcdefghijklmnopqrstuvwxyz123456789_'}
    for s, score in result:
        if markers_processed == 180:
            endresult.append(
                sorted(letter_prob.items(), key=lambda x: x[1])[-1][0])
            letter_prob = {
                i: 0
                for i in 'abcdefghijklmnopqrstuvwxyz123456789_'
            }
            markers_processed = 0
        for letter in s:
            letter_prob[letter] += score
        markers_processed += 1

    print('Letras Encontradas-: %s' % "".join(endresult))
    print('Letras Corretas----: %s' % true_labels_)
    acc = np.count_nonzero(
        np.array(endresult) == np.array(
            list(true_labels_.lower()[:len(endresult)]))) / len(endresult)
    print("Acertividade Final : %d" % (acc * 100))
Example #5
0
def highpass_filt_filt_cnt(cnt, low_cut_off_hz, filt_order=3):
    if (low_cut_off_hz is None) or (low_cut_off_hz == 0):
        log.info("Not doing any highpass, since low 0 or None")
        return cnt.copy()
    b, a = scipy.signal.butter(filt_order,
                               low_cut_off_hz / (cnt.fs / 2.0),
                               btype='highpass')
    assert filter_is_stable(a)
    cnt_highpassed = filtfilt(cnt, b, a)
    return cnt_highpassed
def preprocessing_simple(dat, MRK_DEF, *args, **kwargs):
    """Simple preprocessing that reaches 97% accuracy.
    """
    fs_n = dat.fs / 2
    b, a = proc.signal.butter(5, [10 / fs_n], btype='low')
    dat = proc.filtfilt(dat, b, a)
   
    dat = proc.subsample(dat, 20)
    epo = proc.segment_dat(dat, MRK_DEF, SEG_IVAL)
    fv = proc.create_feature_vectors(epo)
    return fv, epo
Example #7
0
def lowpass_filt_filt_cnt(cnt, high_cut_off_hz, filt_order=3):
    if (high_cut_off_hz is None) or (high_cut_off_hz == cnt.fs):
        log.info(
            "Not doing any lowpass, since ince high cut hz is None or current fs"
        )
    b, a = scipy.signal.butter(filt_order,
                               high_cut_off_hz / (cnt.fs / 2.0),
                               btype='lowpass')
    assert filter_is_stable(a)
    cnt_lowpassed = filtfilt(cnt, b, a)
    return cnt_lowpassed
Example #8
0
def preprocessing_simple(dat, MRK_DEF, *args, **kwargs):
    """Simple preprocessing that reaches 97% accuracy.
    """
    fs_n = dat.fs / 2
    b, a = proc.signal.butter(5, [10 / fs_n], btype='low')
    dat = proc.filtfilt(dat, b, a)

    dat = proc.subsample(dat, 20)
    epo = proc.segment_dat(dat, MRK_DEF, SEG_IVAL)
    fv = proc.create_feature_vectors(epo)
    return fv, epo
def preprocess(data, filt=None):
    dat = data.copy()
    fs_n = 250 # sample rate is 250 for us

    b, a = proc.signal.butter(5, [13 / fs_n], btype='low')
    dat = proc.filtfilt(dat, b, a)

    b, a = proc.signal.butter(5, [9 / fs_n], btype='high')
    dat = proc.filtfilt(dat, b, a)

    dat = proc.subsample(dat, 50)

    if filt is None:
        filt, pattern, _ = proc.calculate_csp(dat)
        plot_csp_pattern(pattern)
    dat = proc.apply_csp(dat, filt)

    dat = proc.variance(dat)
    dat = proc.logarithm(dat)
    return dat, filt
def preprocess(data, filt=None):
    dat = data.copy()
    fs_n = dat.fs / 2

    b, a = proc.signal.butter(5, [13 / fs_n], btype='low')
    dat = proc.filtfilt(dat, b, a)


    b, a = proc.signal.butter(5, [9 / fs_n], btype='high')
    dat = proc.filtfilt(dat, b, a)
    
    dat = proc.subsample(dat, 50)

    if filt is None:
        filt, pattern, _ = proc.calculate_csp(dat)
        plot_csp_pattern(pattern)
    dat = proc.apply_csp(dat, filt)
    
    dat = proc.variance(dat)
    dat = proc.logarithm(dat)
    return dat, filt
Example #11
0
 def test_bandpass(self):
     """Band pass filtering."""
     # bandpass around the middle frequency
     fn = self.dat.fs / 2
     b, a = butter(4, [6 / fn, 8 / fn], btype='band')
     ans = filtfilt(self.dat, b, a)
     # check if the desired band is not damped
     dat = spectrum(ans)
     mask = dat.axes[0] == 7
     self.assertTrue((dat.data[mask] > 6.5).all())
     # check if the outer freqs are damped close to zero
     mask = (dat.axes[0] <= 6) & (dat.axes[0] > 8)
     self.assertTrue((dat.data[mask] < .5).all())
Example #12
0
def bandpass_filt_filt_cnt(cnt, low_cut_hz, high_cut_hz, filt_order=3):
    """Bandpass cnt signal using butterworth filter.
    Uses lowpass in case low cut hz is exactly zero."""
    if (low_cut_hz == 0 or low_cut_hz == None) and (high_cut_hz == None
                                                    or high_cut_hz == cnt.fs):
        log.info("Not doing any bandpass, since low 0 or None and "
                 "high None or current fs")
        return cnt.copy()
    if low_cut_hz == 0 or low_cut_hz == None:
        log.info("Using lowpass filter since low cut hz is 0 or None")
        return lowpass_filt_filt_cnt(cnt, high_cut_hz, filt_order=filt_order)
    if high_cut_hz == None or high_cut_hz == cnt.fs:
        log.info(
            "Using highpass filter since high cut hz is None or current fs")
        return highpass_filt_filt_cnt(cnt, low_cut_hz, filt_order=filt_order)

    nyq_freq = 0.5 * cnt.fs
    low = low_cut_hz / nyq_freq
    high = high_cut_hz / nyq_freq
    b, a = scipy.signal.butter(filt_order, [low, high], btype='bandpass')
    assert filter_is_stable(a), "Filter should be stable..."
    cnt_bandpassed = filtfilt(cnt, b, a)
    return cnt_bandpassed
Example #13
0
def highpass_filt_filt_cnt(cnt, low_cut_off_hz, filt_order=3):
    b,a = scipy.signal.butter(filt_order, low_cut_off_hz/(cnt.fs/2.0),btype='highpass')
    assert filter_is_stable(a)
    cnt_highpassed = filtfilt(cnt,b,a)
    return cnt_highpassed