def correct_ttl(x): """mne expects 8bit channel. biosemi has 24 bit. go down to 16 and adjust >>> correct_ttl(np.array([16128],dtype='int64')) # first stim ch val np.array([0],dtype='int16') """ return x.astype(np.int32) - 127**2 + 1 v = x - 16128 # np.log2(16128+256)==14 v[v == 65536] = 0 # 65536==2**16 return v def add_stim_corrected(raw): """ >>> raw = mne.io.read_raw_bdf('/Volumes/Hera/Raw/EEG/SPA/12192_20251024/12192_20251024_rest.bdf') >>> add_stim_corrected(raw) """ raw.load_data() stim_raw = raw.pick_channels(["Status"]).get_data() info = mne.create_info(["StatusCorrected"], raw.info["sfreq"], ["stim"]) stim_vals = correct_ttl(stim_raw[0]).reshape(stim_raw.shape) stim = mne.io.RawArray(stim_vals, info) raw.add_channels([stim], force_update_info=True)