-
Notifications
You must be signed in to change notification settings - Fork 1
/
baseline.fillPeaks.R
228 lines (193 loc) · 6.13 KB
/
baseline.fillPeaks.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
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
## $Id: baseline.fillPeaks.R 170 2011-01-03 20:38:25Z bhm $
#' @title Fill peaks
#'
#' @description An iterative algorithm using suppression of baseline by means in local
#' windows
#'
#' @details In local windows of buckets the minimum of the mean and the previous
#' iteration is chosen as the new baseline
#'
#' @aliases baseline.fillPeaks fillPeaks
#' @param spectra Matrix with spectra in rows
#' @param lambda 2nd derivative penalty for primary smoothing
#' @param hwi Half width of local windows
#' @param it Number of iterations in suppression loop
#' @param int Number of buckets to divide spectra into
#' @return \item{baseline }{Matrix of baselines corresponding to spectra
#' \code{spectra}} \item{corrected }{Matrix of baseline corrected spectra}
#' @author Kristian Hovde Liland and Bjørn-Helge Mevik
#' @references Kristian Hovde Liland, 4S Peak Filling - baseline estimation by
#' iterative mean suppression, MethodsX 2015
#' @keywords baseline spectra
#' @examples
#'
#' data(milk)
#' bc.fillPeaks <- baseline(milk$spectra[1,, drop=FALSE], lambda=6,
#' hwi=50, it=10, int=2000, method='fillPeaks')
#' \dontrun{
#' plot(bc.fillPeaks)
#' }
#' @export
baseline.fillPeaks <- function(spectra, lambda, hwi, it, int){
## Iterative baseline correction algorithm based on mean suppression
## By Kristian Hovde Liland
# INPUT:
# spectra - rows of spectra
# lambda - 2nd derivative penalty of primary smoothing
# hwi - half width of local window
# it - number of iterations in suppression loop
# int - number of bucket intervals to make of data (or vektor of bucket boundaries)
#
# OUTPUT:
# baseline - estimated baseline
# corrected - baseline corrected spectra
# Initialization
np <- dim(spectra)
if (missing(int)) int <- np[1]-1
baseline <- matrix(0,np[1],np[2])
# Empty matrix (5 x p)
W <- matrix(0.0, 5, np[2])
# Diagonal sparse matrix in compact format (5 x p)
DD <- .create_DD(np[2])*10^lambda
DD[3,] <- DD[3,] + 1
# ------==== S1: Smoothing ====------
Yorig <- spectra
if(lambda > 0){
spectra <- t(Solve.banded(DD,2,2,t(spectra)))
}
# Exponential decrease in interval width
if(it != 1){
d1 <- log10(hwi)
d2 <- 0
w <- ceiling((10)^c(d1+(0:(it-2))*(d2-d1)/(floor(it)-1), d2))
} else {
w <- hwi
}
# Compute bucket locations
if(length(int)==1){
lims <- seq(from = 1, to = np[2], length = int + 1)
} else {
lims <- int
int <- length(int)-1
}
lefts <- ceiling(lims[-(int+1)])
rights <- floor(lims[-1])
minip <- round((lefts + rights)/2)
# Iterate through spectra
for(s in 1:np[1]){
# ------==== S2: Subsampling ====------
xx <- numeric(int)
for (i in 1:int) xx[i] <- min(spectra[s,lefts[i]:rights[i]])
# ------==== S3: Suppression ====------
for(k in 1:it){
# Current window width
w0 <- w[k]
# Point-wise iteration to the right
for(i in 2:(int-1)){
# Interval cut-off close to edges
v <- min(c(i-1,w0,int-i))
# Baseline suppression
a <- mean(xx[(i-v):(i+v)])
xx[i] <- min(a,xx[i])
}
# Point-wise iteration to the left
for(i in 2:(int-1)){
j <- int-i+1
# Interval cut-off close to edges
v <- min(c(i-1,w0,int-i))
# Baseline suppression
a <- mean(xx[(j-v):(j+v)])
xx[j] <- min(a,xx[j])
}
}
# Prepare minimum vector
minip[1] <- 1
minip[int] <- np[2]
# ------==== S4: Stretch ====------
xxx <- approx(minip, xx, 1:np[2])$y
baseline[s,] <- xxx
}
list(baseline = baseline, corrected = Yorig - baseline)
}
.baseline.fillPeaks_old <- function(spectra, lambda, hwi, it, int){
## Iterative baseline correction algorithm based on mean suppression
## By Kristian Hovde Liland
# INPUT:
# spectra - rows of spectra
# lambda - 2nd derivative penalty of primary smoothing
# hwi - half width of local window
# it - number of iterations in suppression loop
# int - number of bucket intervals to make of data (or vektor of bucket boundaries)
#
# OUTPUT:
# baseline - estimated baseline
# corrected - baseline corrected spectra
# Initialization
np <- dim(spectra)
if (missing(int)) int <- np[1]-1
baseline <- matrix(0,np[1],np[2])
# Sparse empty matrix (m x m)
speye <- as.matrix.csr(0,np[2],np[2])
# Diagonal sparse matrix (m x m)
diag(speye) <- 1
D <- diff(speye,differences=2)
# ------==== S1: Smoothing ====------
Yorig <- spectra
if(lambda > 0){
U <- chol(speye+10^lambda*t(D)%*%D)
spectra <- t(backsolve(U, t(spectra)))
}
# Exponential decrease in interval width
if(it != 1){
d1 <- log10(hwi)
d2 <- 0
w <- ceiling((10)^c(d1+(0:(it-2))*(d2-d1)/(floor(it)-1), d2))
} else {
w <- hwi
}
# Compute bucket locations
if(length(int)==1){
lims <- seq(from = 1, to = np[2], length = int + 1)
} else {
lims <- int
int <- length(int)-1
}
lefts <- ceiling(lims[-(int+1)])
rights <- floor(lims[-1])
minip <- round((lefts + rights)/2)
# Iterate through spectra
for(s in 1:np[1]){
# ------==== S2: Subsampling ====------
xx <- numeric(int)
for (i in 1:int) xx[i] <- min(spectra[s,lefts[i]:rights[i]])
# ------==== S3: Suppression ====------
for(k in 1:it){
# Current window width
w0 <- w[k]
# Point-wise iteration to the right
for(i in 2:(int-1)){
# Interval cut-off close to edges
v <- min(c(i-1,w0,int-i))
# Baseline suppression
a <- mean(xx[(i-v):(i+v)])
xx[i] <- min(a,xx[i])
}
# Point-wise iteration to the left
for(i in 2:(int-1)){
j <- int-i+1
# Interval cut-off close to edges
v <- min(c(i-1,w0,int-i))
# Baseline suppression
a <- mean(xx[(j-v):(j+v)])
xx[j] <- min(a,xx[j])
}
}
# Prepare minimum vector
minip[1] <- 1
minip[int] <- np[2]
# ------==== S4: Stretch ====------
xxx <- approx(minip, xx, 1:np[2])$y
baseline[s,] <- xxx
}
list(baseline = baseline, corrected = Yorig - baseline)
}