Beispiel #1
0
int main(int argc, char *argv[])
{
	(void) argc;
	(void) argv;

	struct xdf *xdf = NULL;
	int step, retval = EXIT_FAILURE;
	unsigned int ns, i;
	size_t strides[NARRAYS] = {
		NEEG*sizeof(eeg[0]),
		NSENS*sizeof(sens[0]),
		sizeof(trigger[0])
	};

	/*************************************************
	 *            File preparation                   *
	 *************************************************/
	step = 0;
	xdf = xdf_open(filename, XDF_WRITE, XDF_BDF);
	if (xdf == NULL) 
		goto exit;

	xdf_set_conf(xdf, XDF_F_SAMPLING_FREQ, FS, XDF_NOF);

	step++;
	if (configure_channels(xdf, NEEG, NSENS)) 
		goto exit;

	step++;
	if (xdf_define_arrays(xdf, NARRAYS, strides)
	     || xdf_prepare_transfer(xdf) )
		goto exit;

	/************************************************
	*            Writing loop                       *
	*************************************************/
	step++;
	for (i=0; i<TOTAL_NS; i+=NS) {
		ns = ((TOTAL_NS - i) >= NS) ? NS : (TOTAL_NS - i);
		generate_signal(NEEG, eeg, NSENS, sens, trigger, FS, ns);

		if (xdf_write(xdf, ns, eeg, sens, trigger) < 0) 
			goto exit;
	}

	retval = EXIT_SUCCESS;
exit:
	if (retval != EXIT_SUCCESS) {
		fprintf(stderr, "Error while %s : (%i) %s\n",
		        stepmsg[step], errno, strerror(errno));
	}
	
	xdf_close(xdf);
	return retval;
}
Beispiel #2
0
int copy_xdf(const char* genfilename, const char* reffilename, int dstfmt)
{
	struct xdf *dst = NULL, *src = NULL;
	int srcfmt, retcode = -1;

	/* Open the source file*/
	src = xdf_open(reffilename, XDF_READ, XDF_ANY);
	if (src == NULL)
		goto exit;
	xdf_get_conf(src, XDF_F_FILEFMT, &srcfmt, XDF_NOF);

	/* Create the destination file */
	if (dstfmt == XDF_ANY)
		dstfmt = srcfmt;
	dst = xdf_open(genfilename, XDF_WRITE, dstfmt);
	if (dst == NULL)
		goto exit;

	/* Copy the metadata (time, location, channel layout, event types) */
	if (copy_configuration(dst, src))
		goto exit;

	/* Copy channel data */
	if (copy_datastream(dst, src))
		goto exit;

	/* Copy event table */
	if (copy_eventtable(dst, src))
		goto exit;

	retcode = 0;

exit:
	xdf_close(src);
	xdf_close(dst);

	return retcode;
}
Beispiel #3
0
int acq_prepare_rec(struct acq* acq, const char* filename)
{
	struct xdf* xdf;
	int j, fs;

	if (!acq || !filename)
		return 0;

	// Create the BDF file
	xdf = xdf_open(filename, XDF_WRITE, XDF_BDF);
	if (!xdf) 
		goto abort;

	// Configuration file general header
	fs = egd_get_cap(acq->dev, EGD_CAP_FS, NULL);
	xdf_set_conf(xdf, XDF_F_REC_DURATION, 1.0,
	                  XDF_F_REC_NSAMPLE, fs,
		          XDF_NOF);

	// Set up the file recording channels
	for (j=0; j<3; j++)	
		if (setup_xdf_channel_group(acq, j, xdf))
			goto abort;

	// Make the file ready for recording
	xdf_define_arrays(xdf, 3, acq->strides);
	if (xdf_prepare_transfer(xdf))
		goto abort;

	acq->xdf = xdf;
	return 0;
	
abort:
	fprintf(stderr, "Preparing recording file: %s\n", strerror(errno));
	xdf_close(xdf);
	return -1;
}
Beispiel #4
0
int generate_xdffile(const char* filename)
{
	eeg_t* eegdata;
	sens_t* exgdata;
	tri1_t* tri1data;
	tri2_t* tri2data;
	int retcode = -1;
	struct xdf* xdf = NULL;
	int i,j, evttype1, evttype2;
	char tmpstr[16];
	size_t strides[4] = {
		NEEG*sizeof(*eegdata),
		NEXG*sizeof(*exgdata),
		NTRI*sizeof(*tri1data),
		NTRI*sizeof(*tri2data),
	};


	// Allocate the temporary buffers for samples
	eegdata = malloc(NEEG*NSAMPLE*sizeof(*eegdata));
	exgdata = malloc(NEXG*NSAMPLE*sizeof(*exgdata));
	tri1data = malloc(NTRI*NSAMPLE*sizeof(*tri1data));
	tri2data = malloc(NTRI*NSAMPLE*sizeof(*tri2data));
	if (!eegdata || !exgdata || !tri1data || !tri2data)
		goto exit;
		

	xdf = xdf_open(filename, XDF_WRITE, XDF_GDF1);
	if (!xdf) 
		goto exit;
	xdf_set_conf(xdf, XDF_F_SAMPLING_FREQ, (int)SAMPLINGRATE,
	                  XDF_F_SESS_DESC, sess_str,
			  XDF_F_SUBJ_DESC, subj_str,
			  XDF_NOF);
	
	// Specify the structure (channels and sampling rate)
	if (set_default_analog(xdf, 0, EEG_TYPE))
		goto exit;
	for (j=0; j<NEEG; j++) {
		sprintf(tmpstr, "EEG%i", j);
		if (!xdf_add_channel(xdf, tmpstr))
			goto exit;
	}

	if (set_default_analog(xdf, 1, SENS_TYPE))
		goto exit;
	for (j=0; j<NEXG; j++) {
		sprintf(tmpstr, "EXG%i", j);
		if (!xdf_add_channel(xdf, tmpstr))
			goto exit;
	}

	if (set_default_trigger(xdf, 2, TRI1_TYPE, TRI1_MIN, TRI1_MAX))
		goto exit;
	for (j=0; j<NTRI; j++) {
		sprintf(tmpstr, "TRI1%i", j);
		if (!xdf_add_channel(xdf, tmpstr))
			goto exit;
	}

	if (set_default_trigger(xdf, 3, TRI2_TYPE, TRI2_MIN, TRI2_MAX))
		goto exit;
	for (j=0; j<NTRI; j++) {
		sprintf(tmpstr, "TRI2%i", j);
		if (!xdf_add_channel(xdf, tmpstr))
			goto exit;
	}

	// Add events code
	evttype1 = xdf_add_evttype(xdf, 0x101, NULL);
	evttype2 = xdf_add_evttype(xdf, 0x204, NULL);
	if (evttype1 < 0 || evttype2 < 0)
		goto exit;

	// Make the the file ready for accepting samples
	xdf_define_arrays(xdf, 4, strides);
	if (xdf_prepare_transfer(xdf) < 0)
		goto exit;
	
	// Feed with samples
	for (i=0; i<NITERATION; i++) {
		// Set data signals and unscaled them
		set_signal_values(eegdata, exgdata, tri1data, tri2data);
		if (xdf_write(xdf, NSAMPLE, eegdata, exgdata, tri1data, tri2data) < 0)
			goto exit;

		if (xdf_add_event(xdf, evttype1, NSAMPLE*i/(double)SAMPLINGRATE, -1)
		   || xdf_add_event(xdf, evttype2, (NSAMPLE*(i+1)-1)/(double)SAMPLINGRATE, -1))
			goto exit;
	}
	retcode = 0;

exit:
	// if phase is non zero, a problem occured
	if (retcode) 
		fprintf(stderr, "\terror: %s\n", strerror(errno));

	// Clean the structures and ressources
	xdf_close(xdf);
	free(eegdata);
	free(exgdata);
	free(tri1data);
	free(tri2data);

	return retcode;
}