
HeadModelPath = [hlp_getSiftRoot filesep 'resources' filesep 'headmodels' filesep 'standard-Colin27-385ch.mat'];
hmObj = hlp_validateHeadModelObject(HeadModelPath);

%% Simulate VAR model
[EEGsim] = pop_sim_varmodel([]);
if length(EEGsim)>1
    EEGtrue = EEGsim(2);
    EEGsim  = EEGsim(1);
end

%% Simulate scalp activity using forward model

% these are the source locations
sourceRois = {'Cingulum_Mid_L','Occipital_Mid_L','Parietal_Sup_L','Frontal_Sup_Medial_R','Precentral_R'};

cfg = arg_guipanel('Function',@sim_simulateSources, ...
                   'Parameters',{ ...
                        'hmObj',HeadModelPath, ...
                        'sourceAtlasLabels', setdiff_bc(hmObj.atlas.label,{'Thalamus_L','Thalamus_R'}), ...
                        'Channels',hmObj.label, ... 
                        'sourceShape' {'gausspatch' 'roiOrdered' sourceRois, 'sigma', 10}, ...
                        'addNoise', {'SignalToNoise' 20} ...
                        },'PanelOnly',false);
[scalpData srcData LFM] = sim_simulateSources('sourceAmps',EEGsim.data,cfg);

%% 

chanlabels = hmObj.getChannelLabels();
elocs = hmObj.channelSpace;
EEG = EEGsim;
EEG.data       = scalpData;
EEG.nbchan     = size(EEG.data,1);
EEG.setname    = 'VAR Simulation';
EEG.condition  = 'VAR Simulation';
EEG.srcpot_all = srcData;
EEG = rmfield(EEG,'chanlocs');
for k=1:EEG.nbchan
    EEG.chanlocs(k) = struct(...
        'X',elocs(k,1), ...
        'Y',elocs(k,2), ...
        'Z',elocs(k,3), ...
        'labels',chanlabels{k}, ...
        'type','EEG', ...
        'theta',[], ...
        'radius',0.5, ...
        'sph_theta',[],...
        'sph_phi',[],...
        'sph_radius',[],...
        'urchan',k, ...
        'ref','');
end
EEG.chanlocs = convertlocs(EEG.chanlocs,'cart2all');

%% visualize
latency = [];
gobj=vis_csd('hmObj',HeadModelPath, ...
         'signal',EEG,  ...
         'cortexMesh',[], ...
         'times',latency,        ...
         'avgtimes',false,       ...
         'frameskip',10,          ...
         'cortexlims',99,       ...
         'scalplims',[99], ...
         'showpower',false, ...
         'title','True');
     
