Ejemplo n.º 1
0
    def test_cosines(self):
        """Test if cosines with defined frequencies are passed or attenuated"""
        """Parameters entered for test here: """
        dc_level = .1

        # passband freq should be chosen w.r.t. filter specification, changes
        # for filter to filter, defined in eegProcessor.py
        fcos0 = 2
        fcos1 = 10
        fcos2 = 20
        fcos3 = 30
        fcos4 = 40

        fs = 256
        # fs = 300
        # fs = 1024
        duration = 5  # in seconds for test duration
        '''End of parameters'''

        t = np.arange(0, duration - 1. / fs, 1. / fs)  # time vector

        # Cosines with different frequencies
        x0 = np.cos(2 * np.pi * fcos0 * t)
        x1 = np.cos(2 * np.pi * fcos1 * t)
        x2 = np.cos(2 * np.pi * fcos2 * t)
        x3 = np.cos(2 * np.pi * fcos3 * t)
        x4 = np.cos(2 * np.pi * fcos4 * t)

        x6 = dc_level
        x7 = np.cos(2 * np.pi * 1 * t)
        x8 = np.cos(2 * np.pi * 48 * t)
        x9 = np.cos(2 * np.pi * 70 * t)
        x10 = np.cos(2 * np.pi * 100 * t)

        xpassband = x0 + x1 + x2 + x3 + x4
        xstopband = x6 + x7 + x8 + x9 + x10

        # Two channels
        x = np.zeros((2, xpassband.size))
        x[0][:] = xpassband + xstopband
        x[1][:] = xpassband + xstopband

        y = bandpass.text_filter(x, fs=fs, k=1)

        MSE_perSample = np.sum(
            (xpassband - y[0][:xpassband.size])**2.) / xpassband.size
        MSE_perSample_norm = MSE_perSample / np.sum(x[0][:]**2)

        self.assertTrue(MSE_perSample_norm * 100 < 10)
Ejemplo n.º 2
0
def _demo_validate_real_data():
    ds_rate = 2
    channel_map = [1] * 16 + [0, 0, 1, 1, 0, 1, 1, 1, 0]
    data_train_folder = load_experimental_data()

    mode = 'calibration'

    raw_dat, stamp_time, channels, type_amp, fs = read_data_csv(
        data_train_folder + '/rawdata.csv')

    dat = bandpass.text_filter(raw_dat, fs=fs, k=ds_rate)

    # Get data and labels
    s_i, t_t_i, t_i = trigger_decoder(mode=mode,
                                      trigger_loc=data_train_folder +
                                      '/triggers.txt')
    x_train, y_train, num_seq, _ = trial_reshaper(t_t_i,
                                                  t_i,
                                                  dat,
                                                  mode=mode,
                                                  fs=fs,
                                                  k=ds_rate,
                                                  channel_map=channel_map)

    model = train_pca_rda_kde_model(x_train, y_train, k_folds=10)

    fig = plt.figure()
    ax = fig.add_subplot(211)
    x_plot = np.linspace(np.min(model.line_el[-1]), np.max(model.line_el[-1]),
                         1000)[:, np.newaxis]
    ax.plot(model.line_el[2][y_train == 0],
            -0.005 -
            0.01 * np.random.random(model.line_el[2][y_train == 0].shape[0]),
            'ro',
            label='class(-)')
    ax.plot(model.line_el[2][y_train == 1],
            -0.005 -
            0.01 * np.random.random(model.line_el[2][y_train == 1].shape[0]),
            'go',
            label='class(+)')
    for idx in range(len(model.pipeline[2].list_den_est)):
        log_dens = model.pipeline[2].list_den_est[idx].score_samples(x_plot)
        ax.plot(x_plot[:, 0],
                np.exp(log_dens),
                'r-' * (idx == 0) + 'g-' * (idx == 1),
                linewidth=2.0)

    ax.legend(loc='upper right')
    plt.title('Training Data')
    plt.ylabel('p(e|l)')
    plt.xlabel('scores')

    # Test
    data_test_folder = load_experimental_data()

    mode = 'calibration'

    raw_dat, stamp_time, channels, type_amp, fs = read_data_csv(
        data_test_folder + '/rawdata.csv')
    dat = bandpass.text_filter(raw_dat, fs=fs, k=ds_rate)

    # Get data and labels
    s_i, t_t_i, t_i = trigger_decoder(mode=mode,
                                      trigger_loc=data_test_folder +
                                      '/triggers.txt')
    x_test, y_test, num_seq, _ = trial_reshaper(t_t_i,
                                                t_i,
                                                dat,
                                                mode=mode,
                                                fs=fs,
                                                k=ds_rate,
                                                channel_map=channel_map)

    model.transform(x_test)

    ax.plot(model.line_el[2][y_test == 0],
            -0.01 -
            0.01 * np.random.random(model.line_el[2][y_test == 0].shape[0]),
            'bo',
            label='t_class(-)')
    ax.plot(model.line_el[2][y_test == 1],
            -0.01 -
            0.01 * np.random.random(model.line_el[2][y_test == 1].shape[0]),
            'ko',
            label='t_class(+)')

    bandwidth = 1.06 * min(np.std(model.line_el[2]),
                           iqr(model.line_el[2]) / 1.34) * np.power(
                               model.line_el[2].shape[0], -0.2)
    test_kde = KernelDensityEstimate(bandwidth=bandwidth)
    test_kde.fit(model.line_el[2], y_test)

    for idx in range(len(model.pipeline[2].list_den_est)):
        log_dens = test_kde.list_den_est[idx].score_samples(x_plot)
        ax.plot(x_plot[:, 0],
                np.exp(log_dens),
                'b--' * (idx == 0) + 'k--' * (idx == 1),
                linewidth=2.0)

    ax.legend(loc='upper right')
    plt.title('Training Data')
    plt.ylabel('p(e|l)')
    plt.xlabel('scores')

    plt.show()
Ejemplo n.º 3
0
x6 = dcLevel
x7 = np.cos(2 * np.pi * 1 * t)
x8 = np.cos(2 * np.pi * 48 * t)
x9 = np.cos(2 * np.pi * 70 * t)
x10 = np.cos(2 * np.pi * 100 * t)

xpassband = x0 + x1 + x2 + x3 + x4
xstopband = x6 + x7 + x8 + x9 + x10

# Two channels
x = np.zeros((2, xpassband.size))
x[0][:] = xpassband + xstopband
x[1][:] = xpassband + xstopband

y = bandpass.text_filter(x, fs=fs, k=1)

MSE_perSample = np.sum(
    (xpassband - y[0][:xpassband.size])**2.) / xpassband.size
MSE_perSample_norm = MSE_perSample / np.sum(x[0][:]**2)

print('MSE per sample: {} -> {}%'.format(MSE_perSample,
                                         MSE_perSample / 5. * 100))
print('MSE normalized: {}'.format(MSE_perSample_norm * 100))

plt.figure(1)
plt.plot(t, xpassband, t, y[0][:xpassband.size])
plt.title('Sum of Pass-band Cosines')
plt.legend(('Pass-band signal', 'Filtered Signal'))
plt.show()