fft_thresh = 2.0 fft_set_thresh = 0.0 #Constants fs = 250.0 #Frequency in Hz sample_time = dataset.shape[1]/fs #Total time for sample presample_time = predataset.shape[1]/fs #Total time for sample #To get alpha and beta waves print("Applying filters...") dataset.flags['WRITEABLE'] = True predataset.flags['WRITEABLE'] = True for i in range(0,dataset.shape[0]): for j in range(0,dataset.shape[2]): if enable_dc: predataset[i,:,j] = apply_dc_filter(predataset[i,:,j], fs, dc_lowcut, dc_highcut, dc_order, dc_type, dc_func_type) dataset[i,:,j] = apply_dc_filter(dataset[i,:,j], fs, dc_lowcut, dc_highcut, dc_order, dc_type, dc_func_type) if enable_dwt: predataset[i,:,j] = apply_dwt_filter(predataset[i,:,j], dwt_type, dwt_level, dwt_thresh_func, dwt_thresh_type) dataset[i,:,j] = apply_dwt_filter(dataset[i,:,j], dwt_type, dwt_level, dwt_thresh_func, dwt_thresh_type) if enable_fft: predataset[i,:,j] = apply_stfft_filter(predataset[i,:,j], fs, presample_time, fft_window, fft_step, fft_thresh, fft_set_thresh) dataset[i,:,j] = apply_stfft_filter(dataset[i,:,j], fs, sample_time, fft_window, fft_step, fft_thresh, fft_set_thresh) #Normalize dataset[i,:,j] = dataset[i,:,j]/np.linalg.norm(dataset[i,:,j]) #predataset[i,:,j] = predataset[i,:,j]/np.linalg.norm(predataset[i,:,j]) #np.mean(predataset[i,:,j]) dataset.shape = (dataset.shape[0], num_channels * max_samples)
data.shape = (max_rows, cols) min_thresh = -60000 max_thresh = -10000 #min_thresh = -38000 #max_thresh = -12000 for i in range(0,data.shape[1]): if max(data[:,i]) < max_thresh and min(data[:,i]) > min_thresh: electrodes[i] = True #DC Filter fs = 250 enable_dc = True dc_lowcut = 8.0 #Only get alpha and beta, most related to movement dc_highcut = 30.0 dc_order = 2 dc_type = "bandpass" dc_func_type = "butter" data.flags['WRITEABLE'] = True for i in range(0,data.shape[1]): if enable_dc: data[:,i] = apply_dc_filter(data[:,i], fs, dc_lowcut, dc_highcut, dc_order, dc_type, dc_func_type) #data = np.delete(data, range(data.shape[0] - 25, data.shape[0]), 0) #data = np.delete(data, range(0,25), 0) np.savetxt(path + "/../public/data/test.csv", data, delimiter=",",newline="\n") with open(path+"/testing/out.log", "w") as writestream: writestream.write(json.dumps({"electrodes": electrodes}))
#Constants fs = 250.0 #Frequency in Hz sample_time = dataset.shape[1] / fs #Total time for sample presample_time = predataset.shape[1] / fs #Total time for sample #To get alpha and beta waves print("Applying filters...") dataset.flags['WRITEABLE'] = True predataset.flags['WRITEABLE'] = True for i in range(0, dataset.shape[0]): for j in range(0, dataset.shape[2]): if enable_dc: predataset[i, :, j] = apply_dc_filter(predataset[i, :, j], fs, dc_lowcut, dc_highcut, dc_order, dc_type, dc_func_type) dataset[i, :, j] = apply_dc_filter(dataset[i, :, j], fs, dc_lowcut, dc_highcut, dc_order, dc_type, dc_func_type) if enable_dwt: predataset[i, :, j] = apply_dwt_filter(predataset[i, :, j], dwt_type, dwt_level, dwt_thresh_func, dwt_thresh_type) dataset[i, :, j] = apply_dwt_filter(dataset[i, :, j], dwt_type, dwt_level, dwt_thresh_func, dwt_thresh_type) if enable_fft: predataset[i, :, j] = apply_stfft_filter(predataset[i, :, j], fs,
with open(testingpath + "/" + sys.argv[1], "rb") as readstream: magic = _read32(readstream) rows = _read32(readstream) cols = _read32(readstream) buf = readstream.read(max_rows * cols * dt_f32.itemsize) data = np.frombuffer(buf, dtype=dt_f32) data.shape = (max_rows, cols) #DC Filter fs = 250 enable_dc = True dc_lowcut = 1.0 #Only get alpha and beta, most related to movement dc_highcut = 13.0 dc_order = 2 dc_type = "bandpass" dc_func_type = "butter" data.flags['WRITEABLE'] = True for i in range(0, data.shape[1]): if enable_dc: data[:, i] = apply_dc_filter(data[:, i], fs, dc_lowcut, dc_highcut, dc_order, dc_type, dc_func_type) data[:, i] = data[:, i] / np.linalg.norm(data[:, i]) data.shape = (1, max_rows * num_channels) #Read Model with open(path + '/predicting/eeg.model', 'rb') as readstream: ensemble = pickle.load(readstream) print(ensemble.predict(data)[0])