Skip to content

Commit 9c649fd

Browse files
committed
add script for splitting into different acc, ecg, ppg125, ppg512 csv file
1 parent 64b87f7 commit 9c649fd

1 file changed

Lines changed: 72 additions & 0 deletions

File tree

convert.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
import argparse
5+
import numpy as np
6+
import os
7+
8+
def parse_args():
9+
p = argparse.ArgumentParser()
10+
p.add_argument('raw_data_file', nargs=1, help='Specify the raw data file')
11+
return vars(p.parse_args())
12+
13+
MSEC_PER_SEC = 1000
14+
15+
def get_interpolated_ts(ts, ratio):
16+
new_ts = None
17+
vals, idx = np.unique(ts, return_index=True)
18+
idx = np.hstack((idx,ts.shape[0])) # so we can do idx[i+1] - idx[i]
19+
vals = np.hstack((vals,vals[-1] + MSEC_PER_SEC))
20+
for i in xrange(vals.size-1):
21+
num = idx[i+1] - idx[i]
22+
# we have to expand time intervals according to the ratio for
23+
# we might have multiple data in one row
24+
tmp = np.linspace(vals[i], vals[i+1], num * ratio, False)
25+
new_ts = np.append(new_ts, tmp) if new_ts is not None else tmp
26+
return new_ts
27+
28+
def convert_ecg_to_mv(raw, ratio=1000./6/2097152):
29+
idx = np.where(raw >= 4194304)
30+
raw[idx] -= 8388608
31+
return raw * ratio
32+
33+
def convert_ppg_to_mv(raw):
34+
return convert_ecg_to_mv(raw, 3.2*1000/65536)
35+
36+
if __name__ == "__main__":
37+
args = parse_args()
38+
input_file = os.path.basename(args["raw_data_file"][0])
39+
40+
# 0: ACC, 5: ECG, 9: PPG 125 Hz, 12: PPG 512 Hz
41+
basename = os.path.splitext(input_file)[0]
42+
params = {
43+
#type: [filename, wanted indexes, data per row, data per item, convert function (if any)]
44+
0: [ basename + "_acc.csv", [2,3,4,6,7,8,10,11,12], 3, 3, None],
45+
5: [ basename + "_ecg.csv", range(2,13), 11, 1, convert_ecg_to_mv],
46+
9: [ basename + "_ppg125.csv", range(2,13,2), 6, 1, convert_ppg_to_mv],
47+
12: [ basename + "_ppg512.csv", range(2,14), 12, 1, convert_ppg_to_mv],
48+
}
49+
50+
data = np.genfromtxt(input_file, delimiter=",")
51+
for data_type in params.keys():
52+
fname, wanted, data_per_row, data_per_item, fn_convert = params[data_type]
53+
54+
# check if data_type exists in the input file
55+
idx = np.where(data[:,0] == data_type)[0]
56+
if len(idx) <= 0:
57+
continue
58+
59+
# filter out specific type
60+
raw = data[idx]
61+
# filter out columns and reshape into given row, column
62+
raw = raw[:,wanted].reshape(raw.shape[0] * data_per_row, data_per_item)
63+
# if further convert() is required, call it
64+
if fn_convert:
65+
raw = fn_convert(raw)
66+
# get the interpolated timestamp
67+
ts = get_interpolated_ts(data[idx,-1] * MSEC_PER_SEC, data_per_row)
68+
# match time stamps w/ the parsed data
69+
output = np.column_stack((ts, raw))
70+
# dump into output file
71+
np.savetxt(fname, output, delimiter=',')
72+

0 commit comments

Comments
 (0)