next up previous contents
Next: Viewing the result Up: Coding a sin wave Previous: Preparing the data   Contents


Setting everything together

We are going to use the class Support such as it was created in the chapter 3. By using it, we create the header and the data, but we would need to create the annotations too. To do this, we are going to use an array of Strings. Since we have five records, we will use an array with five Strings. Let us see the code:

char c20 = (char) 20;

String[] annot = new String[5];
annot[0] = "+0"+ c20 + c20 + "This is the first data record" + c20;
annot[1] = "+1"+ c20 + c20 + "olivan@neurotraces.com" + c20;
annot[2] = "+2"+ c20 + c20 + "<EDF_XMLnote>content</EDF_XMLnote>"+ c20;
annot[3] = "+3"+ c20 + c20 + "Unicode  5927 = " + '\u5927'+ c20;
annot[4] = "+4"+ c20 + c20 + " TAL (ann1) "+ c20 + " TAL (ann2)" + c20;

Notice that they represent some of the most common kinds of annotations including:

In our example, the annotations are stored as an Array of Strings. Of course, in a real case the annotations could be in another different file. Now we have two alternatives to include the annotations in the EDF+ file:

In the next program, we choose the second option. Previously, in the Support class we had defined the sampling as 50, meaning that we have 100 bytes to store annotations for each data record.

This is the whole program

import java.io.*;
import java.nio.*;
import java.nio.channels.*;
import java.util.*;

class CreateEDFp{
    public static void main( String[] args)
    throws UnsupportedEncodingException, IOException{

	// Header

	Support mySupport = new Support();
	String header = mySupport.createHeader();

	// Array of String with annotations

	char c20 = (char) 20;

	String[] annot = new String[5];
	annot[0] = "+0"+ c20 + c20 + "This is the first data record" + c20;
	annot[1] = "+1"+ c20 + c20 + "olivan@neurotraces.com" + c20;
	annot[2] = "+2"+ c20 + c20 + "<EDF_XMLnote>content</EDF_XMLnote>"+ c20;
	annot[3] = "+3"+ c20 + c20 + "Unicode  5927 = " + '\u5927'+ c20;
	annot[4] = "+4"+ c20 + c20 + " TAL (ann1) "+ c20 + " TAL (ann2)" + c20;

	// Creation of file and channel

	File ff = new File("wav.edf");
	FileOutputStream fos = new FileOutputStream(ff);
	FileChannel fc = fos.getChannel();

	// Creation of the buffer (order: little endian)

	ByteBuffer bb = ByteBuffer.allocateDirect(61440);
	bb.order((ByteOrder.LITTLE_ENDIAN));

	// Stores the header

	bb.put(header.getBytes("US-ASCII"));

	bb.flip();	
	fc.write(bb);
	bb.clear();

	// Stores the data record

	Vector data = new Vector();

	int sampDat = 100;
	int sampAnn = 50;

	double doubleValue;
	short shortValue;

	for (int i=0; i<5 ; i++){

	    // reads and introduces the ordinary signal into the buffer

	    data = mySupport.readData("sig.asc", 1, i*sampDat, sampDat);

	    for (int j=0; j<sampDat; j++){
		doubleValue =  Double.parseDouble((String) data.get(j));
		shortValue = (short) Math.round(doubleValue);
		bb.putShort(shortValue);
	    } 

	    // introduces the annotation into the buffer

	    bb.put(annot[i].getBytes("UTF-8"));

	    // fills with zeros the non-used annotation

	    for (int j= bb.position(); j< 2 * (sampDat + sampAnn); j++){
		bb.put((byte) 0);
	    }

	    // writes the data record to the file 

	    bb.flip();	
	    fc.write(bb);
	    bb.clear();

	}

    }

}

Now we can create the EDF+ file:

 
[j@localhost SettingTogether]$ javac CreateEDFp.java
[j@localhost SettingTogether]$ java CreateEDFp
[j@localhost SettingTogether]$ ls wav.edf
wav.edf

We have created a file called wav.edf. In the next chapter we are going to evaluate its content to check the process.


next up previous contents
Next: Viewing the result Up: Coding a sin wave Previous: Preparing the data   Contents
j 2003-05-28