### Introduction to FieldTrip Source Modeling FieldTrip is an open-source MATLAB toolbox for MEG, EEG, and iEEG analysis. This cheatsheet focuses on common commands and workflows for source localization, allowing you to estimate the spatial origin of neural activity. ### 1. Data Preparation Before source modeling, basic preprocessing is essential. #### Loading Data ```matlab cfg = []; cfg.dataset = 'subject01.ds'; % Or your data file data = ft_preprocessing(cfg); ``` #### Artifact Rejection - **Visual Inspection:** `ft_databrowser` - **Automatic:** `ft_artifact_zvalue` (example for EOG) ```matlab cfg = []; cfg.artfctdef.eog.channel = 'EOG001'; cfg.artfctdef.eog.pretim = -0.1; cfg.artfctdef.eog.psttim = 0.1; cfg.artfctdef.eog.cutoff = 4; [cfg, artifact_eog] = ft_artifact_eog(cfg, data); ``` #### Trial Definition & Averaging ```matlab cfg = []; cfg.trl = [start_sample end_sample offset_sample]; % Define trials cfg.minlength = 0.5; % Minimum trial length in seconds data_trials = ft_redefinetrial(cfg, data); cfg = []; timelock = ft_timelockanalysis(cfg, data_trials); ``` ### 2. Head Model (Forward Solution) The head model describes how current in the brain propagates to the sensors. #### MRI Preprocessing - **Read MRI:** `mri = ft_read_mri('subject01.mri');` - **Segment MRI:** Identify tissue types (brain, skull, skin). ```matlab cfg = []; cfg.output = {'brain','skull','scalp'}; segmentedmri = ft_volumesegment(cfg, mri); ``` #### Create Mesh Generate a surface mesh from the segmented MRI. ```matlab cfg = []; cfg.method='iso2mesh'; % Or 'projectmesh', 'hexahedral' cfg.tissue = 'brain'; cfg.numvertices = 8000; % Number of vertices for the brain surface mesh = ft_prepare_mesh(cfg, segmentedmri); ``` #### Prepare Head Model Combine the meshes to create the head model. ```matlab cfg = []; cfg.method = 'bemcp'; % Boundary Element Method (BEM) % cfg.geom = mesh; % For single-shell % For multiple shells: cfg.geom(1).unit = 'mm'; cfg.geom(1).coordsys = 'ras'; cfg.geom(1).tri = mesh.tri; cfg.geom(1).pnt = mesh.pnt; headmodel = ft_prepare_headmodel(cfg, segmentedmri); ``` ### 3. Leadfield and Source Grids The leadfield matrix describes the sensitivity of each sensor to sources at different brain locations. #### Prepare Source Model Grid Define the locations in the brain where you want to estimate activity. ```matlab cfg = []; cfg.grad = timelock.grad; % Or 'elec' for EEG cfg.headmodel = headmodel; cfg.resolution = 10; % Grid resolution in mm cfg.unit = 'mm'; cfg.sourcemodel.nonlinear = 'yes'; % Warp grid to individual brain sourcemodel = ft_prepare_sourcemodel(cfg); ``` #### Compute Leadfield ```matlab cfg = []; cfg.grad = timelock.grad; cfg.headmodel = headmodel; cfg.sourcemodel = sourcemodel; leadfield = ft_prepare_leadfield(cfg); ``` ### 4. Source Analysis Estimate source activity using various methods. #### DICS (Dynamic Imaging of Coherent Sources) Good for oscillatory activity. ```matlab cfg = []; cfg.method = 'dics'; cfg.frequency = 10; % Frequency of interest (e.g., alpha band) cfg.refchan = 'MEG'; % Or other reference cfg.dics.lambda = '5%'; % Regularization parameter cfg.dics.keepfilter = 'yes'; cfg.dics.realfilter = 'yes'; source_dics = ft_sourceanalysis(cfg, timelock); ``` #### LCMV (Linearly Constrained Minimum Variance) Good for evoked responses. ```matlab cfg = []; cfg.method = 'lcmv'; cfg.lcmv.lambda = '5%'; cfg.lcmv.keepfilter = 'yes'; source_lcmv = ft_sourceanalysis(cfg, timelock); ``` #### MNE (Minimum Norm Estimate) Simple and fast, but can have depth bias. ```matlab cfg = []; cfg.method = 'mne'; cfg.mne.lambda = 1e-6; source_mne = ft_sourceanalysis(cfg, timelock); ``` ### 5. Visualization Display your source localization results. #### Plot on MRI ```matlab cfg = []; cfg.parameter = 'pow'; % For DICS, use 'pow' cfg.interplimits = [-1 1]; % Min/max for interpolation cfg.colormap = 'jet'; source_interp = ft_sourceinterpolate(cfg, source_dics, mri); cfg = []; cfg.method = 'ortho'; % Orthogonal slices cfg.funparameter = 'pow'; ft_sourceplot(cfg, source_interp); ``` #### Plot on Brain Surface ```matlab cfg = []; cfg.method = 'surface'; cfg.funparameter = 'pow'; cfg.surffile = 'surface_mesh.mat'; % Your brain surface mesh ft_sourceplot(cfg, source_dics); ``` #### Plot Time-Resolved Sources If your source analysis has a time dimension. ```matlab cfg = []; cfg.method = 'slice'; cfg.funparameter = 'avg'; % For LCMV, use 'avg' cfg.latency = 0.1; % Time point to display ft_sourceplot(cfg, source_lcmv); ``` ### Common Issues & Troubleshooting - **Coordinate Systems:** Ensure all components (MRI, headmodel, sensors) are in the same coordinate system (`ft_volumerealign`). - **Memory Errors:** Source analysis can be memory intensive. Reduce grid resolution or process data in smaller chunks. - **Empty Results:** Check for NaNs in your leadfield or source estimates. This often points to issues with coregistration or head model creation. - **FieldTrip Forum:** A great resource for specific problems and examples.