# Generating Simulated EEG Signals and Data

Using matlab and EEGLAB, I generated my own simulated EEG data, according to the classical theory of **event related potentials (ERP)**. ERP are** **small voltages generated in the brain in response to specific events or stimuli.

The theory states that peaks in ERP waveforms reflect phasic bursts of activity in one or more brain regions that are triggered by experimental events of interest. It is assumed that an ERP-like waveform is evoked by each event, but that on any given trial this ERP “signal” is buried in ongoing EEG “noise”.

## Generating a single trial of EEG

The simulated data is generated by adding signal and noise components. These two components are generated by two functions: **peak and noise.**

Noise is generated so that its power spectrum matches the power spectrum of human EEG. The function noise has** 3 parameters**:

- 1st describing the length of a single trial of the signal by the number of samples
- 2nd describing the number of trials
- 3rd describing sampling frequency

To get the details of the parameters of function noise, I typed:

**help noise**

And to generate one trial of 0.8s of noise with sampling frequency 250Hz, I typed:

**mynoise = noise (200, 1, 250);**

The value of the first parameter was calculated by multiplying the duration of the noise by the sampling frequency (i.e. 0.8 * 250 = 200). The function generates a vector containing the samples. Then I typed and ran:

**plot (mynoise);**

and this is the visual that was created:

Function peak** **has a very similar format, but it has additional parameters, including a 4th parameter describing frequency of the peak and a 5th describing position of the centre of the peak.

To generate a peak with frequency 5Hz and centre in 115th sample, I typed:

**mypeak = peak (200, 1, 250, 5, 115); plot (mypeak);**

and when I ran the code, this is the visual that was created:

Once we have both signal and noise, we can combine them. To make the peak negative, we can multiply it by -1 before the addition or scale the amplitudes of noise and signal by multiplying the vectors representing them before addition. Here is what I plotted:

**mysignal = -5 * mypeak + 3 * mynoise; plot (mysignal);**

Comparing this visual versus the visual showing pure noise, we can see that they have a difference of around 110–120 sample due to superposition of the negative peak.

# Generating complete EEG data

Function simulatedEEG generates the complete set of data in the paper “Detection of synchronized oscillations in the electroencephalogram: An evaluation of methods.”

To generate multiple trials of signal, the number of trials need to be specified in the second parameter of functions peak and noise. The resulting data structure will be a vector with sequenced signals.

When generating multiple trials, a 6th parameter may be specified in function peak describing the temporal jitter of the peak across the trials.

We can generate data from multiple electrodes by generating for each electrode separately and then constructing a matrix with a number of rows equal to the number of electrodes, in which each row corresponds to the signal from one electrode.

Peaks have different amplitudes in different electrodes, therefore they should be scaled by the co-efficients from a dipole model.

To generate sample complete set of data, I typed and ran:

**mydata = simulatedEEG; **and** eeglab, **which allowed the program to generate noise for each of the channels up to 31 and open EEGLAB.

# Plotting

To load the data to EEGLAB, I pressed “File,” “Import data,” and “From ASCII/float file or Matlab array”.

Then in the window I typed mydata in “Matlab Variable,” 200 in “Time points per epoch,” and 250 in “Data sampling rate,” according to the information in simulatedEEG.

For “Channel location file” click the corresponding locations of electrodes. (i.e. stored in file “nickloc31.locs”). After setting a name (“plotdata”), this was the dataset that was created:

Then I pressed “plot,” “channel data,” and this was the final visual that was created showing my simulated EEG signals:

Credit:

The functions were used to generate data analysed in the following papers:

- Nick Yeung, Rafal Bogacz, Clay B. Holroyd and Jonathan D. Cohen. (2004) Detection of synchronized oscillations in the electroencephalogram: An evaluation of methods.Psychophysiology, 41: 822–832.
- Nick Yeung, Rafal Bogacz, Clay B. Holroyd, Sander Nieuwenhuis and Jonathan D. Cohen. (2007) Theta phase-resetting and the error-related negativity, Psychophysiology, 44: 39–49.

“Simulated EEG data generator” by Yeung N, Bogacz R, Holroyd C, Nieuwenhuis S, Cohen J is licensed under Attribution-ShareAlike 4.0 International(CC BY-SA 4.0). This work was modified to fit my experiment.

**Thank you for reading! If you enjoyed this article, be sure to share it with your friends and family and/or leave a 👏! Feel free to also connect with me on ****LinkedIn****!**