-
Notifications
You must be signed in to change notification settings - Fork 0
/
fmrib_pas.m
767 lines (624 loc) · 22.4 KB
/
fmrib_pas.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
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
% fmrib_pas() - Remove pulse
% artifacts from EEG data collected inside an MRI machine.
%
% This program removes pulse artifacts from EEG data collected
% collected inside the MRI scanner. A choice of methods is available.
%
% The first choice is Optimal Basis Set [Niazy06].
% This method aligns all the pulse artifacts, in each EEG channel
% separately, in a matrix and performs a Principal Component Analysis
% (PCA) on the data. The first N PCs (the Optimal Basis Set) are then
% fitted to each artifact in that channel. The process is repeated for
% each channel. The other three methods are based on [Allen98] with
% improvements to better capture and subtract the artifacts.
% Basically, in these methods a statistical measurement is used to find a
% template for the artifact at each heart beat. A window of 30 artifacts
% centred around the artifact being processed is used. The 30 artifacts
% are processed by taking the mean ('mean' method), the median
% ('median' method) or a Gaussian-weighted
% ('gmean' method to emphasise shape of current artifact) mean. It is
% recommended to use the first ('obs') method as it generally better
% fits the true artifact.
%
% Usage:
% >> EEGOUT= fmrib_pas(EEG,qrsevents,method)
% or >> EEGOUT= fmrib_pas(EEG,qrsevents,method,npc)
%
% Inputs:
% EEG: EEGLAB data structure
% qrsevents: vector of QRS event locations specified in samples.
% method: 'obs' for artifact principal components. You need to specify
% 'npc', which is the number of PC to use in fitting the
% artifacts. If unsure, use 4.
% 'mean' for simple mean averaging.
% 'gmean' for Gaussian weighted mean.
% 'median' for median filter.
%
%
% [Niazy06] R.K. Niazy, C.F. Beckmann, G.D. Iannetti, J.M. Brady, and
% S.M. Smith (2005) Removal of FMRI environment artifacts from EEG data
% using optimal basis sets. NeuroImage 28 (3), pages 720-737.
%
%
% [Allen98] Allen et.al., 1998, Identification of EEG events in the MR
% scanner: the problem of pulse artifact and a method for its
% subtraction. NeuroImage8,229-239.
%
%
%
% Also See pop_fmrib_pas
%
% Author: Rami K. Niazy
%
% Copyright (c) 2006 University of Oxford
% Copyright (C) 2006 University of Oxford
% Author: Rami K. Niazy, FMRIB Centre
% rami@fmrib.ox.ac.uk
%
% This program is free software; you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation; either version 2 of the License, or
% (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this program; if not, write to the Free Software
% Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
% SEP 20, 2005
% Template range now based on median instead of mean
% AUG 16, 2005
% Fixed bug dealing with single precision data.
% JUN 3, 2005
% Released
% APR 20, 2005
% in OBS mode artifact matrix now 'constant' demeaned instead
% of linear trend removal
% DEC 23, 2004
% updated (c)
% DEC 22, 2004
% fixed end artifact bug
% fixed copyright
% Dec16, 2004
% Added RAPCO
% re-written help
% Nov 4, 2004
% Updated brog bar
% for extra steps
% Oct 26, 2004
% Changed input to accept
% vector of event location
% instead of event type to
% allow for scripting.
function EEG = fmrib_pas(EEG,QRSevents,method,npc)
nargchk(3,4,nargin);
switch method
case 'obs'
if nargin < 4
error('Incorrect number of input arguments');
end
%init
%-----
fs=EEG.srate;
[channels samples]=size(EEG.data);
pcs=npc-1;
%standard delay between QRS peak and artifact (allen,1998)
delay=round(0.21*fs);
Gwindow=20;
GHW=floor(Gwindow/2);
rcount=0;
firstplot=1;
% memory allocation
%------------------
avgdata=zeros(Gwindow,round(fs*2));
drift=zeros(1,round(fs*2)*Gwindow);
fitted_art=zeros(channels,samples);
mPA=zeros(1,round(2*fs));
peakplot=zeros(1,samples);
mEye=eye(channels);
%Extract QRS events
%------------------
peakplot(QRSevents)=1;
sh=zeros(1,delay);
np=length(peakplot);
peakplot=[sh peakplot(1:np-delay)];
peakI=find(peakplot>0);
peakcount=length(peakI);
pa=peakcount;
%make filter
%-----------
a=[0 0 1 1];
f=[0 0.4/(fs/2) 0.9/(fs/2) 1];
ord=round(3*fs/0.5);
fwts=firls(ord,f,a);
%Artifact Subtraction
%---------------------
% for the first window/2 points use arthemitic mean for averageing.
% findg mean QRS peak-to-peak (R-to-R) interval
for ch=1:channels-2
if ch==1
%init waitbar
%------------
barth=5;
barth_step=barth;
Flag25=0;
Flag50=0;
Flag75=0;
fprintf('\nPulse artifact subtraction in progress...Please wait!\n');
end
RR=diff(peakI);
mRR=median(RR);
sRR=std(RR);
%PArange=round(1.25*(mRR+sRR)/2); % half RR
PArange=round(1.5*mRR/2); % half RR
midP=PArange+1;
% if ch==1
% while ((QRSevents(pa-1)+2*PArange-1) > samples)
% pa=pa-1;
% end
% steps=channels*pa;
% end
if ch==1
while (peakI(pa)+PArange > samples)
pa=pa-1;
end
steps=channels*pa;
peakcount=pa;
end
eegchan=filtfilt(fwts,1,double(EEG.data(ch,:)));
pcamat=zeros(pa-1,2*PArange+1);
dpcamat=pcamat;
for p=2:pa
pcamat(p-1,:)=eegchan(peakI(p)-PArange:peakI(p)+PArange);
end
pcamat=detrend(pcamat','constant')';
meaneffect=mean(pcamat);
dpcamat=detrend(pcamat,'constant');
[apc,ascore,asvar]=pca_calc(dpcamat');
papc=[meaneffect' ascore(:,1:pcs)];
try
pad_fit=double(papc)*(double(papc)\...
double(detrend(EEG.data(ch,peakI(1)-PArange:...
peakI(1)+PArange)','constant')));
fitted_art(ch,peakI(1)-PArange:peakI(1)+PArange)=...
pad_fit';
catch
end
%-----------------------------------------------------
for p=2:GHW+1
pad_fit=double(papc)*(double(papc)\...
double(detrend(EEG.data(ch,peakI(p)-PArange:...
peakI(p)+PArange)','constant')));
fitted_art(ch,peakI(p)-PArange:peakI(p)+PArange)=...
pad_fit';
%update bar
%----------
percentdone=floor(((ch-1)*pa+p)*100/steps);
if floor(percentdone)>=barth
if percentdone>=25 & Flag25==0
fprintf('25%% ')
Flag25=1;
elseif percentdone>=50 & Flag50==0
fprintf('50%% ')
Flag50=1;
elseif percentdone>=75 & Flag75==0
fprintf('75%% ')
Flag75=1;
elseif percentdone==100
fprintf('100%%\n')
else
fprintf('.')
end
while barth<=percentdone
barth=barth+barth_step;
end
if barth>100
barth=100;
end
end
end
%---------------- Processing of central data ---------------------
%cycle through peak artifacts identified by peakplot
rcount=GHW;
for p=GHW+2:peakcount-GHW-1
PreP=ceil((peakI(p)-peakI(p-1))/2);
PostP=ceil((peakI(p+1)-peakI(p))/2);
if PreP > PArange
PreP=PArange;
end
if PostP > PArange
PostP=PArange;
end
pad_fit=double(papc(midP-PArange:midP+PArange,:))*...
(double(papc(midP-PArange:midP+PArange,:))\...
double(detrend(EEG.data(ch,peakI(p)-PArange:...
peakI(p)+PArange)','constant')));
fitted_art(ch,peakI(p)-PreP:peakI(p)+PostP)=...
pad_fit(midP-PreP:midP+PostP)';
%update bar
%----------
percentdone=floor(((ch-1)*pa+p)*100/steps);
if floor(percentdone)>=barth
if percentdone>=25 & Flag25==0
fprintf('25%% ')
Flag25=1;
elseif percentdone>=50 & Flag50==0
fprintf('50%% ')
Flag50=1;
elseif percentdone>=75 & Flag75==0
fprintf('75%% ')
Flag75=1;
elseif percentdone==100
fprintf('100%%\n')
else
fprintf('.')
end
while barth<=percentdone
barth=barth+barth_step;
end
if barth>100
barth=100;
end
end
end
%---------------- Processing of last GHW peaks------------------
sectionPoints=samples-(peakI(peakcount-GHW)-PreP)+1;
for p=peakcount-GHW:peakcount
try
pad_fit=double(papc(midP-PArange:midP+PArange,:))*...
(double(papc(midP-PArange:midP+PArange,:))\...
double(detrend(EEG.data(ch,peakI(p)-PArange:...
peakI(p)+PArange)','constant')));
fitted_art(ch,peakI(p)-PreP:peakI(p)+PostP)=...
pad_fit(midP-PreP:midP+PostP)';
catch
end
%update bar
%----------
percentdone=floor(((ch-1)*pa+p)*100/steps);
if floor(percentdone)>=barth
if percentdone>=25 & Flag25==0
fprintf('25%% ')
Flag25=1;
elseif percentdone>=50 & Flag50==0
fprintf('50%% ')
Flag50=1;
elseif percentdone>=75 & Flag75==0
fprintf('75%% ')
Flag75=1;
elseif percentdone==100
fprintf('100%%\n')
else
fprintf('.')
end
while barth<=percentdone
barth=barth+barth_step;
end
if barth>100
barth=100;
end
end
end
end
EEG.data=EEG.data-fitted_art;
otherwise
%init
%-----
fs=EEG.srate;
[channels samples]=size(EEG.data);
%standard delay between QRS peak and artifact (allen,1998)
delay=round(0.21*fs);
Gwindow=20;
GHW=floor(Gwindow/2);
rcount=0;
firstplot=1;
% memory allocation
%------------------
avgdata=zeros(channels,round(fs*2),Gwindow);
drift=zeros(channels,round(fs*2)*Gwindow);
clean=zeros(channels,samples);
mPA=zeros(channels,round(2*fs));
mMag=zeros(channels,Gwindow);
peakplot=zeros(1,samples);
mEye=eye(channels);
%Extract QRS events
%------------------
peakplot(QRSevents)=1;
sh=zeros(1,delay);
np=length(peakplot);
peakplot=[sh peakplot(1:np-delay)];
peakI=find(peakplot>0);
peakcount=length(peakI);
%Artifact Subtraction
%---------------------
% for the first window/2 points use arthemitic mean for averageing.
% findg mean QRS peak-to-peak (R-to-R) interval
for p=2:GHW+1
RR(p-1)=peakI(p)-peakI(p-1);
end
mRR=mean(RR);
sRR=std(RR);
PArange=round((mRR+sRR)/2); % half RR
t=(0:peakI(GHW+1)+PArange-1)/fs;
% subtraction of low freq trend; could use detrend.m function instead
for n=1:channels
pdrift=polyfit(t,EEG.data(n,1:peakI(GHW+1)+PArange),1);
drift(n,1:peakI(GHW+1)+PArange)=polyval(pdrift,t);
end
EEG.data(:,1:peakI(GHW+1)+PArange)=...
EEG.data(:,1:peakI(GHW+1)+PArange)-...
drift(:,1:peakI(GHW+1)+PArange);
for p=2:GHW+1
avgdata(:,1:2*PArange+1,p-1)=...
EEG.data(:,peakI(p)-PArange:peakI(p)+PArange);
end
for chan =1:channels
avgdata(chan,:,:)=detrend(squeeze(avgdata(chan,:,:)),'constant');
end
mPA(:,1:2*PArange+1)=mean(avgdata(:,1:1+2*PArange,1:GHW),3);
if peakI(1) > PArange
alphaV=...
sum(detrend(EEG.data(:,peakI(1)-PArange:peakI(1)+PArange)','constant')'...
.*mPA(:,1:2*PArange+1),2)./...
sum(mPA(:,1:2*PArange+1).*mPA(:,1:2*PArange+1),2);
alphaD=diag(alphaV);
else
alphaV=sum(detrend(EEG.data(:,1:peakI(1)+PArange)','constant')'...
.*mPA(:,PArange-peakI(1)+2:2*PArange+1),2)./...
sum(mPA(:,PArange-peakI(1)+2:2*PArange+1).*...
mPA(:,PArange-peakI(1)+2:2*PArange+1),2);
alphaD=diag(alphaV);
end
EEG.data(:,1:peakI(GHW+1)+PArange)=...
EEG.data(:,1:peakI(GHW+1)+PArange)+...
drift(:,1:peakI(GHW+1)+PArange);
%init waitbar
%------------
barth=5;
barth_step=barth;
Flag25=0;
Flag50=0;
Flag75=0;
fprintf('\nPulse artifact subtraction in progress...Please wait!\n');
if peakI(1) > PArange
clean(:,peakI(1)-PArange:peakI(1)+PArange)=...
EEG.data(:,peakI(1)-PArange:peakI(1)+PArange)-alphaD*mPA(:,1:2*PArange+1);
startpoints=peakI(1)-PArange-1;
clean(:,1:startpoints)=EEG.data(:,1:startpoints);
else
clean(:,1:peakI(1)+PArange)=...
EEG.data(:,1:peakI(1)+PArange)-alphaD*mPA(:,PArange-peakI(1)+2:2*PArange+1);
end
%update bar
%----------
percentdone=floor(1*100/peakcount);
if floor(percentdone)>=barth
if percentdone>=25 & Flag25==0
fprintf('25%% ')
Flag25=1;
elseif percentdone>=50 & Flag50==0
fprintf('50%% ')
Flag50=1;
elseif percentdone>=75 & Flag75==0
fprintf('75%% ')
Flag75=1;
elseif percentdone==100
fprintf('100%%\n')
else
fprintf('.')
end
while barth<=percentdone
barth=barth+barth_step;
end
if barth>100
barth=100;
end
end
%-----------------------------------------------------
for p=2:GHW+1
alphaV=sum(EEG.data(:,peakI(p)-PArange:peakI(p)+PArange).*...
mPA(:,1:2*PArange+1),2)./...
sum(mPA(:,1:2*PArange+1).*mPA(:,1:2*PArange+1),2);
alphaD=diag(alphaV);
clean(:,peakI(p)-PArange:peakI(p)+PArange)=...
EEG.data(:,peakI(p)-PArange:peakI(p)+PArange)-...
alphaD*mPA(:,1:2*PArange+1);
%update bar
%----------
percentdone=floor(p*100/(peakcount-1));
if floor(percentdone)>=barth
if percentdone>=25 & Flag25==0
fprintf('25%% ')
Flag25=1;
elseif percentdone>=50 & Flag50==0
fprintf('50%% ')
Flag50=1;
elseif percentdone>=75 & Flag75==0
fprintf('75%% ')
Flag75=1;
elseif percentdone==100
fprintf('100%%\n')
else
fprintf('.')
end
while barth<=percentdone
barth=barth+barth_step;
end
if barth>100
barth=100;
end
end
end
%---------------- Processing of central data ---------------------
%cycle through peak artifacts identified by peakplot
for p=GHW+2:peakcount-GHW-1
PreP=ceil((peakI(p)-peakI(p-1))/2); % interval before peak artifact
PostP=ceil((peakI(p+1)-peakI(p))/2);% interval after peak artifact
sectionPoints=peakI(p+GHW)+PostP-(peakI(p-GHW)-PreP-1);
% subtract drift
t=(0:sectionPoints-1)/fs;
for n=1:channels
pdrift=...
polyfit(t,EEG.data(n,peakI(p-GHW)-PreP:peakI(p+GHW)+PostP),1);
drift(n,1:sectionPoints)=polyval(pdrift,t);
end
EEG.data(:,peakI(p-GHW)-PreP:peakI(p+GHW)+PostP)=...
EEG.data(:,peakI(p-GHW)-PreP:peakI(p+GHW)+PostP)-...
drift(:,1:sectionPoints);
rr=1;
for r=p-GHW:p+GHW
mMag(:,rr)=mean(abs(EEG.data(:,peakI(r)-PreP:peakI(r)+PostP)),2);
rr=rr+1;
end
minMag=min(mMag,[],2);
%only count those EEG data with no motion or line artifact by rejecting
%excessively large variations. Use channel 1 as ref (Cz).
oldavgdata=avgdata;
oldrcount=rcount;
rcount=0;
for r=p-GHW:p+GHW
if (mean(abs(EEG.data(1,peakI(r)-PreP:peakI(r)+PostP))) ...
< 4*minMag(1) ) ...
& (max(abs(EEG.data(1,peakI(r)-PreP:peakI(r)+PostP)))...
< 2* 175)
avgdata(:,1:PreP+PostP+1,rcount+1)=...
EEG.data(:,peakI(r)-PreP:peakI(r)+PostP);
rcount=rcount+1;
end
end
for chan =1:channels
avgdata(chan,:,:)=detrend(squeeze(avgdata(chan,:,:)),'constant');
end
% rcount
%if more than 8 good data sections found then do averaging, else use
%previous.
if rcount > 5
switch method
case 'gmean'
if p==peakcount-GHW-1
mPA=mean(avgdata(:,:,1:rcount),3);
elseif rcount==(2*GHW+1)
G=gausswin(rcount);
G=G/sum(G);
for r=1:rcount
avgdata(:,:,r)=avgdata(:,:,r)*G(r);
end
mPA=sum(avgdata(:,:,1:rcount),3);
else
mPA=median(avgdata(:,:,1:rcount),3);
end
case 'mean'
mPA=mean(avgdata(:,:,1:rcount),3);
case 'median'
mPA=median(avgdata(:,:,1:rcount),3);
end
else
switch method
case 'mean'
mPA=mean(oldavgdata(:,:,1:oldrcount),3);
case 'median'
mPA=median(oldavgdata(:,:,1:oldrcount),3);
case 'gmean'
mPA=mean(oldavgdata(:,:,1:oldrcount),3);
end
end
alphaV=sum(detrend(EEG.data(:,peakI(p)-PreP:peakI(p)+PostP)','constant')'.*...
mPA(:,1:PreP+PostP+1),2)./...
sum(mPA(:,1:PreP+PostP+1).*mPA(:,1:PreP+PostP+1),2);
alphaD=diag(alphaV);
EEG.data(:,peakI(p-GHW)-PreP:peakI(p+GHW)+PostP)=...
EEG.data(:,peakI(p-GHW)-PreP:peakI(p+GHW)+PostP)+...
drift(:,1:sectionPoints);
clean(:,peakI(p)-PreP:peakI(p)+PostP)=...
EEG.data(:,peakI(p)-PreP:peakI(p)+PostP)-...
alphaD*mPA(:,1:PreP+PostP+1);
%update bar
%----------
percentdone=floor(p*100/(peakcount-1));
if floor(percentdone)>=barth
if percentdone>=25 & Flag25==0
fprintf('25%% ')
Flag25=1;
elseif percentdone>=50 & Flag50==0
fprintf('50%% ')
Flag50=1;
elseif percentdone>=75 & Flag75==0
fprintf('75%% ')
Flag75=1;
elseif percentdone==100
fprintf('100%%\n')
else
fprintf('.')
end
while barth<=percentdone
barth=barth+barth_step;
end
if barth>100
barth=100;
end
end
end
%---------------- Processing of last GHW peaks------------------
sectionPoints=samples-(peakI(peakcount-GHW)-PreP)+1;
if samples-peakI(peakcount) >= PostP
alphaV=sum(detrend(EEG.data(:,peakI(peakcount)-...
PreP:peakI(peakcount)+PostP)','constant')'.*...
mPA(:,1:PreP+PostP+1),2)./...
sum(mPA(:,1:PreP+PostP+1).*mPA(:,1:PreP+PostP+1),2);
alphaD=diag(alphaV);
clean(:,peakI(peakcount)-PreP:peakI(peakcount)+PostP)=...
EEG.data(:,peakI(peakcount)-PreP:peakI(peakcount)+PostP)- ...
alphaD*mPA(:,1:PreP+PostP+1);
endpoints=samples-peakI(peakcount)-PostP;
clean(:,samples-endpoints+1:samples)=...
EEG.data(:,samples-endpoints+1:samples);
else
alphaV=sum(detrend(EEG.data(:,peakI(peakcount)-PreP:samples)','constant')'.*...
mPA(:,1:samples-(peakI(peakcount)-PreP)+1),2)./...
sum(mPA(:,1:samples-(peakI(peakcount)-PreP)+1).*...
mPA(:,1:samples-(peakI(peakcount)-PreP)+1),2);
alphaD=diag(alphaV);
clean(:,peakI(peakcount)-PreP:samples)=...
EEG.data(:,peakI(peakcount)-PreP:samples)-...
alphaD*mPA(:,1:samples-(peakI(peakcount)-PreP)+1);
end
for p=peakcount-GHW:peakcount-1
alphaV=sum(detrend(EEG.data(:,peakI(p)-PreP:peakI(p)+PostP)','constant')'.*...
mPA(:,1:PreP+PostP+1),2)./...
sum(mPA(:,1:PreP+PostP+1).*mPA(:,1:PreP+PostP+1),2);
alphaD=diag(alphaV);
clean(:,peakI(p)-PreP:peakI(p)+PostP)=...
EEG.data(:,peakI(p)-PreP:peakI(p)+PostP)-...
alphaD*mPA(:,1:PreP+PostP+1);
%update bar
%----------
percentdone=floor(p*100/(peakcount-1));
if floor(percentdone)>=barth
if percentdone>=25 & Flag25==0
fprintf('25%% ')
Flag25=1;
elseif percentdone>=50 & Flag50==0
fprintf('50%% ')
Flag50=1;
elseif percentdone>=75 & Flag75==0
fprintf('75%% ')
Flag75=1;
elseif percentdone==100
fprintf('100%%\n')
else
fprintf('.')
end
while barth<=percentdone
barth=barth+barth_step;
end
if barth>100
barth=100;
end
end
end
EEG.data=clean;
end
return;