Noise in simulation

Hi all

I am simulating signals in BST based in scouts.

I’m doing the following with the process menu:

1-signal simulating generic
2-simulate EEG based on scouts

Questions:

1- I have had good results, but now I would add my EEG background noise (normal, blanco.etc).

2- How I can do this?

3- What then do with the noise covariance matrix?

4-It is valid to export the EEG tracing matlab and then add noise here?. If this is possible.

What I can do then to calculate the covariance matrix of noise?

Regards

You can do the following:

  1. Export you simulated EEG file to your workspace (right-click > File > Export to Matlab),
  2. Add the type of noise you want to the .F field
  3. Import the file structure back to your database (right-click on the folder to create a new file if you don’t want to erase the original one)
  4. Compute the noise covariance from this simulated noisy file
  5. Compute again the sources

Thanks for you reply Francois
However i have many questions

  1. If I import my raw data, as you say me. What happen with the file Simulated signal (1 x 100) (with the cerebrum icon)? I have to change this?

  2. I need add the noise in a code that i generated with the process pipeline editor, thus i need modify this code. How i can access to the info (Simulated signals) in the code, and not through the BST interface?

  3. Finally i need modify the file (simulated signal) inside the DB of BST, from outside the DB, in this case my own code. How i can generate this?

Note:

I have this:


sFiles = bst_process('CallProcess', 'process_simulate_matrix', ...
    sFiles, [], ...
    'subjectname', SubjectNames{1}, ...
    'condition', num2str(i), ...
    'samples', 100, ...
    'srate', 100, ...
    'matlab', 'Data(1,:) = 4*sin(1*pi*t)');

% Process: Simulate recordings from scouts
sFiles = bst_process('CallProcess', 'process_simulate_recordings', ...
    sFiles, [], ...
    'scouts', {'Mindboggle_4yMRN', {num2str(i)}}, ...                     
    'savesources', 1);

%% in this point I need add the noise and reload the file Simulated signal


% Process: Compute sources
sFiles = bst_process('CallProcess', 'process_inverse', ...
    sFiles, [], ...
    'comment', '', ...
    'method', 1, ...  % Minimum norm estimates (wMNE)
    'wmne', struct(...
         'NoiseCov', [], ...
         'InverseMethod', 'wmne', ...
         'ChannelTypes', {{}}, ...
         'SNR', 3, ...
         'diagnoise', 0, ...
         'SourceOrient', {{'fixed'}}, ...
         'loose', 0.2, ...
         'depth', 1, ...
         'weightexp', 0.5, ...
         'weightlimit', 10, ...
         'regnoise', 1, ...
         'magreg', 0.1, ...
         'gradreg', 0.1, ...
         'eegreg', 0.1, ...
         'ecogreg', 0.1, ...
         'seegreg', 0.1, ...
         'fMRI', [], ...
         'fMRIthresh', [], ...
         'fMRIoff', 0.1, ...
         'pca', 1), ...
    'sensortypes', 'MEG, MEG MAG, MEG GRAD, EEG', ...
    'output', 3);  % Full results: one per file

Finally attached a picture of what I have in the DB

Thanks for all Francois

Hello,

You can skip the saving of the simulated sources if you don’t need it.
Except for that, your script could look like this:

sFileScout = bst_process('CallProcess', 'process_simulate_matrix', ...
[], [], ...
'subjectname', SubjectNames{1}, ...
'condition', '1', ...
'samples', 100, ...
'srate', 100, ...
'matlab', 'Data(1,:) = 4*sin(1*pi*t);');

% Process: Simulate recordings from scouts
sFileEeg = bst_process('CallProcess', 'process_simulate_recordings', ...
sFileScout, [], ...
'scouts', {'User scouts', {'1'}}, ...
'savesources', 0);   % DO NOT SAVE THE SIMULATED SOURCES

% ==== ADD NOISE TO THE EEG ====
% Load the simulated EEG recordings
sMatEeg = in_bst_data(sFileEeg.FileName);
% Add the noise matrix you want
noise = 0.4 * std(sMatEeg.F(:)) * (rand(size(sMatEeg.F)) - 0.5);
sMatEeg.F = sMatEeg.F + noise;
% Save the file back
bst_save(file_fullpath(sFileEeg.FileName), sMatEeg);

% ==== CREATE A NOISE FILE ====
% Create a new file structure 
sMatNoise = sMatEeg;
sMatNoise.F = noise;
sMatNoise.Comment = 'Noise recordings';
% Save the file
NoiseFilename = bst_fullfile(bst_fileparts(file_fullpath(sFileEeg.FileName)), 'data_noise.mat');
NoiseFilename = file_unique(NoiseFilename);
bst_save(NoiseFilename, sMatNoise);
% Register file to the database
db_add_data(sFileEeg.iStudy, NoiseFilename, sMatNoise);
panel_protocols('UpdateNode', 'Study', sFileEeg.iStudy);

% Process: Compute noise covariance
bst_process('CallProcess', 'process_noisecov', ...
    NoiseFilename, [], ...
    'baseline', [0, 0.99], ...
    'target', 1, ...
    'dcoffset', 1, ...  % Block by block, to avoid effects of slow shifts in data
    'method', 1, ...  % Full noise covariance matrix
    'copycond', 0, ...
    'copysubj', 0);

% Process: Compute sources
sFileSources = bst_process('CallProcess', 'process_inverse', ...
sFileEeg, [], ...
'comment', '', ...
'method', 1, ... % Minimum norm estimates (wMNE)
'wmne', struct(...
'NoiseCov', [], ...
'InverseMethod', 'wmne', ...

Hi!!

I have worked with the code that you gave me for simulate EEG noise. However, I have a basics doubt about the kind of noise that we are simulating in the code. I have read about the different origin of the noise in EEG recorded, and they can be from differents sources. In this case we are simulating noise adding this to our simulated EEG signal (EEG+noise). So, Can i assume some specific origin of this EEG noise?.

Other question is referred to. How can i control in this situation the SNR?. There is some way how can i give a measure of this ratio?

Regards

Hello,

The example I gave you was not supposed to generate realistic noise, it was answering your question on how to add some user defined noise to a file.
And it was in the first place supposed to mimic “sensor noise” due to the acquisition setup, not “brain noise”. So no, you can’t expect a spatial origin for this noise, and the goal is to get rid of it in the source modeling.
I’m sorry, I don’t have any model available yet for simulate realistic EEG noise.

You can set your noise function to achieve the desired SNR.
SNR = var(data)/var(noise) = sum((bsxfun(@minus, Fdata, mean(Fdata,2)).^2, 2) ./ sum((bsxfun(@minus, Fnoise, mean(Fnoise,2)).^2, 2)
(approximately: there might be typos in there)

I will send you some experiments in a separate email.

Cheers,
Francois

FYI: To address all the recent questions we had related with simulations, I added some code in Brainstorm and wrote a new tutorial:
https://neuroimage.usc.edu/brainstorm/Tutorials/Simulations