forked from seongkook/PseudoBend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
processRawDirectionDiscriminationData
73 lines (66 loc) · 2.21 KB
/
processRawDirectionDiscriminationData
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
/*This script is to extract a single wave/attempt from a direction discrimination trial. Have this file
in the same folder level as the raw files to be processed, and craate a folder named "cleaned". All
the processed data will be in the cleaned folder. Use "node processRawDirectionDiscriminationData.js" to run, must have node installed first.*/
const fs = require('fs')
const dir = fs.opendirSync('.')
let dirent;
let count = 0;
async function readFiles() {
while ((dirent = dir.readSync()) !== null) {
const { name } = dirent;
if (name.includes("DirectionDiscrimination")) {
fs.readFile(name, 'utf8', function(err, data){
// Display the file content
let output = 'Timestamp, Fx, Fy, Fz, Magnitude\r';
//console.log("---------------------------------------------------------------------------------------------------------")
data = data.split("\n");
count += 1;
console.log(data.length);
let curIndex = data.length - 2;
let cutOffFound = false;
let highPointFound = false;
let beginningIdx = 0;
while (!cutOffFound && curIndex > -1) {
// data[curIndex] = data[curIndex].split(",");
let row = data[curIndex].split(",");
let mag = row[4];
if (mag >= 1) {
highPointFound = true;
} else if (highPointFound && mag <= 0.2) {
cutOffFound = true;
beginningIdx = curIndex;
}
if (data[curIndex][4]){
let mag = data[curIndex][4];
data[curIndex][4] = mag.substring(0, mag.length - 3);
}
curIndex -= 1
}
beginningIdx = beginningIdx == 0 ? 1 : beginningIdx;
data = data.slice(beginningIdx);
data = splitToArrays(data);
let timeoffset = data[0][0];
for (let j = 0; j < data.length - 1; j++) {
let newTime = data[j][0] - timeoffset;
data[j][0] = newTime;
output += data[j].join(",");
}
fs.writeFile('./cleaned/Clean-' + name , output, (err) => {
// In case of a error throw err.
if (err) throw err;
})
});
}
}
dir.closeSync()
}
function splitToArrays(data) {
for (let i = 0; i < data.length; i++){
if (data[i] != "" || data[i] != null) {
let row = data[i].split(",");
data[i] = row;
}
}
return data;
}
readFiles();