forked from chassall/flynn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFLYNN.m
621 lines (532 loc) · 25.2 KB
/
FLYNN.m
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
function DISC = FLYNN( pathToConfigFile, pathToLocsFile )
%FLYNN 3.5.3 Takes a config file pathname and a locations file pathname, then loads, organizes, and
%analyzes continuous or epoched EEG data.
%
% C. Hassall and O. Krigolson
% December, 2017
%
% FLYNN 3.0 .mat input (EEGLAB format), multiple .mat output
% FLYNN 2.0 trial eeg text file input, multiple .mat output
% FLYNN 1.0 average eeg text file input, single .mat output
% Requires: disc.wav, flynn.jpg, stats toolbox
%
% Example
% myDISC = FLYNN('FLYNNConfiguration.txt','Standard-10-20-NEL-62.locs');
% save('DISC.mat','myDISC');
% plotdisc(myDISC);
% FLYNN version number (major, minor, revision)
version = '3.5.3';
% Load config file
configFileId = fopen(pathToConfigFile);
C = textscan(configFileId, '%q','CommentStyle','%');
fclose(configFileId);
answer = C{1};
% Load locs file
userLocsFile = readlocs(pathToLocsFile, 'filetype', 'locs');
userLocsFile = rmfield(userLocsFile,{'sph_theta_besa','sph_phi_besa'});
% Parse ANSWER
basefilename = answer{1};
subjectnumbers = strsplit(answer{2},',');
numberofsubjects = length(subjectnumbers);
baselinesettings = str2num(answer{3});
artifactsettings = str2num(answer{4});
outfile = answer{5};
% Determine which ERP/FFT/WVLT to do
numAnalyses = length(answer)-5;
if numAnalyses == 0
disp('Error: No analysis specified');
return;
end
% ERP variables
ERP.markers = {};
ERP.startTime = [];
ERP.endTime = [];
ERP.conditions = {};
numErpConditions = 0;
numErpMarkersByCondition = [];
% ALL variables
ALL.markers = {};
ALL.startTime = [];
ALL.endTime = [];
ALL.conditions = {};
numAllConditions = 0;
numAllMarkersByCondition = [];
ALL.whichMarker = {};
ALL.isArtifact = {};
% FFT variables
FFT.markers = {};
FFT.startTime = [];
FFT.endTime = [];
FFT.conditions = {};
numFftConditions = 0;
numFftMarkersByCondition = [];
% WAV variables
WAV.markers = {};
WAV.startTime = [];
WAV.endTime = [];
WAV.baselineStart = [];
WAV.baselineEnd = [];
WAV.frequencyStart = [];
WAV.frequencyEnd = [];
WAV.frequencySteps = [];
WAV.rangeCycles = [];
WAV.conditions = {};
numWavConditions = 0;
numWavMarkersByCondition = [];
for i = 1:length(answer)-5
thisAnalysis = answer{5+i};
temp = strsplit(thisAnalysis,',');
if strcmp(temp{1},'ERP')
numErpConditions = numErpConditions + 1;
numMarkers = length(temp) - 4;
numErpMarkersByCondition(numErpConditions) = numMarkers;
for k = 1:numMarkers
ERP.markers{k,numErpConditions} = temp{1+k};
end
ERP.startTime{numErpConditions} = temp{2 + numMarkers};
ERP.endTime{numErpConditions} = temp{3 + numMarkers};
ERP.conditions{numErpConditions} = temp{4+numMarkers};
elseif strcmp(temp{1},'ALL')
numAllConditions = numAllConditions + 1;
numMarkers = length(temp) - 4;
numAllMarkersByCondition(numAllConditions) = numMarkers;
for k = 1:numMarkers
ALL.markers{k,numAllConditions} = temp{1+k};
end
ALL.startTime{numAllConditions} = temp{2 + numMarkers};
ALL.endTime{numAllConditions} = temp{3 + numMarkers};
ALL.conditions{numAllConditions} = temp{4+numMarkers};
elseif strcmp(temp{1},'FFT')
numFftConditions = numFftConditions + 1;
numFftMarkers = length(temp) - 4;
numFftMarkersByCondition(numFftConditions) = numFftMarkers;
for k = 1:numFftMarkers
FFT.markers{k,numFftConditions} = temp{1+k};
end
FFT.startTime{numFftConditions} = temp{2 + numFftMarkers};
FFT.endTime{numFftConditions} = temp{3 + numFftMarkers};
FFT.conditions{numFftConditions} = temp{4+numFftMarkers};
elseif strcmp(temp{1},'WAV')
numWavConditions = numWavConditions + 1;
numWavMarkers = length(temp) - 10;
numWavMarkersByCondition(numWavConditions) = numWavMarkers;
for k = 1:numWavMarkers
WAV.markers{k,numWavConditions} = temp{1+k};
end
WAV.startTime{numWavConditions} = temp{2 + numWavMarkers};
WAV.endTime{numWavConditions} = temp{3 + numWavMarkers};
WAV.baselineStart{numWavConditions} = temp{4+numWavMarkers};
WAV.baselineEnd{numWavConditions} = temp{5+numWavMarkers};
WAV.frequencyStart{numWavConditions} = temp{6+numWavMarkers};
WAV.frequencyEnd{numWavConditions} = temp{7+numWavMarkers};
WAV.frequencySteps{numWavConditions} = temp{8+numWavMarkers};
WAV.rangeCycles{numWavConditions} = temp{9+numWavMarkers};
WAV.conditions{numWavConditions} = temp{10+numWavMarkers};
else
disp('Error: Unknown analysis');
return;
end
end
% DISC will hold participant summaries
DISC.version = version;
DISC.participants = subjectnumbers;
DISC.N = numberofsubjects;
DISC.EEGSum = []; % EEG Summary (participant, channels, datapoints)
DISC.ALLSum = []; % ALL Summary (participant, channels, datapoints)
DISC.ERPSum = []; % ERP Summary (participant, kept epochs, removed epochs)
DISC.FFTSum = []; % FFT Summary (participant, kept epochs, removed epochs)
DISC.WAVSum = []; % WAV Summary (participant, kept epochs, removed epochs)
firstLocsFile = [];
% Do analysis for each participant (ERP, FFT, WVLT)
for p = 1:numberofsubjects
if isempty(subjectnumbers{p})
disp('Error: No participants present');
return;
end
% Data Import
disp(['Current Subject Being Loaded: ' subjectnumbers{p}]);
filename = [basefilename subjectnumbers{p} '.mat'];
load(filename);
% Add in some empty fields
EEG.icasphere = [];
EEG.icawinv = [];
EEG.icaweights = [];
EEG.icaact = [];
% Check to see if the data have been epoched (i.e. channels X samples
% X trials) or if the data are continuous
dataEpoched = 0;
if length(size(EEG.data)) == 3
dataEpoched = 1;
end
% Interpolate missing channels and reorder (based on code by Marco Simões)
% Check to see if there are any channels in the EEG file that are not
% in the locs file
if any(ismember({EEG.chanlocs.labels}, {userLocsFile.labels}) == 0)
disp('Warning: EEG contains channels that are missing from the user-specified locs file');
return;
end
missingIDs = [];
for i=1:length(userLocsFile)
if isempty(find(ismember({EEG.chanlocs.labels}, userLocsFile(i).labels) == 1, 1))
missingIDs = [missingIDs i];
end
end
interpolated{p} = {userLocsFile(missingIDs).labels};
if ~isempty(missingIDs)
EEG = pop_interp(EEG, userLocsFile(missingIDs), 'spherical'); % Interpolate missing channels
end
newOrder = nan(1, length(userLocsFile));
for c=1:length(userLocsFile)
newOrder(c) = find(ismember({EEG.chanlocs.labels}, userLocsFile(c).labels) == 1, 1);
end
EEG.data(:,:,:) = EEG.data(newOrder,:,:); % Reorder data (should word whether EEG.data is 2D or 3D)
EEG.chanlocs = EEG.chanlocs(newOrder); % Reorder chanlocs
% %% Sort the data
% newOrder = nan(1,length(EEG.chanlocs)); % New channel order
% % Compare user channels to actual channels - if there is a match,
% % record in which position it was found
% for i = 1:length(userLocsFile)
% for k = 1:length(EEG.chanlocs)
% if strcmp(userLocsFile(i).labels,EEG.chanlocs(k).labels)
% newOrder(i) = k;
% end
% end
% end
% % Error checking
% if length(userLocsFile) ~= length(EEG.chanlocs) || any(isnan(newOrder))
% disp('Error: Locs file mismatch');
% return;
% else
% EEG.chanlocs = userLocsFile; % Use the user-defined locs file
% EEG.data = EEG.data(newOrder,:,:); % Reorder data
% end
% chanlocs = EEG.chanlocs; % This may lead to slightly different locs parameters due to interpolation
chanlocs = userLocsFile;
srate = EEG.srate;
times = EEG.xmin*1000:1000/EEG.srate:EEG.xmax*1000;
thisParticipantNumber = str2num(cell2mat(regexp(subjectnumbers{p},'\d','match'))); % Remove non-digits first
%% Epoching
if dataEpoched
allMarkers = {EEG.epoch.eventtype}; % Markers within each epoch
% Problem: epochs contain multiple markers - to know which one is at 0 ms, we need to check latencies
latencies = {EEG.epoch.eventlatency}; % Latencies of all events within each epoch
actualMarkers = {}; % Marker of interest for each epoch
for m = 1:length(allMarkers)
thisSetOfMarkers = allMarkers{m};
if ~iscell(thisSetOfMarkers) % NEW November 27 - need this in case there is only one marker in epoch
if isnumeric(thisSetOfMarkers)
actualMarkers{m} = num2str(thisSetOfMarkers);
else
actualMarkers{m} = thisSetOfMarkers;
end
else
theseLatencies = cell2mat(latencies{m});
[~, whichOne] = min(abs(theseLatencies - abs(EEG.xmin)*1000000)); % Find the latency (in nanoseconds?) closest to 0 ms
if isempty(whichOne)
disp('Error: Timing error in EEGLAB file');
return;
end
if isnumeric(thisSetOfMarkers{whichOne})
actualMarkers{m} = num2str(thisSetOfMarkers{whichOne});
else
actualMarkers{m} = thisSetOfMarkers{whichOne};
end
end
end
else
if isnumeric(EEG.event(1).type)
allMarkers = [EEG.event.type];
for m = 1:length(allMarkers)
actualMarkers{m} = num2str(allMarkers(m));
end
else
allMarkers = {EEG.event.type};
end
latencies = cell2mat({EEG.event.latency}');
end
%% ERP Analysis
for c = 1:length(ERP.conditions)
isThisCondition = false(1,length(actualMarkers));
% Make a logical vector so that all relevant markers are inccluded
for m = 1:numErpMarkersByCondition(c)
isThisCondition = isThisCondition | strcmp(actualMarkers,ERP.markers{m,c});
end
if sum(isThisCondition) == 0
ERP.timepoints{c} = [];
ERP.data{c} = [];
ERP.nAccepted{c} = NaN;
ERP.nRejected{c} = NaN;
disp(['No ERP epochs found: ' ERP.conditions{c}]);
else
ERP.timepoints{c} = str2num(ERP.startTime{c}):1000/EEG.srate:str2num(ERP.endTime{c});
ERP.data{c} = nan(EEG.nbchan,length(ERP.timepoints{c}));
if dataEpoched
erpPoints = dsearchn(times', [str2num(ERP.startTime{c}) str2num(ERP.endTime{c})]');
erpEEG = EEG.data(:,erpPoints(1):erpPoints(2),:);
else
theseLatencies = latencies(isThisCondition);
erpEEG = [];
for m = 1:length(theseLatencies)
erpPoints = dsearchn(times',theseLatencies(m)*1000/EEG.srate + [str2num(ERP.startTime{c}) str2num(ERP.endTime{c})]');
% Had to add this in case an epoch goes past the end of the
% recording
if erpPoints(2)-erpPoints(1)+1 == length(ERP.timepoints{c})
erpEEG(:,:,m) = EEG.data(:,erpPoints(1):erpPoints(2));
end
end
end
% Do baseline correction
if ~isempty(baselinesettings)
baselinePoints = dsearchn(ERP.timepoints{c}',baselinesettings(:)); % Find the baseline indices
baseline = mean(erpEEG(:,baselinePoints(1):baselinePoints(2) ,:),2);
erpEEG = erpEEG - repmat(baseline,[1,length(ERP.timepoints{c}),1]); % EEG data, with baseline correction applied
end
% ERP Artifact Rejection TODO: Make this a function
% Artifact Rejection - Gradient
maxAllowedStep = artifactsettings(1)*(1000/EEG.srate); % E.g. 10 uV/ms ~= 40 uV/4 ms... Equivalent to Analyzer?
gradient = abs(erpEEG(:,2:end,:) - erpEEG(:,1:end-1,:));
gradientViolation = squeeze(any(gradient > maxAllowedStep,2));
% Artifact Rejection - Difference
maxAllowedDifference = artifactsettings(2);
diffEEG = max(erpEEG,[],2) - min(erpEEG,[],2);
differenceViolations = squeeze(diffEEG > maxAllowedDifference);
allViolations = sum(gradientViolation) + sum(differenceViolations);
isArtifact = allViolations ~= 0;
if dataEpoched
ERP.nAccepted{c} = sum(~isArtifact & isThisCondition);
ERP.nRejected{c} = sum(isArtifact & isThisCondition);
thisAverage = mean(erpEEG(:,:,~isArtifact & isThisCondition),3);
else
ERP.nAccepted{c} = sum(~isArtifact);
ERP.nRejected{c} = sum(isArtifact);
thisAverage = mean(erpEEG(:,:,~isArtifact),3);
end
% plot(thisAverage(34,:));
% hold on;
ERP.data{c} = thisAverage;
end
DISC.ERPSum = [DISC.ERPSum; thisParticipantNumber c ERP.nAccepted{c} ERP.nRejected{c}];
end
%% ALL Analysis (will store all trials of a certain type)
for c = 1:length(ALL.conditions)
isThisCondition = false(numAllMarkersByCondition(c),length(actualMarkers));
% Make a logical vector so that all relevant markers are inccluded
for m = 1:numAllMarkersByCondition(c)
isThisCondition(m,:) = strcmp(actualMarkers,ALL.markers{m,c});
end
isAnyCondition = sum([isThisCondition; zeros(1,length(isThisCondition))]) ~= 0;
if sum(isAnyCondition) == 0
ALL.timepoints{c} = [];
ALL.data{c} = [];
ALL.nAccepted{c} = NaN;
ALL.nRejected{c} = NaN;
disp(['No ALL epochs found: ' ALL.conditions{c}]);
else
ALL.timepoints{c} = str2num(ALL.startTime{c}):1000/EEG.srate:str2num(ALL.endTime{c});
%ALL.data{c} = nan(EEG.nbchan,length(ALL.timepoints{c}),);
if dataEpoched
allPoints = dsearchn(times', [str2num(ALL.startTime{c}) str2num(ALL.endTime{c})]');
allEEG = EEG.data(:,allPoints(1):allPoints(2),:);
else
theseLatencies = latencies(isAnyCondition);
allEEG = [];
for m = 1:length(theseLatencies)
allPoints = dsearchn(times',theseLatencies(m)*1000/EEG.srate + [str2num(ALL.startTime{c}) str2num(ALL.endTime{c})]');
% Had to add this in case an epoch goes past the end of the
% recording
if allPoints(2)-allPoints(1)+1 == length(ALL.timepoints{c})
allEEG(:,:,m) = EEG.data(:,allPoints(1):allPoints(2));
end
end
end
% Do baseline correction
if ~isempty(baselinesettings)
baselinePoints = dsearchn(ALL.timepoints{c}',baselinesettings(:)); % Find the baseline indices
baseline = mean(allEEG(:,baselinePoints(1):baselinePoints(2) ,:),2);
allEEG = allEEG - repmat(baseline,[1,length(ALL.timepoints{c}),1]); % EEG data, with baseline correction applied
end
% ERP Artifact Rejection TODO: Make this a function
% Artifact Rejection - Gradient
maxAllowedStep = artifactsettings(1)*(1000/EEG.srate); % E.g. 10 uV/ms ~= 40 uV/4 ms... Equivalent to Analyzer?
gradient = abs(allEEG(:,2:end,:) - allEEG(:,1:end-1,:));
gradientViolation = squeeze(any(gradient > maxAllowedStep,2));
% Artifact Rejection - Difference
maxAllowedDifference = artifactsettings(2);
diffEEG = max(allEEG,[],2) - min(allEEG,[],2);
differenceViolations = squeeze(diffEEG > maxAllowedDifference);
allViolations = sum(gradientViolation) + sum(differenceViolations);
isArtifact = allViolations ~= 0;
if dataEpoched
isArtifact = isArtifact(isAnyCondition);
ALL.nAccepted{c} = sum(~isArtifact);
ALL.nRejected{c} = sum(isArtifact);
ALL.data{c} = allEEG(:,:,isAnyCondition);
else
ALL.nAccepted{c} = sum(~isArtifact);
ALL.nRejected{c} = sum(isArtifact);
ALL.data{c} = allEEG;
end
ALL.whichMarker{c} = isThisCondition(:,isAnyCondition); % Marker for each trial
ALL.isArtifact{c} = isArtifact;
end
DISC.ALLSum = [DISC.ALLSum; thisParticipantNumber c ALL.nAccepted{c} ALL.nRejected{c}];
end
%% FFT Analysis
for c = 1:length(FFT.conditions)
% Contruct a boolean indicating if an epoch should be included
isThisCondition = false(1,length(actualMarkers));
% Make a logical vector so that all relevant markers are inccluded
for m = 1:numFftMarkersByCondition(c)
isThisCondition = isThisCondition | strcmp(actualMarkers,FFT.markers{m,c});
end
if sum(isThisCondition) == 0
FFT.timepoints{c} = [];
FFT.data{c} = [];
FFT.nAccepted{c} = NaN;
FFT.nRejected{c} = NaN;
disp(['No FFT epochs found: ' FFT.conditions{c}]);
else
FFT.timepoints{c} = str2num(FFT.startTime{c}):1000/EEG.srate:str2num(FFT.endTime{c});
FFT.frequencyResolution{c} = EEG.srate / length(FFT.timepoints{c});
if dataEpoched
fftPoints = dsearchn(times', [str2num(FFT.startTime{c}) str2num(FFT.endTime{c})]');
fftEEG = EEG.data(:,fftPoints(1):fftPoints(2),:);
else
theseLatencies = latencies(isThisCondition);
fftEEG = [];
for m = 1:length(theseLatencies)
fftPoints = dsearchn(times',theseLatencies(m)*1000/EEG.srate + [str2num(FFT.startTime{c}) str2num(FFT.endTime{c})]');
% Had to add this in case an epoch goes past the end of the
% recording
if fftPoints(2)-fftPoints(1) + 1 == length(FFT.timepoints{c})
fftEEG(:,:,m) = EEG.data(:,fftPoints(1):fftPoints(2));
end
end
end
% Do baseline correction
if ~isempty(baselinesettings)
baselinePoints = dsearchn(FFT.timepoints{c}',baselinesettings(:)); % Find the baseline indices
baseline = mean(fftEEG(:,baselinePoints(1):baselinePoints(2) ,:),2);
fftEEG = fftEEG - repmat(baseline,[1,length(FFT.timepoints{c}),1]); % EEG data, with baseline correction applied
end
% ERP Artifact Rejection
% Artifact Rejection - Gradient
maxAllowedStep = artifactsettings(1)*(1000/EEG.srate); % E.g. 10 uV/ms ~= 40 uV/4 ms... Equivalent to Analyzer?
gradient = abs(fftEEG(:,2:end,:) - fftEEG(:,1:end-1,:));
gradientViolation = squeeze(any(gradient > maxAllowedStep,2));
% Artifact Rejection - Difference
maxAllowedDifference = artifactsettings(2);
diffEEG = max(fftEEG,[],2) - min(fftEEG,[],2);
differenceViolations = squeeze(diffEEG > maxAllowedDifference);
% diffEEG = movmax(fftEEG,800/(1000/EEG.srate),2,'Endpoints','discard') - movmin(fftEEG,800/(1000/EEG.srate),2,'Endpoints','discard'); % e.g., 800 ms moving window
% differenceViolations = squeeze(any(diffEEG > maxAllowedDifference,2));
allViolations = sum(gradientViolation) + sum(differenceViolations);
isArtifact = allViolations ~= 0;
% Store the number of good epochs for this condition and the
% proportion rejected
if dataEpoched
FFT.nAccepted{c} = sum(~isArtifact & isThisCondition);
FFT.nRejected{c} = sum(isArtifact & isThisCondition);
trimmedEEG.data = fftEEG(:,:,~isArtifact & isThisCondition);
else
FFT.nAccepted{c} = sum(~isArtifact);
FFT.nRejected{c} = sum(isArtifact);
trimmedEEG.data = fftEEG(:,:,~isArtifact);
end
% Prepare the EEG on which the FFT will be run
trimmedEEG.pnts = length(fftPoints(1):fftPoints(2));
trimmedEEG.srate = EEG.srate;
% Call doFFT
[FFT.data{c},FFT.frequencies{c}] = doFFT(trimmedEEG);
end
DISC.FFTSum = [DISC.FFTSum; thisParticipantNumber c FFT.nAccepted{c} FFT.nRejected{c}];
end
%% Wavelet Analysis (TODO)
for c = 1:length(WAV.conditions)
% Contruct a boolean indicating if an epoch should be included
isThisCondition = false(1,length(actualMarkers));
% Make a logical vector so that all relevant markers are inccluded
for m = 1:numWavMarkersByCondition(c)
isThisCondition = isThisCondition | strcmp(actualMarkers,WAV.markers{m,c});
end
if sum(isThisCondition) == 0
WAV.timepoints{c} = [];
WAV.data{c} = [];
WAV.nAccepted{c} = NaN;
WAV.nRejected{c} = NaN;
disp(['No WAV epochs found: ' WAV.conditions{c}]);
else
WAV.timepoints{c} = str2num(WAV.startTime{c}):1000/EEG.srate:str2num(WAV.endTime{c});
WAV.frequencyResolution{c} = EEG.srate / length(WAV.timepoints{c});
if dataEpoched
wavPoints = dsearchn(times', [str2num(WAV.startTime{c}) str2num(WAV.endTime{c})]');
wavEEG = EEG.data(:,wavPoints(1):wavPoints(2),:);
else
theseLatencies = latencies(isThisCondition);
wavEEG = [];
for m = 1:length(theseLatencies)
wavPoints = dsearchn(times',theseLatencies(m)*1000/EEG.srate + [str2num(WAV.startTime{c}) str2num(WAV.endTime{c})]');
% Had to add this in case an epoch goes past the end of the
% recording
if wavPoints(2)-wavPoints(1) + 1 == length(WAV.timepoints{c})
wavEEG(:,:,m) = EEG.data(:,wavPoints(1):wavPoints(2));
end
end
end
% Do baseline correction
if ~isempty(baselinesettings)
baselinePoints = dsearchn(WAV.timepoints{c}',baselinesettings(:)); % Find the baseline indices
baseline = mean(wavEEG(:,baselinePoints(1):baselinePoints(2) ,:),2);
wavEEG = wavEEG - repmat(baseline,[1,length(WAV.timepoints{c}),1]); % EEG data, with baseline correction applied
end
% Artifact Rejection - Gradient
maxAllowedStep = artifactsettings(1)*(1000/EEG.srate); % E.g. 10 uV/ms ~= 40 uV/4 ms... Equivalent to Analyzer?
gradient = abs(wavEEG(:,2:end,:) - wavEEG(:,1:end-1,:));
gradientViolation = squeeze(any(gradient > maxAllowedStep,2));
% Artifact Rejection - Difference
maxAllowedDifference = artifactsettings(2);
diffEEG = max(wavEEG,[],2) - min(wavEEG,[],2);
differenceViolations = squeeze(diffEEG > maxAllowedDifference);
%diffEEG = movmax(wavEEG,800/(1000/EEG.srate),2,'Endpoints','discard') - movmin(wavEEG,800/(1000/EEG.srate),2,'Endpoints','discard'); % e.g., 800 ms moving window
%differenceViolations = squeeze(any(diffEEG > maxAllowedDifference,2));
allViolations = sum(gradientViolation) + sum(differenceViolations);
isArtifact = allViolations ~= 0;
if dataEpoched
WAV.nAccepted{c} = sum(~isArtifact & isThisCondition);
WAV.nRejected{c} = sum(isArtifact & isThisCondition);
trimmedEEG.data = wavEEG(:,:,~isArtifact & isThisCondition);
else
WAV.nAccepted{c} = sum(~isArtifact);
WAV.nRejected{c} = sum(isArtifact);
trimmedEEG.data = wavEEG(:,:,~isArtifact);
end
[~,~,trimmedEEG.trials] = size(trimmedEEG.data);
trimmedEEG.times = WAV.timepoints{c};
trimmedEEG.srate = EEG.srate;
trimmedEEG.pnts = length(WAV.timepoints{c});
baseline_windows = [str2num(WAV.baselineStart{c}) str2num(WAV.baselineEnd{c})];
min_freq = str2num(WAV.frequencyStart{c});
max_freq = str2num(WAV.frequencyEnd{c});
num_frex = str2num(WAV.frequencySteps{c});
range_cycles = str2num(WAV.rangeCycles{c});
[WAV.data{c},WAV.dataPercent{c},WAV.frequencies{c}] = doWavelet(trimmedEEG,baseline_windows,min_freq,max_freq,num_frex,range_cycles);
end
DISC.WAVSum = [DISC.WAVSum; thisParticipantNumber c WAV.nAccepted{c} WAV.nRejected{c}];
end
%% Data Export
outfilename = [outfile subjectnumbers{p} '.mat'];
save(outfilename,'version','srate','chanlocs','ERP','ALL','FFT','WAV');
end
% Store condition names in the DISC
DISC.ALLConditions = ALL.conditions;
DISC.ERPConditions = ERP.conditions;
DISC.FFTConditions = FFT.conditions;
DISC.WAVConditions = WAV.conditions;
% Record interpolated channel names
DISC.interpolated = interpolated;
%% Visualization
plotdisc(DISC);
end
% Use the commented-out code below to display results (condition 1, channel 1)
% plot(FFT.frequencies{1},FFT.data{1}(1,:));
% contourf(WAV.timepoints{1}, WAV.frequencies{1}, squeeze(WAV.data{1}(1,:,:)),'linecolor','none');