Debian Codegraph @

biosig4c++/R/loadgdf.r

raw symbols SCIP
1 #
2 # Copyright (C) 2020,2021 Alois Schloegl <[email protected]>
3 # This file is part of the "BioSig for C/C++" repository
4 # (biosig4c++/libbiosig) at http://biosig.sf.net/
5 #
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 3
9 # of the License, or (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18
19
20 # References
21 # https://www.rdocumentation.org/packages/base/versions/3.6.2/topics/connections
22 # https://www.rdocumentation.org/packages/base/versions/3.6.2/topics/seek
23 # https://www.rdocumentation.org/packages/base/versions/3.6.2/topics/readBin
24 # https://arxiv.org/abs/cs/0608052
25
26 setClass(
27 "HeaderType",
28 slots = list(
29 # H1
30 VersionID = "numeric",
31 # HeadLen = "numeric",
32 EventTablePos = "numeric",
33 NS = "numeric", # number of signals (i.e. channels)
34 NRec = "numeric", # number of records (i.e. blocks)
35 Dur = "numeric", # duration of a record
36 SampleRate = "numeric", # sampling rate = SPR/Dur
37 SPR = "numeric", # samples per record
38 BPB = "numeric", # bytes per block
39 T0 = "numeric", # Startdate and time
40 Birthday = "numeric", # Birthday, date and time
41 tzmin = "numeric", # timezone information, number of minutes east of UTC
42 # H2
43 Label = "character", # channel label
44 Transducer = "character", # transducer
45 PhysDimCode = "numeric", # physical encoding
46 PhysMin = "numeric", # physical minimum
47 PhysMax = "numeric", # physical maximum
48 DigMin = "numeric", # digital minimum
49 DigMax = "numeric", # digital maximum
50 Toffset = "numeric", # time offset of each channel
51 LowPass = "numeric", # lowpass filter
52 HighPass = "numeric", # highpass filter
53 Notch = "numeric", # notch filter
54 spr = "numeric", # samples per record in each channel
55 G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,56, 0, 0, 0, 0, 0, 0, 0,64,
84 0, 0, 0, 0, 0, 0, 0,72, 0, 0, 0, 0, 0, 0, 0,80,
85 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
86 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
87 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
88 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
89 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
90 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
91 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
92 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
93 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
94 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
95 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
96 0, 0, 0, 0, 0, 0, 0, 8, 0,10, 0,12, 0, 0, 0,16,
97 0, 0, 0, 0, 0, 0, 0,24, 0, 0, 0, 0, 0, 0, 0,32,
98 0, 0, 0, 0, 0, 0, 0,40, 0, 0, 0, 0, 0, 0, 0,48,
99 0, 0, 0, 0, 0, 0, 0,56, 0, 0, 0, 0, 0, 0, 0,64,
100 0, 0, 0, 0, 0, 0, 0,72, 0, 0, 0, 0, 0, 0, 0,80
101 ));
102
103
104 "%gcd%" <- function(u, v) {ifelse(u %% v != 0, v %gcd% (u%%v), v)}
105 "%lcm%" <- function(u, v) { abs(u*v)/(u %gcd% v)}
106 skipbytes <- function(fid, nbytes) { readBin(fid, "int", size=1, n=nbytes) }
107
108 #' Loads biomedical signal data in GDF format
109 #'
110 #' @param filename name of the gdf data file
111 #' @param channel indicates which channel(s) should be loaded, (default 0: indicaes all channels)
112 #' @return HeaderType data HDR
113 #' HDR@data is a matrix containing sample values with HDR@NS columns (i.e. channels)
114 #' and HDR@NRec*HDR@SPR rows (sampling time points)
115 #' HDR@SampleRate is the sampling rate
116 #' HDR@Label contain the channel labels
117 #'
118 #' Usage:
119 #' source('loadgdf.r')
120 #' HDR=loadgdf(filename) # loads a biosig file
121 #' uses biosig2gdf to convert and load any (supported) format
122 #' HDR@Label # shows channel names
123 #' HDR=loadgdf(..., 1) # loads 1st channel
124 #' HDR=loadgdf(..., 2) # loads 2nd channel
125 #' HDR=loadgdf(..., c(1,2)) # loads 1st and 2nd channel
126 #' HDR@data contain the data samples
127
128 loadgdf <- function(filename, chan=0) {
129 TMPFILE <- tempfile("gdf", fileext='.gdf')
130 myDir <- getSrcDirectory(function(x) {x})
131 if (.Platform$OS.type=="windows") {
132 converter = file.path(myDir, "biosig2gdf.exe")
133 } else {
134 converter = "biosig2gdf"
135 }
136 status<-system2(converter, args=paste(filename, TMPFILE, sep=" "))
137 if (status) {
138 print(status)
139 print(errmsg)
140 }
141
142 fid <- file(TMPFILE, "rb")
143 if (!isOpen(fid)) {
144 stop("Cannot open file or pipe")
145 }
146 HDR <- new("HeaderType")
147 ### read H1 - fixed header ###
148 HDR@VersionID <- readBin(fid, "int", size=1, n=8)
149 if (!all(HDR@VersionID[1:4] == c(0x47,0x44,0x46,0x20)))
150 stop("This is not a GDF file - convert first to GDF - e.g. with save2gdf command line tool")
151
152 skipbytes(fid, nbytes=168-8)
153 t <- readBin(fid, "int", size=4, n=2, endian="little")
154 HDR@T0 <- t[2]+t[1]*(2^-32)
155 t <- readBin(fid, "int", size=4, n=2, endian="little")
156 HDR@Birthday <- t[2]+t[1]*(2^-32)
157 HeadLen <- readBin(fid, "int", size=2, signed=FALSE, endian="little")*256
158
159 skipbytes(fid, nbytes=236-184-2)
160 HDR@NRec <- readBin(fid, "int", size=8, endian="little")
161 HDR@Dur <- readBin(fid, "double", n=1, size=8, endian="little")
162 HDR@NS <- readBin(fid, "int", size=2, signed=FALSE, endian="little")
163 HDR@tzmin<- readBin(fid, "int", size=2, endian="little") # timezone in minutes east of UTC
164 HDR@SPR <- 1
165 if (any(chan==0)) chan <- c(1:HDR@NS)
166
167 ### read H2 - variable header, channel information ###
168 for (k in c(1:HDR@NS)) {
169 HDR@Label[k] <- readChar(fid, nchars=16, useBytes=TRUE)
170 }
171 for (k in c(1:HDR@NS)) {
172 HDR@Transducer[k] <- readChar(fid, nchars=80, useBytes=TRUE)
173 }
174 skipbytes(fid, nbytes=(102-96)*HDR@NS)
175 HDR@PhysDimCode<- readBin(fid, "int", size=2, n=HDR@NS, endian="little")
176 HDR@PhysMin <- readBin(fid, "double", size=8, n=HDR@NS, endian="little")
177 HDR@PhysMax <- readBin(fid, "double", size=8, n=HDR@NS, endian="little")
178 HDR@DigMin <- readBin(fid, "double", size=8, n=HDR@NS, endian="little")
179 HDR@DigMax <- readBin(fid, "double", size=8, n=HDR@NS, endian="little")
180
181 skipbytes(fid, nbytes=(200-102-34)*HDR@NS)
182 HDR@Toffset <- readBin(fid, "double", size=4, n=HDR@NS, endian="little")
183 HDR@LowPass <- readBin(fid, "double", size=4, n=HDR@NS, endian="little")
184 HDR@HighPass<- readBin(fid, "double", size=4, n=HDR@NS, endian="little")
185 HDR@Notch <- readBin(fid, "double", size=4, n=HDR@NS, endian="little")
186 HDR@spr <- readBin(fid, "int", size=4, n=HDR@NS, endian="little")
187 for (v in unique(HDR@spr[chan])) HDR@SPR <- (HDR@SPR %lcm% v)
188 HDR@SampleRate <- HDR@SPR / HDR@Dur
189
190 HDR@GDFTYP <- readBin(fid, "int", size=4, n=HDR@NS, endian="little")
191 HDR@BPB <- sum(GDFTYPE_BITS[HDR@GDFTYP+1] * HDR@spr)/8
192 bi <- c(0, cumsum(ceiling(GDFTYPE_BITS[HDR@GDFTYP+1]/8) * HDR@spr))
193 spb <- c(0, cumsum(HDR@spr))
194
195 Cal <- (HDR@PhysMax - HDR@PhysMin) / (HDR@DigMax - HDR@DigMin)
196 Off <- HDR@PhysMin - Cal * HDR@DigMin
197 HDR@EventTablePos <- HeadLen + HDR@BPB * HDR@NRec
198
199 ### optional
200 skipbytes(fid, nbytes=HeadLen-256-(200+24)*HDR@NS)
201 if (all(HDR@GDFTYP==0)) {
202 data <- readBin(fid, "int", size=1, n=HDR@BPB*HDR@NRec, endian="little")
203 } else if (all(HDR@GDFTYP==1)) {
204 data <- readBin(fid, "int", size=1, n=HDR@BPB*HDR@NRec, endian="little")
205 } else if (all(HDR@GDFTYP==2)) {
206 data <- readBin(fid, "int", size=1, signed=FALSE, n=HDR@BPB*HDR@NRec, endian="little")
207 } else if (all(HDR@GDFTYP==3)) {
208 data <- readBin(fid, "int", size=2, n=HDR@BPB*HDR@NRec/2, endian="little")
209 } else if (all(HDR@GDFTYP==4)) {
210 data <- readBin(fid, "int", size=2, signed=FALSE, n=HDR@BPB*HDR@NRec/2, endian="little")
211 } else if (all(HDR@GDFTYP==5)) {
212 data <- readBin(fid, "int", size=4, n=HDR@BPB*HDR@NRec/4, endian="little")
213 } else if (all(HDR@GDFTYP==6)) {
214 data <- readBin(fid, "int", size=2, signed=FALSE, n=HDR@BPB*HDR@NRec, endian="little")
215 L=1:(length(data)/2);
216 data = data[L*2-1] + 65536L * data[L*2]
217 } else if (all(HDR@GDFTYP==7)) {
218 data <- readBin(fid, "int", size=4, n=HDR@BPB*HDR@NRec/4, endian="little")
219 } else if (all(HDR@GDFTYP==8)) {
220 data <- readBin(fid, "int", size=2, signed=FALSE, n=HDR@BPB*HDR@NRec/2, endian="little")
221 L=1:(length(data)/4);
222 data = data[L*4-3] + 65536L*(data[L*4-2] + 65536L*(data[L*4-1] + 65536L*data[L*4]))
223 } else if (all(HDR@GDFTYP==16)) {
224 data <- readBin(fid, "double", size=4, n = HDR@BPB*HDR@NRec/4, endian="little")
225 } else if (all(HDR@GDFTYP==17)) {
226 data <- readBin(fid, "double", size=8, n = HDR@BPB*HDR@NRec/8, endian="little")
227 } else if (all(HDR@GDFTYP==18)) {
228 data <- readBin(fid, "double", size=16, n = HDR@BPB*HDR@NRec/16, endian="little")
229 } else if (all(HDR@GDFTYP==(255+24))) {
230 data <- readBin(fid, "int", size=1, signed=FALSE, n = HDR@BPB*HDR@NRec, endian="little")
231 L = 1:(length(data) %/% 3)
232 data = data[L*3-2] + 256L*(data[L*3-1] + 256L*data[L*3])
233 data = data - c(data>=2^23)*2^24
234 } else if (all(HDR@GDFTYP==(511+24))) {
235 data <- readBin(fid, "int", size=1, signed=FALSE, n = HDR@BPB*HDR@NRec, endian="little")
236 L = 1:(length(data) %/% 3)
237 data = data[L*3-2] + 256L*(data[L*3-1] + 256L*data[L*3])
238 } else {
239 stop("channels have different data types - this is not supported yet.")
240 }
241
242 k = 0;
243 if (all(HDR@spr[chan[1]]==HDR@spr[chan])) {
244 SPR <- HDR@spr[chan[1]]
245 d <- matrix(data, nrow=sum(HDR@spr), byrow=FALSE)
246 data <- matrix(c(1:(HDR@NRec * SPR * length(chan))), ncol=length(chan))
247 k = 0
248 for (ch in chan) {
249 k = k+1;
250 data[,k] = matrix(d[spb[ch]+c(1:HDR@spr[ch]),], ncol=1, byrow=FALSE) * Cal[ch] + Off[ch]
251 }
252 } else {
253 stop("this file is currently not supported (channels have different sampling rates)")
254 }
255 # H3 <- readBin(fid, "raw", n=NS, size=256);
256 ## seek EventTablePos
257
258 HDR@data <- data
259 close(fid)
260 file.remove(TMPFILE)
261 return(HDR)
262 }
263
264