-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathCSVFile.java
More file actions
90 lines (80 loc) · 2.38 KB
/
CSVFile.java
File metadata and controls
90 lines (80 loc) · 2.38 KB
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
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStreamWriter;
/**
* class CSVFile - writes ECG data to an output file
*
* @author Dakota Williams
*/
public class CSVFile implements ECGOutputFile {
private OutputStreamWriter out;
/**
* Constructor - initializes the file writer
*
* @param filename the file to write to
*/
public CSVFile(String filename) {
try {
out = new OutputStreamWriter(new FileOutputStream(filename));
} catch (FileNotFoundException e) {}
}
/**
* write - writes data to the file
*
* @param data The data to write.
* Array describes (in order of outermost to innermost):
* 1. channel number
* 2. x (0) or y (1)
* 3. sample number
*/
public void write(ECGDataSet[] data, int offset)
throws IOException {
String s = data.length + " leads sampled @" + (data[0].getAt(1)[0]-data[0].getAt(0)[0]) + "ms";
out.write(s, 0, s.length());
for(int i = 0; i < data.length; i++) {
out.write(",\t\t", 0, 3);
out.write("" + (i+offset), 0, new Integer(i+offset).toString().length());
}
out.write("\n", 0, 1);
for(int i = 0; i < data[0].size(); i++) {
s = String.format("%f", data[0].getAt(i)[0]);
out.write("" + s, 0, s.length());
for(int j = 0; j < data.length; j++) {
s = String.format("%f", data[j].getAt(i)[1]);
out.write(",\t" + s, 0, s.length() + 2);
}
out.write("\n", 0, 1);
}
out.flush();
out.close();
}
/**
* writeSubset - writes part of the data to a file
*
* @param data the data to write. See write() for more detail
* @param start the index to start at
* @param end the index to end at
*/
public void writeSubset(ECGDataSet[] data, int start, int end, int offset)
throws IOException {
String s = data.length + " leads sampled @" + (data[0].getAt(1)[0]-data[0].getAt(0)[0]) + "ms";
out.write(s, 0, s.length());
for(int i = 0; i < data.length; i++) {
out.write(",\t\t", 0, 3);
out.write("" + (i+offset), 0, new Integer(i+offset).toString().length());
}
out.write("\n", 0, 1);
for(int i = start; i < end; i++) {
s = String.format("%f", data[0].getAt(i)[0]);
out.write("" + s, 0, s.length());
for(int j = 0; j < data.length; j++) {
s = String.format("%f", data[j].getAt(i)[1]);
out.write(",\t" + s, 0, s.length() + 2);
}
out.write("\n", 0, 1);
}
out.flush();
out.close();
}
}