-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathremove-spikes.r
More file actions
64 lines (59 loc) · 2.09 KB
/
remove-spikes.r
File metadata and controls
64 lines (59 loc) · 2.09 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
### Remove spikes from the averaged evoked repsonses, based on the
### elimination of outlier DWT components.
### 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 the local library
source ("paths.r")
### * Load the system packages
load.pkgs (c ("EnvStats", "wavelets"))
### * Find outliers using Rosner's generalized extreme Studentized deviate test
which.outliers <- function (x) {
## ** Start with one possible outlier
k <- 1
## ** Grow set until the k-th sample is not an outlier
while (TRUE) {
t <- rosnerTest (x, k = k)
if (! t$all.stats$Outlier [k])
break
k <- k + 1
}
## ** Return value
if (k == 1)
return (NULL)
else
return (t$all.stats$Obs.Num [1 : (k - 1)])
}
### * Remove spikes and return "clean" signal
remove.spikes <- function (x, max.level = NULL) {
## ** Comoute the DWT
d <- dwt (x)
## ** Use all available W bands of the DWT, if max level is not specified
if (is.null (max.level))
max.level <- length (d@W)
## ** Iterate over the W bands
for (i in seq (1, max.level)) {
## *** Replace outliers with the median value of the band
w <- d@W [[i]]
idx <- which.outliers (w)
w [idx] <- median (w)
d@W [[i]] <- w
}
## ** Return value
return (idwt (d))
}