reading EGI data

Giovanni Piantoni g.piantoni at NIN.KNAW.NL
Mon Apr 19 11:35:42 CEST 2010


Dear Joseph,

Thanks for your quick reply, the fix works perfectly!

Much appreciated,

Gio

On Sat, Apr 17, 2010 at 08:58, Joseph Dien <jdien07 at mac.com> wrote:
> Try this as a replacement for the read_sbin_data.m file.  It fixes the
> problem (which, contrary to what I just said, is that the unsegmented data
> code someone added was assuming the files are always int16 whereas your data
> is single, so it wasn't skipping the correct number of bytes).  It's based
> on the fieldtrip-20100406 release rather than today's release but should be
> good enough until I can get the fix posted.  Let me know if there are any
> further problems.
> function [trialData] = read_sbin_data(filename, hdr, begtrial, endtrial,
> chanindx)
>
>
>
> % READ_SBIN_DATA reads the data from an EGI segmented simple binary format
> file
> %
> % Use as
> %   [trialData] = read_sbin_data(filename, hdr, begtrial, endtrial,
> chanindx)
> % with
> %   filename       name of the input file
> %   hdr            header structure, see READ_HEADER
> %   begtrial       first trial to read, mutually exclusive with
> begsample+endsample
> %   endtrial       last trial to read,  mutually exclusive with
> begsample+endsample
> %   chanindx       list with channel indices to read
> %
> % This function returns a 3-D matrix of size Nchans*Nsamples*Ntrials.
> %_______________________________________________________________________
> %
> %
> % Modified from EGI's readEGLY.m with permission 2008-03-31 Joseph Dien
> %
>
>
>
> % Subversion does not use the Log keyword, use 'svn log <filename>' or 'svn
> -v log | less' to get detailled information
>
>
>
> fh=fopen([filename],'r');
> if fh==-1
>   error('wrong filename')
> end
>
>
>
> version = fread(fh,1,'int32');
>
>
>
> %check byteorder
> [str,maxsize,cEndian]=computer;
> if version < 7
>   if cEndian == 'B'
>     endian = 'ieee-be';
>   elseif cEndian == 'L'
>     endian = 'ieee-le';
>   end;
> elseif (version > 6) && ~bitand(version,6)
>   if cEndian == 'B'
>     endian = 'ieee-le';
>   elseif cEndian == 'L'
>     endian = 'ieee-be';
>   end;
>   version = swapbytes(uint32(version)); %hdr.orig.header_array is already
> byte-swapped
> else
>     error('ERROR:  This is not a simple binary file.  Note that NetStation
> does not successfully directly convert EGIS files to simple binary
> format.\n');
> end;
>
>
>
> if bitand(version,1) == 0
>     unsegmented = 1;
> else
>     unsegmented = 0;
> end;
>
>
>
> precision = bitand(version,6);
> Nevents=hdr.orig.header_array(17);
>
>
>
> switch precision
>     case 2
>         trialLength=2*hdr.nSamples*(hdr.nChans+Nevents)+6;
>         dataType='int16';
>         dataLength=2;
>     case 4
>         trialLength=4*hdr.nSamples*(hdr.nChans+Nevents)+6;
>         dataType='single';
>         dataLength=4;
>     case 6
>         trialLength=8*hdr.nSamples*(hdr.nChans+Nevents)+6;
>         dataType='double';
>         dataLength=8;
> end
>
>
>
> if unsegmented
>     %interpret begtrial and endtrial as sample indices
>     fseek(fh, 36+Nevents*4, 'bof'); %skip over header
>     fseek(fh, ((begtrial-1)*(hdr.nChans+Nevents)*dataLength), 'cof'); %skip
> previous trials
>     nSamples  = endtrial-begtrial+1;
>     trialData = fread(fh, [hdr.nChans+Nevents, nSamples],dataType,endian);
> else
>     fseek(fh,
> 40+length(hdr.orig.CatLengths)+sum(hdr.orig.CatLengths)+Nevents*4, 'bof');
> %skip over header
>     fseek(fh, (begtrial-1)*trialLength, 'cof'); %skip over initial segments
>
>
>
>     trialData=zeros(hdr.nChans,hdr.nSamples,endtrial-begtrial+1);
>
>
>
>     for segment=1:(endtrial-begtrial+1)
>         fseek(fh, 6, 'cof'); %skip over segment info
>         temp = fread(fh, [(hdr.nChans+Nevents),
> hdr.nSamples],dataType,endian);
>         trialData(:,:,segment) = temp(1:hdr.nChans,:);
>     end
> end
> trialData=trialData(chanindx, :,:);
> fclose(fh);
>
>
>
> On Apr 17, 2010, at 2:41 AM, Joseph Dien wrote:
>
> Hi,
>    yeah, there's a bug in the code.  When someone added support for
> unsegmented files to my EGI simple binary file format code, it appears they
> didn't test it very well.  There's been a number of problems.  In this case,
> instead of just skipping the first sample, it's skipping hdr.nChans+Nevents
> samples, resulting in mangled data.
>
> I'm at a conference right now but as soon as I get home I'll post a fix and
> let you know that it's done.  Sigh...
>
> Joe
>
>
>
> On Apr 16, 2010, at 12:09 PM, Giovanni Piantoni wrote:
>
> Dear all,
>
> I get this incredible error when I try to read EGI data. If the first
>
> data point is odd, it works well (EGI_data_odd.png), while if the
>
> first data point is even, then the result doesn't make much sense
>
> (EGI_data_even.png).
>
> You can replicate it with this data:
>
> http://bit.ly/cdqzZH
>
> and the following code:
>
> cfg = [];
>
> cfg.dataset = 'EGIrecording.raw';
>
> cfg.trialdef.triallength = Inf;
>
> def = ft_definetrial(cfg);
>
> data = ft_preprocessing(def);
>
> plot(data.trial{1}(1,:)) % odd
>
> def.trl(1) = 2;
>
> data = ft_preprocessing(def);
>
> plot(data.trial{1}(1,:)) % even
>
> Does anybody have an idea on what's going on?
>
> Thanks,
>
> Gio
>
> -------------------------------------------------------------------------------------
>
> MATLAB Version 7.9.0.529 (R2009b)
>
> Operating System: Linux 2.6.31-20-generic #58-Ubuntu SMP Fri Mar 12
>
> 04:38:19 UTC 2010 x86_64
>
> Java VM Version: Java 1.6.0_12-b04 with Sun Microsystems Inc. Java
>
> HotSpot(TM) 64-Bit Server VM mixed mode
>
> -------------------------------------------------------------------------------------
>
> --
>
> Giovanni Piantoni, Ph.D. student
>
> Dept. Sleep & Cognition
>
> Netherlands Institute for Neuroscience
>
> Meibergdreef 47
>
> 1105 BA Amsterdam (NL)
>
> +31 (0)20 5665492
>
> g.piantoni at nin.knaw.nl
>
> www.nin.knaw.nl/research_groups/van_someren_group/
>
> ----------------------------------
>
> The aim of this list is to facilitate the discussion between users of the
> FieldTrip  toolbox, to share experiences and to discuss new ideas for MEG
> and EEG analysis. See also
> http://listserv.surfnet.nl/archives/fieldtrip.html and
> http://www.ru.nl/neuroimaging/fieldtrip.
>
> <EGI_data_odd.png><EGI_data_even.png>
>
> ----------------------------------
> The aim of this list is to facilitate the discussion between users of the
> FieldTrip  toolbox, to share experiences and to discuss new ideas for MEG
> and EEG analysis. See also
> http://listserv.surfnet.nl/archives/fieldtrip.html and
> http://www.ru.nl/neuroimaging/fieldtrip.
>
> ----------------------------------
>
> The aim of this list is to facilitate the discussion between users of the
> FieldTrip toolbox, to share experiences and to discuss new ideas for MEG and
> EEG analysis.
>
> http://listserv.surfnet.nl/archives/fieldtrip.html
>
> http://www.ru.nl/fcdonders/fieldtrip/

----------------------------------
The aim of this list is to facilitate the discussion between users of the FieldTrip  toolbox, to share experiences and to discuss new ideas for MEG and EEG analysis. See also http://listserv.surfnet.nl/archives/fieldtrip.html and http://www.ru.nl/neuroimaging/fieldtrip.



More information about the fieldtrip mailing list