------ = Documentation: Some Practical Examples of how to use the BBCI Toolbox = ------ '''IN CONSTRUCTION''' ---- === Table of Contents === * ERP Analysis: [[#ERP-Analysis]] * Event-Related Spectral Analysis: [[#Spectral-Analysis]] * Long-Term Spectral Analysis: [[#Spectral-Analysis-of-Blocks]] * ERD/ERS Analysis: [[#ERD-Analysis]] * === ERP Analysis === <> {{{#!highlight matlab file= 'VPibv_10_11_02/CenterSpellerMVEP_VPibv'; [cnt, mrk, mnt]= eegfile_loadMatlab(file); % Define some settings disp_ival= [-200 1000]; ref_ival= [-200 0]; crit_maxmin= 70; crit_ival= [100 800]; crit_clab= {'F9,z,10','AF3,4'}; clab= {'Cz','PO7'}; colOrder= [1 0 1; 0.4 0.4 0.4]; % Apply highpass filter to reduce drifts b= procutil_firlsFilter(0.5, cnt.fs); cnt= proc_filtfilt(cnt, b); % Artifact rejection based on variance criterion mrk= reject_varEventsAndChannels(cnt, mrk, disp_ival, 'verbose', 1); % Segmentation epo= cntToEpo(cnt, mrk, disp_ival); % Artifact rejection based on maxmin difference criterion on frontal chans epo= proc_rejectArtifactsMaxMin(epo, crit_maxmin, ... 'clab',crit_clab, 'ival',crit_ival, 'verbose',1); % Baseline subtraction, and calculation of a measure of discriminability epo= proc_baseline(epo, ref_ival); epo_r= proc_r_square_signed(epo); % Select some discriminative intervals, with constraints to find N2, P2, P3 like components. fig_set(1); constraint= ... {{-1, [100 300], {'I#','O#','PO7,8','P9,10'}, [50 300]}, ... {1, [200 350], {'P3-4','CP3-4','C3-4'}, [200 400]}, ... {1, [400 500], {'P3-4','CP3-4','C3-4'}, [350 600]}}; [ival_scalps, nfo]= ... select_time_intervals(epo_r, 'visualize', 1, 'visu_scalps', 1, ... 'title', untex(file), ... 'clab',{'not','E*'}, ... 'constraint', constraint); printFigure('r_matrix', [18 13]); ival_scalps= visutil_correctIvalsForDisplay(ival_scalps, 'fs',epo.fs); fig_set(3) H= grid_plot(epo, mnt, defopt_erps, 'colorOrder',colOrder); grid_addBars(epo_r, 'h_scale',H.scale); printFigure(['erp'], [19 12]); fig_set(2); H= scalpEvolutionPlusChannel(epo, mnt, clab, ival_scalps, defopt_scalp_erp2, ... 'colorOrder',colOrder); grid_addBars(epo_r); printFigure(['erp_topo'], [20 4+5*size(epo.y,1)]); fig_set(4, 'shrink',[1 2/3]); scalpEvolutionPlusChannel(epo_r, mnt, clab, ival_scalps, defopt_scalp_r2); printFigure(['erp_topo_r'], [20 9]); }}} Even in this very basic ERP analysis, there are several steps, in which the choice of processing can have quite a big impact on the results, but nevertheless, the correct choice is not clear to us (maybe we will find out a recommendable choice at some point). 1. Highpass filter (lines 14-15). Highpass filtering may be beneficial to reduce the impact of drifts. But the choice of the filter has quite some impact on the ERPs, in particular, the later ERPs. Alternatives are `proc_subtractMovingAverage(cnt, 1500, 'centered', 'sinus')`, other highpass filters, or no highpass filtering at all. Also bandpass filtering is an alternative (e.g. [0.5 30]), but typically the lowpass filtering is not required since high frequency are dampened also by the normal ERP averaging across trials. 1. Baseline correction (line 28). For the ERP themselves the choice of baseline correction does not matter. They stay the same. But for the measure for discriminability (e.g. r^2-values) it may have a big impact. In this example, baseline correction is performed on a trialwise basis. This way it is done for online experiments. For the ERP analysis this may be have the disturbing consequency, that the r^2-values in/near the baseline interval can become spuriously high, since the trial-to-trial variance is artificially reduced. Alternatives are to subtract the across-trials average of the baseline (option `'trialwise',0` in `proc_baseline`) or to subtract the classwise average of the baseline (option `'classwise',1` in `proc_baseline`). 1. Measure for discriminability (line 29). Choices are, e.g., signed r^2, t-values, p-values, AUC-score. 1. If, in an oddball-like paradigm, stimuli are presented in a fast sequence, it might be beneficial for the ERP analysis to constrain the occurrence of target stimuli within the time interval of investigation. This has to be done as first operation for marker processing (i.e., ''before'' the artifact rejection in line 18). To exclude target occurrences before/after the event at t=0, use the function `mrk_selectTargetDist`. E.g., to exclude targets to be one of the 3 preceding and the 2 subsequent stimuli, use {{{ mrk= mrk_selectTargetDist(mrk, [3 2]); }}}. Defining different constraints for target and nontarget events is also possible. === Event-Related Spectral Analysis === <> {{{#!highlight matlab file= 'Pavel_01_11_23/selfpaced2sPavel'; [cnt, mrk, mnt]= eegfile_loadMatlab(file); colOrder= [245 159 0; 0 150 200]/255; opt_grid_spec= defopt_spec('xTickAxes','O2', ... 'colorOrder',colOrder); ival_spec= [-1000 0]; % Pre-movement interval: investigate motor-preparation band_list= [7 11; 11 14; 20 24; 26 36]; clab= {'C3','C4'}; winlen= cnt.fs; % length of FFT in proc_spectrum: 1s. To investigate spectra of short % epochs taking 0.5s is also possible -> frequency resolution 2Hz. % Artifact rejection based on variance criterion mrk= reject_varEventsAndChannels(cnt, mrk, ival_spec, 'verbose', 1); % Segmentation spec= cntToEpo(cnt, mrk, ival_spec); spec_lar= proc_localAverageReference(spec, mnt, 'radius',0.4); spec_lar= proc_spectrum(spec_lar, [5 40], kaiser(winlen,2)); spec= proc_spectrum(spec, [5 40], kaiser(winlen,2)); spec_r= proc_r_square_signed(spec); spec_lar_r= proc_r_square_signed(spec_lar); fig_set(1); H= grid_plot(spec, mnt, opt_grid_spec); %grid_markIval(band_erd); % to shade a certain frequency band grid_addBars(spec_r, 'h_scale',H.scale); fig_set(5); H= grid_plot(spec_lar, mnt, opt_grid_spec); grid_addBars(spec_lar_r, 'h_scale',H.scale); fig_set(2); H= scalpEvolutionPlusChannel(spec, mnt, clab, band_list, ... defopt_scalp_power2, ... 'colorOrder',colOrder, ... 'scalePos','horiz', ... 'globalCLim',0); grid_addBars(spec_r); fig_set(4, 'shrink',[1 2/3]); scalpEvolutionPlusChannel(spec_r, mnt, clab, band_list, defopt_scalp_r2); %% Do the same with subtracting the spectrum in a reference time interval % Here we use a post-movement interval. ref_ival= [200 1200]; mrk_ref= mrk; mrk_ref.y= ones(1, length(mrk_ref.pos)); mrk_ref.className= {'ref'}; mrk_ref= reject_varEventsAndChannels(cnt, mrk_ref, ref_ival); spec_baseline= makeEpochs(cnt, mrk_ref, ref_ival); spec_baseline= proc_spectrum(spec_baseline, [5 40], kaiser(winlen,2)); spec_baseline= proc_average(spec_baseline); spec_ref= proc_subtractReferenceClass(spec, spec_baseline); fig_set(6); H= scalpEvolutionPlusChannel(spec_ref, mnt, clab, band_list, ... defopt_scalp_power2, ... 'extrapolate', 0, ... 'colorOrder',colOrder); grid_addBars(spec_r); }}} === Long-Term Spectral Analysis === <> {{{#!highlight matlab file= 'VPgce_11_02_08/relaxVPgce'; [cnt, mrk, mnt]= eegfile_loadMatlab(file); band_list= [4 7; 7 10; 10 13; 13 26]; % get information about starting and stopping time of 'eyes open' and % 'eyes closed' phases: blk1= blk_segmentsFromMarkers(mrk, ... 'start_marker','eyes_closed', ... 'end_marker','stop'); blk2= blk_segmentsFromMarkers(mrk, ... 'start_marker','eyes_open', ... 'end_marker','stop'); blk= blk_merge(blk1, blk2, 'className',{'eyes closed','eyes open'}); % Generate a marker structure which has markers every 1000msec with in % blocks of 'eyes-open' and 'eyes-closed'. mkk= mrk_evenlyInBlocks(blk, 1000); % Alternatively, this code can be used to save memory. Here the new cnt % will consist of a concatenation of the blocks that are defined in 'blk', % i.e., parts which do not belong to any block are left out. The structure % 'blkcnt' is the block structure corresponding to the new 'cnt'. %[cnt, blkcnt]= proc_concatBlocks(cnt, blk); %mkk= mrk_evenlyInBlocks(blkcnt, 1000); fig_set(1); [mkk, rClab]= reject_varEventsAndChannels(cnt, mkk, [0 999], ... 'visualize', 1); printFigure(['artifact_rejection'], [19 12]); % Spectra are calculated on raw channels, and on spatially filtered channels. % Laplacian filters can be used if the area of interest is centrally located. % At the border of the cap (e.g. for visual cortex), local average reference % often works better. For the grid plot, spatially filtered channels are mostly % preferable, but for scalp topographies it is better to use spectra from % raw channels. spec= cntToEpo(cnt, mkk, [0 1000], 'mtsp', 'before'); spec_lap= proc_localAverageReference(spec, mnt, 'radius',0.6); spec_lap= proc_spectrum(spec_lap, [1 40], kaiser(cnt.fs,2)); spec= proc_spectrum(spec, [1 40], kaiser(cnt.fs,2)); spec_r= proc_r_square_signed(spec); spec_lap_r= proc_r_square_signed(spec_lap); H= grid_plot(spec, mnt, defopt_spec); grid_addBars(spec_r, 'h_scale',H.scale); printFigure(['spec'], [24 16]); H= grid_plot(spec_lap, mnt, defopt_spec); grid_addBars(spec_lap_r, 'h_scale',H.scale); printFigure(['spec_lap'], [24 16]); fig_set(2); H= scalpEvolutionPlusChannel(spec, mnt, 'Pz', band_list, ... defopt_scalp_power2, ... 'scalePos','horiz', ... 'globalCLim',0); grid_addBars(spec_r, 'rectify',1, 'vpos',1); printFigure(['spec_topo'], [24 15]); fig_set(4, 'shrink',[1 2/3]); spec_r.className= {sprintf('\\pm r^2 (EC,EO)')}; scalpEvolutionPlusChannel(spec_r, mnt, 'Pz', band_list, ... defopt_scalp_r2); printFigure(['spec_topo_r'], [24 10]); }}} === ERD/ERS Analysis === <> Investigating the time course of band-power, i.e., ERD/ERS curves is pretty much like ERP analysis, but with calculating the envelope of the band-pass filtered signals before: {{{#!highlight matlab file= 'Pavel_01_11_23/selfpaced2sPavel'; [cnt, mrk, mnt]= eegfile_loadMatlab(file, 'clab',{'not','E*'}); colOrder= [245 159 0; 0 150 200]/255; ival_erd= [-1000 500]; band_erd= [11 14]; ival_scalps= -800:200:200; % Bandpass to the frequency band of interest [b,a]= butter(5, band_erd/cnt.fs*2); cnt= proc_filt(cnt, b, a); % Artifact rejection based on variance criterion mrk= reject_varEventsAndChannels(cnt, mrk, ival_erd, ... 'do_bandpass', 0, ... 'verbose', 1); epo= cntToEpo(cnt, mrk, ival_erd); erd_lar= proc_localAverageReference(epo, mnt, 'radius',0.4); erd_lar= proc_envelope(erd_lar, 'ma_msec', 200); erd_lar= proc_baseline(erd_lar, [], 'trialwise', 0); erd= proc_envelope(epo, 'ma_msec', 200); erd= proc_baseline(erd, [], 'trialwise', 0); erd_lar_r= proc_r_square_signed(erd_lar); erd_r= proc_r_square_signed(erd); fig_set(1) H= grid_plot(erd, mnt, defopt_erps, 'colorOrder',colOrder); grid_addBars(erd_r, 'h_scale',H.scale); fig_set(5) H= grid_plot(erd_lar, mnt, defopt_erps, 'colorOrder',colOrder); grid_addBars(erd_lar_r, 'h_scale',H.scale); fig_set(2); H= scalpEvolutionPlusChannel(erd, mnt, clab, ival_scalps, defopt_scalp_erp2, ... 'colorOrder',colOrder); grid_addBars(erd_r); fig_set(4, 'shrink',[1 2/3]); scalpEvolutionPlusChannel(erd_r, mnt, clab, ival_scalps, defopt_scalp_r2); }}}