-
Notifications
You must be signed in to change notification settings - Fork 0
/
HRV_parse.R
131 lines (118 loc) · 5.47 KB
/
HRV_parse.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
#parse arguments
args = commandArgs(trailingOnly=TRUE)
if (length(args)!=1) {
args = c("elite")
}
#install required packages
list.of.packages <- c("RHRV")
new.packages <- list.of.packages[!(list.of.packages %in% installed.packages()[,"Package"])]
if(length(new.packages)) install.packages(new.packages, dependencies = TRUE)
library(RHRV)
if (args[1] == "HRmonitor") {
fli <- list.files(path = ".", pattern = ".*(hrv).*(csv)$")
lenfli <- length(fli)
#use only the first measurement of the day
odate <- c(0)
vector <- c()
fdate <- gsub('.{9}$', '',gsub('^[a-zA-Z_]+', '',gsub('.{4}$', '',fli))) #get date
for (i in 1:lenfli) {
if (fdate[i]>odate) {
vector <- c(vector,i)
odate <- fdate[i]
}
}
lenvector <- length(vector)
mat <- matrix(0, lenvector, 15)
colnames(mat) <- c("date","time","eHRV","HR","SDNN","pNN50","SDSD","rMSSD","IRRR","MADRR","TINN","HRVi","LF","HF","LFHF")
# calculate time-domain and frequency-domain statistics on the filtered RR data
for (i in 1:lenvector) {
strip <- c()
con = file(fli[vector[i]], "r")
while ( TRUE ) {
line = readLines(con, n = 1)
if ( line == "" ) {
break
}
strip <- c(strip,strtoi(line))
}
close(con)
flitxt = paste(gsub('.{4}$', '',fli[vector[i]]),".dat")
write.table(strip, file=flitxt, col.names=FALSE, row.names = FALSE)
hrv.data = CreateHRVData()
hrv.data = SetVerbose(hrv.data,FALSE)
hrv.data = LoadBeatRR(hrv.data, flitxt,".",scale=0.001)
hrv.data = BuildNIHR(hrv.data)
hrv.data = FilterNIHR(hrv.data)
hrv.data = SetVerbose(hrv.data,TRUE)
hrv.data = CreateTimeAnalysis(hrv.data, size=360)
hrv.data = InterpolateNIHR (hrv.data, freqhr = 1)
hrv.data = CreateFreqAnalysis(hrv.data)
hrv.data = SetVerbose(hrv.data,TRUE)
hrv.data = CalculatePowerBand(hrv.data, indexFreqAnalysis= 1, size = 120, shift = 10, ULFmin = 0, ULFmax = 0.0033, VLFmin = 0.0033, VLFmax = 0.04, LFmin = 0.04, LFmax = 0.15, HFmin = 0.15, HFmax = 0.4 ) #modified RHRV default according to doi: 10.3389/fpubh.2017.00258
datetime <- gsub('^[a-zA-Z_]+', '',gsub('.{4}$', '',fli[vector[i]]))
mat[i,1] <- gsub('.{9}$', '',datetime)
mat[i,2] <- gsub('[.]',':',gsub('.{11}', '',datetime))
mat[i,3] <- log(hrv.data$TimeAnalysis[[1]]$rMSSD)/6.5*100
mat[i,4] <- mean(hrv.data$Beat$niHR)
mat[i,5] <- hrv.data$TimeAnalysis[[1]]$SDNN
mat[i,6] <- hrv.data$TimeAnalysis[[1]]$pNN50
mat[i,7] <- hrv.data$TimeAnalysis[[1]]$SDSD
mat[i,8] <- hrv.data$TimeAnalysis[[1]]$rMSSD
mat[i,9] <- hrv.data$TimeAnalysis[[1]]$IRRR
mat[i,10] <- hrv.data$TimeAnalysis[[1]]$MADRR
mat[i,11] <- hrv.data$TimeAnalysis[[1]]$TINN
mat[i,12] <- hrv.data$TimeAnalysis[[1]]$HRVi
mat[i,13] <- mean(hrv.data$FreqAnalysis[[1]]$LF)
mat[i,14] <- mean(hrv.data$FreqAnalysis[[1]]$HF)
mat[i,15] <- mean(hrv.data$FreqAnalysis[[1]]$LFHF)
file.remove(flitxt)
}
} else {
#load file into data frame
fli <- list.files(path = ".", pattern = "txt")
lenfli <- length(fli)
#use only the first measurement of the day
odate <- c(0)
vector <- c()
fdate <- gsub('.{9}$', '',gsub('.{4}$', '',fli)) #get date
for (i in 1:lenfli) {
if (fdate[i]>odate) {
vector <- c(vector,i)
odate <- fdate[i]
}
}
lenvector <- length(vector)
mat <- matrix(0, lenvector, 15)
colnames(mat) <- c("date","time","eHRV","HR","SDNN","pNN50","SDSD","rMSSD","IRRR","MADRR","TINN","HRVi","LF","HF","LFHF")
# calculate time-domain and frequency-domain statistics on the filtered RR data
for (i in 1:lenvector) {
hrv.data = CreateHRVData()
hrv.data = SetVerbose(hrv.data,FALSE)
hrv.data = LoadBeatRR(hrv.data, fli[vector[i]],".",scale=0.001)
hrv.data = BuildNIHR(hrv.data)
hrv.data = FilterNIHR(hrv.data)
hrv.data = SetVerbose(hrv.data,TRUE)
hrv.data = CreateTimeAnalysis(hrv.data)
hrv.data = InterpolateNIHR (hrv.data, freqhr = 1)
hrv.data = CreateFreqAnalysis(hrv.data)
hrv.data = SetVerbose(hrv.data,TRUE)
hrv.data = CalculatePowerBand(hrv.data, indexFreqAnalysis= 1, size = 120, shift = 10, ULFmin = 0, ULFmax = 0.0033, VLFmin = 0.0033, VLFmax = 0.04, LFmin = 0.04, LFmax = 0.15, HFmin = 0.15, HFmax = 0.4 ) #modified RHRV default according to doi: 10.3389/fpubh.2017.00258
datetime <- gsub('.{4}$', '',fli[vector[i]])
mat[i,1] <- gsub('.{9}$', '',datetime)
mat[i,2] <- gsub('-',':',gsub('.{11}', '',datetime))
mat[i,3] <- log(hrv.data$TimeAnalysis[[1]]$rMSSD)/6.5*100
mat[i,4] <- mean(hrv.data$Beat$niHR)
mat[i,5] <- hrv.data$TimeAnalysis[[1]]$SDNN
mat[i,6] <- hrv.data$TimeAnalysis[[1]]$pNN50
mat[i,7] <- hrv.data$TimeAnalysis[[1]]$SDSD
mat[i,8] <- hrv.data$TimeAnalysis[[1]]$rMSSD
mat[i,9] <- hrv.data$TimeAnalysis[[1]]$IRRR
mat[i,10] <- hrv.data$TimeAnalysis[[1]]$MADRR
mat[i,11] <- hrv.data$TimeAnalysis[[1]]$TINN
mat[i,12] <- hrv.data$TimeAnalysis[[1]]$HRVi
mat[i,13] <- mean(hrv.data$FreqAnalysis[[1]]$LF)
mat[i,14] <- mean(hrv.data$FreqAnalysis[[1]]$HF)
mat[i,15] <- mean(hrv.data$FreqAnalysis[[1]]$LFHF)
}
}
write.csv(mat, file="HRV_parsed.csv")