El_Chaderino

EEG Simulator Example Base

Aug 23rd, 2025 (edited)
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.70 KB | None | 0 0
  1. # mini_sim.py — 19ch toy EEG (alpha, beta, blink) + optional BrainVision export
  2. import numpy as np
  3.  
  4. CHS = ['Fp1','Fp2','F7','F3','Fz','F4','F8','T3','C3','Cz','C4','T4','T5','P3','Pz','P4','T6','O1','O2']
  5. fs, dur = 256, 20.0                                   # Hz, seconds
  6. t = np.arange(int(fs*dur)) / fs
  7. nch = len(CHS)
  8. data = np.zeros((nch, t.size))
  9.  
  10. def add_sine(ch_names, freq, amp):
  11.     sig = amp * np.sin(2*np.pi*freq*t) * (1 + 0.2*np.sin(2*np.pi*0.1*t))  # slow AM for realism
  12.     for name in ch_names:
  13.         i = CHS.index(name)
  14.         data[i] += sig
  15.  
  16. # rhythms
  17. add_sine(['O1','O2','P3','P4'], freq=10.0, amp=30.0)  # posterior alpha
  18. add_sine(['F3','F4','Fz'],       freq=18.0, amp=12.0) # frontal beta
  19. add_sine(['Cz'],                  freq=25.0, amp=10.0) # central 25 Hz
  20.  
  21. # noise
  22. rng = np.random.default_rng(0)
  23. data += rng.normal(0, 5.0, size=data.shape)           # ~5 µV noise
  24.  
  25. # one blink (~200 ms) frontopolar positive deflection
  26. blink_t0 = int(5.0*fs)
  27. blink_len = int(0.2*fs)
  28. blink = 150.0 * np.hanning(blink_len)                 # ~150 µV
  29. for ch in ['Fp1','Fp2']:
  30.     i = CHS.index(ch)
  31.     data[i, blink_t0:blink_t0+blink_len] += blink
  32.  
  33. # OPTIONAL: save as BrainVision if MNE is available
  34. if __name__ == "__main__":
  35.     try:
  36.         import mne
  37.         info = mne.create_info(CHS, sfreq=fs, ch_types='eeg')
  38.         raw = mne.io.RawArray(data*1e-6, info)        # µV → Volts
  39.         raw.set_montage('standard_1020', on_missing='ignore')
  40.         mne.export.export_raw('mini.vhdr', raw, fmt='brainvision')
  41.         print("Wrote BrainVision files: mini.vhdr/vmrk/eeg")
  42.     except Exception as e:
  43.         print("MNE not installed or export failed; skipping file export:", e)
  44.  
Advertisement
Add Comment
Please, Sign In to add comment