In this chapter we are going to create a small file to show the encoding methods used by EDF+. These methods are
We are going to use a common approach for these methods. First of all, we create a ByteBuffer of 61440. In EDF, the data record size (number of bytes) is recommended not to exceed 61440. So, we use this value to set the maximum length of the buffer.
Consequently, we are going to use the next procedures:
ByteBuffers are easily transferred to files by using a channel. Getting a channel from a file is a direct operation. The program is somewhat difficult. It is necessary to know the following points:
This is the code
We have the mechanisms needed to create the header, to store data as little-endian short integer and to create annotations encoded in UTF-8. Now, we can see the result by looking at the content of the file out
The lines have been cut for convenience. The first characters 41 42 43 31 32 33 correspond to the String ABC123. Then, the series 01 00 02 00 03 00 correspond to the data 1 2 3 stored as little-endian short bytes. Finally, the characters CE B1 CE B2 correspond to the Unicode characters 03b1 03b2.
We need a Unicode editor to see the content of the file. The result is seen in figure 5.1
Notice that the first characters ABC123 are the ASCII characters, then the data as little-endian short byte and finally the two characters that correspond to Unicode 03b1
and Unicode 03b2.
Now we know how to create the header, how to store the data and how to store the annotations. On the other side, we know how to read an ASCII file containing the data. If we set everything together, we can create an EDF+ file.
import java.io.*;
import java.nio.*;
import java.nio.channels.*;
class SaveFile{
public static void main(String[] args)
throws IOException {
// Creation of the file and the channel
File ff = new File("out");
FileOutputStream fos = new FileOutputStream(ff);
FileChannel fc = fos.getChannel();
// Defines the ByteBuffer order as little endian
ByteBuffer bb = ByteBuffer.allocateDirect(61440);
bb.order((ByteOrder.LITTLE_ENDIAN));
// Stores a String in ASCII (similar to the header)
String str = "ABC123";
bb.put(str.getBytes("US-ASCII"));
// Writes the equivalent of the header to the file
bb.flip();
fc.write(bb);
bb.clear();
// Stores several short little endian (similar to data)
bb.putShort((short) 1);
bb.putShort((short) 2);
bb.putShort((short) 3);
// Stores several Unicode in UTF-8 (similar to annotations)
String str2 = "";
str2 += '\u03b1';
str2 += '\u03b2';
bb.put(str2.getBytes("UTF-8"));
// writes to the file the equivalent to a data record
// data and annotations
bb.flip();
fc.write(bb);
}
}
[j@localhost PreparingData]$ hexedit out
41 42 43 31 32 33 01 00 02 00 03 00 CE B1 CE B2 ABC123..........
Next: Setting everything together
Up: Coding a sin wave
Previous: Reading an ASCII file
Contents
j
2003-05-28