-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patheegdemo_asymmetryImportAndRereference.m
More file actions
157 lines (125 loc) · 5.76 KB
/
eegdemo_asymmetryImportAndRereference.m
File metadata and controls
157 lines (125 loc) · 5.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
%% EEG Demos - Importing, arranging, and re-referencing Asymmetry Data
%
% This is a demo script for using the new functions for re-referencing,
% using these and the older tools in EEG Blocks.
%
% MDT
% 2016.09.20
% Version 0.1.0
fprintf('\n\n\nEEG DATA ANALYSIS DEMO: eegdemo_asymmetryImportAndRereference.m\n\n');
%% File Settings
%
% The file used in this script is in the format used for the in-house
% squeezing exepriments--there are three intervals, labelled with markers
% 3, 4, and 5 (pre, during, and post; respectively). These markers appear
% at the start and also at the end of each block. Hopefully you can see
% from the examples given how to set up different markers for other
% experiments! (See below for more on this.)
%
% The demo file is **currently** located in:
%
% S:\Turner-Lab\EEG\Asymmetry\Data\Hand Contraction\Raw_data\not to be analyzed
%
% You may copy but do not disturb the original!!!!!
filename = '702-S1-LO-19.05.2016.11.38.05.set'; % Or use your own data!
%% Extracting File Name Parts
%
% This next block of code extracts the subject name/code (subnum), the
% condition code (handorder; in this case), and the session number
% (sesnum). These could be other codes as well for different experiments.
% Note that the variable namelist will contain all the parts of the
% filename, in the ORDER USED that were separated by dashes ('-'). The last
% element will be the complex date/time code assigned by the Emotiv
% software. It is in namelist{4} but unused here.
%
% Most of the code in this block comes from the older version of the
% program: ge_handcontractionBlockImport.m see that file for details!
cleanname = regexprep(filename, '\.edf|\.set$','');
namelist = strsplit(cleanname, '-');
subnum = namelist{1};
handorder = namelist{3};
sesnum = regexprep(namelist{2}, '^S', '');
%% Import and Filtering
%
% The example here is done for the "common average re-reference" (AVG/AVE
% in the literature). In other reference schemes, only the function that
% does th re-reference needs to be changed in the code just below here, if
% you want another reference scheme.
%
% Most of the code in this section comes from ge_handContraction.m.
lowerBound = 2; % These are frequencies for the filtering
upperBound = 41;
eegChannels = 3:16; % Emotiv EPOC/EPOC+ data channels in EDF
if regexp(filename,'set$') % This code allows both SET/EDF as input
EEG = pop_loadset(filename);
elseif regexp(filename,'edf$')
EEG = pop_biosig(filename);
else
error('eegdemo: File type unknown');
end
% Prints the marker table; for your reference, not important for the
% analysis! The fprintf()'s are just for formatting:
fprintf('\nHere are the "events" in the recording.\nMarker Table:\n\n');
disp(ge_makeEventList(EEG))
fprintf('\n');
% Select the data (dropping the signal quality, gyroscopic, and other
% channels) and then filter the data. Note that there are now two EEG data
% structures -- EEG and EEG_only. The former has all the channels including
% marker tracks and the latter has only the EEG channels:
EEG_only = pop_select(EEG, 'channel', eegChannels);
EEG_only = pop_eegfilt(EEG_only, lowerBound, upperBound, [], [0], 0, 0, 'fir1', 0);
% For each marker (3,4,5) get the corresponding block of EEG -- this is
% assuming that we are doing this without any manual selection of bounds.
% The block marked by 3's is the "pre" or "resting" data which we will use
% below. The other blocks could be used similarly. For that you would need
% a function here to insert hand-chosen boundary points. The data blocks
% are EACH stored inside the data structure called "data" -- at the command
% line type its name to see it. It will be a 1x5 "cell array" in Matlab
% terminology.
for m = 3:5
ss = ge_getSampleBounds(EEG, m);
data{m} = EEG_only.data(:,ss(1):ss(2));
end
% Most functions that we write locally work with EEG Blocks, a collection
% of programs that consume data in a standard format (a blob) and, after
% transforming it, return the revised data in the same format. These
% functions are usually called in the form:
%
% outBlob = ebFunctionName(inBlob);
%
% Where inBlob is the data, as processed so far, and the outBlob is the
% data that results from the function application. Each blob, minimally,
% must have a value called Fs for the sampling rate and a value data tha
% hold the EEG data. The blobs may have many more values. For the
% re-referencing functions, these minimal two must be set. We create a blob
% for the 'pre' data block:
blob_pre.Fs = 128;
blob_pre.data = data{3}'; % NB: data{1} and data{2} are empty--not used!
% IMPORTANT: Remember that the blobs have their data TRANSPOSED from how
% eeglab does things. That is, data in eeglab's "EEG" data block are in
% channels by time/samples; while the blobs are samples/time X channels.
% Just use the Matlab transpose operator to 'flip' the matrix as needed.
% This is the single quote after the data{3} in the last line of code
% above.
fprintf('\n\nHere is the data blob before re-referencing:\n\n');
blob_pre
%% Re-reference!
%
% Now we call the various re-referencing functions to re-reference the data
% in the blob. Here we do :
blob_pre = ebReRefAverage(blob_pre);
blob_pre = ebReRefEPOCParietal(blob_pre);
fprintf('\n\nAnd here it is after re-referencing:\n\n');
blob_pre
fprintf('\nNote that both the original and re-referenced data is saved.\n\n');
%% Plot the data
%
% This is just to finish the demo--at this point you could do anything you
% wanted with the re-referenced data.
segment = 100:200;
figure;
plot(segment, blob_pre.data(segment,3), 'color', [0.4 0.4 0.4], 'Marker', '.');
hold on
plot(segment, blob_pre.dataAveRef(segment,3), 'r');
plot(segment, blob_pre.dataParietalRef(segment,3), 'b');
legend('Left Mastoid (Original)', 'AVE/AVG','Average Parietal', 'Location', 'NorthWest');