-
Notifications
You must be signed in to change notification settings - Fork 1
/
figures-mean-erp.r
126 lines (102 loc) · 3.95 KB
/
figures-mean-erp.r
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
### Plot per subject figures with averaged ERP for each stimulus
###
### The plotting is done only for the the experiment and the electrode
### selected in the cross-validation procedure.
### This program is part of RoLDSIS
###
### Copyright (C) 2020 Rafael Laboissière
### Copyright (C) 2020 Adrielle de Carvalho Santana
### Copyright (C) 2020 Hani Camille Yehia
###
### 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 3 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, see <http://www.gnu.org/licenses/>.
### * Load local libraries
source ("paths.r")
source ("remove-spikes.r")
### * Load system package
load.pkgs ("wavelets")
### * Plotting parameters
vdist <- 10
vscale.x <- 0.23
vscale.x.width <- 0.01
vscale.y <- -42
vscale.y.range <- 10
### * File stem for the plotting
file.stem <- sprintf ("erp-%s-%s-", cv.exp.feature, cv.exp.type)
### * Time basis for plotting
t <- seq (0, by = 1 / eeg.sampfreq, length.out = dwt.length)
### * Loop over the cohort
for (subj in cohort) {
## ** Load the subject data
load (cv.filename (cv.exp.feature, cv.exp.type, subj))
## ** Open PDF file
pdf (file = file.path (figures.dir,
sprintf ("%sS%02d.pdf", file.stem, subj)),
width = 7, height = 4)
par (mar = c (5, 0.1, 0.1, 0.1))
## ** Get the averaged signals for the current subject
x <- dwt.coefs.cv$mean
## ** Low pass filtering for detection of P2 peak
x.lpf <- x
for (i in seq (1, 5)) {
d <- dwt (x.lpf [i, ])
## Eliminate bands W1 to W5
for (j in seq (1, 5))
d@W [[j]] <- 0 * d@W [[j]]
x.lpf [i, ] <- idwt (d)
}
## ** Get extension of the plot
max.v <- max (x [1, ])
min.v <- min (min (x [5, ] - vdist * 4), vscale.y - vscale.y.range)
## ** Loop over the stimuli
for (i in seq (1, 5)) {
## *** Get vertical shift
v.shift <- -vdist * (i - 1)
## *** Plot the signal. Stimuli #1 starts the plot
if (i == 1)
plot (t, remove.spikes (x [1, ]), type = "l", bty = "n",
col = stim.cols [1], ylim = c (min.v, max.v),
xlim = c (min (t) - 0.05, max (t)),
xlab = "time (s)", yaxt = "n", ylab = "")
else
lines (t, remove.spikes (x [i, ])
+ v.shift, col = stim.cols [i])
## *** Plot markers for P1 peak
## This is experimental code and has been commented out.
## mx <- max (x.lpf [i, ])
## idx <- which.max (x.lpf [i, ])
## lines (rep (t [idx], 2),
## v.shift + mx + c (1.5, 3.5) * vscale.y.range / 10,
## lwd = 2)
}
## ** Draw stimuli number
points (rep (-0.025, 5), seq (0, -4) * vdist, cex = 4.5)
text (rep (-0.025, 5), seq (0, -4) * vdist, adj = c (0.5, 0.5),
labels = seq (1, 5), cex = 1.5)
## ** Draw vertical scale
lines (vscale.x + c (0, -rep (vscale.x.width, 2), 0),
vscale.y + c (0, 0, -rep (vscale.y.range, 2)))
text (vscale.x, vscale.y - vscale.y.range / 2, adj = c (0, 0.5),
bquote (.(vscale.y.range) * mu * V))
## ** Close the PDF file
dummy <- dev.off ()
## ** Progress meter
cat (sprintf ("\rSubject %2d", subj))
flush (stdout ())
}
### * Clean the progress meter
cat ("\n")
flush (stdout ())
### * Compose the combo file with all subjects
system (sprintf ("pdftk %s/%sS*.pdf cat output %s/%sall.pdf",
figures.dir, file.stem, figures.dir, file.stem))