if lapl: x = x.dot(montage.make_laplacian_proj('EEG')) del df x *= 1e6 x_split = np.split(x[:x.shape[0] - x.shape[0] % 10], 10) X_split = [] Y_split = [] delay = 0 n_taps = 200 band = (8, 12) for x in x_split: X, Y = get_XY(x, band_hilbert(x[:, channels.index('P4')], fs, band), n_taps, delay) X_split.append(X) Y_split.append(Y) del x corrs = np.zeros((10 - 3 - 2) * 3) mses = np.zeros((10 - 3 - 2) * 3) corrs_one = np.zeros((10 - 3 - 2) * 3) mses_one = np.zeros((10 - 3 - 2) * 3) for k in range(5): X_train = np.concatenate(X_split[k:3 + k]) Y_train = np.concatenate(Y_split[k:3 + k]) X_val = X_split[k + 4]
nperseg=n_fft, return_onesided=False, noverlap=int(n_fft * 0.9)) mask = np.abs(spec).mean(1) w = np.diag(mask) for j, d in enumerate(delays): print('delay', d) # freq. domain ideal filter H = get_ideal_H(n_fft, fs, band, d) b_cfir_f = cLS(F, H, 0) b_cfir_wf = cLS(np.dot(w, F), np.dot(w, H), 0) X_train, Y_train = get_XY(x_train, y_train, n_taps, d) b_cfir_t = cLS(X_train, Y_train, 0) if d >= 0: tar = np.abs(y_test)[:-d] b_fir_band = firwin(d, band / fs * 2, pass_zero=False) b_fir_smooth = firwin(d, 2 / fs * 2) rec_fir = lfilter(b_fir_smooth, [1], np.abs(lfilter(b_fir_band, [1], x_test)))[d:] rec_cfir_f = np.abs(lfilter(b_cfir_f, [1], x_test))[d:] rec_cfir_wf = np.abs(lfilter(b_cfir_wf, [1], x_test))[d:] rec_cfir_t = np.abs(lfilter(b_cfir_t, [1], x_test))[d:] corrs_fir[k, j] = np.corrcoef(rec_fir, tar)[0, 1]
F = np.array([ np.exp(-2j * np.pi / n_fft * k * np.arange(n_taps)) for k in np.arange(n_fft) ]) fig, axes = plt.subplots(3, 2, sharex=True, sharey=True) for j, d in enumerate([200, 0, 100, -50, 50, 'rand']): if d != 'rand': #if d<999: #plt.close() H = get_ideal_H(n_fft, fs, band, d) b_cfir_f = cLS(F, H, 0) X_train, Y_train = get_XY(x[:fs * 60 * 2], band_hilbert(x[:fs * 60 * 2], fs, (8, 12)), n_taps, d) b_cfir_f = cLS(X_train, Y_train, 0) if d > 0: b_fir_band = firwin(d, band / fs * 2, pass_zero=False) b_fir_smooth = firwin(d, 2 / fs * 2) rec_fir = lfilter(b_fir_smooth, [1], np.abs(lfilter(b_fir_band, [1], x))) #plt.plot(b_cfir_f) #plt.show() #plt.plot(x) rec = np.abs(lfilter(b_cfir_f, [1], x)) else: rec = np.random.normal(size=len(x))
corrs_rls = np.zeros((len(indx) - 1, len(delays))) * np.nan min_phase_delays = np.zeros((len(indx) - 1, len(delays))) * np.nan corrs_min_phase = np.zeros((len(indx) - 1, len(delays))) * np.nan d = 0 for k, kk in enumerate(indx[2:]): print('****', k) x_train = x[indx[k - 1]:indx[k - 1] + int(step)] x_std = x_train.std() x_mean = x_train.mean() x_train = (x_train - x_mean) / x_std y_train = band_hilbert(x_train, 500, band) x_test = x[kk:kk + int(step)] y_test = band_hilbert(x_test, 500, band) X_train, Y_train = get_XY(x_train, y_train, n_taps, d) b_cfir_t = cLS(X_train, Y_train, 0) rec_cfir_t = np.abs(lfilter(b_cfir_t, [1], x_test))[d:] X_train, Y_train = get_XY(x_train, y_train, n_taps, 50) b_cfir_t = cLS(X_train, Y_train, 0) rec_cfir_t50 = np.abs(lfilter(b_cfir_t, [1], x_test)) X_train, Y_train = get_XY(x_train, y_train, n_taps, 100) b_cfir_t = cLS(X_train, Y_train, 0) rec_cfir_t100 = np.abs(lfilter(b_cfir_t, [1], x_test)) tar = np.abs(y_test)[:-d if d > 0 else None] plt.plot(np.arange(len(rec_cfir_t100)) / fs, rec_cfir_t100 / rec_cfir_t100.std(),